1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.quicksearchbox.ui 18 19 import android.content.Context 20 import android.util.AttributeSet 21 import android.view.KeyEvent 22 import android.view.View 23 import com.android.quicksearchbox.QsbApplication 24 import com.android.quicksearchbox.R 25 import com.android.quicksearchbox.Suggestion 26 import com.android.quicksearchbox.SuggestionFormatter 27 28 /** View for web search suggestions. */ 29 class WebSearchSuggestionView(context: Context?, attrs: AttributeSet?) : 30 BaseSuggestionView(context, attrs) { 31 private val mSuggestionFormatter: SuggestionFormatter? 32 33 @Override onFinishInflatenull34 override fun onFinishInflate() { 35 super.onFinishInflate() 36 val keyListener: WebSearchSuggestionView.KeyListener = KeyListener() 37 setOnKeyListener(keyListener) 38 mIcon2?.setOnKeyListener(keyListener) 39 mIcon2?.setOnClickListener( 40 object : OnClickListener { 41 override fun onClick(v: View?) { 42 onSuggestionQueryRefineClicked() 43 } 44 } 45 ) 46 mIcon2?.setFocusable(true) 47 } 48 49 @Override bindAsSuggestionnull50 override fun bindAsSuggestion(suggestion: Suggestion?, userQuery: String?) { 51 super.bindAsSuggestion(suggestion, userQuery) 52 val text1 = mSuggestionFormatter?.formatSuggestion(userQuery, suggestion?.suggestionText1) 53 setText1(text1) 54 setIsHistorySuggestion(suggestion?.isHistorySuggestion) 55 } 56 setIsHistorySuggestionnull57 private fun setIsHistorySuggestion(isHistory: Boolean?) { 58 if (isHistory == true) { 59 mIcon1?.setImageResource(R.drawable.ic_history_suggestion) 60 mIcon1?.setVisibility(VISIBLE) 61 } else { 62 mIcon1?.setVisibility(INVISIBLE) 63 } 64 } 65 66 private inner class KeyListener : View.OnKeyListener { onKeynull67 override fun onKey(v: View, keyCode: Int, event: KeyEvent): Boolean { 68 var consumed = false 69 if (event.getAction() == KeyEvent.ACTION_DOWN) { 70 if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && v !== mIcon2) { 71 consumed = mIcon2!!.requestFocus() 72 } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && v === mIcon2) { 73 consumed = requestFocus() 74 } 75 } 76 return consumed 77 } 78 } 79 80 class Factory(context: Context?) : 81 SuggestionViewInflater( 82 VIEW_ID, 83 WebSearchSuggestionView::class.java, 84 R.layout.web_search_suggestion, 85 context 86 ) { 87 @Override canCreateViewnull88 override fun canCreateView(suggestion: Suggestion?): Boolean { 89 return suggestion!!.isWebSearchSuggestion 90 } 91 } 92 93 companion object { 94 private const val VIEW_ID = "web_search" 95 } 96 97 init { 98 mSuggestionFormatter = QsbApplication[context].suggestionFormatter 99 } 100 } 101