blob: 00e0b954d7be59e709fb837d2cf3d68d894929ae [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;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010065import com.android.internal.app.AssistUtils;
Clara Bayarric17a5982016-04-15 12:26:47 +010066import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010067import com.android.internal.logging.nano.MetricsProto;
Andrew Sapperstein5c373442016-06-12 13:17:16 -070068import com.android.settingslib.Utils;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010069import com.android.systemui.R;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000070import java.util.ArrayList;
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010071import java.util.Collections;
72import java.util.Comparator;
Clara Bayarri75e09792015-07-29 16:20:40 +010073import java.util.List;
74
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010075/**
76 * Contains functionality for handling keyboard shortcuts.
77 */
Andrei Stingaceanud1519102016-03-31 15:53:33 +010078public final class KeyboardShortcuts {
Clara Bayarri4e850ff2016-03-02 11:12:32 -080079 private static final String TAG = KeyboardShortcuts.class.getSimpleName();
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +010080 private static final Object sLock = new Object();
81 private static KeyboardShortcuts sInstance;
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +010082
Clara Bayarrib999af52016-04-06 16:02:35 +010083 private final SparseArray<String> mSpecialCharacterNames = new SparseArray<>();
84 private final SparseArray<String> mModifierNames = new SparseArray<>();
85 private final SparseArray<Drawable> mSpecialCharacterDrawables = new SparseArray<>();
86 private final SparseArray<Drawable> mModifierDrawables = new SparseArray<>();
Clara Bayarri7ff37982016-06-27 13:58:44 +010087 // Ordered list of modifiers that are supported. All values in this array must exist in
88 // mModifierNames.
89 private final int[] mModifierList = new int[] {
90 KeyEvent.META_META_ON, KeyEvent.META_CTRL_ON, KeyEvent.META_ALT_ON,
91 KeyEvent.META_SHIFT_ON, KeyEvent.META_SYM_ON, KeyEvent.META_FUNCTION_ON
92 };
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000093
94 private final Handler mHandler = new Handler(Looper.getMainLooper());
95 private final Context mContext;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010096 private final IPackageManager mPackageManager;
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010097 private final OnClickListener mDialogCloseListener = new DialogInterface.OnClickListener() {
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000098 public void onClick(DialogInterface dialog, int id) {
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +010099 dismissKeyboardShortcuts();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000100 }
101 };
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100102 private final Comparator<KeyboardShortcutInfo> mApplicationItemsComparator =
103 new Comparator<KeyboardShortcutInfo>() {
104 @Override
105 public int compare(KeyboardShortcutInfo ksh1, KeyboardShortcutInfo ksh2) {
106 boolean ksh1ShouldBeLast = ksh1.getLabel() == null
107 || ksh1.getLabel().toString().isEmpty();
108 boolean ksh2ShouldBeLast = ksh2.getLabel() == null
109 || ksh2.getLabel().toString().isEmpty();
110 if (ksh1ShouldBeLast && ksh2ShouldBeLast) {
111 return 0;
112 }
113 if (ksh1ShouldBeLast) {
114 return 1;
115 }
116 if (ksh2ShouldBeLast) {
117 return -1;
118 }
119 return (ksh1.getLabel().toString()).compareToIgnoreCase(
120 ksh2.getLabel().toString());
121 }
122 };
Clara Bayarri75e09792015-07-29 16:20:40 +0100123
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100124 private Dialog mKeyboardShortcutsDialog;
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800125 private KeyCharacterMap mKeyCharacterMap;
Clara Bayarri382c59e2016-05-18 12:19:17 +0100126 private KeyCharacterMap mBackupKeyCharacterMap;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100127
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100128 private KeyboardShortcuts(Context context) {
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700129 this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_DeviceDefault_Light);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100130 this.mPackageManager = AppGlobals.getPackageManager();
Clara Bayarrib999af52016-04-06 16:02:35 +0100131 loadResources(context);
132 }
133
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100134 private static KeyboardShortcuts getInstance(Context context) {
135 if (sInstance == null) {
136 sInstance = new KeyboardShortcuts(context);
137 }
138 return sInstance;
139 }
140
141 public static void show(Context context, int deviceId) {
Clara Bayarric17a5982016-04-15 12:26:47 +0100142 MetricsLogger.visible(context,
143 MetricsProto.MetricsEvent.KEYBOARD_SHORTCUTS_HELPER);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100144 synchronized (sLock) {
145 if (sInstance != null && !sInstance.mContext.equals(context)) {
146 dismiss();
147 }
148 getInstance(context).showKeyboardShortcuts(deviceId);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100149 }
150 }
151
152 public static void toggle(Context context, int deviceId) {
153 synchronized (sLock) {
Clara Bayarri3ac0ae02016-06-01 17:28:14 +0100154 if (isShowing()) {
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100155 dismiss();
156 } else {
157 show(context, deviceId);
158 }
159 }
160 }
161
162 public static void dismiss() {
163 synchronized (sLock) {
164 if (sInstance != null) {
Clara Bayarri318ef062016-05-26 11:16:12 +0100165 MetricsLogger.hidden(sInstance.mContext,
166 MetricsProto.MetricsEvent.KEYBOARD_SHORTCUTS_HELPER);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100167 sInstance.dismissKeyboardShortcuts();
168 sInstance = null;
169 }
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100170 }
171 }
172
Clara Bayarri3ac0ae02016-06-01 17:28:14 +0100173 private static boolean isShowing() {
174 return sInstance != null && sInstance.mKeyboardShortcutsDialog != null
175 && sInstance.mKeyboardShortcutsDialog.isShowing();
176 }
177
Clara Bayarrib999af52016-04-06 16:02:35 +0100178 private void loadResources(Context context) {
179 mSpecialCharacterNames.put(
180 KeyEvent.KEYCODE_HOME, context.getString(R.string.keyboard_key_home));
181 mSpecialCharacterNames.put(
182 KeyEvent.KEYCODE_BACK, context.getString(R.string.keyboard_key_back));
183 mSpecialCharacterNames.put(
184 KeyEvent.KEYCODE_DPAD_UP, context.getString(R.string.keyboard_key_dpad_up));
185 mSpecialCharacterNames.put(
186 KeyEvent.KEYCODE_DPAD_DOWN, context.getString(R.string.keyboard_key_dpad_down));
187 mSpecialCharacterNames.put(
188 KeyEvent.KEYCODE_DPAD_LEFT, context.getString(R.string.keyboard_key_dpad_left));
189 mSpecialCharacterNames.put(
190 KeyEvent.KEYCODE_DPAD_RIGHT, context.getString(R.string.keyboard_key_dpad_right));
191 mSpecialCharacterNames.put(
192 KeyEvent.KEYCODE_DPAD_CENTER, context.getString(R.string.keyboard_key_dpad_center));
193 mSpecialCharacterNames.put(KeyEvent.KEYCODE_PERIOD, ".");
194 mSpecialCharacterNames.put(
195 KeyEvent.KEYCODE_TAB, context.getString(R.string.keyboard_key_tab));
196 mSpecialCharacterNames.put(
197 KeyEvent.KEYCODE_SPACE, context.getString(R.string.keyboard_key_space));
198 mSpecialCharacterNames.put(
199 KeyEvent.KEYCODE_ENTER, context.getString(R.string.keyboard_key_enter));
200 mSpecialCharacterNames.put(
201 KeyEvent.KEYCODE_DEL, context.getString(R.string.keyboard_key_backspace));
202 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE,
203 context.getString(R.string.keyboard_key_media_play_pause));
204 mSpecialCharacterNames.put(
205 KeyEvent.KEYCODE_MEDIA_STOP, context.getString(R.string.keyboard_key_media_stop));
206 mSpecialCharacterNames.put(
207 KeyEvent.KEYCODE_MEDIA_NEXT, context.getString(R.string.keyboard_key_media_next));
208 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PREVIOUS,
209 context.getString(R.string.keyboard_key_media_previous));
210 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_REWIND,
211 context.getString(R.string.keyboard_key_media_rewind));
212 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD,
213 context.getString(R.string.keyboard_key_media_fast_forward));
214 mSpecialCharacterNames.put(
215 KeyEvent.KEYCODE_PAGE_UP, context.getString(R.string.keyboard_key_page_up));
216 mSpecialCharacterNames.put(
217 KeyEvent.KEYCODE_PAGE_DOWN, context.getString(R.string.keyboard_key_page_down));
218 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_A,
219 context.getString(R.string.keyboard_key_button_template, "A"));
220 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_B,
221 context.getString(R.string.keyboard_key_button_template, "B"));
222 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_C,
223 context.getString(R.string.keyboard_key_button_template, "C"));
224 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_X,
225 context.getString(R.string.keyboard_key_button_template, "X"));
226 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Y,
227 context.getString(R.string.keyboard_key_button_template, "Y"));
228 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Z,
229 context.getString(R.string.keyboard_key_button_template, "Z"));
230 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L1,
231 context.getString(R.string.keyboard_key_button_template, "L1"));
232 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R1,
233 context.getString(R.string.keyboard_key_button_template, "R1"));
234 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L2,
235 context.getString(R.string.keyboard_key_button_template, "L2"));
236 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R2,
237 context.getString(R.string.keyboard_key_button_template, "R2"));
238 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_START,
239 context.getString(R.string.keyboard_key_button_template, "Start"));
240 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_SELECT,
241 context.getString(R.string.keyboard_key_button_template, "Select"));
242 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_MODE,
243 context.getString(R.string.keyboard_key_button_template, "Mode"));
244 mSpecialCharacterNames.put(
245 KeyEvent.KEYCODE_FORWARD_DEL, context.getString(R.string.keyboard_key_forward_del));
246 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ESCAPE, "Esc");
247 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SYSRQ, "SysRq");
248 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BREAK, "Break");
249 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SCROLL_LOCK, "Scroll Lock");
250 mSpecialCharacterNames.put(
251 KeyEvent.KEYCODE_MOVE_HOME, context.getString(R.string.keyboard_key_move_home));
252 mSpecialCharacterNames.put(
253 KeyEvent.KEYCODE_MOVE_END, context.getString(R.string.keyboard_key_move_end));
254 mSpecialCharacterNames.put(
255 KeyEvent.KEYCODE_INSERT, context.getString(R.string.keyboard_key_insert));
256 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F1, "F1");
257 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F2, "F2");
258 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F3, "F3");
259 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F4, "F4");
260 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F5, "F5");
261 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F6, "F6");
262 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F7, "F7");
263 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F8, "F8");
264 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F9, "F9");
265 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F10, "F10");
266 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F11, "F11");
267 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F12, "F12");
268 mSpecialCharacterNames.put(
269 KeyEvent.KEYCODE_NUM_LOCK, context.getString(R.string.keyboard_key_num_lock));
270 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_0,
271 context.getString(R.string.keyboard_key_numpad_template, "0"));
272 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_1,
273 context.getString(R.string.keyboard_key_numpad_template, "1"));
274 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_2,
275 context.getString(R.string.keyboard_key_numpad_template, "2"));
276 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_3,
277 context.getString(R.string.keyboard_key_numpad_template, "3"));
278 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_4,
279 context.getString(R.string.keyboard_key_numpad_template, "4"));
280 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_5,
281 context.getString(R.string.keyboard_key_numpad_template, "5"));
282 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_6,
283 context.getString(R.string.keyboard_key_numpad_template, "6"));
284 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_7,
285 context.getString(R.string.keyboard_key_numpad_template, "7"));
286 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_8,
287 context.getString(R.string.keyboard_key_numpad_template, "8"));
288 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_9,
289 context.getString(R.string.keyboard_key_numpad_template, "9"));
290 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DIVIDE,
291 context.getString(R.string.keyboard_key_numpad_template, "/"));
292 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_MULTIPLY,
293 context.getString(R.string.keyboard_key_numpad_template, "*"));
294 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_SUBTRACT,
295 context.getString(R.string.keyboard_key_numpad_template, "-"));
296 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ADD,
297 context.getString(R.string.keyboard_key_numpad_template, "+"));
298 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DOT,
299 context.getString(R.string.keyboard_key_numpad_template, "."));
300 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_COMMA,
301 context.getString(R.string.keyboard_key_numpad_template, ","));
302 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ENTER,
303 context.getString(R.string.keyboard_key_numpad_template,
304 context.getString(R.string.keyboard_key_enter)));
305 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_EQUALS,
306 context.getString(R.string.keyboard_key_numpad_template, "="));
307 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN,
308 context.getString(R.string.keyboard_key_numpad_template, "("));
309 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN,
310 context.getString(R.string.keyboard_key_numpad_template, ")"));
311 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ZENKAKU_HANKAKU, "半角/全角");
312 mSpecialCharacterNames.put(KeyEvent.KEYCODE_EISU, "英数");
313 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MUHENKAN, "無変換");
314 mSpecialCharacterNames.put(KeyEvent.KEYCODE_HENKAN, "変換");
315 mSpecialCharacterNames.put(KeyEvent.KEYCODE_KATAKANA_HIRAGANA, "かな");
316
317 mModifierNames.put(KeyEvent.META_META_ON, "Meta");
318 mModifierNames.put(KeyEvent.META_CTRL_ON, "Ctrl");
319 mModifierNames.put(KeyEvent.META_ALT_ON, "Alt");
320 mModifierNames.put(KeyEvent.META_SHIFT_ON, "Shift");
321 mModifierNames.put(KeyEvent.META_SYM_ON, "Sym");
322 mModifierNames.put(KeyEvent.META_FUNCTION_ON, "Fn");
323
324 mSpecialCharacterDrawables.put(
325 KeyEvent.KEYCODE_DEL, context.getDrawable(R.drawable.ic_ksh_key_backspace));
326 mSpecialCharacterDrawables.put(
327 KeyEvent.KEYCODE_ENTER, context.getDrawable(R.drawable.ic_ksh_key_enter));
328 mSpecialCharacterDrawables.put(
329 KeyEvent.KEYCODE_DPAD_UP, context.getDrawable(R.drawable.ic_ksh_key_up));
330 mSpecialCharacterDrawables.put(
331 KeyEvent.KEYCODE_DPAD_RIGHT, context.getDrawable(R.drawable.ic_ksh_key_right));
332 mSpecialCharacterDrawables.put(
333 KeyEvent.KEYCODE_DPAD_DOWN, context.getDrawable(R.drawable.ic_ksh_key_down));
334 mSpecialCharacterDrawables.put(
335 KeyEvent.KEYCODE_DPAD_LEFT, context.getDrawable(R.drawable.ic_ksh_key_left));
336
337 mModifierDrawables.put(
338 KeyEvent.META_META_ON, context.getDrawable(R.drawable.ic_ksh_key_meta));
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000339 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100340
Clara Bayarri03f19552016-04-06 10:59:52 +0100341 /**
342 * Retrieves a {@link KeyCharacterMap} and assigns it to mKeyCharacterMap. If the given id is an
343 * existing device, that device's map is used. Otherwise, it checks first all available devices
344 * and if there is a full keyboard it uses that map, otherwise falls back to the Virtual
345 * Keyboard with its default map.
346 */
347 private void retrieveKeyCharacterMap(int deviceId) {
348 final InputManager inputManager = InputManager.getInstance();
Clara Bayarri382c59e2016-05-18 12:19:17 +0100349 mBackupKeyCharacterMap = inputManager.getInputDevice(-1).getKeyCharacterMap();
Clara Bayarri03f19552016-04-06 10:59:52 +0100350 if (deviceId != -1) {
351 final InputDevice inputDevice = inputManager.getInputDevice(deviceId);
352 if (inputDevice != null) {
353 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
354 return;
355 }
356 }
357 final int[] deviceIds = inputManager.getInputDeviceIds();
358 for (int i = 0; i < deviceIds.length; ++i) {
359 final InputDevice inputDevice = inputManager.getInputDevice(deviceIds[i]);
360 // -1 is the Virtual Keyboard, with the default key map. Use that one only as last
361 // resort.
362 if (inputDevice.getId() != -1 && inputDevice.isFullKeyboard()) {
363 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
364 return;
365 }
366 }
Clara Bayarri382c59e2016-05-18 12:19:17 +0100367 // Fall back to -1, the virtual keyboard.
368 mKeyCharacterMap = mBackupKeyCharacterMap;
Clara Bayarri03f19552016-04-06 10:59:52 +0100369 }
370
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100371 private void showKeyboardShortcuts(int deviceId) {
372 retrieveKeyCharacterMap(deviceId);
Winson Chung67f5c8b2018-09-24 12:09:19 -0700373 WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
374 wm.requestAppKeyboardShortcuts(new KeyboardShortcutsReceiver() {
375 @Override
376 public void onKeyboardShortcutsReceived(
377 final List<KeyboardShortcutGroup> result) {
378 result.add(getSystemShortcuts());
379 final KeyboardShortcutGroup appShortcuts = getDefaultApplicationShortcuts();
380 if (appShortcuts != null) {
381 result.add(appShortcuts);
382 }
383 showKeyboardShortcutsDialog(result);
384 }
385 }, deviceId);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100386 }
387
388 private void dismissKeyboardShortcuts() {
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100389 if (mKeyboardShortcutsDialog != null) {
390 mKeyboardShortcutsDialog.dismiss();
391 mKeyboardShortcutsDialog = null;
392 }
393 }
394
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100395 private KeyboardShortcutGroup getSystemShortcuts() {
396 final KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup(
397 mContext.getString(R.string.keyboard_shortcut_group_system), true);
398 systemGroup.addItem(new KeyboardShortcutInfo(
399 mContext.getString(R.string.keyboard_shortcut_group_system_home),
400 KeyEvent.KEYCODE_ENTER,
401 KeyEvent.META_META_ON));
402 systemGroup.addItem(new KeyboardShortcutInfo(
403 mContext.getString(R.string.keyboard_shortcut_group_system_back),
404 KeyEvent.KEYCODE_DEL,
405 KeyEvent.META_META_ON));
406 systemGroup.addItem(new KeyboardShortcutInfo(
407 mContext.getString(R.string.keyboard_shortcut_group_system_recents),
408 KeyEvent.KEYCODE_TAB,
409 KeyEvent.META_ALT_ON));
410 systemGroup.addItem(new KeyboardShortcutInfo(
411 mContext.getString(
412 R.string.keyboard_shortcut_group_system_notifications),
413 KeyEvent.KEYCODE_N,
414 KeyEvent.META_META_ON));
415 systemGroup.addItem(new KeyboardShortcutInfo(
416 mContext.getString(
417 R.string.keyboard_shortcut_group_system_shortcuts_helper),
418 KeyEvent.KEYCODE_SLASH,
419 KeyEvent.META_META_ON));
420 systemGroup.addItem(new KeyboardShortcutInfo(
421 mContext.getString(
422 R.string.keyboard_shortcut_group_system_switch_input),
423 KeyEvent.KEYCODE_SPACE,
424 KeyEvent.META_META_ON));
425 return systemGroup;
426 }
427
428 private KeyboardShortcutGroup getDefaultApplicationShortcuts() {
429 final int userId = mContext.getUserId();
Andrei Stingaceanu9cfcafc2016-04-12 11:07:39 +0100430 List<KeyboardShortcutInfo> keyboardShortcutInfoAppItems = new ArrayList<>();
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100431
432 // Assist.
433 final AssistUtils assistUtils = new AssistUtils(mContext);
434 final ComponentName assistComponent = assistUtils.getAssistComponentForUser(userId);
Andrei Stingaceanuafb993f2017-09-21 11:36:53 +0100435 // Not all devices have an assist component.
436 if (assistComponent != null) {
437 PackageInfo assistPackageInfo = null;
438 try {
439 assistPackageInfo = mPackageManager.getPackageInfo(
440 assistComponent.getPackageName(), 0, userId);
441 } catch (RemoteException e) {
442 Log.e(TAG, "PackageManagerService is dead");
443 }
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100444
Andrei Stingaceanuafb993f2017-09-21 11:36:53 +0100445 if (assistPackageInfo != null) {
446 final Icon assistIcon = Icon.createWithResource(
447 assistPackageInfo.applicationInfo.packageName,
448 assistPackageInfo.applicationInfo.icon);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100449
Andrei Stingaceanuafb993f2017-09-21 11:36:53 +0100450 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
451 mContext.getString(R.string.keyboard_shortcut_group_applications_assist),
452 assistIcon,
453 KeyEvent.KEYCODE_UNKNOWN,
454 KeyEvent.META_META_ON));
455 }
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100456 }
457
458 // Browser.
459 final Icon browserIcon = getIconForIntentCategory(Intent.CATEGORY_APP_BROWSER, userId);
460 if (browserIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100461 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100462 mContext.getString(R.string.keyboard_shortcut_group_applications_browser),
463 browserIcon,
464 KeyEvent.KEYCODE_B,
465 KeyEvent.META_META_ON));
466 }
467
468
469 // Contacts.
470 final Icon contactsIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CONTACTS, userId);
471 if (contactsIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100472 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100473 mContext.getString(R.string.keyboard_shortcut_group_applications_contacts),
474 contactsIcon,
475 KeyEvent.KEYCODE_C,
476 KeyEvent.META_META_ON));
477 }
478
479 // Email.
480 final Icon emailIcon = getIconForIntentCategory(Intent.CATEGORY_APP_EMAIL, userId);
481 if (emailIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100482 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100483 mContext.getString(R.string.keyboard_shortcut_group_applications_email),
484 emailIcon,
485 KeyEvent.KEYCODE_E,
486 KeyEvent.META_META_ON));
487 }
488
489 // Messaging.
490 final Icon messagingIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MESSAGING, userId);
491 if (messagingIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100492 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Peeyush Agarwald86c1062016-10-17 12:33:45 +0100493 mContext.getString(R.string.keyboard_shortcut_group_applications_sms),
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100494 messagingIcon,
Peeyush Agarwald86c1062016-10-17 12:33:45 +0100495 KeyEvent.KEYCODE_S,
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100496 KeyEvent.META_META_ON));
497 }
498
499 // Music.
500 final Icon musicIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MUSIC, userId);
501 if (musicIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100502 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100503 mContext.getString(R.string.keyboard_shortcut_group_applications_music),
504 musicIcon,
505 KeyEvent.KEYCODE_P,
506 KeyEvent.META_META_ON));
507 }
508
509 // Calendar.
510 final Icon calendarIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CALENDAR, userId);
511 if (calendarIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100512 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100513 mContext.getString(R.string.keyboard_shortcut_group_applications_calendar),
514 calendarIcon,
515 KeyEvent.KEYCODE_L,
516 KeyEvent.META_META_ON));
517 }
518
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100519 final int itemsSize = keyboardShortcutInfoAppItems.size();
520 if (itemsSize == 0) {
521 return null;
522 }
523
524 // Sorts by label, case insensitive with nulls and/or empty labels last.
525 Collections.sort(keyboardShortcutInfoAppItems, mApplicationItemsComparator);
526 return new KeyboardShortcutGroup(
527 mContext.getString(R.string.keyboard_shortcut_group_applications),
528 keyboardShortcutInfoAppItems,
529 true);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100530 }
531
532 private Icon getIconForIntentCategory(String intentCategory, int userId) {
533 final Intent intent = new Intent(Intent.ACTION_MAIN);
534 intent.addCategory(intentCategory);
535
536 final PackageInfo packageInfo = getPackageInfoForIntent(intent, userId);
537 if (packageInfo != null && packageInfo.applicationInfo.icon != 0) {
538 return Icon.createWithResource(
539 packageInfo.applicationInfo.packageName,
540 packageInfo.applicationInfo.icon);
541 }
542 return null;
543 }
544
545 private PackageInfo getPackageInfoForIntent(Intent intent, int userId) {
546 try {
547 ResolveInfo handler;
548 handler = mPackageManager.resolveIntent(
549 intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()), 0, userId);
550 if (handler == null || handler.activityInfo == null) {
551 return null;
552 }
553 return mPackageManager.getPackageInfo(handler.activityInfo.packageName, 0, userId);
554 } catch (RemoteException e) {
555 Log.e(TAG, "PackageManagerService is dead", e);
556 return null;
557 }
558 }
559
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000560 private void showKeyboardShortcutsDialog(
561 final List<KeyboardShortcutGroup> keyboardShortcutGroups) {
562 // Need to post on the main thread.
563 mHandler.post(new Runnable() {
564 @Override
565 public void run() {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000566 handleShowKeyboardShortcuts(keyboardShortcutGroups);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000567 }
568 });
569 }
570
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000571 private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) {
572 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
573 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
574 LAYOUT_INFLATER_SERVICE);
575 final View keyboardShortcutsView = inflater.inflate(
576 R.layout.keyboard_shortcuts_view, null);
577 populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById(
578 R.id.keyboard_shortcuts_container), keyboardShortcutGroups);
579 dialogBuilder.setView(keyboardShortcutsView);
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100580 dialogBuilder.setPositiveButton(R.string.quick_settings_done, mDialogCloseListener);
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000581 mKeyboardShortcutsDialog = dialogBuilder.create();
582 mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true);
583 Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow();
584 keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG);
Peeyush Agarwal64f0cae2017-02-09 19:55:20 +0000585 synchronized (sLock) {
586 // showKeyboardShortcutsDialog only if it has not been dismissed already
587 if (sInstance != null) {
588 mKeyboardShortcutsDialog.show();
589 }
590 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000591 }
592
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000593 private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout,
594 List<KeyboardShortcutGroup> keyboardShortcutGroups) {
595 LayoutInflater inflater = LayoutInflater.from(mContext);
596 final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size();
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100597 TextView shortcutsKeyView = (TextView) inflater.inflate(
598 R.layout.keyboard_shortcuts_key_view, null, false);
599 shortcutsKeyView.measure(
600 View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
601 final int shortcutKeyTextItemMinWidth = shortcutsKeyView.getMeasuredHeight();
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100602 // Needed to be able to scale the image items to the same height as the text items.
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100603 final int shortcutKeyIconItemHeightWidth = shortcutsKeyView.getMeasuredHeight()
604 - shortcutsKeyView.getPaddingTop()
605 - shortcutsKeyView.getPaddingBottom();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000606 for (int i = 0; i < keyboardShortcutGroupsSize; i++) {
607 KeyboardShortcutGroup group = keyboardShortcutGroups.get(i);
608 TextView categoryTitle = (TextView) inflater.inflate(
609 R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false);
610 categoryTitle.setText(group.getLabel());
Jason Chang2386a372018-04-24 16:05:30 +0800611 categoryTitle.setTextColor(group.isSystemGroup() ? Utils.getColorAccent(mContext) :
612 ColorStateList.valueOf(mContext.getColor(R.color.ksh_application_group_color)));
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000613 keyboardShortcutsLayout.addView(categoryTitle);
614
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000615 LinearLayout shortcutContainer = (LinearLayout) inflater.inflate(
616 R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000617 final int itemsSize = group.getItems().size();
618 for (int j = 0; j < itemsSize; j++) {
619 KeyboardShortcutInfo info = group.getItems().get(j);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100620 List<StringDrawableContainer> shortcutKeys = getHumanReadableShortcutKeys(info);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800621 if (shortcutKeys == null) {
622 // Ignore shortcuts we can't display keys for.
623 Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping.");
624 continue;
625 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000626 View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item,
627 shortcutContainer, false);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100628
629 if (info.getIcon() != null) {
630 ImageView shortcutIcon = (ImageView) shortcutView
631 .findViewById(R.id.keyboard_shortcuts_icon);
632 shortcutIcon.setImageIcon(info.getIcon());
633 shortcutIcon.setVisibility(View.VISIBLE);
634 }
635
636 TextView shortcutKeyword = (TextView) shortcutView
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000637 .findViewById(R.id.keyboard_shortcuts_keyword);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100638 shortcutKeyword.setText(info.getLabel());
639 if (info.getIcon() != null) {
640 RelativeLayout.LayoutParams lp =
641 (RelativeLayout.LayoutParams) shortcutKeyword.getLayoutParams();
642 lp.removeRule(RelativeLayout.ALIGN_PARENT_START);
643 shortcutKeyword.setLayoutParams(lp);
644 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000645
Andrei Stingaceanu844927d2016-02-16 14:31:58 +0000646 ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000647 .findViewById(R.id.keyboard_shortcuts_item_container);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000648 final int shortcutKeysSize = shortcutKeys.size();
649 for (int k = 0; k < shortcutKeysSize; k++) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100650 StringDrawableContainer shortcutRepresentation = shortcutKeys.get(k);
651 if (shortcutRepresentation.mDrawable != null) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100652 ImageView shortcutKeyIconView = (ImageView) inflater.inflate(
653 R.layout.keyboard_shortcuts_key_icon_view, shortcutItemsContainer,
654 false);
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100655 Bitmap bitmap = Bitmap.createBitmap(shortcutKeyIconItemHeightWidth,
656 shortcutKeyIconItemHeightWidth, Bitmap.Config.ARGB_8888);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100657 Canvas canvas = new Canvas(bitmap);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100658 shortcutRepresentation.mDrawable.setBounds(0, 0, canvas.getWidth(),
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100659 canvas.getHeight());
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100660 shortcutRepresentation.mDrawable.draw(canvas);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100661 shortcutKeyIconView.setImageBitmap(bitmap);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100662 shortcutKeyIconView.setImportantForAccessibility(
663 IMPORTANT_FOR_ACCESSIBILITY_YES);
664 shortcutKeyIconView.setAccessibilityDelegate(
665 new ShortcutKeyAccessibilityDelegate(
666 shortcutRepresentation.mString));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100667 shortcutItemsContainer.addView(shortcutKeyIconView);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100668 } else if (shortcutRepresentation.mString != null) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100669 TextView shortcutKeyTextView = (TextView) inflater.inflate(
670 R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer,
671 false);
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100672 shortcutKeyTextView.setMinimumWidth(shortcutKeyTextItemMinWidth);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100673 shortcutKeyTextView.setText(shortcutRepresentation.mString);
674 shortcutKeyTextView.setAccessibilityDelegate(
675 new ShortcutKeyAccessibilityDelegate(
676 shortcutRepresentation.mString));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100677 shortcutItemsContainer.addView(shortcutKeyTextView);
678 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000679 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000680 shortcutContainer.addView(shortcutView);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000681 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000682 keyboardShortcutsLayout.addView(shortcutContainer);
683 if (i < keyboardShortcutGroupsSize - 1) {
684 View separator = inflater.inflate(
685 R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout,
686 false);
687 keyboardShortcutsLayout.addView(separator);
688 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000689 }
690 }
691
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100692 private List<StringDrawableContainer> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) {
693 List<StringDrawableContainer> shortcutKeys = getHumanReadableModifiers(info);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800694 if (shortcutKeys == null) {
695 return null;
696 }
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100697 String shortcutKeyString = null;
698 Drawable shortcutKeyDrawable = null;
Clara Bayarri1d648a12016-03-23 17:09:02 +0000699 if (info.getBaseCharacter() > Character.MIN_VALUE) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100700 shortcutKeyString = String.valueOf(info.getBaseCharacter());
Clara Bayarrib999af52016-04-06 16:02:35 +0100701 } else if (mSpecialCharacterDrawables.get(info.getKeycode()) != null) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100702 shortcutKeyDrawable = mSpecialCharacterDrawables.get(info.getKeycode());
703 shortcutKeyString = mSpecialCharacterNames.get(info.getKeycode());
Clara Bayarrib999af52016-04-06 16:02:35 +0100704 } else if (mSpecialCharacterNames.get(info.getKeycode()) != null) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100705 shortcutKeyString = mSpecialCharacterNames.get(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800706 } else {
Clara Bayarri1d648a12016-03-23 17:09:02 +0000707 // Special case for shortcuts with no base key or keycode.
708 if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) {
709 return shortcutKeys;
710 }
Clara Bayarri03f19552016-04-06 10:59:52 +0100711 char displayLabel = mKeyCharacterMap.getDisplayLabel(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800712 if (displayLabel != 0) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100713 shortcutKeyString = String.valueOf(displayLabel);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800714 } else {
Clara Bayarri382c59e2016-05-18 12:19:17 +0100715 displayLabel = mBackupKeyCharacterMap.getDisplayLabel(info.getKeycode());
716 if (displayLabel != 0) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100717 shortcutKeyString = String.valueOf(displayLabel);
Clara Bayarri382c59e2016-05-18 12:19:17 +0100718 } else {
719 return null;
720 }
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800721 }
722 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100723
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100724 if (shortcutKeyString != null) {
725 shortcutKeys.add(new StringDrawableContainer(shortcutKeyString, shortcutKeyDrawable));
726 } else {
727 Log.w(TAG, "Keyboard Shortcut does not have a text representation, skipping.");
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100728 }
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100729
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000730 return shortcutKeys;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100731 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800732
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100733 private List<StringDrawableContainer> getHumanReadableModifiers(KeyboardShortcutInfo info) {
734 final List<StringDrawableContainer> shortcutKeys = new ArrayList<>();
Clara Bayarrib9057df2016-03-02 11:37:09 -0800735 int modifiers = info.getModifiers();
736 if (modifiers == 0) {
737 return shortcutKeys;
738 }
Clara Bayarri7ff37982016-06-27 13:58:44 +0100739 for(int i = 0; i < mModifierList.length; ++i) {
740 final int supportedModifier = mModifierList[i];
Clara Bayarrib9057df2016-03-02 11:37:09 -0800741 if ((modifiers & supportedModifier) != 0) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100742 shortcutKeys.add(new StringDrawableContainer(
743 mModifierNames.get(supportedModifier),
744 mModifierDrawables.get(supportedModifier)));
Clara Bayarrib9057df2016-03-02 11:37:09 -0800745 modifiers &= ~supportedModifier;
746 }
747 }
748 if (modifiers != 0) {
749 // Remaining unsupported modifiers, don't show anything.
750 return null;
751 }
752 return shortcutKeys;
753 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100754
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100755 private final class ShortcutKeyAccessibilityDelegate extends AccessibilityDelegate {
756 private String mContentDescription;
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100757
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100758 ShortcutKeyAccessibilityDelegate(String contentDescription) {
759 mContentDescription = contentDescription;
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100760 }
761
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100762 @Override
763 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
764 super.onInitializeAccessibilityNodeInfo(host, info);
765 if (mContentDescription != null) {
766 info.setContentDescription(mContentDescription.toLowerCase());
767 }
768 }
769 }
770
771 private static final class StringDrawableContainer {
772 @NonNull
773 public String mString;
774 @Nullable
775 public Drawable mDrawable;
776
777 StringDrawableContainer(String string, Drawable drawable) {
778 mString = string;
779 mDrawable = drawable;
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100780 }
781 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100782}