QueryTextView.java revision 014e0d0c0a5102b7cc1c5576a3af25a646731dd0
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 */
16package com.android.quicksearchbox.ui;
17
18import android.content.Context;
19import android.util.AttributeSet;
20import android.util.Log;
21import android.view.inputmethod.CompletionInfo;
22import android.view.inputmethod.InputMethodManager;
23import android.widget.EditText;
24
25/**
26 * The query text field.
27 */
28public class QueryTextView extends EditText {
29
30    private static final boolean DBG = false;
31    private static final String TAG = "QSB.QueryTextView";
32
33    private SuggestionClickListener mSuggestionClickListener;
34
35    public QueryTextView(Context context, AttributeSet attrs, int defStyle) {
36        super(context, attrs, defStyle);
37    }
38
39    public QueryTextView(Context context, AttributeSet attrs) {
40        super(context, attrs);
41    }
42
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     */
54    public void setTextSelection(boolean selectAll) {
55        if (selectAll) {
56            selectAll();
57        } else {
58            setSelection(length());
59        }
60    }
61
62    protected void replaceText(CharSequence text) {
63        clearComposingText();
64        setText(text);
65        setTextSelection(false);
66    }
67
68    public void setSuggestionClickListener(SuggestionClickListener listener) {
69        mSuggestionClickListener = listener;
70    }
71
72    private InputMethodManager getInputMethodManager() {
73        return (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
74    }
75
76    public void showInputMethod() {
77        InputMethodManager imm = getInputMethodManager();
78        if (imm != null) {
79            imm.showSoftInput(this, 0);
80        }
81    }
82
83    public void hideInputMethod() {
84        InputMethodManager imm = getInputMethodManager();
85        if (imm != null) {
86            imm.hideSoftInputFromWindow(getWindowToken(), 0);
87        }
88    }
89
90    @Override
91    public void onCommitCompletion(CompletionInfo completion) {
92        if (DBG) Log.d(TAG, "onCommitCompletion(" + completion + ")");
93        hideInputMethod();
94        replaceText(completion.getText());
95        if (mSuggestionClickListener != null) {
96            mSuggestionClickListener.onSuggestionClicked(completion.getPosition());
97        }
98    }
99
100}
101