blob: 7f16c699efcf1eb17c6453b64b77e809e5bea165 [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
19import android.app.AlertDialog;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010020import android.app.AppGlobals;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010021import android.app.Dialog;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010022import android.content.ComponentName;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010023import android.content.Context;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000024import android.content.DialogInterface;
25import android.content.DialogInterface.OnClickListener;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010026import android.content.Intent;
27import android.content.pm.IPackageManager;
28import android.content.pm.PackageInfo;
29import android.content.pm.ResolveInfo;
30import android.graphics.drawable.Icon;
Andrei Stingaceanud1519102016-03-31 15:53:33 +010031import android.graphics.Bitmap;
32import android.graphics.Canvas;
33import android.graphics.drawable.Drawable;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080034import android.hardware.input.InputManager;
Clara Bayarri75e09792015-07-29 16:20:40 +010035import android.os.Handler;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000036import android.os.Looper;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010037import android.os.RemoteException;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080038import android.util.Log;
39import android.util.SparseArray;
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +000040import android.view.ContextThemeWrapper;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080041import android.view.InputDevice;
42import android.view.KeyCharacterMap;
Clara Bayarri75e09792015-07-29 16:20:40 +010043import android.view.KeyEvent;
44import android.view.KeyboardShortcutGroup;
45import android.view.KeyboardShortcutInfo;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010046import android.view.LayoutInflater;
47import android.view.View;
Andrei Stingaceanu844927d2016-02-16 14:31:58 +000048import android.view.ViewGroup;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010049import android.view.Window;
Clara Bayarri75e09792015-07-29 16:20:40 +010050import android.view.WindowManager.KeyboardShortcutsReceiver;
Andrei Stingaceanud1519102016-03-31 15:53:33 +010051import android.widget.ImageView;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000052import android.widget.LinearLayout;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010053import android.widget.RelativeLayout;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000054import android.widget.TextView;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010055
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010056import com.android.internal.app.AssistUtils;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010057import com.android.systemui.R;
Clara Bayarri75e09792015-07-29 16:20:40 +010058import com.android.systemui.recents.Recents;
59
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000060import java.util.ArrayList;
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010061import java.util.Collections;
62import java.util.Comparator;
Clara Bayarri75e09792015-07-29 16:20:40 +010063import java.util.List;
64
65import static android.content.Context.LAYOUT_INFLATER_SERVICE;
Clara Bayarri75e09792015-07-29 16:20:40 +010066import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010067
68/**
69 * Contains functionality for handling keyboard shortcuts.
70 */
Andrei Stingaceanud1519102016-03-31 15:53:33 +010071public final class KeyboardShortcuts {
Clara Bayarri4e850ff2016-03-02 11:12:32 -080072 private static final String TAG = KeyboardShortcuts.class.getSimpleName();
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +010073 private static final Object sLock = new Object();
74 private static KeyboardShortcuts sInstance;
75 private static boolean sIsShowing;
76
Clara Bayarrib999af52016-04-06 16:02:35 +010077 private final SparseArray<String> mSpecialCharacterNames = new SparseArray<>();
78 private final SparseArray<String> mModifierNames = new SparseArray<>();
79 private final SparseArray<Drawable> mSpecialCharacterDrawables = new SparseArray<>();
80 private final SparseArray<Drawable> mModifierDrawables = new SparseArray<>();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000081
82 private final Handler mHandler = new Handler(Looper.getMainLooper());
83 private final Context mContext;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010084 private final IPackageManager mPackageManager;
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010085 private final OnClickListener mDialogCloseListener = new DialogInterface.OnClickListener() {
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000086 public void onClick(DialogInterface dialog, int id) {
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +010087 dismissKeyboardShortcuts();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000088 }
89 };
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010090 private final Comparator<KeyboardShortcutInfo> mApplicationItemsComparator =
91 new Comparator<KeyboardShortcutInfo>() {
92 @Override
93 public int compare(KeyboardShortcutInfo ksh1, KeyboardShortcutInfo ksh2) {
94 boolean ksh1ShouldBeLast = ksh1.getLabel() == null
95 || ksh1.getLabel().toString().isEmpty();
96 boolean ksh2ShouldBeLast = ksh2.getLabel() == null
97 || ksh2.getLabel().toString().isEmpty();
98 if (ksh1ShouldBeLast && ksh2ShouldBeLast) {
99 return 0;
100 }
101 if (ksh1ShouldBeLast) {
102 return 1;
103 }
104 if (ksh2ShouldBeLast) {
105 return -1;
106 }
107 return (ksh1.getLabel().toString()).compareToIgnoreCase(
108 ksh2.getLabel().toString());
109 }
110 };
Clara Bayarri75e09792015-07-29 16:20:40 +0100111
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100112 private Dialog mKeyboardShortcutsDialog;
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800113 private KeyCharacterMap mKeyCharacterMap;
Clara Bayarri382c59e2016-05-18 12:19:17 +0100114 private KeyCharacterMap mBackupKeyCharacterMap;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100115
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100116 private KeyboardShortcuts(Context context) {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000117 this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_Material_Light);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100118 this.mPackageManager = AppGlobals.getPackageManager();
Clara Bayarrib999af52016-04-06 16:02:35 +0100119 loadResources(context);
120 }
121
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100122 private static KeyboardShortcuts getInstance(Context context) {
123 if (sInstance == null) {
124 sInstance = new KeyboardShortcuts(context);
125 }
126 return sInstance;
127 }
128
129 public static void show(Context context, int deviceId) {
130 synchronized (sLock) {
131 if (sInstance != null && !sInstance.mContext.equals(context)) {
132 dismiss();
133 }
134 getInstance(context).showKeyboardShortcuts(deviceId);
135 sIsShowing = true;
136 }
137 }
138
139 public static void toggle(Context context, int deviceId) {
140 synchronized (sLock) {
141 if (sIsShowing) {
142 dismiss();
143 } else {
144 show(context, deviceId);
145 }
146 }
147 }
148
149 public static void dismiss() {
150 synchronized (sLock) {
151 if (sInstance != null) {
152 sInstance.dismissKeyboardShortcuts();
153 sInstance = null;
154 }
155 sIsShowing = false;
156 }
157 }
158
Clara Bayarrib999af52016-04-06 16:02:35 +0100159 private void loadResources(Context context) {
160 mSpecialCharacterNames.put(
161 KeyEvent.KEYCODE_HOME, context.getString(R.string.keyboard_key_home));
162 mSpecialCharacterNames.put(
163 KeyEvent.KEYCODE_BACK, context.getString(R.string.keyboard_key_back));
164 mSpecialCharacterNames.put(
165 KeyEvent.KEYCODE_DPAD_UP, context.getString(R.string.keyboard_key_dpad_up));
166 mSpecialCharacterNames.put(
167 KeyEvent.KEYCODE_DPAD_DOWN, context.getString(R.string.keyboard_key_dpad_down));
168 mSpecialCharacterNames.put(
169 KeyEvent.KEYCODE_DPAD_LEFT, context.getString(R.string.keyboard_key_dpad_left));
170 mSpecialCharacterNames.put(
171 KeyEvent.KEYCODE_DPAD_RIGHT, context.getString(R.string.keyboard_key_dpad_right));
172 mSpecialCharacterNames.put(
173 KeyEvent.KEYCODE_DPAD_CENTER, context.getString(R.string.keyboard_key_dpad_center));
174 mSpecialCharacterNames.put(KeyEvent.KEYCODE_PERIOD, ".");
175 mSpecialCharacterNames.put(
176 KeyEvent.KEYCODE_TAB, context.getString(R.string.keyboard_key_tab));
177 mSpecialCharacterNames.put(
178 KeyEvent.KEYCODE_SPACE, context.getString(R.string.keyboard_key_space));
179 mSpecialCharacterNames.put(
180 KeyEvent.KEYCODE_ENTER, context.getString(R.string.keyboard_key_enter));
181 mSpecialCharacterNames.put(
182 KeyEvent.KEYCODE_DEL, context.getString(R.string.keyboard_key_backspace));
183 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE,
184 context.getString(R.string.keyboard_key_media_play_pause));
185 mSpecialCharacterNames.put(
186 KeyEvent.KEYCODE_MEDIA_STOP, context.getString(R.string.keyboard_key_media_stop));
187 mSpecialCharacterNames.put(
188 KeyEvent.KEYCODE_MEDIA_NEXT, context.getString(R.string.keyboard_key_media_next));
189 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PREVIOUS,
190 context.getString(R.string.keyboard_key_media_previous));
191 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_REWIND,
192 context.getString(R.string.keyboard_key_media_rewind));
193 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD,
194 context.getString(R.string.keyboard_key_media_fast_forward));
195 mSpecialCharacterNames.put(
196 KeyEvent.KEYCODE_PAGE_UP, context.getString(R.string.keyboard_key_page_up));
197 mSpecialCharacterNames.put(
198 KeyEvent.KEYCODE_PAGE_DOWN, context.getString(R.string.keyboard_key_page_down));
199 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_A,
200 context.getString(R.string.keyboard_key_button_template, "A"));
201 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_B,
202 context.getString(R.string.keyboard_key_button_template, "B"));
203 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_C,
204 context.getString(R.string.keyboard_key_button_template, "C"));
205 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_X,
206 context.getString(R.string.keyboard_key_button_template, "X"));
207 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Y,
208 context.getString(R.string.keyboard_key_button_template, "Y"));
209 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Z,
210 context.getString(R.string.keyboard_key_button_template, "Z"));
211 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L1,
212 context.getString(R.string.keyboard_key_button_template, "L1"));
213 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R1,
214 context.getString(R.string.keyboard_key_button_template, "R1"));
215 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L2,
216 context.getString(R.string.keyboard_key_button_template, "L2"));
217 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R2,
218 context.getString(R.string.keyboard_key_button_template, "R2"));
219 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_START,
220 context.getString(R.string.keyboard_key_button_template, "Start"));
221 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_SELECT,
222 context.getString(R.string.keyboard_key_button_template, "Select"));
223 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_MODE,
224 context.getString(R.string.keyboard_key_button_template, "Mode"));
225 mSpecialCharacterNames.put(
226 KeyEvent.KEYCODE_FORWARD_DEL, context.getString(R.string.keyboard_key_forward_del));
227 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ESCAPE, "Esc");
228 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SYSRQ, "SysRq");
229 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BREAK, "Break");
230 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SCROLL_LOCK, "Scroll Lock");
231 mSpecialCharacterNames.put(
232 KeyEvent.KEYCODE_MOVE_HOME, context.getString(R.string.keyboard_key_move_home));
233 mSpecialCharacterNames.put(
234 KeyEvent.KEYCODE_MOVE_END, context.getString(R.string.keyboard_key_move_end));
235 mSpecialCharacterNames.put(
236 KeyEvent.KEYCODE_INSERT, context.getString(R.string.keyboard_key_insert));
237 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F1, "F1");
238 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F2, "F2");
239 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F3, "F3");
240 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F4, "F4");
241 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F5, "F5");
242 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F6, "F6");
243 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F7, "F7");
244 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F8, "F8");
245 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F9, "F9");
246 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F10, "F10");
247 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F11, "F11");
248 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F12, "F12");
249 mSpecialCharacterNames.put(
250 KeyEvent.KEYCODE_NUM_LOCK, context.getString(R.string.keyboard_key_num_lock));
251 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_0,
252 context.getString(R.string.keyboard_key_numpad_template, "0"));
253 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_1,
254 context.getString(R.string.keyboard_key_numpad_template, "1"));
255 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_2,
256 context.getString(R.string.keyboard_key_numpad_template, "2"));
257 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_3,
258 context.getString(R.string.keyboard_key_numpad_template, "3"));
259 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_4,
260 context.getString(R.string.keyboard_key_numpad_template, "4"));
261 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_5,
262 context.getString(R.string.keyboard_key_numpad_template, "5"));
263 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_6,
264 context.getString(R.string.keyboard_key_numpad_template, "6"));
265 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_7,
266 context.getString(R.string.keyboard_key_numpad_template, "7"));
267 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_8,
268 context.getString(R.string.keyboard_key_numpad_template, "8"));
269 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_9,
270 context.getString(R.string.keyboard_key_numpad_template, "9"));
271 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DIVIDE,
272 context.getString(R.string.keyboard_key_numpad_template, "/"));
273 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_MULTIPLY,
274 context.getString(R.string.keyboard_key_numpad_template, "*"));
275 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_SUBTRACT,
276 context.getString(R.string.keyboard_key_numpad_template, "-"));
277 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ADD,
278 context.getString(R.string.keyboard_key_numpad_template, "+"));
279 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DOT,
280 context.getString(R.string.keyboard_key_numpad_template, "."));
281 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_COMMA,
282 context.getString(R.string.keyboard_key_numpad_template, ","));
283 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ENTER,
284 context.getString(R.string.keyboard_key_numpad_template,
285 context.getString(R.string.keyboard_key_enter)));
286 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_EQUALS,
287 context.getString(R.string.keyboard_key_numpad_template, "="));
288 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN,
289 context.getString(R.string.keyboard_key_numpad_template, "("));
290 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN,
291 context.getString(R.string.keyboard_key_numpad_template, ")"));
292 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ZENKAKU_HANKAKU, "半角/全角");
293 mSpecialCharacterNames.put(KeyEvent.KEYCODE_EISU, "英数");
294 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MUHENKAN, "無変換");
295 mSpecialCharacterNames.put(KeyEvent.KEYCODE_HENKAN, "変換");
296 mSpecialCharacterNames.put(KeyEvent.KEYCODE_KATAKANA_HIRAGANA, "かな");
297
298 mModifierNames.put(KeyEvent.META_META_ON, "Meta");
299 mModifierNames.put(KeyEvent.META_CTRL_ON, "Ctrl");
300 mModifierNames.put(KeyEvent.META_ALT_ON, "Alt");
301 mModifierNames.put(KeyEvent.META_SHIFT_ON, "Shift");
302 mModifierNames.put(KeyEvent.META_SYM_ON, "Sym");
303 mModifierNames.put(KeyEvent.META_FUNCTION_ON, "Fn");
304
305 mSpecialCharacterDrawables.put(
306 KeyEvent.KEYCODE_DEL, context.getDrawable(R.drawable.ic_ksh_key_backspace));
307 mSpecialCharacterDrawables.put(
308 KeyEvent.KEYCODE_ENTER, context.getDrawable(R.drawable.ic_ksh_key_enter));
309 mSpecialCharacterDrawables.put(
310 KeyEvent.KEYCODE_DPAD_UP, context.getDrawable(R.drawable.ic_ksh_key_up));
311 mSpecialCharacterDrawables.put(
312 KeyEvent.KEYCODE_DPAD_RIGHT, context.getDrawable(R.drawable.ic_ksh_key_right));
313 mSpecialCharacterDrawables.put(
314 KeyEvent.KEYCODE_DPAD_DOWN, context.getDrawable(R.drawable.ic_ksh_key_down));
315 mSpecialCharacterDrawables.put(
316 KeyEvent.KEYCODE_DPAD_LEFT, context.getDrawable(R.drawable.ic_ksh_key_left));
317
318 mModifierDrawables.put(
319 KeyEvent.META_META_ON, context.getDrawable(R.drawable.ic_ksh_key_meta));
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000320 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100321
Clara Bayarri03f19552016-04-06 10:59:52 +0100322 /**
323 * Retrieves a {@link KeyCharacterMap} and assigns it to mKeyCharacterMap. If the given id is an
324 * existing device, that device's map is used. Otherwise, it checks first all available devices
325 * and if there is a full keyboard it uses that map, otherwise falls back to the Virtual
326 * Keyboard with its default map.
327 */
328 private void retrieveKeyCharacterMap(int deviceId) {
329 final InputManager inputManager = InputManager.getInstance();
Clara Bayarri382c59e2016-05-18 12:19:17 +0100330 mBackupKeyCharacterMap = inputManager.getInputDevice(-1).getKeyCharacterMap();
Clara Bayarri03f19552016-04-06 10:59:52 +0100331 if (deviceId != -1) {
332 final InputDevice inputDevice = inputManager.getInputDevice(deviceId);
333 if (inputDevice != null) {
334 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
335 return;
336 }
337 }
338 final int[] deviceIds = inputManager.getInputDeviceIds();
339 for (int i = 0; i < deviceIds.length; ++i) {
340 final InputDevice inputDevice = inputManager.getInputDevice(deviceIds[i]);
341 // -1 is the Virtual Keyboard, with the default key map. Use that one only as last
342 // resort.
343 if (inputDevice.getId() != -1 && inputDevice.isFullKeyboard()) {
344 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
345 return;
346 }
347 }
Clara Bayarri382c59e2016-05-18 12:19:17 +0100348 // Fall back to -1, the virtual keyboard.
349 mKeyCharacterMap = mBackupKeyCharacterMap;
Clara Bayarri03f19552016-04-06 10:59:52 +0100350 }
351
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100352 private void showKeyboardShortcuts(int deviceId) {
353 retrieveKeyCharacterMap(deviceId);
354 Recents.getSystemServices().requestKeyboardShortcuts(mContext,
355 new KeyboardShortcutsReceiver() {
356 @Override
357 public void onKeyboardShortcutsReceived(
358 final List<KeyboardShortcutGroup> result) {
359 result.add(getSystemShortcuts());
360 final KeyboardShortcutGroup appShortcuts = getDefaultApplicationShortcuts();
361 if (appShortcuts != null) {
362 result.add(appShortcuts);
363 }
364 showKeyboardShortcutsDialog(result);
365 }
366 }, deviceId);
367 }
368
369 private void dismissKeyboardShortcuts() {
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100370 if (mKeyboardShortcutsDialog != null) {
371 mKeyboardShortcutsDialog.dismiss();
372 mKeyboardShortcutsDialog = null;
373 }
374 }
375
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100376 private KeyboardShortcutGroup getSystemShortcuts() {
377 final KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup(
378 mContext.getString(R.string.keyboard_shortcut_group_system), true);
379 systemGroup.addItem(new KeyboardShortcutInfo(
380 mContext.getString(R.string.keyboard_shortcut_group_system_home),
381 KeyEvent.KEYCODE_ENTER,
382 KeyEvent.META_META_ON));
383 systemGroup.addItem(new KeyboardShortcutInfo(
384 mContext.getString(R.string.keyboard_shortcut_group_system_back),
385 KeyEvent.KEYCODE_DEL,
386 KeyEvent.META_META_ON));
387 systemGroup.addItem(new KeyboardShortcutInfo(
388 mContext.getString(R.string.keyboard_shortcut_group_system_recents),
389 KeyEvent.KEYCODE_TAB,
390 KeyEvent.META_ALT_ON));
391 systemGroup.addItem(new KeyboardShortcutInfo(
392 mContext.getString(
393 R.string.keyboard_shortcut_group_system_notifications),
394 KeyEvent.KEYCODE_N,
395 KeyEvent.META_META_ON));
396 systemGroup.addItem(new KeyboardShortcutInfo(
397 mContext.getString(
398 R.string.keyboard_shortcut_group_system_shortcuts_helper),
399 KeyEvent.KEYCODE_SLASH,
400 KeyEvent.META_META_ON));
401 systemGroup.addItem(new KeyboardShortcutInfo(
402 mContext.getString(
403 R.string.keyboard_shortcut_group_system_switch_input),
404 KeyEvent.KEYCODE_SPACE,
405 KeyEvent.META_META_ON));
406 return systemGroup;
407 }
408
409 private KeyboardShortcutGroup getDefaultApplicationShortcuts() {
410 final int userId = mContext.getUserId();
Andrei Stingaceanu9cfcafc2016-04-12 11:07:39 +0100411 List<KeyboardShortcutInfo> keyboardShortcutInfoAppItems = new ArrayList<>();
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100412
413 // Assist.
414 final AssistUtils assistUtils = new AssistUtils(mContext);
415 final ComponentName assistComponent = assistUtils.getAssistComponentForUser(userId);
416 PackageInfo assistPackageInfo = null;
417 try {
418 assistPackageInfo = mPackageManager.getPackageInfo(
419 assistComponent.getPackageName(), 0, userId);
420 } catch (RemoteException e) {
421 Log.e(TAG, "PackageManagerService is dead");
422 }
423
424 if (assistPackageInfo != null) {
425 final Icon assistIcon = Icon.createWithResource(
426 assistPackageInfo.applicationInfo.packageName,
427 assistPackageInfo.applicationInfo.icon);
428
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100429 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100430 mContext.getString(R.string.keyboard_shortcut_group_applications_assist),
431 assistIcon,
432 KeyEvent.KEYCODE_UNKNOWN,
433 KeyEvent.META_META_ON));
434 }
435
436 // Browser.
437 final Icon browserIcon = getIconForIntentCategory(Intent.CATEGORY_APP_BROWSER, userId);
438 if (browserIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100439 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100440 mContext.getString(R.string.keyboard_shortcut_group_applications_browser),
441 browserIcon,
442 KeyEvent.KEYCODE_B,
443 KeyEvent.META_META_ON));
444 }
445
446
447 // Contacts.
448 final Icon contactsIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CONTACTS, userId);
449 if (contactsIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100450 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100451 mContext.getString(R.string.keyboard_shortcut_group_applications_contacts),
452 contactsIcon,
453 KeyEvent.KEYCODE_C,
454 KeyEvent.META_META_ON));
455 }
456
457 // Email.
458 final Icon emailIcon = getIconForIntentCategory(Intent.CATEGORY_APP_EMAIL, userId);
459 if (emailIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100460 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100461 mContext.getString(R.string.keyboard_shortcut_group_applications_email),
462 emailIcon,
463 KeyEvent.KEYCODE_E,
464 KeyEvent.META_META_ON));
465 }
466
467 // Messaging.
468 final Icon messagingIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MESSAGING, userId);
469 if (messagingIcon != 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_im),
472 messagingIcon,
473 KeyEvent.KEYCODE_T,
474 KeyEvent.META_META_ON));
475 }
476
477 // Music.
478 final Icon musicIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MUSIC, userId);
479 if (musicIcon != 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_music),
482 musicIcon,
483 KeyEvent.KEYCODE_P,
484 KeyEvent.META_META_ON));
485 }
486
487 // Calendar.
488 final Icon calendarIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CALENDAR, userId);
489 if (calendarIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100490 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100491 mContext.getString(R.string.keyboard_shortcut_group_applications_calendar),
492 calendarIcon,
493 KeyEvent.KEYCODE_L,
494 KeyEvent.META_META_ON));
495 }
496
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100497 final int itemsSize = keyboardShortcutInfoAppItems.size();
498 if (itemsSize == 0) {
499 return null;
500 }
501
502 // Sorts by label, case insensitive with nulls and/or empty labels last.
503 Collections.sort(keyboardShortcutInfoAppItems, mApplicationItemsComparator);
504 return new KeyboardShortcutGroup(
505 mContext.getString(R.string.keyboard_shortcut_group_applications),
506 keyboardShortcutInfoAppItems,
507 true);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100508 }
509
510 private Icon getIconForIntentCategory(String intentCategory, int userId) {
511 final Intent intent = new Intent(Intent.ACTION_MAIN);
512 intent.addCategory(intentCategory);
513
514 final PackageInfo packageInfo = getPackageInfoForIntent(intent, userId);
515 if (packageInfo != null && packageInfo.applicationInfo.icon != 0) {
516 return Icon.createWithResource(
517 packageInfo.applicationInfo.packageName,
518 packageInfo.applicationInfo.icon);
519 }
520 return null;
521 }
522
523 private PackageInfo getPackageInfoForIntent(Intent intent, int userId) {
524 try {
525 ResolveInfo handler;
526 handler = mPackageManager.resolveIntent(
527 intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()), 0, userId);
528 if (handler == null || handler.activityInfo == null) {
529 return null;
530 }
531 return mPackageManager.getPackageInfo(handler.activityInfo.packageName, 0, userId);
532 } catch (RemoteException e) {
533 Log.e(TAG, "PackageManagerService is dead", e);
534 return null;
535 }
536 }
537
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000538 private void showKeyboardShortcutsDialog(
539 final List<KeyboardShortcutGroup> keyboardShortcutGroups) {
540 // Need to post on the main thread.
541 mHandler.post(new Runnable() {
542 @Override
543 public void run() {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000544 handleShowKeyboardShortcuts(keyboardShortcutGroups);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000545 }
546 });
547 }
548
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000549 private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) {
550 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
551 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
552 LAYOUT_INFLATER_SERVICE);
553 final View keyboardShortcutsView = inflater.inflate(
554 R.layout.keyboard_shortcuts_view, null);
555 populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById(
556 R.id.keyboard_shortcuts_container), keyboardShortcutGroups);
557 dialogBuilder.setView(keyboardShortcutsView);
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100558 dialogBuilder.setPositiveButton(R.string.quick_settings_done, mDialogCloseListener);
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000559 mKeyboardShortcutsDialog = dialogBuilder.create();
560 mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true);
561 Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow();
562 keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG);
563 mKeyboardShortcutsDialog.show();
564 }
565
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000566 private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout,
567 List<KeyboardShortcutGroup> keyboardShortcutGroups) {
568 LayoutInflater inflater = LayoutInflater.from(mContext);
569 final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size();
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100570 TextView shortcutsKeyView = (TextView) inflater.inflate(
571 R.layout.keyboard_shortcuts_key_view, null, false);
572 shortcutsKeyView.measure(
573 View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
574 final int shortcutKeyTextItemMinWidth = shortcutsKeyView.getMeasuredHeight();
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100575 // Needed to be able to scale the image items to the same height as the text items.
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100576 final int shortcutKeyIconItemHeightWidth = shortcutsKeyView.getMeasuredHeight()
577 - shortcutsKeyView.getPaddingTop()
578 - shortcutsKeyView.getPaddingBottom();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000579 for (int i = 0; i < keyboardShortcutGroupsSize; i++) {
580 KeyboardShortcutGroup group = keyboardShortcutGroups.get(i);
581 TextView categoryTitle = (TextView) inflater.inflate(
582 R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false);
583 categoryTitle.setText(group.getLabel());
584 categoryTitle.setTextColor(group.isSystemGroup()
585 ? mContext.getColor(R.color.ksh_system_group_color)
586 : mContext.getColor(R.color.ksh_application_group_color));
587 keyboardShortcutsLayout.addView(categoryTitle);
588
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000589 LinearLayout shortcutContainer = (LinearLayout) inflater.inflate(
590 R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000591 final int itemsSize = group.getItems().size();
592 for (int j = 0; j < itemsSize; j++) {
593 KeyboardShortcutInfo info = group.getItems().get(j);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100594 List<StringOrDrawable> shortcutKeys = getHumanReadableShortcutKeys(info);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800595 if (shortcutKeys == null) {
596 // Ignore shortcuts we can't display keys for.
597 Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping.");
598 continue;
599 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000600 View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item,
601 shortcutContainer, false);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100602
603 if (info.getIcon() != null) {
604 ImageView shortcutIcon = (ImageView) shortcutView
605 .findViewById(R.id.keyboard_shortcuts_icon);
606 shortcutIcon.setImageIcon(info.getIcon());
607 shortcutIcon.setVisibility(View.VISIBLE);
608 }
609
610 TextView shortcutKeyword = (TextView) shortcutView
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000611 .findViewById(R.id.keyboard_shortcuts_keyword);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100612 shortcutKeyword.setText(info.getLabel());
613 if (info.getIcon() != null) {
614 RelativeLayout.LayoutParams lp =
615 (RelativeLayout.LayoutParams) shortcutKeyword.getLayoutParams();
616 lp.removeRule(RelativeLayout.ALIGN_PARENT_START);
617 shortcutKeyword.setLayoutParams(lp);
618 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000619
Andrei Stingaceanu844927d2016-02-16 14:31:58 +0000620 ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000621 .findViewById(R.id.keyboard_shortcuts_item_container);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000622 final int shortcutKeysSize = shortcutKeys.size();
623 for (int k = 0; k < shortcutKeysSize; k++) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100624 StringOrDrawable shortcutRepresentation = shortcutKeys.get(k);
625 if (shortcutRepresentation.drawable != null) {
626 ImageView shortcutKeyIconView = (ImageView) inflater.inflate(
627 R.layout.keyboard_shortcuts_key_icon_view, shortcutItemsContainer,
628 false);
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100629 Bitmap bitmap = Bitmap.createBitmap(shortcutKeyIconItemHeightWidth,
630 shortcutKeyIconItemHeightWidth, Bitmap.Config.ARGB_8888);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100631 Canvas canvas = new Canvas(bitmap);
632 shortcutRepresentation.drawable.setBounds(0, 0, canvas.getWidth(),
633 canvas.getHeight());
634 shortcutRepresentation.drawable.draw(canvas);
635 shortcutKeyIconView.setImageBitmap(bitmap);
636 shortcutItemsContainer.addView(shortcutKeyIconView);
637 } else if (shortcutRepresentation.string != null) {
638 TextView shortcutKeyTextView = (TextView) inflater.inflate(
639 R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer,
640 false);
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100641 shortcutKeyTextView.setMinimumWidth(shortcutKeyTextItemMinWidth);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100642 shortcutKeyTextView.setText(shortcutRepresentation.string);
643 shortcutItemsContainer.addView(shortcutKeyTextView);
644 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000645 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000646 shortcutContainer.addView(shortcutView);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000647 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000648 keyboardShortcutsLayout.addView(shortcutContainer);
649 if (i < keyboardShortcutGroupsSize - 1) {
650 View separator = inflater.inflate(
651 R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout,
652 false);
653 keyboardShortcutsLayout.addView(separator);
654 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000655 }
656 }
657
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100658 private List<StringOrDrawable> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) {
659 List<StringOrDrawable> shortcutKeys = getHumanReadableModifiers(info);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800660 if (shortcutKeys == null) {
661 return null;
662 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100663 String displayLabelString = null;
664 Drawable displayLabelDrawable = null;
Clara Bayarri1d648a12016-03-23 17:09:02 +0000665 if (info.getBaseCharacter() > Character.MIN_VALUE) {
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800666 displayLabelString = String.valueOf(info.getBaseCharacter());
Clara Bayarrib999af52016-04-06 16:02:35 +0100667 } else if (mSpecialCharacterDrawables.get(info.getKeycode()) != null) {
668 displayLabelDrawable = mSpecialCharacterDrawables.get(info.getKeycode());
669 } else if (mSpecialCharacterNames.get(info.getKeycode()) != null) {
670 displayLabelString = mSpecialCharacterNames.get(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800671 } else {
Clara Bayarri1d648a12016-03-23 17:09:02 +0000672 // Special case for shortcuts with no base key or keycode.
673 if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) {
674 return shortcutKeys;
675 }
Clara Bayarri03f19552016-04-06 10:59:52 +0100676 char displayLabel = mKeyCharacterMap.getDisplayLabel(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800677 if (displayLabel != 0) {
678 displayLabelString = String.valueOf(displayLabel);
679 } else {
Clara Bayarri382c59e2016-05-18 12:19:17 +0100680 displayLabel = mBackupKeyCharacterMap.getDisplayLabel(info.getKeycode());
681 if (displayLabel != 0) {
682 displayLabelString = String.valueOf(displayLabel);
683 } else {
684 return null;
685 }
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800686 }
687 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100688
689 if (displayLabelDrawable != null) {
690 shortcutKeys.add(new StringOrDrawable(displayLabelDrawable));
691 } else if (displayLabelString != null) {
692 shortcutKeys.add(new StringOrDrawable(displayLabelString.toUpperCase()));
693 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000694 return shortcutKeys;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100695 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800696
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100697 private List<StringOrDrawable> getHumanReadableModifiers(KeyboardShortcutInfo info) {
698 final List<StringOrDrawable> shortcutKeys = new ArrayList<>();
Clara Bayarrib9057df2016-03-02 11:37:09 -0800699 int modifiers = info.getModifiers();
700 if (modifiers == 0) {
701 return shortcutKeys;
702 }
Clara Bayarrib999af52016-04-06 16:02:35 +0100703 for(int i = 0; i < mModifierNames.size(); ++i) {
704 final int supportedModifier = mModifierNames.keyAt(i);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800705 if ((modifiers & supportedModifier) != 0) {
Clara Bayarrib999af52016-04-06 16:02:35 +0100706 if (mModifierDrawables.get(supportedModifier) != null) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100707 shortcutKeys.add(new StringOrDrawable(
Clara Bayarrib999af52016-04-06 16:02:35 +0100708 mModifierDrawables.get(supportedModifier)));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100709 } else {
710 shortcutKeys.add(new StringOrDrawable(
Clara Bayarrib999af52016-04-06 16:02:35 +0100711 mModifierNames.get(supportedModifier).toUpperCase()));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100712 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800713 modifiers &= ~supportedModifier;
714 }
715 }
716 if (modifiers != 0) {
717 // Remaining unsupported modifiers, don't show anything.
718 return null;
719 }
720 return shortcutKeys;
721 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100722
723 private static final class StringOrDrawable {
724 public String string;
725 public Drawable drawable;
726
727 public StringOrDrawable(String string) {
728 this.string = string;
729 }
730
731 public StringOrDrawable(Drawable drawable) {
732 this.drawable = drawable;
733 }
734 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100735}