blob: a6b120b6a8e64e8c6711c8ac69e5b196c7924fef [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;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010063import com.android.systemui.R;
Clara Bayarri75e09792015-07-29 16:20:40 +010064import com.android.systemui.recents.Recents;
65
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000066import java.util.ArrayList;
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010067import java.util.Collections;
68import java.util.Comparator;
Clara Bayarri75e09792015-07-29 16:20:40 +010069import java.util.List;
70
71import static android.content.Context.LAYOUT_INFLATER_SERVICE;
Andrei Stingaceanub4817012016-06-13 17:26:39 +010072import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_YES;
Clara Bayarri75e09792015-07-29 16:20:40 +010073import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010074
75/**
76 * Contains functionality for handling keyboard shortcuts.
77 */
Andrei Stingaceanud1519102016-03-31 15:53:33 +010078public final class KeyboardShortcuts {
Clara Bayarri4e850ff2016-03-02 11:12:32 -080079 private static final String TAG = KeyboardShortcuts.class.getSimpleName();
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +010080 private static final Object sLock = new Object();
81 private static KeyboardShortcuts sInstance;
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +010082
Clara Bayarrib999af52016-04-06 16:02:35 +010083 private final SparseArray<String> mSpecialCharacterNames = new SparseArray<>();
84 private final SparseArray<String> mModifierNames = new SparseArray<>();
85 private final SparseArray<Drawable> mSpecialCharacterDrawables = new SparseArray<>();
86 private final SparseArray<Drawable> mModifierDrawables = new SparseArray<>();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000087
88 private final Handler mHandler = new Handler(Looper.getMainLooper());
89 private final Context mContext;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010090 private final IPackageManager mPackageManager;
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010091 private final OnClickListener mDialogCloseListener = new DialogInterface.OnClickListener() {
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000092 public void onClick(DialogInterface dialog, int id) {
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +010093 dismissKeyboardShortcuts();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000094 }
95 };
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010096 private final Comparator<KeyboardShortcutInfo> mApplicationItemsComparator =
97 new Comparator<KeyboardShortcutInfo>() {
98 @Override
99 public int compare(KeyboardShortcutInfo ksh1, KeyboardShortcutInfo ksh2) {
100 boolean ksh1ShouldBeLast = ksh1.getLabel() == null
101 || ksh1.getLabel().toString().isEmpty();
102 boolean ksh2ShouldBeLast = ksh2.getLabel() == null
103 || ksh2.getLabel().toString().isEmpty();
104 if (ksh1ShouldBeLast && ksh2ShouldBeLast) {
105 return 0;
106 }
107 if (ksh1ShouldBeLast) {
108 return 1;
109 }
110 if (ksh2ShouldBeLast) {
111 return -1;
112 }
113 return (ksh1.getLabel().toString()).compareToIgnoreCase(
114 ksh2.getLabel().toString());
115 }
116 };
Clara Bayarri75e09792015-07-29 16:20:40 +0100117
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100118 private Dialog mKeyboardShortcutsDialog;
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800119 private KeyCharacterMap mKeyCharacterMap;
Clara Bayarri382c59e2016-05-18 12:19:17 +0100120 private KeyCharacterMap mBackupKeyCharacterMap;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100121
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100122 private KeyboardShortcuts(Context context) {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000123 this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_Material_Light);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100124 this.mPackageManager = AppGlobals.getPackageManager();
Clara Bayarrib999af52016-04-06 16:02:35 +0100125 loadResources(context);
126 }
127
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100128 private static KeyboardShortcuts getInstance(Context context) {
129 if (sInstance == null) {
130 sInstance = new KeyboardShortcuts(context);
131 }
132 return sInstance;
133 }
134
135 public static void show(Context context, int deviceId) {
Clara Bayarric17a5982016-04-15 12:26:47 +0100136 MetricsLogger.visible(context,
137 MetricsProto.MetricsEvent.KEYBOARD_SHORTCUTS_HELPER);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100138 synchronized (sLock) {
139 if (sInstance != null && !sInstance.mContext.equals(context)) {
140 dismiss();
141 }
142 getInstance(context).showKeyboardShortcuts(deviceId);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100143 }
144 }
145
146 public static void toggle(Context context, int deviceId) {
147 synchronized (sLock) {
Clara Bayarri3ac0ae02016-06-01 17:28:14 +0100148 if (isShowing()) {
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100149 dismiss();
150 } else {
151 show(context, deviceId);
152 }
153 }
154 }
155
156 public static void dismiss() {
157 synchronized (sLock) {
158 if (sInstance != null) {
Clara Bayarri318ef062016-05-26 11:16:12 +0100159 MetricsLogger.hidden(sInstance.mContext,
160 MetricsProto.MetricsEvent.KEYBOARD_SHORTCUTS_HELPER);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100161 sInstance.dismissKeyboardShortcuts();
162 sInstance = null;
163 }
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100164 }
165 }
166
Clara Bayarri3ac0ae02016-06-01 17:28:14 +0100167 private static boolean isShowing() {
168 return sInstance != null && sInstance.mKeyboardShortcutsDialog != null
169 && sInstance.mKeyboardShortcutsDialog.isShowing();
170 }
171
Clara Bayarrib999af52016-04-06 16:02:35 +0100172 private void loadResources(Context context) {
173 mSpecialCharacterNames.put(
174 KeyEvent.KEYCODE_HOME, context.getString(R.string.keyboard_key_home));
175 mSpecialCharacterNames.put(
176 KeyEvent.KEYCODE_BACK, context.getString(R.string.keyboard_key_back));
177 mSpecialCharacterNames.put(
178 KeyEvent.KEYCODE_DPAD_UP, context.getString(R.string.keyboard_key_dpad_up));
179 mSpecialCharacterNames.put(
180 KeyEvent.KEYCODE_DPAD_DOWN, context.getString(R.string.keyboard_key_dpad_down));
181 mSpecialCharacterNames.put(
182 KeyEvent.KEYCODE_DPAD_LEFT, context.getString(R.string.keyboard_key_dpad_left));
183 mSpecialCharacterNames.put(
184 KeyEvent.KEYCODE_DPAD_RIGHT, context.getString(R.string.keyboard_key_dpad_right));
185 mSpecialCharacterNames.put(
186 KeyEvent.KEYCODE_DPAD_CENTER, context.getString(R.string.keyboard_key_dpad_center));
187 mSpecialCharacterNames.put(KeyEvent.KEYCODE_PERIOD, ".");
188 mSpecialCharacterNames.put(
189 KeyEvent.KEYCODE_TAB, context.getString(R.string.keyboard_key_tab));
190 mSpecialCharacterNames.put(
191 KeyEvent.KEYCODE_SPACE, context.getString(R.string.keyboard_key_space));
192 mSpecialCharacterNames.put(
193 KeyEvent.KEYCODE_ENTER, context.getString(R.string.keyboard_key_enter));
194 mSpecialCharacterNames.put(
195 KeyEvent.KEYCODE_DEL, context.getString(R.string.keyboard_key_backspace));
196 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE,
197 context.getString(R.string.keyboard_key_media_play_pause));
198 mSpecialCharacterNames.put(
199 KeyEvent.KEYCODE_MEDIA_STOP, context.getString(R.string.keyboard_key_media_stop));
200 mSpecialCharacterNames.put(
201 KeyEvent.KEYCODE_MEDIA_NEXT, context.getString(R.string.keyboard_key_media_next));
202 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PREVIOUS,
203 context.getString(R.string.keyboard_key_media_previous));
204 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_REWIND,
205 context.getString(R.string.keyboard_key_media_rewind));
206 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD,
207 context.getString(R.string.keyboard_key_media_fast_forward));
208 mSpecialCharacterNames.put(
209 KeyEvent.KEYCODE_PAGE_UP, context.getString(R.string.keyboard_key_page_up));
210 mSpecialCharacterNames.put(
211 KeyEvent.KEYCODE_PAGE_DOWN, context.getString(R.string.keyboard_key_page_down));
212 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_A,
213 context.getString(R.string.keyboard_key_button_template, "A"));
214 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_B,
215 context.getString(R.string.keyboard_key_button_template, "B"));
216 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_C,
217 context.getString(R.string.keyboard_key_button_template, "C"));
218 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_X,
219 context.getString(R.string.keyboard_key_button_template, "X"));
220 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Y,
221 context.getString(R.string.keyboard_key_button_template, "Y"));
222 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Z,
223 context.getString(R.string.keyboard_key_button_template, "Z"));
224 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L1,
225 context.getString(R.string.keyboard_key_button_template, "L1"));
226 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R1,
227 context.getString(R.string.keyboard_key_button_template, "R1"));
228 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L2,
229 context.getString(R.string.keyboard_key_button_template, "L2"));
230 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R2,
231 context.getString(R.string.keyboard_key_button_template, "R2"));
232 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_START,
233 context.getString(R.string.keyboard_key_button_template, "Start"));
234 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_SELECT,
235 context.getString(R.string.keyboard_key_button_template, "Select"));
236 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_MODE,
237 context.getString(R.string.keyboard_key_button_template, "Mode"));
238 mSpecialCharacterNames.put(
239 KeyEvent.KEYCODE_FORWARD_DEL, context.getString(R.string.keyboard_key_forward_del));
240 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ESCAPE, "Esc");
241 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SYSRQ, "SysRq");
242 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BREAK, "Break");
243 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SCROLL_LOCK, "Scroll Lock");
244 mSpecialCharacterNames.put(
245 KeyEvent.KEYCODE_MOVE_HOME, context.getString(R.string.keyboard_key_move_home));
246 mSpecialCharacterNames.put(
247 KeyEvent.KEYCODE_MOVE_END, context.getString(R.string.keyboard_key_move_end));
248 mSpecialCharacterNames.put(
249 KeyEvent.KEYCODE_INSERT, context.getString(R.string.keyboard_key_insert));
250 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F1, "F1");
251 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F2, "F2");
252 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F3, "F3");
253 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F4, "F4");
254 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F5, "F5");
255 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F6, "F6");
256 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F7, "F7");
257 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F8, "F8");
258 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F9, "F9");
259 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F10, "F10");
260 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F11, "F11");
261 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F12, "F12");
262 mSpecialCharacterNames.put(
263 KeyEvent.KEYCODE_NUM_LOCK, context.getString(R.string.keyboard_key_num_lock));
264 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_0,
265 context.getString(R.string.keyboard_key_numpad_template, "0"));
266 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_1,
267 context.getString(R.string.keyboard_key_numpad_template, "1"));
268 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_2,
269 context.getString(R.string.keyboard_key_numpad_template, "2"));
270 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_3,
271 context.getString(R.string.keyboard_key_numpad_template, "3"));
272 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_4,
273 context.getString(R.string.keyboard_key_numpad_template, "4"));
274 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_5,
275 context.getString(R.string.keyboard_key_numpad_template, "5"));
276 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_6,
277 context.getString(R.string.keyboard_key_numpad_template, "6"));
278 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_7,
279 context.getString(R.string.keyboard_key_numpad_template, "7"));
280 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_8,
281 context.getString(R.string.keyboard_key_numpad_template, "8"));
282 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_9,
283 context.getString(R.string.keyboard_key_numpad_template, "9"));
284 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DIVIDE,
285 context.getString(R.string.keyboard_key_numpad_template, "/"));
286 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_MULTIPLY,
287 context.getString(R.string.keyboard_key_numpad_template, "*"));
288 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_SUBTRACT,
289 context.getString(R.string.keyboard_key_numpad_template, "-"));
290 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ADD,
291 context.getString(R.string.keyboard_key_numpad_template, "+"));
292 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DOT,
293 context.getString(R.string.keyboard_key_numpad_template, "."));
294 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_COMMA,
295 context.getString(R.string.keyboard_key_numpad_template, ","));
296 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ENTER,
297 context.getString(R.string.keyboard_key_numpad_template,
298 context.getString(R.string.keyboard_key_enter)));
299 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_EQUALS,
300 context.getString(R.string.keyboard_key_numpad_template, "="));
301 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN,
302 context.getString(R.string.keyboard_key_numpad_template, "("));
303 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN,
304 context.getString(R.string.keyboard_key_numpad_template, ")"));
305 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ZENKAKU_HANKAKU, "半角/全角");
306 mSpecialCharacterNames.put(KeyEvent.KEYCODE_EISU, "英数");
307 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MUHENKAN, "無変換");
308 mSpecialCharacterNames.put(KeyEvent.KEYCODE_HENKAN, "変換");
309 mSpecialCharacterNames.put(KeyEvent.KEYCODE_KATAKANA_HIRAGANA, "かな");
310
311 mModifierNames.put(KeyEvent.META_META_ON, "Meta");
312 mModifierNames.put(KeyEvent.META_CTRL_ON, "Ctrl");
313 mModifierNames.put(KeyEvent.META_ALT_ON, "Alt");
314 mModifierNames.put(KeyEvent.META_SHIFT_ON, "Shift");
315 mModifierNames.put(KeyEvent.META_SYM_ON, "Sym");
316 mModifierNames.put(KeyEvent.META_FUNCTION_ON, "Fn");
317
318 mSpecialCharacterDrawables.put(
319 KeyEvent.KEYCODE_DEL, context.getDrawable(R.drawable.ic_ksh_key_backspace));
320 mSpecialCharacterDrawables.put(
321 KeyEvent.KEYCODE_ENTER, context.getDrawable(R.drawable.ic_ksh_key_enter));
322 mSpecialCharacterDrawables.put(
323 KeyEvent.KEYCODE_DPAD_UP, context.getDrawable(R.drawable.ic_ksh_key_up));
324 mSpecialCharacterDrawables.put(
325 KeyEvent.KEYCODE_DPAD_RIGHT, context.getDrawable(R.drawable.ic_ksh_key_right));
326 mSpecialCharacterDrawables.put(
327 KeyEvent.KEYCODE_DPAD_DOWN, context.getDrawable(R.drawable.ic_ksh_key_down));
328 mSpecialCharacterDrawables.put(
329 KeyEvent.KEYCODE_DPAD_LEFT, context.getDrawable(R.drawable.ic_ksh_key_left));
330
331 mModifierDrawables.put(
332 KeyEvent.META_META_ON, context.getDrawable(R.drawable.ic_ksh_key_meta));
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000333 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100334
Clara Bayarri03f19552016-04-06 10:59:52 +0100335 /**
336 * Retrieves a {@link KeyCharacterMap} and assigns it to mKeyCharacterMap. If the given id is an
337 * existing device, that device's map is used. Otherwise, it checks first all available devices
338 * and if there is a full keyboard it uses that map, otherwise falls back to the Virtual
339 * Keyboard with its default map.
340 */
341 private void retrieveKeyCharacterMap(int deviceId) {
342 final InputManager inputManager = InputManager.getInstance();
Clara Bayarri382c59e2016-05-18 12:19:17 +0100343 mBackupKeyCharacterMap = inputManager.getInputDevice(-1).getKeyCharacterMap();
Clara Bayarri03f19552016-04-06 10:59:52 +0100344 if (deviceId != -1) {
345 final InputDevice inputDevice = inputManager.getInputDevice(deviceId);
346 if (inputDevice != null) {
347 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
348 return;
349 }
350 }
351 final int[] deviceIds = inputManager.getInputDeviceIds();
352 for (int i = 0; i < deviceIds.length; ++i) {
353 final InputDevice inputDevice = inputManager.getInputDevice(deviceIds[i]);
354 // -1 is the Virtual Keyboard, with the default key map. Use that one only as last
355 // resort.
356 if (inputDevice.getId() != -1 && inputDevice.isFullKeyboard()) {
357 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
358 return;
359 }
360 }
Clara Bayarri382c59e2016-05-18 12:19:17 +0100361 // Fall back to -1, the virtual keyboard.
362 mKeyCharacterMap = mBackupKeyCharacterMap;
Clara Bayarri03f19552016-04-06 10:59:52 +0100363 }
364
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100365 private void showKeyboardShortcuts(int deviceId) {
366 retrieveKeyCharacterMap(deviceId);
367 Recents.getSystemServices().requestKeyboardShortcuts(mContext,
368 new KeyboardShortcutsReceiver() {
369 @Override
370 public void onKeyboardShortcutsReceived(
371 final List<KeyboardShortcutGroup> result) {
372 result.add(getSystemShortcuts());
373 final KeyboardShortcutGroup appShortcuts = getDefaultApplicationShortcuts();
374 if (appShortcuts != null) {
375 result.add(appShortcuts);
376 }
377 showKeyboardShortcutsDialog(result);
378 }
379 }, deviceId);
380 }
381
382 private void dismissKeyboardShortcuts() {
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100383 if (mKeyboardShortcutsDialog != null) {
384 mKeyboardShortcutsDialog.dismiss();
385 mKeyboardShortcutsDialog = null;
386 }
387 }
388
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100389 private KeyboardShortcutGroup getSystemShortcuts() {
390 final KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup(
391 mContext.getString(R.string.keyboard_shortcut_group_system), true);
392 systemGroup.addItem(new KeyboardShortcutInfo(
393 mContext.getString(R.string.keyboard_shortcut_group_system_home),
394 KeyEvent.KEYCODE_ENTER,
395 KeyEvent.META_META_ON));
396 systemGroup.addItem(new KeyboardShortcutInfo(
397 mContext.getString(R.string.keyboard_shortcut_group_system_back),
398 KeyEvent.KEYCODE_DEL,
399 KeyEvent.META_META_ON));
400 systemGroup.addItem(new KeyboardShortcutInfo(
401 mContext.getString(R.string.keyboard_shortcut_group_system_recents),
402 KeyEvent.KEYCODE_TAB,
403 KeyEvent.META_ALT_ON));
404 systemGroup.addItem(new KeyboardShortcutInfo(
405 mContext.getString(
406 R.string.keyboard_shortcut_group_system_notifications),
407 KeyEvent.KEYCODE_N,
408 KeyEvent.META_META_ON));
409 systemGroup.addItem(new KeyboardShortcutInfo(
410 mContext.getString(
411 R.string.keyboard_shortcut_group_system_shortcuts_helper),
412 KeyEvent.KEYCODE_SLASH,
413 KeyEvent.META_META_ON));
414 systemGroup.addItem(new KeyboardShortcutInfo(
415 mContext.getString(
416 R.string.keyboard_shortcut_group_system_switch_input),
417 KeyEvent.KEYCODE_SPACE,
418 KeyEvent.META_META_ON));
419 return systemGroup;
420 }
421
422 private KeyboardShortcutGroup getDefaultApplicationShortcuts() {
423 final int userId = mContext.getUserId();
Andrei Stingaceanu9cfcafc2016-04-12 11:07:39 +0100424 List<KeyboardShortcutInfo> keyboardShortcutInfoAppItems = new ArrayList<>();
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100425
426 // Assist.
427 final AssistUtils assistUtils = new AssistUtils(mContext);
428 final ComponentName assistComponent = assistUtils.getAssistComponentForUser(userId);
429 PackageInfo assistPackageInfo = null;
430 try {
431 assistPackageInfo = mPackageManager.getPackageInfo(
432 assistComponent.getPackageName(), 0, userId);
433 } catch (RemoteException e) {
434 Log.e(TAG, "PackageManagerService is dead");
435 }
436
437 if (assistPackageInfo != null) {
438 final Icon assistIcon = Icon.createWithResource(
439 assistPackageInfo.applicationInfo.packageName,
440 assistPackageInfo.applicationInfo.icon);
441
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100442 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100443 mContext.getString(R.string.keyboard_shortcut_group_applications_assist),
444 assistIcon,
445 KeyEvent.KEYCODE_UNKNOWN,
446 KeyEvent.META_META_ON));
447 }
448
449 // Browser.
450 final Icon browserIcon = getIconForIntentCategory(Intent.CATEGORY_APP_BROWSER, userId);
451 if (browserIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100452 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100453 mContext.getString(R.string.keyboard_shortcut_group_applications_browser),
454 browserIcon,
455 KeyEvent.KEYCODE_B,
456 KeyEvent.META_META_ON));
457 }
458
459
460 // Contacts.
461 final Icon contactsIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CONTACTS, userId);
462 if (contactsIcon != 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_contacts),
465 contactsIcon,
466 KeyEvent.KEYCODE_C,
467 KeyEvent.META_META_ON));
468 }
469
470 // Email.
471 final Icon emailIcon = getIconForIntentCategory(Intent.CATEGORY_APP_EMAIL, userId);
472 if (emailIcon != 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_email),
475 emailIcon,
476 KeyEvent.KEYCODE_E,
477 KeyEvent.META_META_ON));
478 }
479
480 // Messaging.
481 final Icon messagingIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MESSAGING, userId);
482 if (messagingIcon != 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_im),
485 messagingIcon,
486 KeyEvent.KEYCODE_T,
487 KeyEvent.META_META_ON));
488 }
489
490 // Music.
491 final Icon musicIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MUSIC, userId);
492 if (musicIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100493 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100494 mContext.getString(R.string.keyboard_shortcut_group_applications_music),
495 musicIcon,
496 KeyEvent.KEYCODE_P,
497 KeyEvent.META_META_ON));
498 }
499
500 // Calendar.
501 final Icon calendarIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CALENDAR, userId);
502 if (calendarIcon != 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_calendar),
505 calendarIcon,
506 KeyEvent.KEYCODE_L,
507 KeyEvent.META_META_ON));
508 }
509
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100510 final int itemsSize = keyboardShortcutInfoAppItems.size();
511 if (itemsSize == 0) {
512 return null;
513 }
514
515 // Sorts by label, case insensitive with nulls and/or empty labels last.
516 Collections.sort(keyboardShortcutInfoAppItems, mApplicationItemsComparator);
517 return new KeyboardShortcutGroup(
518 mContext.getString(R.string.keyboard_shortcut_group_applications),
519 keyboardShortcutInfoAppItems,
520 true);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100521 }
522
523 private Icon getIconForIntentCategory(String intentCategory, int userId) {
524 final Intent intent = new Intent(Intent.ACTION_MAIN);
525 intent.addCategory(intentCategory);
526
527 final PackageInfo packageInfo = getPackageInfoForIntent(intent, userId);
528 if (packageInfo != null && packageInfo.applicationInfo.icon != 0) {
529 return Icon.createWithResource(
530 packageInfo.applicationInfo.packageName,
531 packageInfo.applicationInfo.icon);
532 }
533 return null;
534 }
535
536 private PackageInfo getPackageInfoForIntent(Intent intent, int userId) {
537 try {
538 ResolveInfo handler;
539 handler = mPackageManager.resolveIntent(
540 intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()), 0, userId);
541 if (handler == null || handler.activityInfo == null) {
542 return null;
543 }
544 return mPackageManager.getPackageInfo(handler.activityInfo.packageName, 0, userId);
545 } catch (RemoteException e) {
546 Log.e(TAG, "PackageManagerService is dead", e);
547 return null;
548 }
549 }
550
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000551 private void showKeyboardShortcutsDialog(
552 final List<KeyboardShortcutGroup> keyboardShortcutGroups) {
553 // Need to post on the main thread.
554 mHandler.post(new Runnable() {
555 @Override
556 public void run() {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000557 handleShowKeyboardShortcuts(keyboardShortcutGroups);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000558 }
559 });
560 }
561
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000562 private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) {
563 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
564 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
565 LAYOUT_INFLATER_SERVICE);
566 final View keyboardShortcutsView = inflater.inflate(
567 R.layout.keyboard_shortcuts_view, null);
568 populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById(
569 R.id.keyboard_shortcuts_container), keyboardShortcutGroups);
570 dialogBuilder.setView(keyboardShortcutsView);
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100571 dialogBuilder.setPositiveButton(R.string.quick_settings_done, mDialogCloseListener);
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000572 mKeyboardShortcutsDialog = dialogBuilder.create();
573 mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true);
574 Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow();
575 keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG);
576 mKeyboardShortcutsDialog.show();
577 }
578
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000579 private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout,
580 List<KeyboardShortcutGroup> keyboardShortcutGroups) {
581 LayoutInflater inflater = LayoutInflater.from(mContext);
582 final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size();
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100583 TextView shortcutsKeyView = (TextView) inflater.inflate(
584 R.layout.keyboard_shortcuts_key_view, null, false);
585 shortcutsKeyView.measure(
586 View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
587 final int shortcutKeyTextItemMinWidth = shortcutsKeyView.getMeasuredHeight();
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100588 // Needed to be able to scale the image items to the same height as the text items.
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100589 final int shortcutKeyIconItemHeightWidth = shortcutsKeyView.getMeasuredHeight()
590 - shortcutsKeyView.getPaddingTop()
591 - shortcutsKeyView.getPaddingBottom();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000592 for (int i = 0; i < keyboardShortcutGroupsSize; i++) {
593 KeyboardShortcutGroup group = keyboardShortcutGroups.get(i);
594 TextView categoryTitle = (TextView) inflater.inflate(
595 R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false);
596 categoryTitle.setText(group.getLabel());
597 categoryTitle.setTextColor(group.isSystemGroup()
598 ? mContext.getColor(R.color.ksh_system_group_color)
599 : mContext.getColor(R.color.ksh_application_group_color));
600 keyboardShortcutsLayout.addView(categoryTitle);
601
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000602 LinearLayout shortcutContainer = (LinearLayout) inflater.inflate(
603 R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000604 final int itemsSize = group.getItems().size();
605 for (int j = 0; j < itemsSize; j++) {
606 KeyboardShortcutInfo info = group.getItems().get(j);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100607 List<StringDrawableContainer> shortcutKeys = getHumanReadableShortcutKeys(info);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800608 if (shortcutKeys == null) {
609 // Ignore shortcuts we can't display keys for.
610 Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping.");
611 continue;
612 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000613 View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item,
614 shortcutContainer, false);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100615
616 if (info.getIcon() != null) {
617 ImageView shortcutIcon = (ImageView) shortcutView
618 .findViewById(R.id.keyboard_shortcuts_icon);
619 shortcutIcon.setImageIcon(info.getIcon());
620 shortcutIcon.setVisibility(View.VISIBLE);
621 }
622
623 TextView shortcutKeyword = (TextView) shortcutView
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000624 .findViewById(R.id.keyboard_shortcuts_keyword);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100625 shortcutKeyword.setText(info.getLabel());
626 if (info.getIcon() != null) {
627 RelativeLayout.LayoutParams lp =
628 (RelativeLayout.LayoutParams) shortcutKeyword.getLayoutParams();
629 lp.removeRule(RelativeLayout.ALIGN_PARENT_START);
630 shortcutKeyword.setLayoutParams(lp);
631 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000632
Andrei Stingaceanu844927d2016-02-16 14:31:58 +0000633 ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000634 .findViewById(R.id.keyboard_shortcuts_item_container);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000635 final int shortcutKeysSize = shortcutKeys.size();
636 for (int k = 0; k < shortcutKeysSize; k++) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100637 StringDrawableContainer shortcutRepresentation = shortcutKeys.get(k);
638 if (shortcutRepresentation.mDrawable != null) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100639 ImageView shortcutKeyIconView = (ImageView) inflater.inflate(
640 R.layout.keyboard_shortcuts_key_icon_view, shortcutItemsContainer,
641 false);
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100642 Bitmap bitmap = Bitmap.createBitmap(shortcutKeyIconItemHeightWidth,
643 shortcutKeyIconItemHeightWidth, Bitmap.Config.ARGB_8888);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100644 Canvas canvas = new Canvas(bitmap);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100645 shortcutRepresentation.mDrawable.setBounds(0, 0, canvas.getWidth(),
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100646 canvas.getHeight());
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100647 shortcutRepresentation.mDrawable.draw(canvas);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100648 shortcutKeyIconView.setImageBitmap(bitmap);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100649 shortcutKeyIconView.setImportantForAccessibility(
650 IMPORTANT_FOR_ACCESSIBILITY_YES);
651 shortcutKeyIconView.setAccessibilityDelegate(
652 new ShortcutKeyAccessibilityDelegate(
653 shortcutRepresentation.mString));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100654 shortcutItemsContainer.addView(shortcutKeyIconView);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100655 } else if (shortcutRepresentation.mString != null) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100656 TextView shortcutKeyTextView = (TextView) inflater.inflate(
657 R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer,
658 false);
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100659 shortcutKeyTextView.setMinimumWidth(shortcutKeyTextItemMinWidth);
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100660 shortcutKeyTextView.setText(shortcutRepresentation.mString);
661 shortcutKeyTextView.setAccessibilityDelegate(
662 new ShortcutKeyAccessibilityDelegate(
663 shortcutRepresentation.mString));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100664 shortcutItemsContainer.addView(shortcutKeyTextView);
665 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000666 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000667 shortcutContainer.addView(shortcutView);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000668 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000669 keyboardShortcutsLayout.addView(shortcutContainer);
670 if (i < keyboardShortcutGroupsSize - 1) {
671 View separator = inflater.inflate(
672 R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout,
673 false);
674 keyboardShortcutsLayout.addView(separator);
675 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000676 }
677 }
678
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100679 private List<StringDrawableContainer> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) {
680 List<StringDrawableContainer> shortcutKeys = getHumanReadableModifiers(info);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800681 if (shortcutKeys == null) {
682 return null;
683 }
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100684 String shortcutKeyString = null;
685 Drawable shortcutKeyDrawable = null;
Clara Bayarri1d648a12016-03-23 17:09:02 +0000686 if (info.getBaseCharacter() > Character.MIN_VALUE) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100687 shortcutKeyString = String.valueOf(info.getBaseCharacter());
Clara Bayarrib999af52016-04-06 16:02:35 +0100688 } else if (mSpecialCharacterDrawables.get(info.getKeycode()) != null) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100689 shortcutKeyDrawable = mSpecialCharacterDrawables.get(info.getKeycode());
690 shortcutKeyString = mSpecialCharacterNames.get(info.getKeycode());
Clara Bayarrib999af52016-04-06 16:02:35 +0100691 } else if (mSpecialCharacterNames.get(info.getKeycode()) != null) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100692 shortcutKeyString = mSpecialCharacterNames.get(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800693 } else {
Clara Bayarri1d648a12016-03-23 17:09:02 +0000694 // Special case for shortcuts with no base key or keycode.
695 if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) {
696 return shortcutKeys;
697 }
Clara Bayarri03f19552016-04-06 10:59:52 +0100698 char displayLabel = mKeyCharacterMap.getDisplayLabel(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800699 if (displayLabel != 0) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100700 shortcutKeyString = String.valueOf(displayLabel);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800701 } else {
Clara Bayarri382c59e2016-05-18 12:19:17 +0100702 displayLabel = mBackupKeyCharacterMap.getDisplayLabel(info.getKeycode());
703 if (displayLabel != 0) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100704 shortcutKeyString = String.valueOf(displayLabel);
Clara Bayarri382c59e2016-05-18 12:19:17 +0100705 } else {
706 return null;
707 }
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800708 }
709 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100710
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100711 if (shortcutKeyString != null) {
712 shortcutKeys.add(new StringDrawableContainer(shortcutKeyString, shortcutKeyDrawable));
713 } else {
714 Log.w(TAG, "Keyboard Shortcut does not have a text representation, skipping.");
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100715 }
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100716
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000717 return shortcutKeys;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100718 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800719
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100720 private List<StringDrawableContainer> getHumanReadableModifiers(KeyboardShortcutInfo info) {
721 final List<StringDrawableContainer> shortcutKeys = new ArrayList<>();
Clara Bayarrib9057df2016-03-02 11:37:09 -0800722 int modifiers = info.getModifiers();
723 if (modifiers == 0) {
724 return shortcutKeys;
725 }
Clara Bayarrib999af52016-04-06 16:02:35 +0100726 for(int i = 0; i < mModifierNames.size(); ++i) {
727 final int supportedModifier = mModifierNames.keyAt(i);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800728 if ((modifiers & supportedModifier) != 0) {
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100729 shortcutKeys.add(new StringDrawableContainer(
730 mModifierNames.get(supportedModifier),
731 mModifierDrawables.get(supportedModifier)));
Clara Bayarrib9057df2016-03-02 11:37:09 -0800732 modifiers &= ~supportedModifier;
733 }
734 }
735 if (modifiers != 0) {
736 // Remaining unsupported modifiers, don't show anything.
737 return null;
738 }
739 return shortcutKeys;
740 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100741
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100742 private final class ShortcutKeyAccessibilityDelegate extends AccessibilityDelegate {
743 private String mContentDescription;
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100744
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100745 ShortcutKeyAccessibilityDelegate(String contentDescription) {
746 mContentDescription = contentDescription;
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100747 }
748
Andrei Stingaceanub4817012016-06-13 17:26:39 +0100749 @Override
750 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
751 super.onInitializeAccessibilityNodeInfo(host, info);
752 if (mContentDescription != null) {
753 info.setContentDescription(mContentDescription.toLowerCase());
754 }
755 }
756 }
757
758 private static final class StringDrawableContainer {
759 @NonNull
760 public String mString;
761 @Nullable
762 public Drawable mDrawable;
763
764 StringDrawableContainer(String string, Drawable drawable) {
765 mString = string;
766 mDrawable = drawable;
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100767 }
768 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100769}