The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher; |
| 18 | |
| 19 | import android.widget.AutoCompleteTextView; |
| 20 | import android.content.Context; |
| 21 | import android.content.res.Configuration; |
The Android Open Source Project | 1ff70f7 | 2009-03-05 14:34:38 -0800 | [diff] [blame] | 22 | import android.os.Handler; |
| 23 | import android.os.Message; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 24 | import android.util.AttributeSet; |
| 25 | import android.graphics.Rect; |
| 26 | import android.view.WindowManager; |
| 27 | import android.view.inputmethod.InputMethodManager; |
| 28 | import 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 | */ |
| 37 | public class SearchAutoCompleteTextView extends AutoCompleteTextView { |
| 38 | private boolean mShowKeyboard; |
| 39 | |
The Android Open Source Project | 1ff70f7 | 2009-03-05 14:34:38 -0800 | [diff] [blame] | 40 | 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 49 | 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 Project | 1ff70f7 | 2009-03-05 14:34:38 -0800 | [diff] [blame] | 75 | // If we don't immediately gain focus, we want to hide the IME. |
| 76 | mLoseFocusHandler.sendEmptyMessage(1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 77 | } |
| 78 | |
The Android Open Source Project | 1ff70f7 | 2009-03-05 14:34:38 -0800 | [diff] [blame] | 79 | if (getWindowToken() != null) { |
| 80 | final WindowManager manager = (WindowManager) |
| 81 | getContext().getSystemService(Context.WINDOW_SERVICE); |
| 82 | manager.updateViewLayout(getRootView(), lp); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 83 | |
The Android Open Source Project | 1ff70f7 | 2009-03-05 14:34:38 -0800 | [diff] [blame] | 84 | 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 92 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 93 | } |
| 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 | } |