Daniel Sandler | a84a69f | 2010-10-08 10:39:46 -0400 | [diff] [blame] | 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 | |
| 17 | package com.android.systemui.statusbar.tablet; |
| 18 | |
| 19 | import android.content.Context; |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 20 | import android.os.IBinder; |
satok | 04d5020 | 2010-10-25 22:20:12 +0900 | [diff] [blame] | 21 | import android.provider.Settings; |
Daniel Sandler | a84a69f | 2010-10-08 10:39:46 -0400 | [diff] [blame] | 22 | import android.util.AttributeSet; |
satok | 04d5020 | 2010-10-25 22:20:12 +0900 | [diff] [blame] | 23 | import android.view.inputmethod.InputMethodInfo; |
Daniel Sandler | a84a69f | 2010-10-08 10:39:46 -0400 | [diff] [blame] | 24 | import android.view.inputmethod.InputMethodManager; |
Ken Wakasa | bd391ba | 2011-08-04 22:44:47 +0900 | [diff] [blame] | 25 | import android.view.inputmethod.InputMethodSubtype; |
satok | 04d5020 | 2010-10-25 22:20:12 +0900 | [diff] [blame] | 26 | import android.view.View; |
| 27 | import android.widget.ImageView; |
Daniel Sandler | a84a69f | 2010-10-08 10:39:46 -0400 | [diff] [blame] | 28 | |
satok | 04d5020 | 2010-10-25 22:20:12 +0900 | [diff] [blame] | 29 | import com.android.systemui.R; |
| 30 | |
| 31 | import java.util.List; |
Daniel Sandler | a84a69f | 2010-10-08 10:39:46 -0400 | [diff] [blame] | 32 | |
| 33 | public class InputMethodButton extends ImageView { |
| 34 | |
satok | 04d5020 | 2010-10-25 22:20:12 +0900 | [diff] [blame] | 35 | private static final String TAG = "StatusBar/InputMethodButton"; |
satok | 06e0744 | 2010-11-02 19:46:55 +0900 | [diff] [blame] | 36 | private static final boolean DEBUG = false; |
satok | 04d5020 | 2010-10-25 22:20:12 +0900 | [diff] [blame] | 37 | |
satok | d57896a | 2010-12-20 18:13:30 +0900 | [diff] [blame] | 38 | // These values are defined in Settings application. |
| 39 | private static final int ID_IME_BUTTON_VISIBILITY_AUTO = 0; |
| 40 | private static final int ID_IME_BUTTON_VISIBILITY_ALWAYS_SHOW = 1; |
| 41 | private static final int ID_IME_BUTTON_VISIBILITY_ALWAYS_HIDE = 2; |
| 42 | |
Daniel Sandler | a84a69f | 2010-10-08 10:39:46 -0400 | [diff] [blame] | 43 | // other services we wish to talk to |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 44 | private final InputMethodManager mImm; |
| 45 | private final int mId; |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 46 | private ImageView mIcon; |
| 47 | private IBinder mToken; |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 48 | private boolean mShowButton = false; |
satok | b70c82d | 2010-12-20 17:59:19 +0900 | [diff] [blame] | 49 | private boolean mScreenLocked = false; |
Jeff Brown | 2992ea7 | 2011-01-28 22:04:14 -0800 | [diff] [blame] | 50 | private boolean mHardKeyboardAvailable; |
Daniel Sandler | a84a69f | 2010-10-08 10:39:46 -0400 | [diff] [blame] | 51 | |
Ken Wakasa | bd391ba | 2011-08-04 22:44:47 +0900 | [diff] [blame] | 52 | // Please refer to InputMethodManagerService.TAG_TRY_SUPPRESSING_IME_SWITCHER |
| 53 | private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher"; |
| 54 | |
Daniel Sandler | a84a69f | 2010-10-08 10:39:46 -0400 | [diff] [blame] | 55 | public InputMethodButton(Context context, AttributeSet attrs) { |
| 56 | super(context, attrs); |
| 57 | |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 58 | // Resource Id of the input method button. This id is defined in status_bar.xml |
| 59 | mId = getId(); |
Daniel Sandler | a84a69f | 2010-10-08 10:39:46 -0400 | [diff] [blame] | 60 | // IME hookup |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 61 | mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); |
Daniel Sandler | a84a69f | 2010-10-08 10:39:46 -0400 | [diff] [blame] | 62 | } |
Daniel Sandler | a84a69f | 2010-10-08 10:39:46 -0400 | [diff] [blame] | 63 | |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 64 | @Override |
satok | 04d5020 | 2010-10-25 22:20:12 +0900 | [diff] [blame] | 65 | protected void onAttachedToWindow() { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 66 | mIcon = (ImageView) findViewById(mId); |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 67 | |
satok | b70c82d | 2010-12-20 17:59:19 +0900 | [diff] [blame] | 68 | refreshStatusIcon(); |
satok | 04d5020 | 2010-10-25 22:20:12 +0900 | [diff] [blame] | 69 | } |
| 70 | |
Ken Wakasa | bd391ba | 2011-08-04 22:44:47 +0900 | [diff] [blame] | 71 | // Refer to InputMethodManagerService.needsToShowImeSwitchOngoingNotification() |
| 72 | private boolean needsToShowIMEButtonWhenVisibilityAuto() { |
| 73 | List<InputMethodInfo> imis = mImm.getEnabledInputMethodList(); |
| 74 | final int N = imis.size(); |
| 75 | if (N > 2) return true; |
| 76 | if (N < 1) return false; |
| 77 | int nonAuxCount = 0; |
| 78 | int auxCount = 0; |
| 79 | InputMethodSubtype nonAuxSubtype = null; |
| 80 | InputMethodSubtype auxSubtype = null; |
| 81 | for(int i = 0; i < N; ++i) { |
| 82 | final InputMethodInfo imi = imis.get(i); |
| 83 | final List<InputMethodSubtype> subtypes = mImm.getEnabledInputMethodSubtypeList( |
| 84 | imi, true); |
| 85 | final int subtypeCount = subtypes.size(); |
| 86 | if (subtypeCount == 0) { |
| 87 | ++nonAuxCount; |
| 88 | } else { |
| 89 | for (int j = 0; j < subtypeCount; ++j) { |
| 90 | final InputMethodSubtype subtype = subtypes.get(j); |
| 91 | if (!subtype.isAuxiliary()) { |
| 92 | ++nonAuxCount; |
| 93 | nonAuxSubtype = subtype; |
| 94 | } else { |
| 95 | ++auxCount; |
| 96 | auxSubtype = subtype; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | if (nonAuxCount > 1 || auxCount > 1) { |
| 102 | return true; |
| 103 | } else if (nonAuxCount == 1 && auxCount == 1) { |
| 104 | if (nonAuxSubtype != null && auxSubtype != null |
satok | 9747f89 | 2011-09-12 15:56:40 +0900 | [diff] [blame] | 105 | && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale()) |
| 106 | || auxSubtype.overridesImplicitlyEnabledSubtype() |
| 107 | || nonAuxSubtype.overridesImplicitlyEnabledSubtype()) |
Ken Wakasa | bd391ba | 2011-08-04 22:44:47 +0900 | [diff] [blame] | 108 | && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) { |
| 109 | return false; |
| 110 | } |
| 111 | return true; |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | |
satok | a84b8d9 | 2010-12-14 13:58:49 +0900 | [diff] [blame] | 116 | private boolean needsToShowIMEButton() { |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 117 | if (!mShowButton || mScreenLocked) return false; |
Jeff Brown | 2992ea7 | 2011-01-28 22:04:14 -0800 | [diff] [blame] | 118 | |
| 119 | if (mHardKeyboardAvailable) { |
| 120 | return true; |
| 121 | } |
| 122 | |
satok | d57896a | 2010-12-20 18:13:30 +0900 | [diff] [blame] | 123 | final int visibility = loadInputMethodSelectorVisibility(); |
| 124 | switch (visibility) { |
| 125 | case ID_IME_BUTTON_VISIBILITY_AUTO: |
Ken Wakasa | bd391ba | 2011-08-04 22:44:47 +0900 | [diff] [blame] | 126 | return needsToShowIMEButtonWhenVisibilityAuto(); |
satok | d57896a | 2010-12-20 18:13:30 +0900 | [diff] [blame] | 127 | case ID_IME_BUTTON_VISIBILITY_ALWAYS_SHOW: |
| 128 | return true; |
| 129 | case ID_IME_BUTTON_VISIBILITY_ALWAYS_HIDE: |
| 130 | return false; |
| 131 | } |
| 132 | return false; |
satok | a84b8d9 | 2010-12-14 13:58:49 +0900 | [diff] [blame] | 133 | } |
| 134 | |
satok | b70c82d | 2010-12-20 17:59:19 +0900 | [diff] [blame] | 135 | private void refreshStatusIcon() { |
satok | 4fdf1de | 2011-01-06 19:30:54 +0900 | [diff] [blame] | 136 | if (mIcon == null) { |
| 137 | return; |
| 138 | } |
satok | b70c82d | 2010-12-20 17:59:19 +0900 | [diff] [blame] | 139 | if (!needsToShowIMEButton()) { |
satok | 82beadf | 2010-12-27 19:03:06 +0900 | [diff] [blame] | 140 | setVisibility(View.GONE); |
satok | 04d5020 | 2010-10-25 22:20:12 +0900 | [diff] [blame] | 141 | return; |
| 142 | } else { |
| 143 | setVisibility(View.VISIBLE); |
| 144 | } |
Jeff Brown | 2992ea7 | 2011-01-28 22:04:14 -0800 | [diff] [blame] | 145 | mIcon.setImageResource(R.drawable.ic_sysbar_ime); |
satok | 04d5020 | 2010-10-25 22:20:12 +0900 | [diff] [blame] | 146 | } |
| 147 | |
satok | d57896a | 2010-12-20 18:13:30 +0900 | [diff] [blame] | 148 | private int loadInputMethodSelectorVisibility() { |
| 149 | return Settings.Secure.getInt(getContext().getContentResolver(), |
| 150 | Settings.Secure.INPUT_METHOD_SELECTOR_VISIBILITY, ID_IME_BUTTON_VISIBILITY_AUTO); |
| 151 | } |
| 152 | |
satok | 913f42d | 2011-01-17 16:58:10 +0900 | [diff] [blame] | 153 | public void setIconImage(int resId) { |
| 154 | if (mIcon != null) { |
| 155 | mIcon.setImageResource(resId); |
| 156 | } |
| 157 | } |
| 158 | |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 159 | public void setImeWindowStatus(IBinder token, boolean showButton) { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 160 | mToken = token; |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 161 | mShowButton = showButton; |
satok | b70c82d | 2010-12-20 17:59:19 +0900 | [diff] [blame] | 162 | refreshStatusIcon(); |
| 163 | } |
| 164 | |
Jeff Brown | 2992ea7 | 2011-01-28 22:04:14 -0800 | [diff] [blame] | 165 | public void setHardKeyboardStatus(boolean available) { |
| 166 | if (mHardKeyboardAvailable != available) { |
| 167 | mHardKeyboardAvailable = available; |
| 168 | refreshStatusIcon(); |
| 169 | } |
| 170 | } |
| 171 | |
satok | b70c82d | 2010-12-20 17:59:19 +0900 | [diff] [blame] | 172 | public void setScreenLocked(boolean locked) { |
| 173 | mScreenLocked = locked; |
| 174 | refreshStatusIcon(); |
satok | 04d5020 | 2010-10-25 22:20:12 +0900 | [diff] [blame] | 175 | } |
| 176 | } |