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.util.Log 22 import android.view.inputmethod.CompletionInfo 23 import android.view.inputmethod.InputMethodManager 24 import android.widget.EditText 25 26 /** The query text field. */ 27 class QueryTextView : EditText { 28 private var mCommitCompletionListener: CommitCompletionListener? = null 29 30 constructor( 31 context: Context?, 32 attrs: AttributeSet?, 33 defStyle: Int 34 ) : super(context, attrs, defStyle) 35 36 constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) 37 constructor(context: Context?) : super(context) 38 39 /** 40 * Sets the text selection in the query text view. 41 * 42 * @param selectAll If `true`, selects the entire query. If {@false}, no characters are selected, 43 * and the cursor is placed at the end of the query. 44 */ setTextSelectionnull45 fun setTextSelection(selectAll: Boolean) { 46 if (selectAll) { 47 selectAll() 48 } else { 49 setSelection(length()) 50 } 51 } 52 replaceTextnull53 protected fun replaceText(text: CharSequence?) { 54 clearComposingText() 55 setText(text) 56 setTextSelection(false) 57 } 58 setCommitCompletionListenernull59 fun setCommitCompletionListener(listener: CommitCompletionListener?) { 60 mCommitCompletionListener = listener 61 } 62 63 private val inputMethodManager: InputMethodManager? 64 get() = getContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager 65 showInputMethodnull66 fun showInputMethod() { 67 val imm: InputMethodManager? = inputMethodManager 68 if (imm != null) { 69 imm.showSoftInput(this, 0) 70 } 71 } 72 hideInputMethodnull73 fun hideInputMethod() { 74 val imm: InputMethodManager? = inputMethodManager 75 if (imm != null) { 76 imm.hideSoftInputFromWindow(getWindowToken(), 0) 77 } 78 } 79 80 @Override onCommitCompletionnull81 override fun onCommitCompletion(completion: CompletionInfo) { 82 if (DBG) Log.d(TAG, "onCommitCompletion($completion)") 83 hideInputMethod() 84 replaceText(completion.getText()) 85 if (mCommitCompletionListener != null) { 86 mCommitCompletionListener?.onCommitCompletion(completion.getPosition()) 87 } 88 } 89 90 interface CommitCompletionListener { onCommitCompletionnull91 fun onCommitCompletion(position: Int) 92 } 93 94 companion object { 95 private const val DBG = false 96 private const val TAG = "QSB.QueryTextView" 97 } 98 } 99