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.widget.ListAdapter
22 import android.widget.ListView
23 import com.android.quicksearchbox.SuggestionPosition
24 
25 /** Holds a list of suggestions. */
26 class SuggestionsView(context: Context?, attrs: AttributeSet?) :
27   ListView(context, attrs), SuggestionsListView<ListAdapter?> {
28   private var mSuggestionsAdapter: SuggestionsAdapter<ListAdapter?>? = null
29 
30   @Override
setSuggestionsAdapternull31   override fun setSuggestionsAdapter(adapter: SuggestionsAdapter<ListAdapter?>?) {
32     super.setAdapter(adapter?.listAdapter)
33     mSuggestionsAdapter = adapter
34   }
35 
36   @Override
getSuggestionsAdapternull37   override fun getSuggestionsAdapter(): SuggestionsAdapter<ListAdapter?>? {
38     return mSuggestionsAdapter
39   }
40 
41   @Override
onFinishInflatenull42   override fun onFinishInflate() {
43     super.onFinishInflate()
44     setItemsCanFocus(true)
45   }
46 
47   /**
48    * Gets the position of the selected suggestion.
49    *
50    * @return A 0-based index, or `-1` if no suggestion is selected.
51    */
52   val selectedPosition: Int
53     get() = getSelectedItemPosition()
54 
55   /**
56    * Gets the selected suggestion.
57    *
58    * @return `null` if no suggestion is selected.
59    */
60   val selectedSuggestion: SuggestionPosition
61     get() = getSelectedItem() as SuggestionPosition
62 
63   companion object {
64     private const val DBG = false
65     private const val TAG = "QSB.SuggestionsView"
66   }
67 }
68