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; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.graphics.Rect; |
| 24 | import android.view.WindowManager; |
| 25 | import android.view.inputmethod.InputMethodManager; |
| 26 | import android.app.Activity; |
| 27 | |
| 28 | /** |
| 29 | * This class is not for the faint of heart. Home works in the pan & scan |
| 30 | * soft input mode. However, this mode gets rid of the soft keyboard on rotation, |
| 31 | * which is a probelm when the Search widget has focus. This special class |
| 32 | * changes Home's soft input method temporarily as long as the Search widget holds |
| 33 | * the focus. This way, the soft keyboard remains after rotation. |
| 34 | */ |
| 35 | public class SearchAutoCompleteTextView extends AutoCompleteTextView { |
| 36 | private boolean mShowKeyboard; |
| 37 | |
| 38 | public SearchAutoCompleteTextView(Context context) { |
| 39 | super(context); |
| 40 | } |
| 41 | |
| 42 | public SearchAutoCompleteTextView(Context context, AttributeSet attrs) { |
| 43 | super(context, attrs); |
| 44 | } |
| 45 | |
| 46 | public SearchAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) { |
| 47 | super(context, attrs, defStyle); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { |
| 52 | super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); |
| 53 | |
| 54 | final WindowManager.LayoutParams lp = ((Activity) getContext()).getWindow().getAttributes(); |
| 55 | if (gainFocus) { |
| 56 | lp.softInputMode = |
| 57 | (lp.softInputMode & ~WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) | |
| 58 | WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED; |
| 59 | } else { |
| 60 | //noinspection PointlessBitwiseExpression |
| 61 | lp.softInputMode = |
| 62 | (lp.softInputMode & ~WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) | |
| 63 | WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED; |
| 64 | |
| 65 | // Hide the soft keyboard when the search widget loses the focus |
| 66 | InputMethodManager.peekInstance().hideSoftInputFromWindow(getWindowToken(), 0); |
| 67 | } |
| 68 | |
| 69 | final WindowManager manager = (WindowManager) |
| 70 | getContext().getSystemService(Context.WINDOW_SERVICE); |
| 71 | manager.updateViewLayout(getRootView(), lp); |
| 72 | |
| 73 | if (mShowKeyboard) { |
| 74 | if (getContext().getResources().getConfiguration().hardKeyboardHidden == |
| 75 | Configuration.HARDKEYBOARDHIDDEN_YES) { |
| 76 | InputMethodManager inputManager = (InputMethodManager) |
| 77 | getContext().getSystemService(Context.INPUT_METHOD_SERVICE); |
| 78 | inputManager.showSoftInput(this, 0); |
| 79 | } |
| 80 | mShowKeyboard = false; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 86 | super.onWindowFocusChanged(hasWindowFocus); |
| 87 | // See Workspace#focusOnSearch() |
| 88 | setFocusableInTouchMode(false); |
| 89 | } |
| 90 | |
| 91 | void showKeyboardOnNextFocus() { |
| 92 | mShowKeyboard = true; |
| 93 | } |
| 94 | } |