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.text.TextUtils 21 import android.util.AttributeSet 22 import android.view.View 23 import android.widget.ImageView 24 import android.widget.RelativeLayout 25 import android.widget.TextView 26 import com.android.quicksearchbox.R 27 import com.android.quicksearchbox.Suggestion 28 29 /** Base class for suggestion views. */ 30 abstract class BaseSuggestionView : RelativeLayout, SuggestionView { 31 @JvmField protected var mText1: TextView? = null 32 @JvmField protected var mText2: TextView? = null 33 @JvmField protected var mIcon1: ImageView? = null 34 @JvmField protected var mIcon2: ImageView? = null 35 private var mSuggestionId: Long = 0 36 private var mAdapter: SuggestionsAdapter<*>? = null 37 38 constructor( 39 context: Context?, 40 attrs: AttributeSet?, 41 defStyle: Int 42 ) : super(context, attrs, defStyle) 43 44 constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) 45 constructor(context: Context?) : super(context) 46 47 @Override onFinishInflatenull48 protected override fun onFinishInflate() { 49 super.onFinishInflate() 50 mText1 = findViewById(R.id.text1) as TextView? 51 mText2 = findViewById(R.id.text2) as TextView? 52 mIcon1 = findViewById(R.id.icon1) as ImageView? 53 mIcon2 = findViewById(R.id.icon2) as ImageView? 54 } 55 56 @Override bindAsSuggestionnull57 override fun bindAsSuggestion(suggestion: Suggestion?, userQuery: String?) { 58 setOnClickListener(ClickListener()) 59 } 60 61 @Override bindAdapternull62 override fun bindAdapter(adapter: SuggestionsAdapter<*>?, position: Long) { 63 mAdapter = adapter 64 mSuggestionId = position 65 } 66 isFromHistorynull67 protected fun isFromHistory(suggestion: Suggestion?): Boolean { 68 return suggestion?.isSuggestionShortcut == true || suggestion?.isHistorySuggestion == true 69 } 70 71 /** Sets the first text line. */ setText1null72 protected fun setText1(text: CharSequence?) { 73 mText1?.setText(text) 74 } 75 76 /** Sets the second text line. */ setText2null77 protected fun setText2(text: CharSequence?) { 78 mText2?.setText(text) 79 if (TextUtils.isEmpty(text)) { 80 mText2?.setVisibility(GONE) 81 } else { 82 mText2?.setVisibility(VISIBLE) 83 } 84 } 85 onSuggestionClickednull86 protected fun onSuggestionClicked() { 87 if (mAdapter != null) { 88 mAdapter!!.onSuggestionClicked(mSuggestionId) 89 } 90 } 91 onSuggestionQueryRefineClickednull92 protected fun onSuggestionQueryRefineClicked() { 93 if (mAdapter != null) { 94 mAdapter!!.onSuggestionQueryRefineClicked(mSuggestionId) 95 } 96 } 97 98 private inner class ClickListener : OnClickListener { 99 @Override onClicknull100 override fun onClick(v: View?) { 101 onSuggestionClicked() 102 } 103 } 104 } 105