blob: f4387629b8b31b9b261de12e9275f5d37180cb52 [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;
62import com.android.internal.logging.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;
Clara Bayarri75e09792015-07-29 16:20:40 +010065import com.android.systemui.recents.Recents;
66
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);
374 Recents.getSystemServices().requestKeyboardShortcuts(mContext,
375 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 }
384 showKeyboardShortcutsDialog(result);
385 }
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);
436 PackageInfo assistPackageInfo = null;
437 try {
438 assistPackageInfo = mPackageManager.getPackageInfo(
439 assistComponent.getPackageName(), 0, userId);
440 } catch (RemoteException e) {
441 Log.e(TAG, "PackageManagerService is dead");
442 }
443
444 if (assistPackageInfo != null) {
445 final Icon assistIcon = Icon.createWithResource(
446 assistPackageInfo.applicationInfo.packageName,
447 assistPackageInfo.applicationInfo.icon);
448
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100449 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100450 mContext.getString(R.string.keyboard_shortcut_group_applications_assist),
451 assistIcon,
452 KeyEvent.KEYCODE_UNKNOWN,
453 KeyEvent.META_META_ON));
454 }
455
456 // Browser.
457 final Icon browserIcon = getIconForIntentCategory(Intent.CATEGORY_APP_BROWSER, userId);
458 if (browserIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100459 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100460 mContext.getString(R.string.keyboard_shortcut_group_applications_browser),
461 browserIcon,
462 KeyEvent.KEYCODE_B,
463 KeyEvent.META_META_ON));
464 }
465
466
467 // Contacts.
468 final Icon contactsIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CONTACTS, userId);
469 if (contactsIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100470 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100471 mContext.getString(R.string.keyboard_shortcut_group_applications_contacts),
472 contactsIcon,
473 KeyEvent.KEYCODE_C,
474 KeyEvent.META_META_ON));
475 }
476
477 // Email.
478 final Icon emailIcon = getIconForIntentCategory(Intent.CATEGORY_APP_EMAIL, userId);
479 if (emailIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100480 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100481 mContext.getString(R.string.keyboard_shortcut_group_applications_email),
482 emailIcon,
483 KeyEvent.KEYCODE_E,
484 KeyEvent.META_META_ON));
485 }
486
487 // Messaging.
488 final Icon messagingIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MESSAGING, userId);
489 if (messagingIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100490 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Peeyush Agarwald86c1062016-10-17 12:33:45 +0100491 mContext.getString(R.string.keyboard_shortcut_group_applications_sms),
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100492 messagingIcon,
Peeyush Agarwald86c1062016-10-17 12:33:45 +0100493 KeyEvent.KEYCODE_S,
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100494 KeyEvent.META_META_ON));
495 }
496
497 // Music.
498 final Icon musicIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MUSIC, userId);
499 if (musicIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100500 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100501 mContext.getString(R.string.keyboard_shortcut_group_applications_music),
502 musicIcon,
503 KeyEvent.KEYCODE_P,
504 KeyEvent.META_META_ON));
505 }
506
507 // Calendar.
508 final Icon calendarIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CALENDAR, userId);
509 if (calendarIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100510 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100511 mContext.getString(R.string.keyboard_shortcut_group_applications_calendar),
512 calendarIcon,
513 KeyEvent.KEYCODE_L,
514 KeyEvent.META_META_ON));
515 }
516
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100517 final int itemsSize = keyboardShortcutInfoAppItems.size();
518 if (itemsSize == 0) {
519 return null;
520 }
521
522 // Sorts by label, case insensitive with nulls and/or empty labels last.
523 Collections.sort(keyboardShortcutInfoAppItems, mApplicationItemsComparator);
524 return new KeyboardShortcutGroup(
525 mContext.getString(R.string.keyboard_shortcut_group_applications),
526 keyboardShortcutInfoAppItems,
527 true);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100528 }
529
530 private Icon getIconForIntentCategory(String intentCategory, int userId) {
531 final Intent intent = new Intent(Intent.ACTION_MAIN);
532 intent.addCategory(intentCategory);
533
534 final PackageInfo packageInfo = getPackageInfoForIntent(intent, userId);
535 if (packageInfo != null && packageInfo.applicationInfo.icon != 0) {
536 return Icon.createWithResource(
537 packageInfo.applicationInfo.packageName,
538 packageInfo.applicationInfo.icon);
539 }
540 return null;
541 }
542
543 private PackageInfo getPackageInfoForIntent(Intent intent, int userId) {
544 try {
545 ResolveInfo handler;
546 handler = mPackageManager.resolveIntent(
547 intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()), 0, userId);
548 if (handler == null || handler.activityInfo == null) {
549 return null;
550 }
551 return mPackageManager.getPackageInfo(handler.activityInfo.packageName, 0, userId);
552 } catch (RemoteException e) {
553 Log.e(TAG, "PackageManagerService is dead", e);
554 return null;
555 }
556 }
557
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000558 private void showKeyboardShortcutsDialog(
559 final List<KeyboardShortcutGroup> keyboardShortcutGroups) {
560 // Need to post on the main thread.
561 mHandler.post(new Runnable() {
562 @Override
563 public void run() {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000564 handleShowKeyboardShortcuts(keyboardShortcutGroups);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000565 }
566 });
567 }
568
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000569 private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) {
570 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
571 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
572 LAYOUT_INFLATER_SERVICE);
573 final View keyboardShortcutsView = inflater.inflate(
574 R.layout.keyboard_shortcuts_view, null);
575 populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById(
576 R.id.keyboard_shortcuts_container), keyboardShortcutGroups);
577 dialogBuilder.setView(keyboardShortcutsView);
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100578 dialogBuilder.setPositiveButton(R.string.quick_settings_done, mDialogCloseListener);
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000579 mKeyboardShortcutsDialog = dialogBuilder.create();
580 mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true);
581 Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow();
582 keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG);
583 mKeyboardShortcutsDialog.show();
584 }
585
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000586 private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout,
587 List<KeyboardShortcutGroup> keyboardShortcutGroups) {
588 LayoutInflater inflater = LayoutInflater.from(mContext);
589 final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size();
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100590 TextView shortcutsKeyView = (TextView) inflater.inflate(
591 R.layout.keyboard_shortcuts_key_view, null, false);
592 shortcutsKeyView.measure(
593 View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
594 final int shortcutKeyTextItemMinWidth = shortcutsKeyView.getMeasuredHeight();
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100595 // Needed to be able to scale the image items to the same height as the text items.
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100596 final int shortcutKeyIconItemHeightWidth = shortcutsKeyView.getMeasuredHeight()
597 - shortcutsKeyView.getPaddingTop()
598 - shortcutsKeyView.getPaddingBottom();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000599 for (int i = 0; i < keyboardShortcutGroupsSize; i++) {
600 KeyboardShortcutGroup group = keyboardShortcutGroups.get(i);
601 TextView categoryTitle = (TextView) inflater.inflate(
602 R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false);
603 categoryTitle.setText(group.getLabel());
604 categoryTitle.setTextColor(group.isSystemGroup()
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700605 ? Utils.getColorAccent(mContext)
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000606 : mContext.getColor(R.color.ksh_application_group_color));
607 keyboardShortcutsLayout.addView(categoryTitle);
608
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000609 LinearLayout shortcutContainer = (LinearLayout) inflater.inflate(
610 R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000611 final int itemsSize = group.getItems().size();
612 for (int j = 0; j < itemsSize; j++) {
613 KeyboardShortcutInfo info = group.getItems().get(j);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100614 List<StringDrawableContainer> shortcutKeys = getHumanReadableShortcutKeys(info);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800615 if (shortcutKeys == null) {
616 // Ignore shortcuts we can't display keys for.
617 Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping.");
618 continue;
619 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000620 View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item,
621 shortcutContainer, false);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100622
623 if (info.getIcon() != null) {
624 ImageView shortcutIcon = (ImageView) shortcutView
625 .findViewById(R.id.keyboard_shortcuts_icon);
626 shortcutIcon.setImageIcon(info.getIcon());
627 shortcutIcon.setVisibility(View.VISIBLE);
628 }
629
630 TextView shortcutKeyword = (TextView) shortcutView
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000631 .findViewById(R.id.keyboard_shortcuts_keyword);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100632 shortcutKeyword.setText(info.getLabel());
633 if (info.getIcon() != null) {
634 RelativeLayout.LayoutParams lp =
635 (RelativeLayout.LayoutParams) shortcutKeyword.getLayoutParams();
636 lp.removeRule(RelativeLayout.ALIGN_PARENT_START);
637 shortcutKeyword.setLayoutParams(lp);
638 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000639
Andrei Stingaceanu844927d2016-02-16 14:31:58 +0000640 ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000641 .findViewById(R.id.keyboard_shortcuts_item_container);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000642 final int shortcutKeysSize = shortcutKeys.size();
643 for (int k = 0; k < shortcutKeysSize; k++) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100644 StringDrawableContainer shortcutRepresentation = shortcutKeys.get(k);
645 if (shortcutRepresentation.mDrawable != null) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100646 ImageView shortcutKeyIconView = (ImageView) inflater.inflate(
647 R.layout.keyboard_shortcuts_key_icon_view, shortcutItemsContainer,
648 false);
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100649 Bitmap bitmap = Bitmap.createBitmap(shortcutKeyIconItemHeightWidth,
650 shortcutKeyIconItemHeightWidth, Bitmap.Config.ARGB_8888);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100651 Canvas canvas = new Canvas(bitmap);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100652 shortcutRepresentation.mDrawable.setBounds(0, 0, canvas.getWidth(),
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100653 canvas.getHeight());
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100654 shortcutRepresentation.mDrawable.draw(canvas);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100655 shortcutKeyIconView.setImageBitmap(bitmap);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100656 shortcutKeyIconView.setImportantForAccessibility(
657 IMPORTANT_FOR_ACCESSIBILITY_YES);
658 shortcutKeyIconView.setAccessibilityDelegate(
659 new ShortcutKeyAccessibilityDelegate(
660 shortcutRepresentation.mString));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100661 shortcutItemsContainer.addView(shortcutKeyIconView);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100662 } else if (shortcutRepresentation.mString != null) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100663 TextView shortcutKeyTextView = (TextView) inflater.inflate(
664 R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer,
665 false);
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100666 shortcutKeyTextView.setMinimumWidth(shortcutKeyTextItemMinWidth);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100667 shortcutKeyTextView.setText(shortcutRepresentation.mString);
668 shortcutKeyTextView.setAccessibilityDelegate(
669 new ShortcutKeyAccessibilityDelegate(
670 shortcutRepresentation.mString));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100671 shortcutItemsContainer.addView(shortcutKeyTextView);
672 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000673 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000674 shortcutContainer.addView(shortcutView);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000675 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000676 keyboardShortcutsLayout.addView(shortcutContainer);
677 if (i < keyboardShortcutGroupsSize - 1) {
678 View separator = inflater.inflate(
679 R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout,
680 false);
681 keyboardShortcutsLayout.addView(separator);
682 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000683 }
684 }
685
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100686 private List<StringDrawableContainer> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) {
687 List<StringDrawableContainer> shortcutKeys = getHumanReadableModifiers(info);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800688 if (shortcutKeys == null) {
689 return null;
690 }
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100691 String shortcutKeyString = null;
692 Drawable shortcutKeyDrawable = null;
Clara Bayarri1d648a12016-03-23 17:09:02 +0000693 if (info.getBaseCharacter() > Character.MIN_VALUE) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100694 shortcutKeyString = String.valueOf(info.getBaseCharacter());
Clara Bayarrib999af52016-04-06 16:02:35 +0100695 } else if (mSpecialCharacterDrawables.get(info.getKeycode()) != null) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100696 shortcutKeyDrawable = mSpecialCharacterDrawables.get(info.getKeycode());
697 shortcutKeyString = mSpecialCharacterNames.get(info.getKeycode());
Clara Bayarrib999af52016-04-06 16:02:35 +0100698 } else if (mSpecialCharacterNames.get(info.getKeycode()) != null) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100699 shortcutKeyString = mSpecialCharacterNames.get(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800700 } else {
Clara Bayarri1d648a12016-03-23 17:09:02 +0000701 // Special case for shortcuts with no base key or keycode.
702 if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) {
703 return shortcutKeys;
704 }
Clara Bayarri03f19552016-04-06 10:59:52 +0100705 char displayLabel = mKeyCharacterMap.getDisplayLabel(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800706 if (displayLabel != 0) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100707 shortcutKeyString = String.valueOf(displayLabel);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800708 } else {
Clara Bayarri382c59e2016-05-18 12:19:17 +0100709 displayLabel = mBackupKeyCharacterMap.getDisplayLabel(info.getKeycode());
710 if (displayLabel != 0) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100711 shortcutKeyString = String.valueOf(displayLabel);
Clara Bayarri382c59e2016-05-18 12:19:17 +0100712 } else {
713 return null;
714 }
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800715 }
716 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100717
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100718 if (shortcutKeyString != null) {
719 shortcutKeys.add(new StringDrawableContainer(shortcutKeyString, shortcutKeyDrawable));
720 } else {
721 Log.w(TAG, "Keyboard Shortcut does not have a text representation, skipping.");
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100722 }
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100723
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000724 return shortcutKeys;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100725 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800726
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100727 private List<StringDrawableContainer> getHumanReadableModifiers(KeyboardShortcutInfo info) {
728 final List<StringDrawableContainer> shortcutKeys = new ArrayList<>();
Clara Bayarrib9057df2016-03-02 11:37:09 -0800729 int modifiers = info.getModifiers();
730 if (modifiers == 0) {
731 return shortcutKeys;
732 }
Clara Bayarri7ff37982016-06-27 13:58:44 +0100733 for(int i = 0; i < mModifierList.length; ++i) {
734 final int supportedModifier = mModifierList[i];
Clara Bayarrib9057df2016-03-02 11:37:09 -0800735 if ((modifiers & supportedModifier) != 0) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100736 shortcutKeys.add(new StringDrawableContainer(
737 mModifierNames.get(supportedModifier),
738 mModifierDrawables.get(supportedModifier)));
Clara Bayarrib9057df2016-03-02 11:37:09 -0800739 modifiers &= ~supportedModifier;
740 }
741 }
742 if (modifiers != 0) {
743 // Remaining unsupported modifiers, don't show anything.
744 return null;
745 }
746 return shortcutKeys;
747 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100748
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100749 private final class ShortcutKeyAccessibilityDelegate extends AccessibilityDelegate {
750 private String mContentDescription;
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100751
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100752 ShortcutKeyAccessibilityDelegate(String contentDescription) {
753 mContentDescription = contentDescription;
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100754 }
755
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100756 @Override
757 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
758 super.onInitializeAccessibilityNodeInfo(host, info);
759 if (mContentDescription != null) {
760 info.setContentDescription(mContentDescription.toLowerCase());
761 }
762 }
763 }
764
765 private static final class StringDrawableContainer {
766 @NonNull
767 public String mString;
768 @Nullable
769 public Drawable mDrawable;
770
771 StringDrawableContainer(String string, Drawable drawable) {
772 mString = string;
773 mDrawable = drawable;
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100774 }
775 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100776}