blob: 2d16d2209c9e5e6c8b2eb34f2cab3937dc512428 [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
Andrei Stingaceanub4817012016-06-13 17:26:39 +010019import android.annotation.NonNull;
20import android.annotation.Nullable;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010021import android.app.AlertDialog;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010022import android.app.AppGlobals;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010023import android.app.Dialog;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010024import android.content.ComponentName;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010025import android.content.Context;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000026import android.content.DialogInterface;
27import android.content.DialogInterface.OnClickListener;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010028import android.content.Intent;
29import android.content.pm.IPackageManager;
30import android.content.pm.PackageInfo;
31import android.content.pm.ResolveInfo;
32import android.graphics.drawable.Icon;
Andrei Stingaceanud1519102016-03-31 15:53:33 +010033import android.graphics.Bitmap;
34import android.graphics.Canvas;
35import android.graphics.drawable.Drawable;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080036import android.hardware.input.InputManager;
Clara Bayarri75e09792015-07-29 16:20:40 +010037import android.os.Handler;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000038import android.os.Looper;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010039import android.os.RemoteException;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080040import android.util.Log;
41import android.util.SparseArray;
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +000042import android.view.ContextThemeWrapper;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080043import android.view.InputDevice;
44import android.view.KeyCharacterMap;
Clara Bayarri75e09792015-07-29 16:20:40 +010045import android.view.KeyEvent;
46import android.view.KeyboardShortcutGroup;
47import android.view.KeyboardShortcutInfo;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010048import android.view.LayoutInflater;
49import android.view.View;
Andrei Stingaceanub4817012016-06-13 17:26:39 +010050import android.view.View.AccessibilityDelegate;
Andrei Stingaceanu844927d2016-02-16 14:31:58 +000051import android.view.ViewGroup;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010052import android.view.Window;
Clara Bayarri75e09792015-07-29 16:20:40 +010053import android.view.WindowManager.KeyboardShortcutsReceiver;
Andrei Stingaceanub4817012016-06-13 17:26:39 +010054import android.view.accessibility.AccessibilityNodeInfo;
Andrei Stingaceanud1519102016-03-31 15:53:33 +010055import android.widget.ImageView;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000056import android.widget.LinearLayout;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010057import android.widget.RelativeLayout;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000058import android.widget.TextView;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010059
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010060import com.android.internal.app.AssistUtils;
Clara Bayarric17a5982016-04-15 12:26:47 +010061import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010062import com.android.internal.logging.nano.MetricsProto;
Andrew Sapperstein5c373442016-06-12 13:17:16 -070063import com.android.settingslib.Utils;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010064import com.android.systemui.R;
Daichi Hirono3871faa2017-04-05 12:57:28 +090065import com.android.systemui.recents.misc.SystemServicesProxy;
Clara Bayarri75e09792015-07-29 16:20:40 +010066
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000067import java.util.ArrayList;
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010068import java.util.Collections;
69import java.util.Comparator;
Clara Bayarri75e09792015-07-29 16:20:40 +010070import java.util.List;
71
72import static android.content.Context.LAYOUT_INFLATER_SERVICE;
Andrei Stingaceanub4817012016-06-13 17:26:39 +010073import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_YES;
Clara Bayarri75e09792015-07-29 16:20:40 +010074import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010075
76/**
77 * Contains functionality for handling keyboard shortcuts.
78 */
Andrei Stingaceanud1519102016-03-31 15:53:33 +010079public final class KeyboardShortcuts {
Clara Bayarri4e850ff2016-03-02 11:12:32 -080080 private static final String TAG = KeyboardShortcuts.class.getSimpleName();
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +010081 private static final Object sLock = new Object();
82 private static KeyboardShortcuts sInstance;
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +010083
Clara Bayarrib999af52016-04-06 16:02:35 +010084 private final SparseArray<String> mSpecialCharacterNames = new SparseArray<>();
85 private final SparseArray<String> mModifierNames = new SparseArray<>();
86 private final SparseArray<Drawable> mSpecialCharacterDrawables = new SparseArray<>();
87 private final SparseArray<Drawable> mModifierDrawables = new SparseArray<>();
Clara Bayarri7ff37982016-06-27 13:58:44 +010088 // Ordered list of modifiers that are supported. All values in this array must exist in
89 // mModifierNames.
90 private final int[] mModifierList = new int[] {
91 KeyEvent.META_META_ON, KeyEvent.META_CTRL_ON, KeyEvent.META_ALT_ON,
92 KeyEvent.META_SHIFT_ON, KeyEvent.META_SYM_ON, KeyEvent.META_FUNCTION_ON
93 };
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000094
95 private final Handler mHandler = new Handler(Looper.getMainLooper());
96 private final Context mContext;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010097 private final IPackageManager mPackageManager;
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010098 private final OnClickListener mDialogCloseListener = new DialogInterface.OnClickListener() {
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000099 public void onClick(DialogInterface dialog, int id) {
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100100 dismissKeyboardShortcuts();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000101 }
102 };
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100103 private final Comparator<KeyboardShortcutInfo> mApplicationItemsComparator =
104 new Comparator<KeyboardShortcutInfo>() {
105 @Override
106 public int compare(KeyboardShortcutInfo ksh1, KeyboardShortcutInfo ksh2) {
107 boolean ksh1ShouldBeLast = ksh1.getLabel() == null
108 || ksh1.getLabel().toString().isEmpty();
109 boolean ksh2ShouldBeLast = ksh2.getLabel() == null
110 || ksh2.getLabel().toString().isEmpty();
111 if (ksh1ShouldBeLast && ksh2ShouldBeLast) {
112 return 0;
113 }
114 if (ksh1ShouldBeLast) {
115 return 1;
116 }
117 if (ksh2ShouldBeLast) {
118 return -1;
119 }
120 return (ksh1.getLabel().toString()).compareToIgnoreCase(
121 ksh2.getLabel().toString());
122 }
123 };
Clara Bayarri75e09792015-07-29 16:20:40 +0100124
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100125 private Dialog mKeyboardShortcutsDialog;
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800126 private KeyCharacterMap mKeyCharacterMap;
Clara Bayarri382c59e2016-05-18 12:19:17 +0100127 private KeyCharacterMap mBackupKeyCharacterMap;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100128
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100129 private KeyboardShortcuts(Context context) {
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700130 this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_DeviceDefault_Light);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100131 this.mPackageManager = AppGlobals.getPackageManager();
Clara Bayarrib999af52016-04-06 16:02:35 +0100132 loadResources(context);
133 }
134
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100135 private static KeyboardShortcuts getInstance(Context context) {
136 if (sInstance == null) {
137 sInstance = new KeyboardShortcuts(context);
138 }
139 return sInstance;
140 }
141
142 public static void show(Context context, int deviceId) {
Clara Bayarric17a5982016-04-15 12:26:47 +0100143 MetricsLogger.visible(context,
144 MetricsProto.MetricsEvent.KEYBOARD_SHORTCUTS_HELPER);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100145 synchronized (sLock) {
146 if (sInstance != null && !sInstance.mContext.equals(context)) {
147 dismiss();
148 }
149 getInstance(context).showKeyboardShortcuts(deviceId);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100150 }
151 }
152
153 public static void toggle(Context context, int deviceId) {
154 synchronized (sLock) {
Clara Bayarri3ac0ae02016-06-01 17:28:14 +0100155 if (isShowing()) {
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100156 dismiss();
157 } else {
158 show(context, deviceId);
159 }
160 }
161 }
162
163 public static void dismiss() {
164 synchronized (sLock) {
165 if (sInstance != null) {
Clara Bayarri318ef062016-05-26 11:16:12 +0100166 MetricsLogger.hidden(sInstance.mContext,
167 MetricsProto.MetricsEvent.KEYBOARD_SHORTCUTS_HELPER);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100168 sInstance.dismissKeyboardShortcuts();
169 sInstance = null;
170 }
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100171 }
172 }
173
Clara Bayarri3ac0ae02016-06-01 17:28:14 +0100174 private static boolean isShowing() {
175 return sInstance != null && sInstance.mKeyboardShortcutsDialog != null
176 && sInstance.mKeyboardShortcutsDialog.isShowing();
177 }
178
Clara Bayarrib999af52016-04-06 16:02:35 +0100179 private void loadResources(Context context) {
180 mSpecialCharacterNames.put(
181 KeyEvent.KEYCODE_HOME, context.getString(R.string.keyboard_key_home));
182 mSpecialCharacterNames.put(
183 KeyEvent.KEYCODE_BACK, context.getString(R.string.keyboard_key_back));
184 mSpecialCharacterNames.put(
185 KeyEvent.KEYCODE_DPAD_UP, context.getString(R.string.keyboard_key_dpad_up));
186 mSpecialCharacterNames.put(
187 KeyEvent.KEYCODE_DPAD_DOWN, context.getString(R.string.keyboard_key_dpad_down));
188 mSpecialCharacterNames.put(
189 KeyEvent.KEYCODE_DPAD_LEFT, context.getString(R.string.keyboard_key_dpad_left));
190 mSpecialCharacterNames.put(
191 KeyEvent.KEYCODE_DPAD_RIGHT, context.getString(R.string.keyboard_key_dpad_right));
192 mSpecialCharacterNames.put(
193 KeyEvent.KEYCODE_DPAD_CENTER, context.getString(R.string.keyboard_key_dpad_center));
194 mSpecialCharacterNames.put(KeyEvent.KEYCODE_PERIOD, ".");
195 mSpecialCharacterNames.put(
196 KeyEvent.KEYCODE_TAB, context.getString(R.string.keyboard_key_tab));
197 mSpecialCharacterNames.put(
198 KeyEvent.KEYCODE_SPACE, context.getString(R.string.keyboard_key_space));
199 mSpecialCharacterNames.put(
200 KeyEvent.KEYCODE_ENTER, context.getString(R.string.keyboard_key_enter));
201 mSpecialCharacterNames.put(
202 KeyEvent.KEYCODE_DEL, context.getString(R.string.keyboard_key_backspace));
203 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE,
204 context.getString(R.string.keyboard_key_media_play_pause));
205 mSpecialCharacterNames.put(
206 KeyEvent.KEYCODE_MEDIA_STOP, context.getString(R.string.keyboard_key_media_stop));
207 mSpecialCharacterNames.put(
208 KeyEvent.KEYCODE_MEDIA_NEXT, context.getString(R.string.keyboard_key_media_next));
209 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PREVIOUS,
210 context.getString(R.string.keyboard_key_media_previous));
211 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_REWIND,
212 context.getString(R.string.keyboard_key_media_rewind));
213 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD,
214 context.getString(R.string.keyboard_key_media_fast_forward));
215 mSpecialCharacterNames.put(
216 KeyEvent.KEYCODE_PAGE_UP, context.getString(R.string.keyboard_key_page_up));
217 mSpecialCharacterNames.put(
218 KeyEvent.KEYCODE_PAGE_DOWN, context.getString(R.string.keyboard_key_page_down));
219 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_A,
220 context.getString(R.string.keyboard_key_button_template, "A"));
221 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_B,
222 context.getString(R.string.keyboard_key_button_template, "B"));
223 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_C,
224 context.getString(R.string.keyboard_key_button_template, "C"));
225 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_X,
226 context.getString(R.string.keyboard_key_button_template, "X"));
227 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Y,
228 context.getString(R.string.keyboard_key_button_template, "Y"));
229 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Z,
230 context.getString(R.string.keyboard_key_button_template, "Z"));
231 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L1,
232 context.getString(R.string.keyboard_key_button_template, "L1"));
233 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R1,
234 context.getString(R.string.keyboard_key_button_template, "R1"));
235 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L2,
236 context.getString(R.string.keyboard_key_button_template, "L2"));
237 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R2,
238 context.getString(R.string.keyboard_key_button_template, "R2"));
239 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_START,
240 context.getString(R.string.keyboard_key_button_template, "Start"));
241 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_SELECT,
242 context.getString(R.string.keyboard_key_button_template, "Select"));
243 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_MODE,
244 context.getString(R.string.keyboard_key_button_template, "Mode"));
245 mSpecialCharacterNames.put(
246 KeyEvent.KEYCODE_FORWARD_DEL, context.getString(R.string.keyboard_key_forward_del));
247 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ESCAPE, "Esc");
248 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SYSRQ, "SysRq");
249 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BREAK, "Break");
250 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SCROLL_LOCK, "Scroll Lock");
251 mSpecialCharacterNames.put(
252 KeyEvent.KEYCODE_MOVE_HOME, context.getString(R.string.keyboard_key_move_home));
253 mSpecialCharacterNames.put(
254 KeyEvent.KEYCODE_MOVE_END, context.getString(R.string.keyboard_key_move_end));
255 mSpecialCharacterNames.put(
256 KeyEvent.KEYCODE_INSERT, context.getString(R.string.keyboard_key_insert));
257 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F1, "F1");
258 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F2, "F2");
259 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F3, "F3");
260 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F4, "F4");
261 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F5, "F5");
262 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F6, "F6");
263 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F7, "F7");
264 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F8, "F8");
265 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F9, "F9");
266 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F10, "F10");
267 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F11, "F11");
268 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F12, "F12");
269 mSpecialCharacterNames.put(
270 KeyEvent.KEYCODE_NUM_LOCK, context.getString(R.string.keyboard_key_num_lock));
271 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_0,
272 context.getString(R.string.keyboard_key_numpad_template, "0"));
273 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_1,
274 context.getString(R.string.keyboard_key_numpad_template, "1"));
275 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_2,
276 context.getString(R.string.keyboard_key_numpad_template, "2"));
277 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_3,
278 context.getString(R.string.keyboard_key_numpad_template, "3"));
279 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_4,
280 context.getString(R.string.keyboard_key_numpad_template, "4"));
281 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_5,
282 context.getString(R.string.keyboard_key_numpad_template, "5"));
283 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_6,
284 context.getString(R.string.keyboard_key_numpad_template, "6"));
285 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_7,
286 context.getString(R.string.keyboard_key_numpad_template, "7"));
287 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_8,
288 context.getString(R.string.keyboard_key_numpad_template, "8"));
289 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_9,
290 context.getString(R.string.keyboard_key_numpad_template, "9"));
291 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DIVIDE,
292 context.getString(R.string.keyboard_key_numpad_template, "/"));
293 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_MULTIPLY,
294 context.getString(R.string.keyboard_key_numpad_template, "*"));
295 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_SUBTRACT,
296 context.getString(R.string.keyboard_key_numpad_template, "-"));
297 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ADD,
298 context.getString(R.string.keyboard_key_numpad_template, "+"));
299 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DOT,
300 context.getString(R.string.keyboard_key_numpad_template, "."));
301 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_COMMA,
302 context.getString(R.string.keyboard_key_numpad_template, ","));
303 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ENTER,
304 context.getString(R.string.keyboard_key_numpad_template,
305 context.getString(R.string.keyboard_key_enter)));
306 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_EQUALS,
307 context.getString(R.string.keyboard_key_numpad_template, "="));
308 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN,
309 context.getString(R.string.keyboard_key_numpad_template, "("));
310 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN,
311 context.getString(R.string.keyboard_key_numpad_template, ")"));
312 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ZENKAKU_HANKAKU, "半角/全角");
313 mSpecialCharacterNames.put(KeyEvent.KEYCODE_EISU, "英数");
314 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MUHENKAN, "無変換");
315 mSpecialCharacterNames.put(KeyEvent.KEYCODE_HENKAN, "変換");
316 mSpecialCharacterNames.put(KeyEvent.KEYCODE_KATAKANA_HIRAGANA, "かな");
317
318 mModifierNames.put(KeyEvent.META_META_ON, "Meta");
319 mModifierNames.put(KeyEvent.META_CTRL_ON, "Ctrl");
320 mModifierNames.put(KeyEvent.META_ALT_ON, "Alt");
321 mModifierNames.put(KeyEvent.META_SHIFT_ON, "Shift");
322 mModifierNames.put(KeyEvent.META_SYM_ON, "Sym");
323 mModifierNames.put(KeyEvent.META_FUNCTION_ON, "Fn");
324
325 mSpecialCharacterDrawables.put(
326 KeyEvent.KEYCODE_DEL, context.getDrawable(R.drawable.ic_ksh_key_backspace));
327 mSpecialCharacterDrawables.put(
328 KeyEvent.KEYCODE_ENTER, context.getDrawable(R.drawable.ic_ksh_key_enter));
329 mSpecialCharacterDrawables.put(
330 KeyEvent.KEYCODE_DPAD_UP, context.getDrawable(R.drawable.ic_ksh_key_up));
331 mSpecialCharacterDrawables.put(
332 KeyEvent.KEYCODE_DPAD_RIGHT, context.getDrawable(R.drawable.ic_ksh_key_right));
333 mSpecialCharacterDrawables.put(
334 KeyEvent.KEYCODE_DPAD_DOWN, context.getDrawable(R.drawable.ic_ksh_key_down));
335 mSpecialCharacterDrawables.put(
336 KeyEvent.KEYCODE_DPAD_LEFT, context.getDrawable(R.drawable.ic_ksh_key_left));
337
338 mModifierDrawables.put(
339 KeyEvent.META_META_ON, context.getDrawable(R.drawable.ic_ksh_key_meta));
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000340 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100341
Clara Bayarri03f19552016-04-06 10:59:52 +0100342 /**
343 * Retrieves a {@link KeyCharacterMap} and assigns it to mKeyCharacterMap. If the given id is an
344 * existing device, that device's map is used. Otherwise, it checks first all available devices
345 * and if there is a full keyboard it uses that map, otherwise falls back to the Virtual
346 * Keyboard with its default map.
347 */
348 private void retrieveKeyCharacterMap(int deviceId) {
349 final InputManager inputManager = InputManager.getInstance();
Clara Bayarri382c59e2016-05-18 12:19:17 +0100350 mBackupKeyCharacterMap = inputManager.getInputDevice(-1).getKeyCharacterMap();
Clara Bayarri03f19552016-04-06 10:59:52 +0100351 if (deviceId != -1) {
352 final InputDevice inputDevice = inputManager.getInputDevice(deviceId);
353 if (inputDevice != null) {
354 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
355 return;
356 }
357 }
358 final int[] deviceIds = inputManager.getInputDeviceIds();
359 for (int i = 0; i < deviceIds.length; ++i) {
360 final InputDevice inputDevice = inputManager.getInputDevice(deviceIds[i]);
361 // -1 is the Virtual Keyboard, with the default key map. Use that one only as last
362 // resort.
363 if (inputDevice.getId() != -1 && inputDevice.isFullKeyboard()) {
364 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
365 return;
366 }
367 }
Clara Bayarri382c59e2016-05-18 12:19:17 +0100368 // Fall back to -1, the virtual keyboard.
369 mKeyCharacterMap = mBackupKeyCharacterMap;
Clara Bayarri03f19552016-04-06 10:59:52 +0100370 }
371
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100372 private void showKeyboardShortcuts(int deviceId) {
373 retrieveKeyCharacterMap(deviceId);
Daichi Hirono3871faa2017-04-05 12:57:28 +0900374 SystemServicesProxy.getInstance(mContext).requestKeyboardShortcuts(mContext,
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100375 new KeyboardShortcutsReceiver() {
376 @Override
377 public void onKeyboardShortcutsReceived(
378 final List<KeyboardShortcutGroup> result) {
379 result.add(getSystemShortcuts());
380 final KeyboardShortcutGroup appShortcuts = getDefaultApplicationShortcuts();
381 if (appShortcuts != null) {
382 result.add(appShortcuts);
383 }
Peeyush Agarwal64f0cae2017-02-09 19:55:20 +0000384 showKeyboardShortcutsDialog(result);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100385 }
386 }, deviceId);
387 }
388
389 private void dismissKeyboardShortcuts() {
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100390 if (mKeyboardShortcutsDialog != null) {
391 mKeyboardShortcutsDialog.dismiss();
392 mKeyboardShortcutsDialog = null;
393 }
394 }
395
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100396 private KeyboardShortcutGroup getSystemShortcuts() {
397 final KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup(
398 mContext.getString(R.string.keyboard_shortcut_group_system), true);
399 systemGroup.addItem(new KeyboardShortcutInfo(
400 mContext.getString(R.string.keyboard_shortcut_group_system_home),
401 KeyEvent.KEYCODE_ENTER,
402 KeyEvent.META_META_ON));
403 systemGroup.addItem(new KeyboardShortcutInfo(
404 mContext.getString(R.string.keyboard_shortcut_group_system_back),
405 KeyEvent.KEYCODE_DEL,
406 KeyEvent.META_META_ON));
407 systemGroup.addItem(new KeyboardShortcutInfo(
408 mContext.getString(R.string.keyboard_shortcut_group_system_recents),
409 KeyEvent.KEYCODE_TAB,
410 KeyEvent.META_ALT_ON));
411 systemGroup.addItem(new KeyboardShortcutInfo(
412 mContext.getString(
413 R.string.keyboard_shortcut_group_system_notifications),
414 KeyEvent.KEYCODE_N,
415 KeyEvent.META_META_ON));
416 systemGroup.addItem(new KeyboardShortcutInfo(
417 mContext.getString(
418 R.string.keyboard_shortcut_group_system_shortcuts_helper),
419 KeyEvent.KEYCODE_SLASH,
420 KeyEvent.META_META_ON));
421 systemGroup.addItem(new KeyboardShortcutInfo(
422 mContext.getString(
423 R.string.keyboard_shortcut_group_system_switch_input),
424 KeyEvent.KEYCODE_SPACE,
425 KeyEvent.META_META_ON));
426 return systemGroup;
427 }
428
429 private KeyboardShortcutGroup getDefaultApplicationShortcuts() {
430 final int userId = mContext.getUserId();
Andrei Stingaceanu9cfcafc2016-04-12 11:07:39 +0100431 List<KeyboardShortcutInfo> keyboardShortcutInfoAppItems = new ArrayList<>();
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100432
433 // Assist.
434 final AssistUtils assistUtils = new AssistUtils(mContext);
435 final ComponentName assistComponent = assistUtils.getAssistComponentForUser(userId);
Andrei Stingaceanuafb993f2017-09-21 11:36:53 +0100436 // Not all devices have an assist component.
437 if (assistComponent != null) {
438 PackageInfo assistPackageInfo = null;
439 try {
440 assistPackageInfo = mPackageManager.getPackageInfo(
441 assistComponent.getPackageName(), 0, userId);
442 } catch (RemoteException e) {
443 Log.e(TAG, "PackageManagerService is dead");
444 }
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100445
Andrei Stingaceanuafb993f2017-09-21 11:36:53 +0100446 if (assistPackageInfo != null) {
447 final Icon assistIcon = Icon.createWithResource(
448 assistPackageInfo.applicationInfo.packageName,
449 assistPackageInfo.applicationInfo.icon);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100450
Andrei Stingaceanuafb993f2017-09-21 11:36:53 +0100451 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
452 mContext.getString(R.string.keyboard_shortcut_group_applications_assist),
453 assistIcon,
454 KeyEvent.KEYCODE_UNKNOWN,
455 KeyEvent.META_META_ON));
456 }
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100457 }
458
459 // Browser.
460 final Icon browserIcon = getIconForIntentCategory(Intent.CATEGORY_APP_BROWSER, userId);
461 if (browserIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100462 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100463 mContext.getString(R.string.keyboard_shortcut_group_applications_browser),
464 browserIcon,
465 KeyEvent.KEYCODE_B,
466 KeyEvent.META_META_ON));
467 }
468
469
470 // Contacts.
471 final Icon contactsIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CONTACTS, userId);
472 if (contactsIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100473 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100474 mContext.getString(R.string.keyboard_shortcut_group_applications_contacts),
475 contactsIcon,
476 KeyEvent.KEYCODE_C,
477 KeyEvent.META_META_ON));
478 }
479
480 // Email.
481 final Icon emailIcon = getIconForIntentCategory(Intent.CATEGORY_APP_EMAIL, userId);
482 if (emailIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100483 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100484 mContext.getString(R.string.keyboard_shortcut_group_applications_email),
485 emailIcon,
486 KeyEvent.KEYCODE_E,
487 KeyEvent.META_META_ON));
488 }
489
490 // Messaging.
491 final Icon messagingIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MESSAGING, userId);
492 if (messagingIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100493 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Peeyush Agarwald86c1062016-10-17 12:33:45 +0100494 mContext.getString(R.string.keyboard_shortcut_group_applications_sms),
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100495 messagingIcon,
Peeyush Agarwald86c1062016-10-17 12:33:45 +0100496 KeyEvent.KEYCODE_S,
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100497 KeyEvent.META_META_ON));
498 }
499
500 // Music.
501 final Icon musicIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MUSIC, userId);
502 if (musicIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100503 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100504 mContext.getString(R.string.keyboard_shortcut_group_applications_music),
505 musicIcon,
506 KeyEvent.KEYCODE_P,
507 KeyEvent.META_META_ON));
508 }
509
510 // Calendar.
511 final Icon calendarIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CALENDAR, userId);
512 if (calendarIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100513 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100514 mContext.getString(R.string.keyboard_shortcut_group_applications_calendar),
515 calendarIcon,
516 KeyEvent.KEYCODE_L,
517 KeyEvent.META_META_ON));
518 }
519
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100520 final int itemsSize = keyboardShortcutInfoAppItems.size();
521 if (itemsSize == 0) {
522 return null;
523 }
524
525 // Sorts by label, case insensitive with nulls and/or empty labels last.
526 Collections.sort(keyboardShortcutInfoAppItems, mApplicationItemsComparator);
527 return new KeyboardShortcutGroup(
528 mContext.getString(R.string.keyboard_shortcut_group_applications),
529 keyboardShortcutInfoAppItems,
530 true);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100531 }
532
533 private Icon getIconForIntentCategory(String intentCategory, int userId) {
534 final Intent intent = new Intent(Intent.ACTION_MAIN);
535 intent.addCategory(intentCategory);
536
537 final PackageInfo packageInfo = getPackageInfoForIntent(intent, userId);
538 if (packageInfo != null && packageInfo.applicationInfo.icon != 0) {
539 return Icon.createWithResource(
540 packageInfo.applicationInfo.packageName,
541 packageInfo.applicationInfo.icon);
542 }
543 return null;
544 }
545
546 private PackageInfo getPackageInfoForIntent(Intent intent, int userId) {
547 try {
548 ResolveInfo handler;
549 handler = mPackageManager.resolveIntent(
550 intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()), 0, userId);
551 if (handler == null || handler.activityInfo == null) {
552 return null;
553 }
554 return mPackageManager.getPackageInfo(handler.activityInfo.packageName, 0, userId);
555 } catch (RemoteException e) {
556 Log.e(TAG, "PackageManagerService is dead", e);
557 return null;
558 }
559 }
560
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000561 private void showKeyboardShortcutsDialog(
562 final List<KeyboardShortcutGroup> keyboardShortcutGroups) {
563 // Need to post on the main thread.
564 mHandler.post(new Runnable() {
565 @Override
566 public void run() {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000567 handleShowKeyboardShortcuts(keyboardShortcutGroups);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000568 }
569 });
570 }
571
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000572 private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) {
573 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
574 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
575 LAYOUT_INFLATER_SERVICE);
576 final View keyboardShortcutsView = inflater.inflate(
577 R.layout.keyboard_shortcuts_view, null);
578 populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById(
579 R.id.keyboard_shortcuts_container), keyboardShortcutGroups);
580 dialogBuilder.setView(keyboardShortcutsView);
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100581 dialogBuilder.setPositiveButton(R.string.quick_settings_done, mDialogCloseListener);
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000582 mKeyboardShortcutsDialog = dialogBuilder.create();
583 mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true);
584 Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow();
585 keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG);
Peeyush Agarwal64f0cae2017-02-09 19:55:20 +0000586 synchronized (sLock) {
587 // showKeyboardShortcutsDialog only if it has not been dismissed already
588 if (sInstance != null) {
589 mKeyboardShortcutsDialog.show();
590 }
591 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000592 }
593
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000594 private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout,
595 List<KeyboardShortcutGroup> keyboardShortcutGroups) {
596 LayoutInflater inflater = LayoutInflater.from(mContext);
597 final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size();
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100598 TextView shortcutsKeyView = (TextView) inflater.inflate(
599 R.layout.keyboard_shortcuts_key_view, null, false);
600 shortcutsKeyView.measure(
601 View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
602 final int shortcutKeyTextItemMinWidth = shortcutsKeyView.getMeasuredHeight();
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100603 // Needed to be able to scale the image items to the same height as the text items.
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100604 final int shortcutKeyIconItemHeightWidth = shortcutsKeyView.getMeasuredHeight()
605 - shortcutsKeyView.getPaddingTop()
606 - shortcutsKeyView.getPaddingBottom();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000607 for (int i = 0; i < keyboardShortcutGroupsSize; i++) {
608 KeyboardShortcutGroup group = keyboardShortcutGroups.get(i);
609 TextView categoryTitle = (TextView) inflater.inflate(
610 R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false);
611 categoryTitle.setText(group.getLabel());
612 categoryTitle.setTextColor(group.isSystemGroup()
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700613 ? Utils.getColorAccent(mContext)
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000614 : mContext.getColor(R.color.ksh_application_group_color));
615 keyboardShortcutsLayout.addView(categoryTitle);
616
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000617 LinearLayout shortcutContainer = (LinearLayout) inflater.inflate(
618 R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000619 final int itemsSize = group.getItems().size();
620 for (int j = 0; j < itemsSize; j++) {
621 KeyboardShortcutInfo info = group.getItems().get(j);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100622 List<StringDrawableContainer> shortcutKeys = getHumanReadableShortcutKeys(info);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800623 if (shortcutKeys == null) {
624 // Ignore shortcuts we can't display keys for.
625 Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping.");
626 continue;
627 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000628 View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item,
629 shortcutContainer, false);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100630
631 if (info.getIcon() != null) {
632 ImageView shortcutIcon = (ImageView) shortcutView
633 .findViewById(R.id.keyboard_shortcuts_icon);
634 shortcutIcon.setImageIcon(info.getIcon());
635 shortcutIcon.setVisibility(View.VISIBLE);
636 }
637
638 TextView shortcutKeyword = (TextView) shortcutView
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000639 .findViewById(R.id.keyboard_shortcuts_keyword);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100640 shortcutKeyword.setText(info.getLabel());
641 if (info.getIcon() != null) {
642 RelativeLayout.LayoutParams lp =
643 (RelativeLayout.LayoutParams) shortcutKeyword.getLayoutParams();
644 lp.removeRule(RelativeLayout.ALIGN_PARENT_START);
645 shortcutKeyword.setLayoutParams(lp);
646 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000647
Andrei Stingaceanu844927d2016-02-16 14:31:58 +0000648 ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000649 .findViewById(R.id.keyboard_shortcuts_item_container);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000650 final int shortcutKeysSize = shortcutKeys.size();
651 for (int k = 0; k < shortcutKeysSize; k++) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100652 StringDrawableContainer shortcutRepresentation = shortcutKeys.get(k);
653 if (shortcutRepresentation.mDrawable != null) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100654 ImageView shortcutKeyIconView = (ImageView) inflater.inflate(
655 R.layout.keyboard_shortcuts_key_icon_view, shortcutItemsContainer,
656 false);
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100657 Bitmap bitmap = Bitmap.createBitmap(shortcutKeyIconItemHeightWidth,
658 shortcutKeyIconItemHeightWidth, Bitmap.Config.ARGB_8888);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100659 Canvas canvas = new Canvas(bitmap);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100660 shortcutRepresentation.mDrawable.setBounds(0, 0, canvas.getWidth(),
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100661 canvas.getHeight());
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100662 shortcutRepresentation.mDrawable.draw(canvas);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100663 shortcutKeyIconView.setImageBitmap(bitmap);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100664 shortcutKeyIconView.setImportantForAccessibility(
665 IMPORTANT_FOR_ACCESSIBILITY_YES);
666 shortcutKeyIconView.setAccessibilityDelegate(
667 new ShortcutKeyAccessibilityDelegate(
668 shortcutRepresentation.mString));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100669 shortcutItemsContainer.addView(shortcutKeyIconView);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100670 } else if (shortcutRepresentation.mString != null) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100671 TextView shortcutKeyTextView = (TextView) inflater.inflate(
672 R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer,
673 false);
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100674 shortcutKeyTextView.setMinimumWidth(shortcutKeyTextItemMinWidth);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100675 shortcutKeyTextView.setText(shortcutRepresentation.mString);
676 shortcutKeyTextView.setAccessibilityDelegate(
677 new ShortcutKeyAccessibilityDelegate(
678 shortcutRepresentation.mString));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100679 shortcutItemsContainer.addView(shortcutKeyTextView);
680 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000681 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000682 shortcutContainer.addView(shortcutView);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000683 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000684 keyboardShortcutsLayout.addView(shortcutContainer);
685 if (i < keyboardShortcutGroupsSize - 1) {
686 View separator = inflater.inflate(
687 R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout,
688 false);
689 keyboardShortcutsLayout.addView(separator);
690 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000691 }
692 }
693
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100694 private List<StringDrawableContainer> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) {
695 List<StringDrawableContainer> shortcutKeys = getHumanReadableModifiers(info);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800696 if (shortcutKeys == null) {
697 return null;
698 }
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100699 String shortcutKeyString = null;
700 Drawable shortcutKeyDrawable = null;
Clara Bayarri1d648a12016-03-23 17:09:02 +0000701 if (info.getBaseCharacter() > Character.MIN_VALUE) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100702 shortcutKeyString = String.valueOf(info.getBaseCharacter());
Clara Bayarrib999af52016-04-06 16:02:35 +0100703 } else if (mSpecialCharacterDrawables.get(info.getKeycode()) != null) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100704 shortcutKeyDrawable = mSpecialCharacterDrawables.get(info.getKeycode());
705 shortcutKeyString = mSpecialCharacterNames.get(info.getKeycode());
Clara Bayarrib999af52016-04-06 16:02:35 +0100706 } else if (mSpecialCharacterNames.get(info.getKeycode()) != null) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100707 shortcutKeyString = mSpecialCharacterNames.get(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800708 } else {
Clara Bayarri1d648a12016-03-23 17:09:02 +0000709 // Special case for shortcuts with no base key or keycode.
710 if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) {
711 return shortcutKeys;
712 }
Clara Bayarri03f19552016-04-06 10:59:52 +0100713 char displayLabel = mKeyCharacterMap.getDisplayLabel(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800714 if (displayLabel != 0) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100715 shortcutKeyString = String.valueOf(displayLabel);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800716 } else {
Clara Bayarri382c59e2016-05-18 12:19:17 +0100717 displayLabel = mBackupKeyCharacterMap.getDisplayLabel(info.getKeycode());
718 if (displayLabel != 0) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100719 shortcutKeyString = String.valueOf(displayLabel);
Clara Bayarri382c59e2016-05-18 12:19:17 +0100720 } else {
721 return null;
722 }
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800723 }
724 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100725
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100726 if (shortcutKeyString != null) {
727 shortcutKeys.add(new StringDrawableContainer(shortcutKeyString, shortcutKeyDrawable));
728 } else {
729 Log.w(TAG, "Keyboard Shortcut does not have a text representation, skipping.");
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100730 }
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100731
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000732 return shortcutKeys;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100733 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800734
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100735 private List<StringDrawableContainer> getHumanReadableModifiers(KeyboardShortcutInfo info) {
736 final List<StringDrawableContainer> shortcutKeys = new ArrayList<>();
Clara Bayarrib9057df2016-03-02 11:37:09 -0800737 int modifiers = info.getModifiers();
738 if (modifiers == 0) {
739 return shortcutKeys;
740 }
Clara Bayarri7ff37982016-06-27 13:58:44 +0100741 for(int i = 0; i < mModifierList.length; ++i) {
742 final int supportedModifier = mModifierList[i];
Clara Bayarrib9057df2016-03-02 11:37:09 -0800743 if ((modifiers & supportedModifier) != 0) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100744 shortcutKeys.add(new StringDrawableContainer(
745 mModifierNames.get(supportedModifier),
746 mModifierDrawables.get(supportedModifier)));
Clara Bayarrib9057df2016-03-02 11:37:09 -0800747 modifiers &= ~supportedModifier;
748 }
749 }
750 if (modifiers != 0) {
751 // Remaining unsupported modifiers, don't show anything.
752 return null;
753 }
754 return shortcutKeys;
755 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100756
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100757 private final class ShortcutKeyAccessibilityDelegate extends AccessibilityDelegate {
758 private String mContentDescription;
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100759
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100760 ShortcutKeyAccessibilityDelegate(String contentDescription) {
761 mContentDescription = contentDescription;
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100762 }
763
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100764 @Override
765 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
766 super.onInitializeAccessibilityNodeInfo(host, info);
767 if (mContentDescription != null) {
768 info.setContentDescription(mContentDescription.toLowerCase());
769 }
770 }
771 }
772
773 private static final class StringDrawableContainer {
774 @NonNull
775 public String mString;
776 @Nullable
777 public Drawable mDrawable;
778
779 StringDrawableContainer(String string, Drawable drawable) {
780 mString = string;
781 mDrawable = drawable;
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100782 }
783 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100784}