blob: fa8aa6d1f79e1d37dfbdb1132d2b63a98f35745a [file] [log] [blame]
Daniel Sandlera84a69f2010-10-08 10:39:46 -04001/*
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
17package com.android.systemui.statusbar.tablet;
18
19import android.content.Context;
satokcd7cd292010-11-20 15:46:23 +090020import android.os.IBinder;
satok04d50202010-10-25 22:20:12 +090021import android.provider.Settings;
Daniel Sandlera84a69f2010-10-08 10:39:46 -040022import android.util.AttributeSet;
satok04d50202010-10-25 22:20:12 +090023import android.view.inputmethod.InputMethodInfo;
Daniel Sandlera84a69f2010-10-08 10:39:46 -040024import android.view.inputmethod.InputMethodManager;
Ken Wakasabd391ba2011-08-04 22:44:47 +090025import android.view.inputmethod.InputMethodSubtype;
satok04d50202010-10-25 22:20:12 +090026import android.view.View;
27import android.widget.ImageView;
Daniel Sandlera84a69f2010-10-08 10:39:46 -040028
satok04d50202010-10-25 22:20:12 +090029import com.android.systemui.R;
30
31import java.util.List;
Daniel Sandlera84a69f2010-10-08 10:39:46 -040032
33public class InputMethodButton extends ImageView {
34
satok04d50202010-10-25 22:20:12 +090035 private static final String TAG = "StatusBar/InputMethodButton";
satok06e07442010-11-02 19:46:55 +090036 private static final boolean DEBUG = false;
satok04d50202010-10-25 22:20:12 +090037
satokd57896a2010-12-20 18:13:30 +090038 // 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 Sandlera84a69f2010-10-08 10:39:46 -040043 // other services we wish to talk to
satokcd7cd292010-11-20 15:46:23 +090044 private final InputMethodManager mImm;
45 private final int mId;
satokcd7cd292010-11-20 15:46:23 +090046 private ImageView mIcon;
47 private IBinder mToken;
Joe Onorato857fd9b2011-01-27 15:08:35 -080048 private boolean mShowButton = false;
satokb70c82d2010-12-20 17:59:19 +090049 private boolean mScreenLocked = false;
Jeff Brown2992ea72011-01-28 22:04:14 -080050 private boolean mHardKeyboardAvailable;
Daniel Sandlera84a69f2010-10-08 10:39:46 -040051
Ken Wakasabd391ba2011-08-04 22:44:47 +090052 // Please refer to InputMethodManagerService.TAG_TRY_SUPPRESSING_IME_SWITCHER
53 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
54
Daniel Sandlera84a69f2010-10-08 10:39:46 -040055 public InputMethodButton(Context context, AttributeSet attrs) {
56 super(context, attrs);
57
satokcd7cd292010-11-20 15:46:23 +090058 // Resource Id of the input method button. This id is defined in status_bar.xml
59 mId = getId();
Daniel Sandlera84a69f2010-10-08 10:39:46 -040060 // IME hookup
satokcd7cd292010-11-20 15:46:23 +090061 mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
Daniel Sandlera84a69f2010-10-08 10:39:46 -040062 }
Daniel Sandlera84a69f2010-10-08 10:39:46 -040063
satok06487a52010-10-29 11:37:18 +090064 @Override
satok04d50202010-10-25 22:20:12 +090065 protected void onAttachedToWindow() {
satokcd7cd292010-11-20 15:46:23 +090066 mIcon = (ImageView) findViewById(mId);
satok06487a52010-10-29 11:37:18 +090067
satokb70c82d2010-12-20 17:59:19 +090068 refreshStatusIcon();
satok04d50202010-10-25 22:20:12 +090069 }
70
Ken Wakasabd391ba2011-08-04 22:44:47 +090071 // 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
satok9747f892011-09-12 15:56:40 +0900105 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
106 || auxSubtype.overridesImplicitlyEnabledSubtype()
107 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
Ken Wakasabd391ba2011-08-04 22:44:47 +0900108 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
109 return false;
110 }
111 return true;
112 }
113 return false;
114 }
115
satoka84b8d92010-12-14 13:58:49 +0900116 private boolean needsToShowIMEButton() {
Joe Onorato857fd9b2011-01-27 15:08:35 -0800117 if (!mShowButton || mScreenLocked) return false;
Jeff Brown2992ea72011-01-28 22:04:14 -0800118
119 if (mHardKeyboardAvailable) {
120 return true;
121 }
122
satokd57896a2010-12-20 18:13:30 +0900123 final int visibility = loadInputMethodSelectorVisibility();
124 switch (visibility) {
125 case ID_IME_BUTTON_VISIBILITY_AUTO:
Ken Wakasabd391ba2011-08-04 22:44:47 +0900126 return needsToShowIMEButtonWhenVisibilityAuto();
satokd57896a2010-12-20 18:13:30 +0900127 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;
satoka84b8d92010-12-14 13:58:49 +0900133 }
134
satokb70c82d2010-12-20 17:59:19 +0900135 private void refreshStatusIcon() {
satok4fdf1de2011-01-06 19:30:54 +0900136 if (mIcon == null) {
137 return;
138 }
satokb70c82d2010-12-20 17:59:19 +0900139 if (!needsToShowIMEButton()) {
satok82beadf2010-12-27 19:03:06 +0900140 setVisibility(View.GONE);
satok04d50202010-10-25 22:20:12 +0900141 return;
142 } else {
143 setVisibility(View.VISIBLE);
144 }
Jeff Brown2992ea72011-01-28 22:04:14 -0800145 mIcon.setImageResource(R.drawable.ic_sysbar_ime);
satok04d50202010-10-25 22:20:12 +0900146 }
147
satokd57896a2010-12-20 18:13:30 +0900148 private int loadInputMethodSelectorVisibility() {
149 return Settings.Secure.getInt(getContext().getContentResolver(),
150 Settings.Secure.INPUT_METHOD_SELECTOR_VISIBILITY, ID_IME_BUTTON_VISIBILITY_AUTO);
151 }
152
satok913f42d2011-01-17 16:58:10 +0900153 public void setIconImage(int resId) {
154 if (mIcon != null) {
155 mIcon.setImageResource(resId);
156 }
157 }
158
Joe Onorato857fd9b2011-01-27 15:08:35 -0800159 public void setImeWindowStatus(IBinder token, boolean showButton) {
satokcd7cd292010-11-20 15:46:23 +0900160 mToken = token;
Joe Onorato857fd9b2011-01-27 15:08:35 -0800161 mShowButton = showButton;
satokb70c82d2010-12-20 17:59:19 +0900162 refreshStatusIcon();
163 }
164
Jeff Brown2992ea72011-01-28 22:04:14 -0800165 public void setHardKeyboardStatus(boolean available) {
166 if (mHardKeyboardAvailable != available) {
167 mHardKeyboardAvailable = available;
168 refreshStatusIcon();
169 }
170 }
171
satokb70c82d2010-12-20 17:59:19 +0900172 public void setScreenLocked(boolean locked) {
173 mScreenLocked = locked;
174 refreshStatusIcon();
satok04d50202010-10-25 22:20:12 +0900175 }
176}