blob: 0e1441ef1c3be2470a7c88086ea9f91b2132f6a8 [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;
Clara Bayarric17a5982016-04-15 12:26:47 +010057import com.android.internal.logging.MetricsLogger;
58import com.android.internal.logging.MetricsProto;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010059import com.android.systemui.R;
Clara Bayarri75e09792015-07-29 16:20:40 +010060import com.android.systemui.recents.Recents;
61
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000062import java.util.ArrayList;
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010063import java.util.Collections;
64import java.util.Comparator;
Clara Bayarri75e09792015-07-29 16:20:40 +010065import java.util.List;
66
67import static android.content.Context.LAYOUT_INFLATER_SERVICE;
Clara Bayarri75e09792015-07-29 16:20:40 +010068import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010069
70/**
71 * Contains functionality for handling keyboard shortcuts.
72 */
Andrei Stingaceanud1519102016-03-31 15:53:33 +010073public final class KeyboardShortcuts {
Clara Bayarri4e850ff2016-03-02 11:12:32 -080074 private static final String TAG = KeyboardShortcuts.class.getSimpleName();
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +010075 private static final Object sLock = new Object();
76 private static KeyboardShortcuts sInstance;
77 private static boolean sIsShowing;
78
Clara Bayarrib999af52016-04-06 16:02:35 +010079 private final SparseArray<String> mSpecialCharacterNames = new SparseArray<>();
80 private final SparseArray<String> mModifierNames = new SparseArray<>();
81 private final SparseArray<Drawable> mSpecialCharacterDrawables = new SparseArray<>();
82 private final SparseArray<Drawable> mModifierDrawables = new SparseArray<>();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000083
84 private final Handler mHandler = new Handler(Looper.getMainLooper());
85 private final Context mContext;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010086 private final IPackageManager mPackageManager;
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010087 private final OnClickListener mDialogCloseListener = new DialogInterface.OnClickListener() {
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000088 public void onClick(DialogInterface dialog, int id) {
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +010089 dismissKeyboardShortcuts();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000090 }
91 };
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010092 private final Comparator<KeyboardShortcutInfo> mApplicationItemsComparator =
93 new Comparator<KeyboardShortcutInfo>() {
94 @Override
95 public int compare(KeyboardShortcutInfo ksh1, KeyboardShortcutInfo ksh2) {
96 boolean ksh1ShouldBeLast = ksh1.getLabel() == null
97 || ksh1.getLabel().toString().isEmpty();
98 boolean ksh2ShouldBeLast = ksh2.getLabel() == null
99 || ksh2.getLabel().toString().isEmpty();
100 if (ksh1ShouldBeLast && ksh2ShouldBeLast) {
101 return 0;
102 }
103 if (ksh1ShouldBeLast) {
104 return 1;
105 }
106 if (ksh2ShouldBeLast) {
107 return -1;
108 }
109 return (ksh1.getLabel().toString()).compareToIgnoreCase(
110 ksh2.getLabel().toString());
111 }
112 };
Clara Bayarri75e09792015-07-29 16:20:40 +0100113
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100114 private Dialog mKeyboardShortcutsDialog;
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800115 private KeyCharacterMap mKeyCharacterMap;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100116
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100117 private KeyboardShortcuts(Context context) {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000118 this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_Material_Light);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100119 this.mPackageManager = AppGlobals.getPackageManager();
Clara Bayarrib999af52016-04-06 16:02:35 +0100120 loadResources(context);
121 }
122
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100123 private static KeyboardShortcuts getInstance(Context context) {
124 if (sInstance == null) {
125 sInstance = new KeyboardShortcuts(context);
126 }
127 return sInstance;
128 }
129
130 public static void show(Context context, int deviceId) {
Clara Bayarric17a5982016-04-15 12:26:47 +0100131 MetricsLogger.visible(context,
132 MetricsProto.MetricsEvent.KEYBOARD_SHORTCUTS_HELPER);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100133 synchronized (sLock) {
134 if (sInstance != null && !sInstance.mContext.equals(context)) {
135 dismiss();
136 }
137 getInstance(context).showKeyboardShortcuts(deviceId);
138 sIsShowing = true;
139 }
140 }
141
142 public static void toggle(Context context, int deviceId) {
143 synchronized (sLock) {
144 if (sIsShowing) {
145 dismiss();
146 } else {
147 show(context, deviceId);
148 }
149 }
150 }
151
152 public static void dismiss() {
Clara Bayarric17a5982016-04-15 12:26:47 +0100153 MetricsLogger.hidden(sInstance.mContext,
154 MetricsProto.MetricsEvent.KEYBOARD_SHORTCUTS_HELPER);
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100155 synchronized (sLock) {
156 if (sInstance != null) {
157 sInstance.dismissKeyboardShortcuts();
158 sInstance = null;
159 }
160 sIsShowing = false;
161 }
162 }
163
Clara Bayarrib999af52016-04-06 16:02:35 +0100164 private void loadResources(Context context) {
165 mSpecialCharacterNames.put(
166 KeyEvent.KEYCODE_HOME, context.getString(R.string.keyboard_key_home));
167 mSpecialCharacterNames.put(
168 KeyEvent.KEYCODE_BACK, context.getString(R.string.keyboard_key_back));
169 mSpecialCharacterNames.put(
170 KeyEvent.KEYCODE_DPAD_UP, context.getString(R.string.keyboard_key_dpad_up));
171 mSpecialCharacterNames.put(
172 KeyEvent.KEYCODE_DPAD_DOWN, context.getString(R.string.keyboard_key_dpad_down));
173 mSpecialCharacterNames.put(
174 KeyEvent.KEYCODE_DPAD_LEFT, context.getString(R.string.keyboard_key_dpad_left));
175 mSpecialCharacterNames.put(
176 KeyEvent.KEYCODE_DPAD_RIGHT, context.getString(R.string.keyboard_key_dpad_right));
177 mSpecialCharacterNames.put(
178 KeyEvent.KEYCODE_DPAD_CENTER, context.getString(R.string.keyboard_key_dpad_center));
179 mSpecialCharacterNames.put(KeyEvent.KEYCODE_PERIOD, ".");
180 mSpecialCharacterNames.put(
181 KeyEvent.KEYCODE_TAB, context.getString(R.string.keyboard_key_tab));
182 mSpecialCharacterNames.put(
183 KeyEvent.KEYCODE_SPACE, context.getString(R.string.keyboard_key_space));
184 mSpecialCharacterNames.put(
185 KeyEvent.KEYCODE_ENTER, context.getString(R.string.keyboard_key_enter));
186 mSpecialCharacterNames.put(
187 KeyEvent.KEYCODE_DEL, context.getString(R.string.keyboard_key_backspace));
188 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE,
189 context.getString(R.string.keyboard_key_media_play_pause));
190 mSpecialCharacterNames.put(
191 KeyEvent.KEYCODE_MEDIA_STOP, context.getString(R.string.keyboard_key_media_stop));
192 mSpecialCharacterNames.put(
193 KeyEvent.KEYCODE_MEDIA_NEXT, context.getString(R.string.keyboard_key_media_next));
194 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PREVIOUS,
195 context.getString(R.string.keyboard_key_media_previous));
196 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_REWIND,
197 context.getString(R.string.keyboard_key_media_rewind));
198 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD,
199 context.getString(R.string.keyboard_key_media_fast_forward));
200 mSpecialCharacterNames.put(
201 KeyEvent.KEYCODE_PAGE_UP, context.getString(R.string.keyboard_key_page_up));
202 mSpecialCharacterNames.put(
203 KeyEvent.KEYCODE_PAGE_DOWN, context.getString(R.string.keyboard_key_page_down));
204 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_A,
205 context.getString(R.string.keyboard_key_button_template, "A"));
206 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_B,
207 context.getString(R.string.keyboard_key_button_template, "B"));
208 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_C,
209 context.getString(R.string.keyboard_key_button_template, "C"));
210 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_X,
211 context.getString(R.string.keyboard_key_button_template, "X"));
212 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Y,
213 context.getString(R.string.keyboard_key_button_template, "Y"));
214 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Z,
215 context.getString(R.string.keyboard_key_button_template, "Z"));
216 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L1,
217 context.getString(R.string.keyboard_key_button_template, "L1"));
218 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R1,
219 context.getString(R.string.keyboard_key_button_template, "R1"));
220 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L2,
221 context.getString(R.string.keyboard_key_button_template, "L2"));
222 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R2,
223 context.getString(R.string.keyboard_key_button_template, "R2"));
224 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_START,
225 context.getString(R.string.keyboard_key_button_template, "Start"));
226 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_SELECT,
227 context.getString(R.string.keyboard_key_button_template, "Select"));
228 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_MODE,
229 context.getString(R.string.keyboard_key_button_template, "Mode"));
230 mSpecialCharacterNames.put(
231 KeyEvent.KEYCODE_FORWARD_DEL, context.getString(R.string.keyboard_key_forward_del));
232 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ESCAPE, "Esc");
233 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SYSRQ, "SysRq");
234 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BREAK, "Break");
235 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SCROLL_LOCK, "Scroll Lock");
236 mSpecialCharacterNames.put(
237 KeyEvent.KEYCODE_MOVE_HOME, context.getString(R.string.keyboard_key_move_home));
238 mSpecialCharacterNames.put(
239 KeyEvent.KEYCODE_MOVE_END, context.getString(R.string.keyboard_key_move_end));
240 mSpecialCharacterNames.put(
241 KeyEvent.KEYCODE_INSERT, context.getString(R.string.keyboard_key_insert));
242 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F1, "F1");
243 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F2, "F2");
244 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F3, "F3");
245 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F4, "F4");
246 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F5, "F5");
247 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F6, "F6");
248 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F7, "F7");
249 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F8, "F8");
250 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F9, "F9");
251 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F10, "F10");
252 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F11, "F11");
253 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F12, "F12");
254 mSpecialCharacterNames.put(
255 KeyEvent.KEYCODE_NUM_LOCK, context.getString(R.string.keyboard_key_num_lock));
256 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_0,
257 context.getString(R.string.keyboard_key_numpad_template, "0"));
258 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_1,
259 context.getString(R.string.keyboard_key_numpad_template, "1"));
260 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_2,
261 context.getString(R.string.keyboard_key_numpad_template, "2"));
262 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_3,
263 context.getString(R.string.keyboard_key_numpad_template, "3"));
264 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_4,
265 context.getString(R.string.keyboard_key_numpad_template, "4"));
266 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_5,
267 context.getString(R.string.keyboard_key_numpad_template, "5"));
268 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_6,
269 context.getString(R.string.keyboard_key_numpad_template, "6"));
270 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_7,
271 context.getString(R.string.keyboard_key_numpad_template, "7"));
272 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_8,
273 context.getString(R.string.keyboard_key_numpad_template, "8"));
274 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_9,
275 context.getString(R.string.keyboard_key_numpad_template, "9"));
276 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DIVIDE,
277 context.getString(R.string.keyboard_key_numpad_template, "/"));
278 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_MULTIPLY,
279 context.getString(R.string.keyboard_key_numpad_template, "*"));
280 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_SUBTRACT,
281 context.getString(R.string.keyboard_key_numpad_template, "-"));
282 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ADD,
283 context.getString(R.string.keyboard_key_numpad_template, "+"));
284 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DOT,
285 context.getString(R.string.keyboard_key_numpad_template, "."));
286 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_COMMA,
287 context.getString(R.string.keyboard_key_numpad_template, ","));
288 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ENTER,
289 context.getString(R.string.keyboard_key_numpad_template,
290 context.getString(R.string.keyboard_key_enter)));
291 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_EQUALS,
292 context.getString(R.string.keyboard_key_numpad_template, "="));
293 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN,
294 context.getString(R.string.keyboard_key_numpad_template, "("));
295 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN,
296 context.getString(R.string.keyboard_key_numpad_template, ")"));
297 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ZENKAKU_HANKAKU, "半角/全角");
298 mSpecialCharacterNames.put(KeyEvent.KEYCODE_EISU, "英数");
299 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MUHENKAN, "無変換");
300 mSpecialCharacterNames.put(KeyEvent.KEYCODE_HENKAN, "変換");
301 mSpecialCharacterNames.put(KeyEvent.KEYCODE_KATAKANA_HIRAGANA, "かな");
302
303 mModifierNames.put(KeyEvent.META_META_ON, "Meta");
304 mModifierNames.put(KeyEvent.META_CTRL_ON, "Ctrl");
305 mModifierNames.put(KeyEvent.META_ALT_ON, "Alt");
306 mModifierNames.put(KeyEvent.META_SHIFT_ON, "Shift");
307 mModifierNames.put(KeyEvent.META_SYM_ON, "Sym");
308 mModifierNames.put(KeyEvent.META_FUNCTION_ON, "Fn");
309
310 mSpecialCharacterDrawables.put(
311 KeyEvent.KEYCODE_DEL, context.getDrawable(R.drawable.ic_ksh_key_backspace));
312 mSpecialCharacterDrawables.put(
313 KeyEvent.KEYCODE_ENTER, context.getDrawable(R.drawable.ic_ksh_key_enter));
314 mSpecialCharacterDrawables.put(
315 KeyEvent.KEYCODE_DPAD_UP, context.getDrawable(R.drawable.ic_ksh_key_up));
316 mSpecialCharacterDrawables.put(
317 KeyEvent.KEYCODE_DPAD_RIGHT, context.getDrawable(R.drawable.ic_ksh_key_right));
318 mSpecialCharacterDrawables.put(
319 KeyEvent.KEYCODE_DPAD_DOWN, context.getDrawable(R.drawable.ic_ksh_key_down));
320 mSpecialCharacterDrawables.put(
321 KeyEvent.KEYCODE_DPAD_LEFT, context.getDrawable(R.drawable.ic_ksh_key_left));
322
323 mModifierDrawables.put(
324 KeyEvent.META_META_ON, context.getDrawable(R.drawable.ic_ksh_key_meta));
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000325 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100326
Clara Bayarri03f19552016-04-06 10:59:52 +0100327 /**
328 * Retrieves a {@link KeyCharacterMap} and assigns it to mKeyCharacterMap. If the given id is an
329 * existing device, that device's map is used. Otherwise, it checks first all available devices
330 * and if there is a full keyboard it uses that map, otherwise falls back to the Virtual
331 * Keyboard with its default map.
332 */
333 private void retrieveKeyCharacterMap(int deviceId) {
334 final InputManager inputManager = InputManager.getInstance();
335 if (deviceId != -1) {
336 final InputDevice inputDevice = inputManager.getInputDevice(deviceId);
337 if (inputDevice != null) {
338 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
339 return;
340 }
341 }
342 final int[] deviceIds = inputManager.getInputDeviceIds();
343 for (int i = 0; i < deviceIds.length; ++i) {
344 final InputDevice inputDevice = inputManager.getInputDevice(deviceIds[i]);
345 // -1 is the Virtual Keyboard, with the default key map. Use that one only as last
346 // resort.
347 if (inputDevice.getId() != -1 && inputDevice.isFullKeyboard()) {
348 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
349 return;
350 }
351 }
352 final InputDevice inputDevice = inputManager.getInputDevice(-1);
353 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
354 }
355
Andrei Stingaceanuf86bc972016-04-12 15:29:25 +0100356 private void showKeyboardShortcuts(int deviceId) {
357 retrieveKeyCharacterMap(deviceId);
358 Recents.getSystemServices().requestKeyboardShortcuts(mContext,
359 new KeyboardShortcutsReceiver() {
360 @Override
361 public void onKeyboardShortcutsReceived(
362 final List<KeyboardShortcutGroup> result) {
363 result.add(getSystemShortcuts());
364 final KeyboardShortcutGroup appShortcuts = getDefaultApplicationShortcuts();
365 if (appShortcuts != null) {
366 result.add(appShortcuts);
367 }
368 showKeyboardShortcutsDialog(result);
369 }
370 }, deviceId);
371 }
372
373 private void dismissKeyboardShortcuts() {
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100374 if (mKeyboardShortcutsDialog != null) {
375 mKeyboardShortcutsDialog.dismiss();
376 mKeyboardShortcutsDialog = null;
377 }
378 }
379
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100380 private KeyboardShortcutGroup getSystemShortcuts() {
381 final KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup(
382 mContext.getString(R.string.keyboard_shortcut_group_system), true);
383 systemGroup.addItem(new KeyboardShortcutInfo(
384 mContext.getString(R.string.keyboard_shortcut_group_system_home),
385 KeyEvent.KEYCODE_ENTER,
386 KeyEvent.META_META_ON));
387 systemGroup.addItem(new KeyboardShortcutInfo(
388 mContext.getString(R.string.keyboard_shortcut_group_system_back),
389 KeyEvent.KEYCODE_DEL,
390 KeyEvent.META_META_ON));
391 systemGroup.addItem(new KeyboardShortcutInfo(
392 mContext.getString(R.string.keyboard_shortcut_group_system_recents),
393 KeyEvent.KEYCODE_TAB,
394 KeyEvent.META_ALT_ON));
395 systemGroup.addItem(new KeyboardShortcutInfo(
396 mContext.getString(
397 R.string.keyboard_shortcut_group_system_notifications),
398 KeyEvent.KEYCODE_N,
399 KeyEvent.META_META_ON));
400 systemGroup.addItem(new KeyboardShortcutInfo(
401 mContext.getString(
402 R.string.keyboard_shortcut_group_system_shortcuts_helper),
403 KeyEvent.KEYCODE_SLASH,
404 KeyEvent.META_META_ON));
405 systemGroup.addItem(new KeyboardShortcutInfo(
406 mContext.getString(
407 R.string.keyboard_shortcut_group_system_switch_input),
408 KeyEvent.KEYCODE_SPACE,
409 KeyEvent.META_META_ON));
410 return systemGroup;
411 }
412
413 private KeyboardShortcutGroup getDefaultApplicationShortcuts() {
414 final int userId = mContext.getUserId();
Andrei Stingaceanu9cfcafc2016-04-12 11:07:39 +0100415 List<KeyboardShortcutInfo> keyboardShortcutInfoAppItems = new ArrayList<>();
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100416
417 // Assist.
418 final AssistUtils assistUtils = new AssistUtils(mContext);
419 final ComponentName assistComponent = assistUtils.getAssistComponentForUser(userId);
420 PackageInfo assistPackageInfo = null;
421 try {
422 assistPackageInfo = mPackageManager.getPackageInfo(
423 assistComponent.getPackageName(), 0, userId);
424 } catch (RemoteException e) {
425 Log.e(TAG, "PackageManagerService is dead");
426 }
427
428 if (assistPackageInfo != null) {
429 final Icon assistIcon = Icon.createWithResource(
430 assistPackageInfo.applicationInfo.packageName,
431 assistPackageInfo.applicationInfo.icon);
432
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100433 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100434 mContext.getString(R.string.keyboard_shortcut_group_applications_assist),
435 assistIcon,
436 KeyEvent.KEYCODE_UNKNOWN,
437 KeyEvent.META_META_ON));
438 }
439
440 // Browser.
441 final Icon browserIcon = getIconForIntentCategory(Intent.CATEGORY_APP_BROWSER, userId);
442 if (browserIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100443 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100444 mContext.getString(R.string.keyboard_shortcut_group_applications_browser),
445 browserIcon,
446 KeyEvent.KEYCODE_B,
447 KeyEvent.META_META_ON));
448 }
449
450
451 // Contacts.
452 final Icon contactsIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CONTACTS, userId);
453 if (contactsIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100454 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100455 mContext.getString(R.string.keyboard_shortcut_group_applications_contacts),
456 contactsIcon,
457 KeyEvent.KEYCODE_C,
458 KeyEvent.META_META_ON));
459 }
460
461 // Email.
462 final Icon emailIcon = getIconForIntentCategory(Intent.CATEGORY_APP_EMAIL, userId);
463 if (emailIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100464 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100465 mContext.getString(R.string.keyboard_shortcut_group_applications_email),
466 emailIcon,
467 KeyEvent.KEYCODE_E,
468 KeyEvent.META_META_ON));
469 }
470
471 // Messaging.
472 final Icon messagingIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MESSAGING, userId);
473 if (messagingIcon != 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_im),
476 messagingIcon,
477 KeyEvent.KEYCODE_T,
478 KeyEvent.META_META_ON));
479 }
480
481 // Music.
482 final Icon musicIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MUSIC, userId);
483 if (musicIcon != 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_music),
486 musicIcon,
487 KeyEvent.KEYCODE_P,
488 KeyEvent.META_META_ON));
489 }
490
491 // Calendar.
492 final Icon calendarIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CALENDAR, userId);
493 if (calendarIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100494 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100495 mContext.getString(R.string.keyboard_shortcut_group_applications_calendar),
496 calendarIcon,
497 KeyEvent.KEYCODE_L,
498 KeyEvent.META_META_ON));
499 }
500
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100501 final int itemsSize = keyboardShortcutInfoAppItems.size();
502 if (itemsSize == 0) {
503 return null;
504 }
505
506 // Sorts by label, case insensitive with nulls and/or empty labels last.
507 Collections.sort(keyboardShortcutInfoAppItems, mApplicationItemsComparator);
508 return new KeyboardShortcutGroup(
509 mContext.getString(R.string.keyboard_shortcut_group_applications),
510 keyboardShortcutInfoAppItems,
511 true);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100512 }
513
514 private Icon getIconForIntentCategory(String intentCategory, int userId) {
515 final Intent intent = new Intent(Intent.ACTION_MAIN);
516 intent.addCategory(intentCategory);
517
518 final PackageInfo packageInfo = getPackageInfoForIntent(intent, userId);
519 if (packageInfo != null && packageInfo.applicationInfo.icon != 0) {
520 return Icon.createWithResource(
521 packageInfo.applicationInfo.packageName,
522 packageInfo.applicationInfo.icon);
523 }
524 return null;
525 }
526
527 private PackageInfo getPackageInfoForIntent(Intent intent, int userId) {
528 try {
529 ResolveInfo handler;
530 handler = mPackageManager.resolveIntent(
531 intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()), 0, userId);
532 if (handler == null || handler.activityInfo == null) {
533 return null;
534 }
535 return mPackageManager.getPackageInfo(handler.activityInfo.packageName, 0, userId);
536 } catch (RemoteException e) {
537 Log.e(TAG, "PackageManagerService is dead", e);
538 return null;
539 }
540 }
541
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000542 private void showKeyboardShortcutsDialog(
543 final List<KeyboardShortcutGroup> keyboardShortcutGroups) {
544 // Need to post on the main thread.
545 mHandler.post(new Runnable() {
546 @Override
547 public void run() {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000548 handleShowKeyboardShortcuts(keyboardShortcutGroups);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000549 }
550 });
551 }
552
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000553 private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) {
554 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
555 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
556 LAYOUT_INFLATER_SERVICE);
557 final View keyboardShortcutsView = inflater.inflate(
558 R.layout.keyboard_shortcuts_view, null);
559 populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById(
560 R.id.keyboard_shortcuts_container), keyboardShortcutGroups);
561 dialogBuilder.setView(keyboardShortcutsView);
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100562 dialogBuilder.setPositiveButton(R.string.quick_settings_done, mDialogCloseListener);
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000563 mKeyboardShortcutsDialog = dialogBuilder.create();
564 mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true);
565 Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow();
566 keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG);
567 mKeyboardShortcutsDialog.show();
568 }
569
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000570 private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout,
571 List<KeyboardShortcutGroup> keyboardShortcutGroups) {
572 LayoutInflater inflater = LayoutInflater.from(mContext);
573 final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size();
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100574 TextView shortcutsKeyView = (TextView) inflater.inflate(
575 R.layout.keyboard_shortcuts_key_view, null, false);
576 shortcutsKeyView.measure(
577 View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
578 final int shortcutKeyTextItemMinWidth = shortcutsKeyView.getMeasuredHeight();
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100579 // Needed to be able to scale the image items to the same height as the text items.
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100580 final int shortcutKeyIconItemHeightWidth = shortcutsKeyView.getMeasuredHeight()
581 - shortcutsKeyView.getPaddingTop()
582 - shortcutsKeyView.getPaddingBottom();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000583 for (int i = 0; i < keyboardShortcutGroupsSize; i++) {
584 KeyboardShortcutGroup group = keyboardShortcutGroups.get(i);
585 TextView categoryTitle = (TextView) inflater.inflate(
586 R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false);
587 categoryTitle.setText(group.getLabel());
588 categoryTitle.setTextColor(group.isSystemGroup()
589 ? mContext.getColor(R.color.ksh_system_group_color)
590 : mContext.getColor(R.color.ksh_application_group_color));
591 keyboardShortcutsLayout.addView(categoryTitle);
592
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000593 LinearLayout shortcutContainer = (LinearLayout) inflater.inflate(
594 R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000595 final int itemsSize = group.getItems().size();
596 for (int j = 0; j < itemsSize; j++) {
597 KeyboardShortcutInfo info = group.getItems().get(j);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100598 List<StringOrDrawable> shortcutKeys = getHumanReadableShortcutKeys(info);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800599 if (shortcutKeys == null) {
600 // Ignore shortcuts we can't display keys for.
601 Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping.");
602 continue;
603 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000604 View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item,
605 shortcutContainer, false);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100606
607 if (info.getIcon() != null) {
608 ImageView shortcutIcon = (ImageView) shortcutView
609 .findViewById(R.id.keyboard_shortcuts_icon);
610 shortcutIcon.setImageIcon(info.getIcon());
611 shortcutIcon.setVisibility(View.VISIBLE);
612 }
613
614 TextView shortcutKeyword = (TextView) shortcutView
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000615 .findViewById(R.id.keyboard_shortcuts_keyword);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100616 shortcutKeyword.setText(info.getLabel());
617 if (info.getIcon() != null) {
618 RelativeLayout.LayoutParams lp =
619 (RelativeLayout.LayoutParams) shortcutKeyword.getLayoutParams();
620 lp.removeRule(RelativeLayout.ALIGN_PARENT_START);
621 shortcutKeyword.setLayoutParams(lp);
622 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000623
Andrei Stingaceanu844927d2016-02-16 14:31:58 +0000624 ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000625 .findViewById(R.id.keyboard_shortcuts_item_container);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000626 final int shortcutKeysSize = shortcutKeys.size();
627 for (int k = 0; k < shortcutKeysSize; k++) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100628 StringOrDrawable shortcutRepresentation = shortcutKeys.get(k);
629 if (shortcutRepresentation.drawable != null) {
630 ImageView shortcutKeyIconView = (ImageView) inflater.inflate(
631 R.layout.keyboard_shortcuts_key_icon_view, shortcutItemsContainer,
632 false);
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100633 Bitmap bitmap = Bitmap.createBitmap(shortcutKeyIconItemHeightWidth,
634 shortcutKeyIconItemHeightWidth, Bitmap.Config.ARGB_8888);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100635 Canvas canvas = new Canvas(bitmap);
636 shortcutRepresentation.drawable.setBounds(0, 0, canvas.getWidth(),
637 canvas.getHeight());
638 shortcutRepresentation.drawable.draw(canvas);
639 shortcutKeyIconView.setImageBitmap(bitmap);
640 shortcutItemsContainer.addView(shortcutKeyIconView);
641 } else if (shortcutRepresentation.string != null) {
642 TextView shortcutKeyTextView = (TextView) inflater.inflate(
643 R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer,
644 false);
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100645 shortcutKeyTextView.setMinimumWidth(shortcutKeyTextItemMinWidth);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100646 shortcutKeyTextView.setText(shortcutRepresentation.string);
647 shortcutItemsContainer.addView(shortcutKeyTextView);
648 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000649 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000650 shortcutContainer.addView(shortcutView);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000651 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000652 keyboardShortcutsLayout.addView(shortcutContainer);
653 if (i < keyboardShortcutGroupsSize - 1) {
654 View separator = inflater.inflate(
655 R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout,
656 false);
657 keyboardShortcutsLayout.addView(separator);
658 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000659 }
660 }
661
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100662 private List<StringOrDrawable> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) {
663 List<StringOrDrawable> shortcutKeys = getHumanReadableModifiers(info);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800664 if (shortcutKeys == null) {
665 return null;
666 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100667 String displayLabelString = null;
668 Drawable displayLabelDrawable = null;
Clara Bayarri1d648a12016-03-23 17:09:02 +0000669 if (info.getBaseCharacter() > Character.MIN_VALUE) {
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800670 displayLabelString = String.valueOf(info.getBaseCharacter());
Clara Bayarrib999af52016-04-06 16:02:35 +0100671 } else if (mSpecialCharacterDrawables.get(info.getKeycode()) != null) {
672 displayLabelDrawable = mSpecialCharacterDrawables.get(info.getKeycode());
673 } else if (mSpecialCharacterNames.get(info.getKeycode()) != null) {
674 displayLabelString = mSpecialCharacterNames.get(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800675 } else {
Clara Bayarri1d648a12016-03-23 17:09:02 +0000676 // Special case for shortcuts with no base key or keycode.
677 if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) {
678 return shortcutKeys;
679 }
Clara Bayarri03f19552016-04-06 10:59:52 +0100680 char displayLabel = mKeyCharacterMap.getDisplayLabel(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800681 if (displayLabel != 0) {
682 displayLabelString = String.valueOf(displayLabel);
683 } else {
684 return null;
685 }
686 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100687
688 if (displayLabelDrawable != null) {
689 shortcutKeys.add(new StringOrDrawable(displayLabelDrawable));
690 } else if (displayLabelString != null) {
691 shortcutKeys.add(new StringOrDrawable(displayLabelString.toUpperCase()));
692 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000693 return shortcutKeys;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100694 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800695
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100696 private List<StringOrDrawable> getHumanReadableModifiers(KeyboardShortcutInfo info) {
697 final List<StringOrDrawable> shortcutKeys = new ArrayList<>();
Clara Bayarrib9057df2016-03-02 11:37:09 -0800698 int modifiers = info.getModifiers();
699 if (modifiers == 0) {
700 return shortcutKeys;
701 }
Clara Bayarrib999af52016-04-06 16:02:35 +0100702 for(int i = 0; i < mModifierNames.size(); ++i) {
703 final int supportedModifier = mModifierNames.keyAt(i);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800704 if ((modifiers & supportedModifier) != 0) {
Clara Bayarrib999af52016-04-06 16:02:35 +0100705 if (mModifierDrawables.get(supportedModifier) != null) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100706 shortcutKeys.add(new StringOrDrawable(
Clara Bayarrib999af52016-04-06 16:02:35 +0100707 mModifierDrawables.get(supportedModifier)));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100708 } else {
709 shortcutKeys.add(new StringOrDrawable(
Clara Bayarrib999af52016-04-06 16:02:35 +0100710 mModifierNames.get(supportedModifier).toUpperCase()));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100711 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800712 modifiers &= ~supportedModifier;
713 }
714 }
715 if (modifiers != 0) {
716 // Remaining unsupported modifiers, don't show anything.
717 return null;
718 }
719 return shortcutKeys;
720 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100721
722 private static final class StringOrDrawable {
723 public String string;
724 public Drawable drawable;
725
726 public StringOrDrawable(String string) {
727 this.string = string;
728 }
729
730 public StringOrDrawable(Drawable drawable) {
731 this.drawable = drawable;
732 }
733 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100734}