blob: e25a8f19e9550a75d3a0968e168200583b19edb0 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 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
17package com.android.launcher;
18
19import android.widget.AutoCompleteTextView;
20import android.content.Context;
21import android.content.res.Configuration;
The Android Open Source Project1ff70f72009-03-05 14:34:38 -080022import android.os.Handler;
23import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.util.AttributeSet;
25import android.graphics.Rect;
26import android.view.WindowManager;
27import android.view.inputmethod.InputMethodManager;
28import android.app.Activity;
29
30/**
31 * This class is not for the faint of heart. Home works in the pan & scan
32 * soft input mode. However, this mode gets rid of the soft keyboard on rotation,
33 * which is a probelm when the Search widget has focus. This special class
34 * changes Home's soft input method temporarily as long as the Search widget holds
35 * the focus. This way, the soft keyboard remains after rotation.
36 */
37public class SearchAutoCompleteTextView extends AutoCompleteTextView {
38 private boolean mShowKeyboard;
39
The Android Open Source Project1ff70f72009-03-05 14:34:38 -080040 private Handler mLoseFocusHandler = new Handler() {
41 public void handleMessage(Message msg) {
42 if (msg.what == 1 && !hasFocus()) {
43 // Hide the soft keyboard when the search widget loses the focus
44 InputMethodManager.peekInstance().hideSoftInputFromWindow(getWindowToken(), 0);
45 }
46 }
47 };
48
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049 public SearchAutoCompleteTextView(Context context) {
50 super(context);
51 }
52
53 public SearchAutoCompleteTextView(Context context, AttributeSet attrs) {
54 super(context, attrs);
55 }
56
57 public SearchAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
58 super(context, attrs, defStyle);
59 }
60
61 @Override
62 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
63 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
64
65 final WindowManager.LayoutParams lp = ((Activity) getContext()).getWindow().getAttributes();
66 if (gainFocus) {
67 lp.softInputMode =
68 (lp.softInputMode & ~WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) |
69 WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED;
70 } else {
71 //noinspection PointlessBitwiseExpression
72 lp.softInputMode =
73 (lp.softInputMode & ~WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) |
74 WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED;
The Android Open Source Project1ff70f72009-03-05 14:34:38 -080075 // If we don't immediately gain focus, we want to hide the IME.
76 mLoseFocusHandler.sendEmptyMessage(1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077 }
78
The Android Open Source Project1ff70f72009-03-05 14:34:38 -080079 if (getWindowToken() != null) {
80 final WindowManager manager = (WindowManager)
81 getContext().getSystemService(Context.WINDOW_SERVICE);
82 manager.updateViewLayout(getRootView(), lp);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083
The Android Open Source Project1ff70f72009-03-05 14:34:38 -080084 if (mShowKeyboard) {
85 if (getContext().getResources().getConfiguration().hardKeyboardHidden ==
86 Configuration.HARDKEYBOARDHIDDEN_YES) {
87 InputMethodManager inputManager = (InputMethodManager)
88 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
89 inputManager.showSoftInput(this, 0);
90 }
91 mShowKeyboard = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 }
94 }
95
96 @Override
97 public void onWindowFocusChanged(boolean hasWindowFocus) {
98 super.onWindowFocusChanged(hasWindowFocus);
99 // See Workspace#focusOnSearch()
100 setFocusableInTouchMode(false);
101 }
102
103 void showKeyboardOnNextFocus() {
104 mShowKeyboard = true;
105 }
106}