blob: 247e3d342bbbaa2bc034166c0db0a4b0fa1efd77 [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;
Jason Chang2386a372018-04-24 16:05:30 +080032import android.content.res.ColorStateList;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010033import android.graphics.drawable.Icon;
Andrei Stingaceanud1519102016-03-31 15:53:33 +010034import android.graphics.Bitmap;
35import android.graphics.Canvas;
36import android.graphics.drawable.Drawable;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080037import android.hardware.input.InputManager;
Clara Bayarri75e09792015-07-29 16:20:40 +010038import android.os.Handler;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000039import android.os.Looper;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010040import android.os.RemoteException;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080041import android.util.Log;
42import android.util.SparseArray;
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +000043import android.view.ContextThemeWrapper;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080044import android.view.InputDevice;
45import android.view.KeyCharacterMap;
Clara Bayarri75e09792015-07-29 16:20:40 +010046import android.view.KeyEvent;
47import android.view.KeyboardShortcutGroup;
48import android.view.KeyboardShortcutInfo;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010049import android.view.LayoutInflater;
50import android.view.View;
Andrei Stingaceanub4817012016-06-13 17:26:39 +010051import android.view.View.AccessibilityDelegate;
Andrei Stingaceanu844927d2016-02-16 14:31:58 +000052import android.view.ViewGroup;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010053import android.view.Window;
Clara Bayarri75e09792015-07-29 16:20:40 +010054import android.view.WindowManager.KeyboardShortcutsReceiver;
Andrei Stingaceanub4817012016-06-13 17:26:39 +010055import android.view.accessibility.AccessibilityNodeInfo;
Andrei Stingaceanud1519102016-03-31 15:53:33 +010056import android.widget.ImageView;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000057import android.widget.LinearLayout;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010058import android.widget.RelativeLayout;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000059import android.widget.TextView;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010060
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010061import com.android.internal.app.AssistUtils;
Clara Bayarric17a5982016-04-15 12:26:47 +010062import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010063import com.android.internal.logging.nano.MetricsProto;
Andrew Sapperstein5c373442016-06-12 13:17:16 -070064import com.android.settingslib.Utils;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010065import com.android.systemui.R;
Daichi Hirono3871faa2017-04-05 12:57:28 +090066import com.android.systemui.recents.misc.SystemServicesProxy;
Clara Bayarri75e09792015-07-29 16:20:40 +010067
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000068import java.util.ArrayList;
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010069import java.util.Collections;
70import java.util.Comparator;
Clara Bayarri75e09792015-07-29 16:20:40 +010071import java.util.List;
72
73import static android.content.Context.LAYOUT_INFLATER_SERVICE;
Andrei Stingaceanub4817012016-06-13 17:26:39 +010074import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_YES;
Clara Bayarri75e09792015-07-29 16:20:40 +010075import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010076
77/**
78 * Contains functionality for handling keyboard shortcuts.
79 */
Andrei Stingaceanud1519102016-03-31 15:53:33 +010080public final class KeyboardShortcuts {
Clara Bayarri4e850ff2016-03-02 11:12:32 -080081 private static final String TAG = KeyboardShortcuts.class.getSimpleName();
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +010082 private static final Object sLock = new Object();
83 private static KeyboardShortcuts sInstance;
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +010084
Clara Bayarrib999af52016-04-06 16:02:35 +010085 private final SparseArray<String> mSpecialCharacterNames = new SparseArray<>();
86 private final SparseArray<String> mModifierNames = new SparseArray<>();
87 private final SparseArray<Drawable> mSpecialCharacterDrawables = new SparseArray<>();
88 private final SparseArray<Drawable> mModifierDrawables = new SparseArray<>();
Clara Bayarri7ff37982016-06-27 13:58:44 +010089 // Ordered list of modifiers that are supported. All values in this array must exist in
90 // mModifierNames.
91 private final int[] mModifierList = new int[] {
92 KeyEvent.META_META_ON, KeyEvent.META_CTRL_ON, KeyEvent.META_ALT_ON,
93 KeyEvent.META_SHIFT_ON, KeyEvent.META_SYM_ON, KeyEvent.META_FUNCTION_ON
94 };
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000095
96 private final Handler mHandler = new Handler(Looper.getMainLooper());
97 private final Context mContext;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010098 private final IPackageManager mPackageManager;
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010099 private final OnClickListener mDialogCloseListener = new DialogInterface.OnClickListener() {
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000100 public void onClick(DialogInterface dialog, int id) {
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100101 dismissKeyboardShortcuts();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000102 }
103 };
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100104 private final Comparator<KeyboardShortcutInfo> mApplicationItemsComparator =
105 new Comparator<KeyboardShortcutInfo>() {
106 @Override
107 public int compare(KeyboardShortcutInfo ksh1, KeyboardShortcutInfo ksh2) {
108 boolean ksh1ShouldBeLast = ksh1.getLabel() == null
109 || ksh1.getLabel().toString().isEmpty();
110 boolean ksh2ShouldBeLast = ksh2.getLabel() == null
111 || ksh2.getLabel().toString().isEmpty();
112 if (ksh1ShouldBeLast && ksh2ShouldBeLast) {
113 return 0;
114 }
115 if (ksh1ShouldBeLast) {
116 return 1;
117 }
118 if (ksh2ShouldBeLast) {
119 return -1;
120 }
121 return (ksh1.getLabel().toString()).compareToIgnoreCase(
122 ksh2.getLabel().toString());
123 }
124 };
Clara Bayarri75e09792015-07-29 16:20:40 +0100125
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100126 private Dialog mKeyboardShortcutsDialog;
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800127 private KeyCharacterMap mKeyCharacterMap;
Clara Bayarri382c59e2016-05-18 12:19:17 +0100128 private KeyCharacterMap mBackupKeyCharacterMap;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100129
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100130 private KeyboardShortcuts(Context context) {
Andrew Sapperstein5c373442016-06-12 13:17:16 -0700131 this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_DeviceDefault_Light);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100132 this.mPackageManager = AppGlobals.getPackageManager();
Clara Bayarrib999af52016-04-06 16:02:35 +0100133 loadResources(context);
134 }
135
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100136 private static KeyboardShortcuts getInstance(Context context) {
137 if (sInstance == null) {
138 sInstance = new KeyboardShortcuts(context);
139 }
140 return sInstance;
141 }
142
143 public static void show(Context context, int deviceId) {
Clara Bayarric17a5982016-04-15 12:26:47 +0100144 MetricsLogger.visible(context,
145 MetricsProto.MetricsEvent.KEYBOARD_SHORTCUTS_HELPER);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100146 synchronized (sLock) {
147 if (sInstance != null && !sInstance.mContext.equals(context)) {
148 dismiss();
149 }
150 getInstance(context).showKeyboardShortcuts(deviceId);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100151 }
152 }
153
154 public static void toggle(Context context, int deviceId) {
155 synchronized (sLock) {
Clara Bayarri3ac0ae02016-06-01 17:28:14 +0100156 if (isShowing()) {
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100157 dismiss();
158 } else {
159 show(context, deviceId);
160 }
161 }
162 }
163
164 public static void dismiss() {
165 synchronized (sLock) {
166 if (sInstance != null) {
Clara Bayarri318ef062016-05-26 11:16:12 +0100167 MetricsLogger.hidden(sInstance.mContext,
168 MetricsProto.MetricsEvent.KEYBOARD_SHORTCUTS_HELPER);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100169 sInstance.dismissKeyboardShortcuts();
170 sInstance = null;
171 }
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100172 }
173 }
174
Clara Bayarri3ac0ae02016-06-01 17:28:14 +0100175 private static boolean isShowing() {
176 return sInstance != null && sInstance.mKeyboardShortcutsDialog != null
177 && sInstance.mKeyboardShortcutsDialog.isShowing();
178 }
179
Clara Bayarrib999af52016-04-06 16:02:35 +0100180 private void loadResources(Context context) {
181 mSpecialCharacterNames.put(
182 KeyEvent.KEYCODE_HOME, context.getString(R.string.keyboard_key_home));
183 mSpecialCharacterNames.put(
184 KeyEvent.KEYCODE_BACK, context.getString(R.string.keyboard_key_back));
185 mSpecialCharacterNames.put(
186 KeyEvent.KEYCODE_DPAD_UP, context.getString(R.string.keyboard_key_dpad_up));
187 mSpecialCharacterNames.put(
188 KeyEvent.KEYCODE_DPAD_DOWN, context.getString(R.string.keyboard_key_dpad_down));
189 mSpecialCharacterNames.put(
190 KeyEvent.KEYCODE_DPAD_LEFT, context.getString(R.string.keyboard_key_dpad_left));
191 mSpecialCharacterNames.put(
192 KeyEvent.KEYCODE_DPAD_RIGHT, context.getString(R.string.keyboard_key_dpad_right));
193 mSpecialCharacterNames.put(
194 KeyEvent.KEYCODE_DPAD_CENTER, context.getString(R.string.keyboard_key_dpad_center));
195 mSpecialCharacterNames.put(KeyEvent.KEYCODE_PERIOD, ".");
196 mSpecialCharacterNames.put(
197 KeyEvent.KEYCODE_TAB, context.getString(R.string.keyboard_key_tab));
198 mSpecialCharacterNames.put(
199 KeyEvent.KEYCODE_SPACE, context.getString(R.string.keyboard_key_space));
200 mSpecialCharacterNames.put(
201 KeyEvent.KEYCODE_ENTER, context.getString(R.string.keyboard_key_enter));
202 mSpecialCharacterNames.put(
203 KeyEvent.KEYCODE_DEL, context.getString(R.string.keyboard_key_backspace));
204 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE,
205 context.getString(R.string.keyboard_key_media_play_pause));
206 mSpecialCharacterNames.put(
207 KeyEvent.KEYCODE_MEDIA_STOP, context.getString(R.string.keyboard_key_media_stop));
208 mSpecialCharacterNames.put(
209 KeyEvent.KEYCODE_MEDIA_NEXT, context.getString(R.string.keyboard_key_media_next));
210 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PREVIOUS,
211 context.getString(R.string.keyboard_key_media_previous));
212 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_REWIND,
213 context.getString(R.string.keyboard_key_media_rewind));
214 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD,
215 context.getString(R.string.keyboard_key_media_fast_forward));
216 mSpecialCharacterNames.put(
217 KeyEvent.KEYCODE_PAGE_UP, context.getString(R.string.keyboard_key_page_up));
218 mSpecialCharacterNames.put(
219 KeyEvent.KEYCODE_PAGE_DOWN, context.getString(R.string.keyboard_key_page_down));
220 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_A,
221 context.getString(R.string.keyboard_key_button_template, "A"));
222 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_B,
223 context.getString(R.string.keyboard_key_button_template, "B"));
224 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_C,
225 context.getString(R.string.keyboard_key_button_template, "C"));
226 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_X,
227 context.getString(R.string.keyboard_key_button_template, "X"));
228 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Y,
229 context.getString(R.string.keyboard_key_button_template, "Y"));
230 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Z,
231 context.getString(R.string.keyboard_key_button_template, "Z"));
232 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L1,
233 context.getString(R.string.keyboard_key_button_template, "L1"));
234 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R1,
235 context.getString(R.string.keyboard_key_button_template, "R1"));
236 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L2,
237 context.getString(R.string.keyboard_key_button_template, "L2"));
238 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R2,
239 context.getString(R.string.keyboard_key_button_template, "R2"));
240 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_START,
241 context.getString(R.string.keyboard_key_button_template, "Start"));
242 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_SELECT,
243 context.getString(R.string.keyboard_key_button_template, "Select"));
244 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_MODE,
245 context.getString(R.string.keyboard_key_button_template, "Mode"));
246 mSpecialCharacterNames.put(
247 KeyEvent.KEYCODE_FORWARD_DEL, context.getString(R.string.keyboard_key_forward_del));
248 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ESCAPE, "Esc");
249 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SYSRQ, "SysRq");
250 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BREAK, "Break");
251 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SCROLL_LOCK, "Scroll Lock");
252 mSpecialCharacterNames.put(
253 KeyEvent.KEYCODE_MOVE_HOME, context.getString(R.string.keyboard_key_move_home));
254 mSpecialCharacterNames.put(
255 KeyEvent.KEYCODE_MOVE_END, context.getString(R.string.keyboard_key_move_end));
256 mSpecialCharacterNames.put(
257 KeyEvent.KEYCODE_INSERT, context.getString(R.string.keyboard_key_insert));
258 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F1, "F1");
259 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F2, "F2");
260 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F3, "F3");
261 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F4, "F4");
262 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F5, "F5");
263 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F6, "F6");
264 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F7, "F7");
265 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F8, "F8");
266 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F9, "F9");
267 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F10, "F10");
268 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F11, "F11");
269 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F12, "F12");
270 mSpecialCharacterNames.put(
271 KeyEvent.KEYCODE_NUM_LOCK, context.getString(R.string.keyboard_key_num_lock));
272 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_0,
273 context.getString(R.string.keyboard_key_numpad_template, "0"));
274 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_1,
275 context.getString(R.string.keyboard_key_numpad_template, "1"));
276 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_2,
277 context.getString(R.string.keyboard_key_numpad_template, "2"));
278 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_3,
279 context.getString(R.string.keyboard_key_numpad_template, "3"));
280 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_4,
281 context.getString(R.string.keyboard_key_numpad_template, "4"));
282 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_5,
283 context.getString(R.string.keyboard_key_numpad_template, "5"));
284 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_6,
285 context.getString(R.string.keyboard_key_numpad_template, "6"));
286 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_7,
287 context.getString(R.string.keyboard_key_numpad_template, "7"));
288 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_8,
289 context.getString(R.string.keyboard_key_numpad_template, "8"));
290 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_9,
291 context.getString(R.string.keyboard_key_numpad_template, "9"));
292 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DIVIDE,
293 context.getString(R.string.keyboard_key_numpad_template, "/"));
294 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_MULTIPLY,
295 context.getString(R.string.keyboard_key_numpad_template, "*"));
296 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_SUBTRACT,
297 context.getString(R.string.keyboard_key_numpad_template, "-"));
298 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ADD,
299 context.getString(R.string.keyboard_key_numpad_template, "+"));
300 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DOT,
301 context.getString(R.string.keyboard_key_numpad_template, "."));
302 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_COMMA,
303 context.getString(R.string.keyboard_key_numpad_template, ","));
304 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ENTER,
305 context.getString(R.string.keyboard_key_numpad_template,
306 context.getString(R.string.keyboard_key_enter)));
307 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_EQUALS,
308 context.getString(R.string.keyboard_key_numpad_template, "="));
309 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN,
310 context.getString(R.string.keyboard_key_numpad_template, "("));
311 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN,
312 context.getString(R.string.keyboard_key_numpad_template, ")"));
313 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ZENKAKU_HANKAKU, "半角/全角");
314 mSpecialCharacterNames.put(KeyEvent.KEYCODE_EISU, "英数");
315 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MUHENKAN, "無変換");
316 mSpecialCharacterNames.put(KeyEvent.KEYCODE_HENKAN, "変換");
317 mSpecialCharacterNames.put(KeyEvent.KEYCODE_KATAKANA_HIRAGANA, "かな");
318
319 mModifierNames.put(KeyEvent.META_META_ON, "Meta");
320 mModifierNames.put(KeyEvent.META_CTRL_ON, "Ctrl");
321 mModifierNames.put(KeyEvent.META_ALT_ON, "Alt");
322 mModifierNames.put(KeyEvent.META_SHIFT_ON, "Shift");
323 mModifierNames.put(KeyEvent.META_SYM_ON, "Sym");
324 mModifierNames.put(KeyEvent.META_FUNCTION_ON, "Fn");
325
326 mSpecialCharacterDrawables.put(
327 KeyEvent.KEYCODE_DEL, context.getDrawable(R.drawable.ic_ksh_key_backspace));
328 mSpecialCharacterDrawables.put(
329 KeyEvent.KEYCODE_ENTER, context.getDrawable(R.drawable.ic_ksh_key_enter));
330 mSpecialCharacterDrawables.put(
331 KeyEvent.KEYCODE_DPAD_UP, context.getDrawable(R.drawable.ic_ksh_key_up));
332 mSpecialCharacterDrawables.put(
333 KeyEvent.KEYCODE_DPAD_RIGHT, context.getDrawable(R.drawable.ic_ksh_key_right));
334 mSpecialCharacterDrawables.put(
335 KeyEvent.KEYCODE_DPAD_DOWN, context.getDrawable(R.drawable.ic_ksh_key_down));
336 mSpecialCharacterDrawables.put(
337 KeyEvent.KEYCODE_DPAD_LEFT, context.getDrawable(R.drawable.ic_ksh_key_left));
338
339 mModifierDrawables.put(
340 KeyEvent.META_META_ON, context.getDrawable(R.drawable.ic_ksh_key_meta));
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000341 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100342
Clara Bayarri03f19552016-04-06 10:59:52 +0100343 /**
344 * Retrieves a {@link KeyCharacterMap} and assigns it to mKeyCharacterMap. If the given id is an
345 * existing device, that device's map is used. Otherwise, it checks first all available devices
346 * and if there is a full keyboard it uses that map, otherwise falls back to the Virtual
347 * Keyboard with its default map.
348 */
349 private void retrieveKeyCharacterMap(int deviceId) {
350 final InputManager inputManager = InputManager.getInstance();
Clara Bayarri382c59e2016-05-18 12:19:17 +0100351 mBackupKeyCharacterMap = inputManager.getInputDevice(-1).getKeyCharacterMap();
Clara Bayarri03f19552016-04-06 10:59:52 +0100352 if (deviceId != -1) {
353 final InputDevice inputDevice = inputManager.getInputDevice(deviceId);
354 if (inputDevice != null) {
355 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
356 return;
357 }
358 }
359 final int[] deviceIds = inputManager.getInputDeviceIds();
360 for (int i = 0; i < deviceIds.length; ++i) {
361 final InputDevice inputDevice = inputManager.getInputDevice(deviceIds[i]);
362 // -1 is the Virtual Keyboard, with the default key map. Use that one only as last
363 // resort.
364 if (inputDevice.getId() != -1 && inputDevice.isFullKeyboard()) {
365 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
366 return;
367 }
368 }
Clara Bayarri382c59e2016-05-18 12:19:17 +0100369 // Fall back to -1, the virtual keyboard.
370 mKeyCharacterMap = mBackupKeyCharacterMap;
Clara Bayarri03f19552016-04-06 10:59:52 +0100371 }
372
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100373 private void showKeyboardShortcuts(int deviceId) {
374 retrieveKeyCharacterMap(deviceId);
Daichi Hirono3871faa2017-04-05 12:57:28 +0900375 SystemServicesProxy.getInstance(mContext).requestKeyboardShortcuts(mContext,
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100376 new KeyboardShortcutsReceiver() {
377 @Override
378 public void onKeyboardShortcutsReceived(
379 final List<KeyboardShortcutGroup> result) {
380 result.add(getSystemShortcuts());
381 final KeyboardShortcutGroup appShortcuts = getDefaultApplicationShortcuts();
382 if (appShortcuts != null) {
383 result.add(appShortcuts);
384 }
Peeyush Agarwal64f0cae2017-02-09 19:55:20 +0000385 showKeyboardShortcutsDialog(result);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100386 }
387 }, deviceId);
388 }
389
390 private void dismissKeyboardShortcuts() {
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100391 if (mKeyboardShortcutsDialog != null) {
392 mKeyboardShortcutsDialog.dismiss();
393 mKeyboardShortcutsDialog = null;
394 }
395 }
396
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100397 private KeyboardShortcutGroup getSystemShortcuts() {
398 final KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup(
399 mContext.getString(R.string.keyboard_shortcut_group_system), true);
400 systemGroup.addItem(new KeyboardShortcutInfo(
401 mContext.getString(R.string.keyboard_shortcut_group_system_home),
402 KeyEvent.KEYCODE_ENTER,
403 KeyEvent.META_META_ON));
404 systemGroup.addItem(new KeyboardShortcutInfo(
405 mContext.getString(R.string.keyboard_shortcut_group_system_back),
406 KeyEvent.KEYCODE_DEL,
407 KeyEvent.META_META_ON));
408 systemGroup.addItem(new KeyboardShortcutInfo(
409 mContext.getString(R.string.keyboard_shortcut_group_system_recents),
410 KeyEvent.KEYCODE_TAB,
411 KeyEvent.META_ALT_ON));
412 systemGroup.addItem(new KeyboardShortcutInfo(
413 mContext.getString(
414 R.string.keyboard_shortcut_group_system_notifications),
415 KeyEvent.KEYCODE_N,
416 KeyEvent.META_META_ON));
417 systemGroup.addItem(new KeyboardShortcutInfo(
418 mContext.getString(
419 R.string.keyboard_shortcut_group_system_shortcuts_helper),
420 KeyEvent.KEYCODE_SLASH,
421 KeyEvent.META_META_ON));
422 systemGroup.addItem(new KeyboardShortcutInfo(
423 mContext.getString(
424 R.string.keyboard_shortcut_group_system_switch_input),
425 KeyEvent.KEYCODE_SPACE,
426 KeyEvent.META_META_ON));
427 return systemGroup;
428 }
429
430 private KeyboardShortcutGroup getDefaultApplicationShortcuts() {
431 final int userId = mContext.getUserId();
Andrei Stingaceanu9cfcafc2016-04-12 11:07:39 +0100432 List<KeyboardShortcutInfo> keyboardShortcutInfoAppItems = new ArrayList<>();
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100433
434 // Assist.
435 final AssistUtils assistUtils = new AssistUtils(mContext);
436 final ComponentName assistComponent = assistUtils.getAssistComponentForUser(userId);
Andrei Stingaceanuafb993f2017-09-21 11:36:53 +0100437 // Not all devices have an assist component.
438 if (assistComponent != null) {
439 PackageInfo assistPackageInfo = null;
440 try {
441 assistPackageInfo = mPackageManager.getPackageInfo(
442 assistComponent.getPackageName(), 0, userId);
443 } catch (RemoteException e) {
444 Log.e(TAG, "PackageManagerService is dead");
445 }
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100446
Andrei Stingaceanuafb993f2017-09-21 11:36:53 +0100447 if (assistPackageInfo != null) {
448 final Icon assistIcon = Icon.createWithResource(
449 assistPackageInfo.applicationInfo.packageName,
450 assistPackageInfo.applicationInfo.icon);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100451
Andrei Stingaceanuafb993f2017-09-21 11:36:53 +0100452 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
453 mContext.getString(R.string.keyboard_shortcut_group_applications_assist),
454 assistIcon,
455 KeyEvent.KEYCODE_UNKNOWN,
456 KeyEvent.META_META_ON));
457 }
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100458 }
459
460 // Browser.
461 final Icon browserIcon = getIconForIntentCategory(Intent.CATEGORY_APP_BROWSER, userId);
462 if (browserIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100463 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100464 mContext.getString(R.string.keyboard_shortcut_group_applications_browser),
465 browserIcon,
466 KeyEvent.KEYCODE_B,
467 KeyEvent.META_META_ON));
468 }
469
470
471 // Contacts.
472 final Icon contactsIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CONTACTS, userId);
473 if (contactsIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100474 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100475 mContext.getString(R.string.keyboard_shortcut_group_applications_contacts),
476 contactsIcon,
477 KeyEvent.KEYCODE_C,
478 KeyEvent.META_META_ON));
479 }
480
481 // Email.
482 final Icon emailIcon = getIconForIntentCategory(Intent.CATEGORY_APP_EMAIL, userId);
483 if (emailIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100484 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100485 mContext.getString(R.string.keyboard_shortcut_group_applications_email),
486 emailIcon,
487 KeyEvent.KEYCODE_E,
488 KeyEvent.META_META_ON));
489 }
490
491 // Messaging.
492 final Icon messagingIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MESSAGING, userId);
493 if (messagingIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100494 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Peeyush Agarwald86c1062016-10-17 12:33:45 +0100495 mContext.getString(R.string.keyboard_shortcut_group_applications_sms),
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100496 messagingIcon,
Peeyush Agarwald86c1062016-10-17 12:33:45 +0100497 KeyEvent.KEYCODE_S,
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100498 KeyEvent.META_META_ON));
499 }
500
501 // Music.
502 final Icon musicIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MUSIC, userId);
503 if (musicIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100504 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100505 mContext.getString(R.string.keyboard_shortcut_group_applications_music),
506 musicIcon,
507 KeyEvent.KEYCODE_P,
508 KeyEvent.META_META_ON));
509 }
510
511 // Calendar.
512 final Icon calendarIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CALENDAR, userId);
513 if (calendarIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100514 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100515 mContext.getString(R.string.keyboard_shortcut_group_applications_calendar),
516 calendarIcon,
517 KeyEvent.KEYCODE_L,
518 KeyEvent.META_META_ON));
519 }
520
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100521 final int itemsSize = keyboardShortcutInfoAppItems.size();
522 if (itemsSize == 0) {
523 return null;
524 }
525
526 // Sorts by label, case insensitive with nulls and/or empty labels last.
527 Collections.sort(keyboardShortcutInfoAppItems, mApplicationItemsComparator);
528 return new KeyboardShortcutGroup(
529 mContext.getString(R.string.keyboard_shortcut_group_applications),
530 keyboardShortcutInfoAppItems,
531 true);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100532 }
533
534 private Icon getIconForIntentCategory(String intentCategory, int userId) {
535 final Intent intent = new Intent(Intent.ACTION_MAIN);
536 intent.addCategory(intentCategory);
537
538 final PackageInfo packageInfo = getPackageInfoForIntent(intent, userId);
539 if (packageInfo != null && packageInfo.applicationInfo.icon != 0) {
540 return Icon.createWithResource(
541 packageInfo.applicationInfo.packageName,
542 packageInfo.applicationInfo.icon);
543 }
544 return null;
545 }
546
547 private PackageInfo getPackageInfoForIntent(Intent intent, int userId) {
548 try {
549 ResolveInfo handler;
550 handler = mPackageManager.resolveIntent(
551 intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()), 0, userId);
552 if (handler == null || handler.activityInfo == null) {
553 return null;
554 }
555 return mPackageManager.getPackageInfo(handler.activityInfo.packageName, 0, userId);
556 } catch (RemoteException e) {
557 Log.e(TAG, "PackageManagerService is dead", e);
558 return null;
559 }
560 }
561
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000562 private void showKeyboardShortcutsDialog(
563 final List<KeyboardShortcutGroup> keyboardShortcutGroups) {
564 // Need to post on the main thread.
565 mHandler.post(new Runnable() {
566 @Override
567 public void run() {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000568 handleShowKeyboardShortcuts(keyboardShortcutGroups);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000569 }
570 });
571 }
572
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000573 private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) {
574 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
575 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
576 LAYOUT_INFLATER_SERVICE);
577 final View keyboardShortcutsView = inflater.inflate(
578 R.layout.keyboard_shortcuts_view, null);
579 populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById(
580 R.id.keyboard_shortcuts_container), keyboardShortcutGroups);
581 dialogBuilder.setView(keyboardShortcutsView);
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100582 dialogBuilder.setPositiveButton(R.string.quick_settings_done, mDialogCloseListener);
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000583 mKeyboardShortcutsDialog = dialogBuilder.create();
584 mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true);
585 Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow();
586 keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG);
Peeyush Agarwal64f0cae2017-02-09 19:55:20 +0000587 synchronized (sLock) {
588 // showKeyboardShortcutsDialog only if it has not been dismissed already
589 if (sInstance != null) {
590 mKeyboardShortcutsDialog.show();
591 }
592 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000593 }
594
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000595 private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout,
596 List<KeyboardShortcutGroup> keyboardShortcutGroups) {
597 LayoutInflater inflater = LayoutInflater.from(mContext);
598 final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size();
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100599 TextView shortcutsKeyView = (TextView) inflater.inflate(
600 R.layout.keyboard_shortcuts_key_view, null, false);
601 shortcutsKeyView.measure(
602 View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
603 final int shortcutKeyTextItemMinWidth = shortcutsKeyView.getMeasuredHeight();
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100604 // Needed to be able to scale the image items to the same height as the text items.
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100605 final int shortcutKeyIconItemHeightWidth = shortcutsKeyView.getMeasuredHeight()
606 - shortcutsKeyView.getPaddingTop()
607 - shortcutsKeyView.getPaddingBottom();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000608 for (int i = 0; i < keyboardShortcutGroupsSize; i++) {
609 KeyboardShortcutGroup group = keyboardShortcutGroups.get(i);
610 TextView categoryTitle = (TextView) inflater.inflate(
611 R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false);
612 categoryTitle.setText(group.getLabel());
Jason Chang2386a372018-04-24 16:05:30 +0800613 categoryTitle.setTextColor(group.isSystemGroup() ? Utils.getColorAccent(mContext) :
614 ColorStateList.valueOf(mContext.getColor(R.color.ksh_application_group_color)));
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000615 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}