1 /*
2  * Copyright (C) 2010 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 package com.android.quicksearchbox.ui;
17 
18 import android.content.Context;
19 import android.util.AttributeSet;
20 import android.util.Log;
21 import android.view.inputmethod.CompletionInfo;
22 import android.view.inputmethod.InputMethodManager;
23 import android.widget.EditText;
24 
25 /**
26  * The query text field.
27  */
28 public class QueryTextView extends EditText {
29 
30     private static final boolean DBG = false;
31     private static final String TAG = "QSB.QueryTextView";
32 
33     private CommitCompletionListener mCommitCompletionListener;
34 
QueryTextView(Context context, AttributeSet attrs, int defStyle)35     public QueryTextView(Context context, AttributeSet attrs, int defStyle) {
36         super(context, attrs, defStyle);
37     }
38 
QueryTextView(Context context, AttributeSet attrs)39     public QueryTextView(Context context, AttributeSet attrs) {
40         super(context, attrs);
41     }
42 
QueryTextView(Context context)43     public QueryTextView(Context context) {
44         super(context);
45     }
46 
47     /**
48      * Sets the text selection in the query text view.
49      *
50      * @param selectAll If {@code true}, selects the entire query.
51      *        If {@false}, no characters are selected, and the cursor is placed
52      *        at the end of the query.
53      */
setTextSelection(boolean selectAll)54     public void setTextSelection(boolean selectAll) {
55         if (selectAll) {
56             selectAll();
57         } else {
58             setSelection(length());
59         }
60     }
61 
replaceText(CharSequence text)62     protected void replaceText(CharSequence text) {
63         clearComposingText();
64         setText(text);
65         setTextSelection(false);
66     }
67 
setCommitCompletionListener(CommitCompletionListener listener)68     public void setCommitCompletionListener(CommitCompletionListener listener) {
69         mCommitCompletionListener = listener;
70     }
71 
getInputMethodManager()72     private InputMethodManager getInputMethodManager() {
73         return (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
74     }
75 
showInputMethod()76     public void showInputMethod() {
77         InputMethodManager imm = getInputMethodManager();
78         if (imm != null) {
79             imm.showSoftInput(this, 0);
80         }
81     }
82 
hideInputMethod()83     public void hideInputMethod() {
84         InputMethodManager imm = getInputMethodManager();
85         if (imm != null) {
86             imm.hideSoftInputFromWindow(getWindowToken(), 0);
87         }
88     }
89 
90     @Override
onCommitCompletion(CompletionInfo completion)91     public void onCommitCompletion(CompletionInfo completion) {
92         if (DBG) Log.d(TAG, "onCommitCompletion(" + completion + ")");
93         hideInputMethod();
94         replaceText(completion.getText());
95         if (mCommitCompletionListener != null) {
96             mCommitCompletionListener.onCommitCompletion(completion.getPosition());
97         }
98     }
99 
100     public interface CommitCompletionListener {
onCommitCompletion(int position)101         void onCommitCompletion(int position);
102     }
103 
104 }
105