blob: 7e6ddcfea76208d8918f18f3d05c5d025c9659a3 [file] [log] [blame]
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +01001/*
2 * Copyright (C) 2016 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;
18
Winson Chung67f5c8b2018-09-24 12:09:19 -070019import static android.content.Context.LAYOUT_INFLATER_SERVICE;
20import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_YES;
21import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
22
Andrei Stingaceanub4817012016-06-13 17:26:39 +010023import android.annotation.NonNull;
24import android.annotation.Nullable;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010025import android.app.AlertDialog;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010026import android.app.AppGlobals;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010027import android.app.Dialog;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010028import android.content.ComponentName;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010029import android.content.Context;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000030import android.content.DialogInterface;
31import android.content.DialogInterface.OnClickListener;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010032import android.content.Intent;
33import android.content.pm.IPackageManager;
34import android.content.pm.PackageInfo;
35import android.content.pm.ResolveInfo;
Jason Chang2386a372018-04-24 16:05:30 +080036import android.content.res.ColorStateList;
Andrei Stingaceanud1519102016-03-31 15:53:33 +010037import android.graphics.Bitmap;
38import android.graphics.Canvas;
39import android.graphics.drawable.Drawable;
Winson Chung67f5c8b2018-09-24 12:09:19 -070040import android.graphics.drawable.Icon;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080041import android.hardware.input.InputManager;
Clara Bayarri75e09792015-07-29 16:20:40 +010042import android.os.Handler;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000043import android.os.Looper;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010044import android.os.RemoteException;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080045import android.util.Log;
46import android.util.SparseArray;
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +000047import android.view.ContextThemeWrapper;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080048import android.view.InputDevice;
49import android.view.KeyCharacterMap;
Clara Bayarri75e09792015-07-29 16:20:40 +010050import android.view.KeyEvent;
51import android.view.KeyboardShortcutGroup;
52import android.view.KeyboardShortcutInfo;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010053import android.view.LayoutInflater;
54import android.view.View;
Andrei Stingaceanub4817012016-06-13 17:26:39 +010055import android.view.View.AccessibilityDelegate;
Andrei Stingaceanu844927d2016-02-16 14:31:58 +000056import android.view.ViewGroup;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010057import android.view.Window;
Winson Chung67f5c8b2018-09-24 12:09:19 -070058import android.view.WindowManager;
Clara Bayarri75e09792015-07-29 16:20:40 +010059import android.view.WindowManager.KeyboardShortcutsReceiver;
Andrei Stingaceanub4817012016-06-13 17:26:39 +010060import android.view.accessibility.AccessibilityNodeInfo;
Andrei Stingaceanud1519102016-03-31 15:53:33 +010061import android.widget.ImageView;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000062import android.widget.LinearLayout;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010063import android.widget.RelativeLayout;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000064import android.widget.TextView;
Gus Prevasab336792018-11-14 13:52:20 -050065
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010066import com.android.internal.app.AssistUtils;
Clara Bayarric17a5982016-04-15 12:26:47 +010067import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010068import com.android.internal.logging.nano.MetricsProto;
Andrew Sapperstein5c373442016-06-12 13:17:16 -070069import com.android.settingslib.Utils;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010070import com.android.systemui.R;
Gus Prevasab336792018-11-14 13:52:20 -050071
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000072import java.util.ArrayList;
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010073import java.util.Collections;
74import java.util.Comparator;
Clara Bayarri75e09792015-07-29 16:20:40 +010075import java.util.List;
76
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010077/**
78 * Contains functionality for handling keyboard shortcuts.
79 */
Andrei Stingaceanud1519102016-03-31 15:53:33 +010080public final class KeyboardShortcuts {
Clara Bayarri4e850ff2016-03-02 11:12:32 -080081 private static final String TAG = KeyboardShortcuts.class.getSimpleName();
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +010082 private static final Object sLock = new Object();
83 private static KeyboardShortcuts sInstance;
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +010084
Clara Bayarrib999af52016-04-06 16:02:35 +010085 private final SparseArray<String> mSpecialCharacterNames = new SparseArray<>();
86 private final SparseArray<String> mModifierNames = new SparseArray<>();
87 private final SparseArray<Drawable> mSpecialCharacterDrawables = new SparseArray<>();
88 private final SparseArray<Drawable> mModifierDrawables = new SparseArray<>();
Clara Bayarri7ff37982016-06-27 13:58:44 +010089 // Ordered list of modifiers that are supported. All values in this array must exist in
90 // mModifierNames.
91 private final int[] mModifierList = new int[] {
92 KeyEvent.META_META_ON, KeyEvent.META_CTRL_ON, KeyEvent.META_ALT_ON,
93 KeyEvent.META_SHIFT_ON, KeyEvent.META_SYM_ON, KeyEvent.META_FUNCTION_ON
94 };
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000095
96 private final Handler mHandler = new Handler(Looper.getMainLooper());
97 private final Context mContext;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010098 private final IPackageManager mPackageManager;
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010099 private final OnClickListener mDialogCloseListener = new DialogInterface.OnClickListener() {
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000100 public void onClick(DialogInterface dialog, int id) {
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100101 dismissKeyboardShortcuts();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000102 }
103 };
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100104 private final Comparator<KeyboardShortcutInfo> mApplicationItemsComparator =
105 new Comparator<KeyboardShortcutInfo>() {
106 @Override
107 public int compare(KeyboardShortcutInfo ksh1, KeyboardShortcutInfo ksh2) {
108 boolean ksh1ShouldBeLast = ksh1.getLabel() == null
109 || ksh1.getLabel().toString().isEmpty();
110 boolean ksh2ShouldBeLast = ksh2.getLabel() == null
111 || ksh2.getLabel().toString().isEmpty();
112 if (ksh1ShouldBeLast && ksh2ShouldBeLast) {
113 return 0;
114 }
115 if (ksh1ShouldBeLast) {
116 return 1;
117 }
118 if (ksh2ShouldBeLast) {
119 return -1;
120 }
121 return (ksh1.getLabel().toString()).compareToIgnoreCase(
122 ksh2.getLabel().toString());
123 }
124 };
Clara Bayarri75e09792015-07-29 16:20:40 +0100125
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100126 private Dialog mKeyboardShortcutsDialog;
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800127 private KeyCharacterMap mKeyCharacterMap;
Clara Bayarri382c59e2016-05-18 12:19:17 +0100128 private KeyCharacterMap mBackupKeyCharacterMap;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100129
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100130 private KeyboardShortcuts(Context context) {
Salvador Martinezc85d6c22019-03-28 19:15:57 -0700131 this.mContext = new ContextThemeWrapper(
132 context, android.R.style.Theme_DeviceDefault_Settings);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100133 this.mPackageManager = AppGlobals.getPackageManager();
Clara Bayarrib999af52016-04-06 16:02:35 +0100134 loadResources(context);
135 }
136
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100137 private static KeyboardShortcuts getInstance(Context context) {
138 if (sInstance == null) {
139 sInstance = new KeyboardShortcuts(context);
140 }
141 return sInstance;
142 }
143
144 public static void show(Context context, int deviceId) {
Clara Bayarric17a5982016-04-15 12:26:47 +0100145 MetricsLogger.visible(context,
146 MetricsProto.MetricsEvent.KEYBOARD_SHORTCUTS_HELPER);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100147 synchronized (sLock) {
148 if (sInstance != null && !sInstance.mContext.equals(context)) {
149 dismiss();
150 }
151 getInstance(context).showKeyboardShortcuts(deviceId);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100152 }
153 }
154
155 public static void toggle(Context context, int deviceId) {
156 synchronized (sLock) {
Clara Bayarri3ac0ae02016-06-01 17:28:14 +0100157 if (isShowing()) {
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100158 dismiss();
159 } else {
160 show(context, deviceId);
161 }
162 }
163 }
164
165 public static void dismiss() {
166 synchronized (sLock) {
167 if (sInstance != null) {
Clara Bayarri318ef062016-05-26 11:16:12 +0100168 MetricsLogger.hidden(sInstance.mContext,
169 MetricsProto.MetricsEvent.KEYBOARD_SHORTCUTS_HELPER);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100170 sInstance.dismissKeyboardShortcuts();
171 sInstance = null;
172 }
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100173 }
174 }
175
Clara Bayarri3ac0ae02016-06-01 17:28:14 +0100176 private static boolean isShowing() {
177 return sInstance != null && sInstance.mKeyboardShortcutsDialog != null
178 && sInstance.mKeyboardShortcutsDialog.isShowing();
179 }
180
Clara Bayarrib999af52016-04-06 16:02:35 +0100181 private void loadResources(Context context) {
182 mSpecialCharacterNames.put(
183 KeyEvent.KEYCODE_HOME, context.getString(R.string.keyboard_key_home));
184 mSpecialCharacterNames.put(
185 KeyEvent.KEYCODE_BACK, context.getString(R.string.keyboard_key_back));
186 mSpecialCharacterNames.put(
187 KeyEvent.KEYCODE_DPAD_UP, context.getString(R.string.keyboard_key_dpad_up));
188 mSpecialCharacterNames.put(
189 KeyEvent.KEYCODE_DPAD_DOWN, context.getString(R.string.keyboard_key_dpad_down));
190 mSpecialCharacterNames.put(
191 KeyEvent.KEYCODE_DPAD_LEFT, context.getString(R.string.keyboard_key_dpad_left));
192 mSpecialCharacterNames.put(
193 KeyEvent.KEYCODE_DPAD_RIGHT, context.getString(R.string.keyboard_key_dpad_right));
194 mSpecialCharacterNames.put(
195 KeyEvent.KEYCODE_DPAD_CENTER, context.getString(R.string.keyboard_key_dpad_center));
196 mSpecialCharacterNames.put(KeyEvent.KEYCODE_PERIOD, ".");
197 mSpecialCharacterNames.put(
198 KeyEvent.KEYCODE_TAB, context.getString(R.string.keyboard_key_tab));
199 mSpecialCharacterNames.put(
200 KeyEvent.KEYCODE_SPACE, context.getString(R.string.keyboard_key_space));
201 mSpecialCharacterNames.put(
202 KeyEvent.KEYCODE_ENTER, context.getString(R.string.keyboard_key_enter));
203 mSpecialCharacterNames.put(
204 KeyEvent.KEYCODE_DEL, context.getString(R.string.keyboard_key_backspace));
205 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE,
206 context.getString(R.string.keyboard_key_media_play_pause));
207 mSpecialCharacterNames.put(
208 KeyEvent.KEYCODE_MEDIA_STOP, context.getString(R.string.keyboard_key_media_stop));
209 mSpecialCharacterNames.put(
210 KeyEvent.KEYCODE_MEDIA_NEXT, context.getString(R.string.keyboard_key_media_next));
211 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PREVIOUS,
212 context.getString(R.string.keyboard_key_media_previous));
213 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_REWIND,
214 context.getString(R.string.keyboard_key_media_rewind));
215 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD,
216 context.getString(R.string.keyboard_key_media_fast_forward));
217 mSpecialCharacterNames.put(
218 KeyEvent.KEYCODE_PAGE_UP, context.getString(R.string.keyboard_key_page_up));
219 mSpecialCharacterNames.put(
220 KeyEvent.KEYCODE_PAGE_DOWN, context.getString(R.string.keyboard_key_page_down));
221 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_A,
222 context.getString(R.string.keyboard_key_button_template, "A"));
223 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_B,
224 context.getString(R.string.keyboard_key_button_template, "B"));
225 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_C,
226 context.getString(R.string.keyboard_key_button_template, "C"));
227 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_X,
228 context.getString(R.string.keyboard_key_button_template, "X"));
229 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Y,
230 context.getString(R.string.keyboard_key_button_template, "Y"));
231 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Z,
232 context.getString(R.string.keyboard_key_button_template, "Z"));
233 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L1,
234 context.getString(R.string.keyboard_key_button_template, "L1"));
235 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R1,
236 context.getString(R.string.keyboard_key_button_template, "R1"));
237 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L2,
238 context.getString(R.string.keyboard_key_button_template, "L2"));
239 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R2,
240 context.getString(R.string.keyboard_key_button_template, "R2"));
241 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_START,
242 context.getString(R.string.keyboard_key_button_template, "Start"));
243 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_SELECT,
244 context.getString(R.string.keyboard_key_button_template, "Select"));
245 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_MODE,
246 context.getString(R.string.keyboard_key_button_template, "Mode"));
247 mSpecialCharacterNames.put(
248 KeyEvent.KEYCODE_FORWARD_DEL, context.getString(R.string.keyboard_key_forward_del));
249 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ESCAPE, "Esc");
250 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SYSRQ, "SysRq");
251 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BREAK, "Break");
252 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SCROLL_LOCK, "Scroll Lock");
253 mSpecialCharacterNames.put(
254 KeyEvent.KEYCODE_MOVE_HOME, context.getString(R.string.keyboard_key_move_home));
255 mSpecialCharacterNames.put(
256 KeyEvent.KEYCODE_MOVE_END, context.getString(R.string.keyboard_key_move_end));
257 mSpecialCharacterNames.put(
258 KeyEvent.KEYCODE_INSERT, context.getString(R.string.keyboard_key_insert));
259 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F1, "F1");
260 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F2, "F2");
261 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F3, "F3");
262 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F4, "F4");
263 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F5, "F5");
264 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F6, "F6");
265 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F7, "F7");
266 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F8, "F8");
267 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F9, "F9");
268 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F10, "F10");
269 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F11, "F11");
270 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F12, "F12");
271 mSpecialCharacterNames.put(
272 KeyEvent.KEYCODE_NUM_LOCK, context.getString(R.string.keyboard_key_num_lock));
273 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_0,
274 context.getString(R.string.keyboard_key_numpad_template, "0"));
275 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_1,
276 context.getString(R.string.keyboard_key_numpad_template, "1"));
277 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_2,
278 context.getString(R.string.keyboard_key_numpad_template, "2"));
279 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_3,
280 context.getString(R.string.keyboard_key_numpad_template, "3"));
281 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_4,
282 context.getString(R.string.keyboard_key_numpad_template, "4"));
283 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_5,
284 context.getString(R.string.keyboard_key_numpad_template, "5"));
285 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_6,
286 context.getString(R.string.keyboard_key_numpad_template, "6"));
287 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_7,
288 context.getString(R.string.keyboard_key_numpad_template, "7"));
289 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_8,
290 context.getString(R.string.keyboard_key_numpad_template, "8"));
291 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_9,
292 context.getString(R.string.keyboard_key_numpad_template, "9"));
293 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DIVIDE,
294 context.getString(R.string.keyboard_key_numpad_template, "/"));
295 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_MULTIPLY,
296 context.getString(R.string.keyboard_key_numpad_template, "*"));
297 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_SUBTRACT,
298 context.getString(R.string.keyboard_key_numpad_template, "-"));
299 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ADD,
300 context.getString(R.string.keyboard_key_numpad_template, "+"));
301 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DOT,
302 context.getString(R.string.keyboard_key_numpad_template, "."));
303 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_COMMA,
304 context.getString(R.string.keyboard_key_numpad_template, ","));
305 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ENTER,
306 context.getString(R.string.keyboard_key_numpad_template,
307 context.getString(R.string.keyboard_key_enter)));
308 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_EQUALS,
309 context.getString(R.string.keyboard_key_numpad_template, "="));
310 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN,
311 context.getString(R.string.keyboard_key_numpad_template, "("));
312 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN,
313 context.getString(R.string.keyboard_key_numpad_template, ")"));
314 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ZENKAKU_HANKAKU, "半角/全角");
315 mSpecialCharacterNames.put(KeyEvent.KEYCODE_EISU, "英数");
316 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MUHENKAN, "無変換");
317 mSpecialCharacterNames.put(KeyEvent.KEYCODE_HENKAN, "変換");
318 mSpecialCharacterNames.put(KeyEvent.KEYCODE_KATAKANA_HIRAGANA, "かな");
319
320 mModifierNames.put(KeyEvent.META_META_ON, "Meta");
321 mModifierNames.put(KeyEvent.META_CTRL_ON, "Ctrl");
322 mModifierNames.put(KeyEvent.META_ALT_ON, "Alt");
323 mModifierNames.put(KeyEvent.META_SHIFT_ON, "Shift");
324 mModifierNames.put(KeyEvent.META_SYM_ON, "Sym");
325 mModifierNames.put(KeyEvent.META_FUNCTION_ON, "Fn");
326
327 mSpecialCharacterDrawables.put(
328 KeyEvent.KEYCODE_DEL, context.getDrawable(R.drawable.ic_ksh_key_backspace));
329 mSpecialCharacterDrawables.put(
330 KeyEvent.KEYCODE_ENTER, context.getDrawable(R.drawable.ic_ksh_key_enter));
331 mSpecialCharacterDrawables.put(
332 KeyEvent.KEYCODE_DPAD_UP, context.getDrawable(R.drawable.ic_ksh_key_up));
333 mSpecialCharacterDrawables.put(
334 KeyEvent.KEYCODE_DPAD_RIGHT, context.getDrawable(R.drawable.ic_ksh_key_right));
335 mSpecialCharacterDrawables.put(
336 KeyEvent.KEYCODE_DPAD_DOWN, context.getDrawable(R.drawable.ic_ksh_key_down));
337 mSpecialCharacterDrawables.put(
338 KeyEvent.KEYCODE_DPAD_LEFT, context.getDrawable(R.drawable.ic_ksh_key_left));
339
340 mModifierDrawables.put(
341 KeyEvent.META_META_ON, context.getDrawable(R.drawable.ic_ksh_key_meta));
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000342 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100343
Clara Bayarri03f19552016-04-06 10:59:52 +0100344 /**
345 * Retrieves a {@link KeyCharacterMap} and assigns it to mKeyCharacterMap. If the given id is an
346 * existing device, that device's map is used. Otherwise, it checks first all available devices
347 * and if there is a full keyboard it uses that map, otherwise falls back to the Virtual
348 * Keyboard with its default map.
349 */
350 private void retrieveKeyCharacterMap(int deviceId) {
351 final InputManager inputManager = InputManager.getInstance();
Clara Bayarri382c59e2016-05-18 12:19:17 +0100352 mBackupKeyCharacterMap = inputManager.getInputDevice(-1).getKeyCharacterMap();
Clara Bayarri03f19552016-04-06 10:59:52 +0100353 if (deviceId != -1) {
354 final InputDevice inputDevice = inputManager.getInputDevice(deviceId);
355 if (inputDevice != null) {
356 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
357 return;
358 }
359 }
360 final int[] deviceIds = inputManager.getInputDeviceIds();
361 for (int i = 0; i < deviceIds.length; ++i) {
362 final InputDevice inputDevice = inputManager.getInputDevice(deviceIds[i]);
363 // -1 is the Virtual Keyboard, with the default key map. Use that one only as last
364 // resort.
365 if (inputDevice.getId() != -1 && inputDevice.isFullKeyboard()) {
366 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
367 return;
368 }
369 }
Clara Bayarri382c59e2016-05-18 12:19:17 +0100370 // Fall back to -1, the virtual keyboard.
371 mKeyCharacterMap = mBackupKeyCharacterMap;
Clara Bayarri03f19552016-04-06 10:59:52 +0100372 }
373
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100374 private void showKeyboardShortcuts(int deviceId) {
375 retrieveKeyCharacterMap(deviceId);
Winson Chung67f5c8b2018-09-24 12:09:19 -0700376 WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
377 wm.requestAppKeyboardShortcuts(new KeyboardShortcutsReceiver() {
378 @Override
379 public void onKeyboardShortcutsReceived(
380 final List<KeyboardShortcutGroup> result) {
381 result.add(getSystemShortcuts());
382 final KeyboardShortcutGroup appShortcuts = getDefaultApplicationShortcuts();
383 if (appShortcuts != null) {
384 result.add(appShortcuts);
385 }
386 showKeyboardShortcutsDialog(result);
387 }
388 }, deviceId);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100389 }
390
391 private void dismissKeyboardShortcuts() {
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100392 if (mKeyboardShortcutsDialog != null) {
393 mKeyboardShortcutsDialog.dismiss();
394 mKeyboardShortcutsDialog = null;
395 }
396 }
397
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100398 private KeyboardShortcutGroup getSystemShortcuts() {
399 final KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup(
400 mContext.getString(R.string.keyboard_shortcut_group_system), true);
401 systemGroup.addItem(new KeyboardShortcutInfo(
402 mContext.getString(R.string.keyboard_shortcut_group_system_home),
403 KeyEvent.KEYCODE_ENTER,
404 KeyEvent.META_META_ON));
405 systemGroup.addItem(new KeyboardShortcutInfo(
406 mContext.getString(R.string.keyboard_shortcut_group_system_back),
407 KeyEvent.KEYCODE_DEL,
408 KeyEvent.META_META_ON));
409 systemGroup.addItem(new KeyboardShortcutInfo(
410 mContext.getString(R.string.keyboard_shortcut_group_system_recents),
411 KeyEvent.KEYCODE_TAB,
412 KeyEvent.META_ALT_ON));
413 systemGroup.addItem(new KeyboardShortcutInfo(
414 mContext.getString(
415 R.string.keyboard_shortcut_group_system_notifications),
416 KeyEvent.KEYCODE_N,
417 KeyEvent.META_META_ON));
418 systemGroup.addItem(new KeyboardShortcutInfo(
419 mContext.getString(
420 R.string.keyboard_shortcut_group_system_shortcuts_helper),
421 KeyEvent.KEYCODE_SLASH,
422 KeyEvent.META_META_ON));
423 systemGroup.addItem(new KeyboardShortcutInfo(
424 mContext.getString(
425 R.string.keyboard_shortcut_group_system_switch_input),
426 KeyEvent.KEYCODE_SPACE,
427 KeyEvent.META_META_ON));
428 return systemGroup;
429 }
430
431 private KeyboardShortcutGroup getDefaultApplicationShortcuts() {
432 final int userId = mContext.getUserId();
Andrei Stingaceanu9cfcafc2016-04-12 11:07:39 +0100433 List<KeyboardShortcutInfo> keyboardShortcutInfoAppItems = new ArrayList<>();
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100434
435 // Assist.
436 final AssistUtils assistUtils = new AssistUtils(mContext);
437 final ComponentName assistComponent = assistUtils.getAssistComponentForUser(userId);
Andrei Stingaceanuafb993f2017-09-21 11:36:53 +0100438 // Not all devices have an assist component.
439 if (assistComponent != null) {
440 PackageInfo assistPackageInfo = null;
441 try {
442 assistPackageInfo = mPackageManager.getPackageInfo(
443 assistComponent.getPackageName(), 0, userId);
444 } catch (RemoteException e) {
445 Log.e(TAG, "PackageManagerService is dead");
446 }
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100447
Andrei Stingaceanuafb993f2017-09-21 11:36:53 +0100448 if (assistPackageInfo != null) {
449 final Icon assistIcon = Icon.createWithResource(
450 assistPackageInfo.applicationInfo.packageName,
451 assistPackageInfo.applicationInfo.icon);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100452
Andrei Stingaceanuafb993f2017-09-21 11:36:53 +0100453 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
454 mContext.getString(R.string.keyboard_shortcut_group_applications_assist),
455 assistIcon,
456 KeyEvent.KEYCODE_UNKNOWN,
457 KeyEvent.META_META_ON));
458 }
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100459 }
460
461 // Browser.
462 final Icon browserIcon = getIconForIntentCategory(Intent.CATEGORY_APP_BROWSER, userId);
463 if (browserIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100464 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100465 mContext.getString(R.string.keyboard_shortcut_group_applications_browser),
466 browserIcon,
467 KeyEvent.KEYCODE_B,
468 KeyEvent.META_META_ON));
469 }
470
471
472 // Contacts.
473 final Icon contactsIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CONTACTS, userId);
474 if (contactsIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100475 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100476 mContext.getString(R.string.keyboard_shortcut_group_applications_contacts),
477 contactsIcon,
478 KeyEvent.KEYCODE_C,
479 KeyEvent.META_META_ON));
480 }
481
482 // Email.
483 final Icon emailIcon = getIconForIntentCategory(Intent.CATEGORY_APP_EMAIL, userId);
484 if (emailIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100485 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100486 mContext.getString(R.string.keyboard_shortcut_group_applications_email),
487 emailIcon,
488 KeyEvent.KEYCODE_E,
489 KeyEvent.META_META_ON));
490 }
491
492 // Messaging.
493 final Icon messagingIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MESSAGING, userId);
494 if (messagingIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100495 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Peeyush Agarwald86c1062016-10-17 12:33:45 +0100496 mContext.getString(R.string.keyboard_shortcut_group_applications_sms),
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100497 messagingIcon,
Peeyush Agarwald86c1062016-10-17 12:33:45 +0100498 KeyEvent.KEYCODE_S,
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100499 KeyEvent.META_META_ON));
500 }
501
502 // Music.
503 final Icon musicIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MUSIC, userId);
504 if (musicIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100505 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100506 mContext.getString(R.string.keyboard_shortcut_group_applications_music),
507 musicIcon,
508 KeyEvent.KEYCODE_P,
509 KeyEvent.META_META_ON));
510 }
511
512 // Calendar.
513 final Icon calendarIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CALENDAR, userId);
514 if (calendarIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100515 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100516 mContext.getString(R.string.keyboard_shortcut_group_applications_calendar),
517 calendarIcon,
518 KeyEvent.KEYCODE_L,
519 KeyEvent.META_META_ON));
520 }
521
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100522 final int itemsSize = keyboardShortcutInfoAppItems.size();
523 if (itemsSize == 0) {
524 return null;
525 }
526
527 // Sorts by label, case insensitive with nulls and/or empty labels last.
528 Collections.sort(keyboardShortcutInfoAppItems, mApplicationItemsComparator);
529 return new KeyboardShortcutGroup(
530 mContext.getString(R.string.keyboard_shortcut_group_applications),
531 keyboardShortcutInfoAppItems,
532 true);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100533 }
534
535 private Icon getIconForIntentCategory(String intentCategory, int userId) {
536 final Intent intent = new Intent(Intent.ACTION_MAIN);
537 intent.addCategory(intentCategory);
538
539 final PackageInfo packageInfo = getPackageInfoForIntent(intent, userId);
540 if (packageInfo != null && packageInfo.applicationInfo.icon != 0) {
541 return Icon.createWithResource(
542 packageInfo.applicationInfo.packageName,
543 packageInfo.applicationInfo.icon);
544 }
545 return null;
546 }
547
548 private PackageInfo getPackageInfoForIntent(Intent intent, int userId) {
549 try {
550 ResolveInfo handler;
551 handler = mPackageManager.resolveIntent(
552 intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()), 0, userId);
553 if (handler == null || handler.activityInfo == null) {
554 return null;
555 }
556 return mPackageManager.getPackageInfo(handler.activityInfo.packageName, 0, userId);
557 } catch (RemoteException e) {
558 Log.e(TAG, "PackageManagerService is dead", e);
559 return null;
560 }
561 }
562
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000563 private void showKeyboardShortcutsDialog(
564 final List<KeyboardShortcutGroup> keyboardShortcutGroups) {
565 // Need to post on the main thread.
566 mHandler.post(new Runnable() {
567 @Override
568 public void run() {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000569 handleShowKeyboardShortcuts(keyboardShortcutGroups);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000570 }
571 });
572 }
573
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000574 private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) {
575 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
576 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
577 LAYOUT_INFLATER_SERVICE);
578 final View keyboardShortcutsView = inflater.inflate(
579 R.layout.keyboard_shortcuts_view, null);
580 populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById(
581 R.id.keyboard_shortcuts_container), keyboardShortcutGroups);
582 dialogBuilder.setView(keyboardShortcutsView);
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100583 dialogBuilder.setPositiveButton(R.string.quick_settings_done, mDialogCloseListener);
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000584 mKeyboardShortcutsDialog = dialogBuilder.create();
585 mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true);
586 Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow();
587 keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG);
Peeyush Agarwal64f0cae2017-02-09 19:55:20 +0000588 synchronized (sLock) {
589 // showKeyboardShortcutsDialog only if it has not been dismissed already
590 if (sInstance != null) {
591 mKeyboardShortcutsDialog.show();
592 }
593 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000594 }
595
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000596 private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout,
597 List<KeyboardShortcutGroup> keyboardShortcutGroups) {
598 LayoutInflater inflater = LayoutInflater.from(mContext);
599 final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size();
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100600 TextView shortcutsKeyView = (TextView) inflater.inflate(
601 R.layout.keyboard_shortcuts_key_view, null, false);
602 shortcutsKeyView.measure(
603 View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
604 final int shortcutKeyTextItemMinWidth = shortcutsKeyView.getMeasuredHeight();
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100605 // Needed to be able to scale the image items to the same height as the text items.
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100606 final int shortcutKeyIconItemHeightWidth = shortcutsKeyView.getMeasuredHeight()
607 - shortcutsKeyView.getPaddingTop()
608 - shortcutsKeyView.getPaddingBottom();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000609 for (int i = 0; i < keyboardShortcutGroupsSize; i++) {
610 KeyboardShortcutGroup group = keyboardShortcutGroups.get(i);
611 TextView categoryTitle = (TextView) inflater.inflate(
612 R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false);
613 categoryTitle.setText(group.getLabel());
Jason Chang2386a372018-04-24 16:05:30 +0800614 categoryTitle.setTextColor(group.isSystemGroup() ? Utils.getColorAccent(mContext) :
615 ColorStateList.valueOf(mContext.getColor(R.color.ksh_application_group_color)));
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000616 keyboardShortcutsLayout.addView(categoryTitle);
617
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000618 LinearLayout shortcutContainer = (LinearLayout) inflater.inflate(
619 R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000620 final int itemsSize = group.getItems().size();
621 for (int j = 0; j < itemsSize; j++) {
622 KeyboardShortcutInfo info = group.getItems().get(j);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100623 List<StringDrawableContainer> shortcutKeys = getHumanReadableShortcutKeys(info);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800624 if (shortcutKeys == null) {
625 // Ignore shortcuts we can't display keys for.
626 Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping.");
627 continue;
628 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000629 View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item,
630 shortcutContainer, false);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100631
632 if (info.getIcon() != null) {
633 ImageView shortcutIcon = (ImageView) shortcutView
634 .findViewById(R.id.keyboard_shortcuts_icon);
635 shortcutIcon.setImageIcon(info.getIcon());
636 shortcutIcon.setVisibility(View.VISIBLE);
637 }
638
639 TextView shortcutKeyword = (TextView) shortcutView
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000640 .findViewById(R.id.keyboard_shortcuts_keyword);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100641 shortcutKeyword.setText(info.getLabel());
642 if (info.getIcon() != null) {
643 RelativeLayout.LayoutParams lp =
644 (RelativeLayout.LayoutParams) shortcutKeyword.getLayoutParams();
645 lp.removeRule(RelativeLayout.ALIGN_PARENT_START);
646 shortcutKeyword.setLayoutParams(lp);
647 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000648
Andrei Stingaceanu844927d2016-02-16 14:31:58 +0000649 ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000650 .findViewById(R.id.keyboard_shortcuts_item_container);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000651 final int shortcutKeysSize = shortcutKeys.size();
652 for (int k = 0; k < shortcutKeysSize; k++) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100653 StringDrawableContainer shortcutRepresentation = shortcutKeys.get(k);
654 if (shortcutRepresentation.mDrawable != null) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100655 ImageView shortcutKeyIconView = (ImageView) inflater.inflate(
656 R.layout.keyboard_shortcuts_key_icon_view, shortcutItemsContainer,
657 false);
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100658 Bitmap bitmap = Bitmap.createBitmap(shortcutKeyIconItemHeightWidth,
659 shortcutKeyIconItemHeightWidth, Bitmap.Config.ARGB_8888);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100660 Canvas canvas = new Canvas(bitmap);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100661 shortcutRepresentation.mDrawable.setBounds(0, 0, canvas.getWidth(),
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100662 canvas.getHeight());
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100663 shortcutRepresentation.mDrawable.draw(canvas);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100664 shortcutKeyIconView.setImageBitmap(bitmap);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100665 shortcutKeyIconView.setImportantForAccessibility(
666 IMPORTANT_FOR_ACCESSIBILITY_YES);
667 shortcutKeyIconView.setAccessibilityDelegate(
668 new ShortcutKeyAccessibilityDelegate(
669 shortcutRepresentation.mString));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100670 shortcutItemsContainer.addView(shortcutKeyIconView);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100671 } else if (shortcutRepresentation.mString != null) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100672 TextView shortcutKeyTextView = (TextView) inflater.inflate(
673 R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer,
674 false);
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100675 shortcutKeyTextView.setMinimumWidth(shortcutKeyTextItemMinWidth);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100676 shortcutKeyTextView.setText(shortcutRepresentation.mString);
677 shortcutKeyTextView.setAccessibilityDelegate(
678 new ShortcutKeyAccessibilityDelegate(
679 shortcutRepresentation.mString));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100680 shortcutItemsContainer.addView(shortcutKeyTextView);
681 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000682 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000683 shortcutContainer.addView(shortcutView);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000684 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000685 keyboardShortcutsLayout.addView(shortcutContainer);
686 if (i < keyboardShortcutGroupsSize - 1) {
687 View separator = inflater.inflate(
688 R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout,
689 false);
690 keyboardShortcutsLayout.addView(separator);
691 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000692 }
693 }
694
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100695 private List<StringDrawableContainer> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) {
696 List<StringDrawableContainer> shortcutKeys = getHumanReadableModifiers(info);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800697 if (shortcutKeys == null) {
698 return null;
699 }
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100700 String shortcutKeyString = null;
701 Drawable shortcutKeyDrawable = null;
Clara Bayarri1d648a12016-03-23 17:09:02 +0000702 if (info.getBaseCharacter() > Character.MIN_VALUE) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100703 shortcutKeyString = String.valueOf(info.getBaseCharacter());
Clara Bayarrib999af52016-04-06 16:02:35 +0100704 } else if (mSpecialCharacterDrawables.get(info.getKeycode()) != null) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100705 shortcutKeyDrawable = mSpecialCharacterDrawables.get(info.getKeycode());
706 shortcutKeyString = mSpecialCharacterNames.get(info.getKeycode());
Clara Bayarrib999af52016-04-06 16:02:35 +0100707 } else if (mSpecialCharacterNames.get(info.getKeycode()) != null) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100708 shortcutKeyString = mSpecialCharacterNames.get(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800709 } else {
Clara Bayarri1d648a12016-03-23 17:09:02 +0000710 // Special case for shortcuts with no base key or keycode.
711 if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) {
712 return shortcutKeys;
713 }
Clara Bayarri03f19552016-04-06 10:59:52 +0100714 char displayLabel = mKeyCharacterMap.getDisplayLabel(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800715 if (displayLabel != 0) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100716 shortcutKeyString = String.valueOf(displayLabel);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800717 } else {
Clara Bayarri382c59e2016-05-18 12:19:17 +0100718 displayLabel = mBackupKeyCharacterMap.getDisplayLabel(info.getKeycode());
719 if (displayLabel != 0) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100720 shortcutKeyString = String.valueOf(displayLabel);
Clara Bayarri382c59e2016-05-18 12:19:17 +0100721 } else {
722 return null;
723 }
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800724 }
725 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100726
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100727 if (shortcutKeyString != null) {
728 shortcutKeys.add(new StringDrawableContainer(shortcutKeyString, shortcutKeyDrawable));
729 } else {
730 Log.w(TAG, "Keyboard Shortcut does not have a text representation, skipping.");
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100731 }
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100732
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000733 return shortcutKeys;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100734 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800735
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100736 private List<StringDrawableContainer> getHumanReadableModifiers(KeyboardShortcutInfo info) {
737 final List<StringDrawableContainer> shortcutKeys = new ArrayList<>();
Clara Bayarrib9057df2016-03-02 11:37:09 -0800738 int modifiers = info.getModifiers();
739 if (modifiers == 0) {
740 return shortcutKeys;
741 }
Clara Bayarri7ff37982016-06-27 13:58:44 +0100742 for(int i = 0; i < mModifierList.length; ++i) {
743 final int supportedModifier = mModifierList[i];
Clara Bayarrib9057df2016-03-02 11:37:09 -0800744 if ((modifiers & supportedModifier) != 0) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100745 shortcutKeys.add(new StringDrawableContainer(
746 mModifierNames.get(supportedModifier),
747 mModifierDrawables.get(supportedModifier)));
Clara Bayarrib9057df2016-03-02 11:37:09 -0800748 modifiers &= ~supportedModifier;
749 }
750 }
751 if (modifiers != 0) {
752 // Remaining unsupported modifiers, don't show anything.
753 return null;
754 }
755 return shortcutKeys;
756 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100757
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100758 private final class ShortcutKeyAccessibilityDelegate extends AccessibilityDelegate {
759 private String mContentDescription;
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100760
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100761 ShortcutKeyAccessibilityDelegate(String contentDescription) {
762 mContentDescription = contentDescription;
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100763 }
764
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100765 @Override
766 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
767 super.onInitializeAccessibilityNodeInfo(host, info);
768 if (mContentDescription != null) {
769 info.setContentDescription(mContentDescription.toLowerCase());
770 }
771 }
772 }
773
774 private static final class StringDrawableContainer {
775 @NonNull
776 public String mString;
777 @Nullable
778 public Drawable mDrawable;
779
780 StringDrawableContainer(String string, Drawable drawable) {
781 mString = string;
782 mDrawable = drawable;
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100783 }
784 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100785}