blob: c2521b3d7d56ba4f8ec378e5c6f680e78ec6f289 [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();
Clara Bayarrib999af52016-04-06 16:02:35 +010073 private final SparseArray<String> mSpecialCharacterNames = new SparseArray<>();
74 private final SparseArray<String> mModifierNames = new SparseArray<>();
75 private final SparseArray<Drawable> mSpecialCharacterDrawables = new SparseArray<>();
76 private final SparseArray<Drawable> mModifierDrawables = new SparseArray<>();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000077
78 private final Handler mHandler = new Handler(Looper.getMainLooper());
79 private final Context mContext;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010080 private final IPackageManager mPackageManager;
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010081 private final OnClickListener mDialogCloseListener = new DialogInterface.OnClickListener() {
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000082 public void onClick(DialogInterface dialog, int id) {
83 dismissKeyboardShortcutsDialog();
84 }
85 };
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +010086 private final Comparator<KeyboardShortcutInfo> mApplicationItemsComparator =
87 new Comparator<KeyboardShortcutInfo>() {
88 @Override
89 public int compare(KeyboardShortcutInfo ksh1, KeyboardShortcutInfo ksh2) {
90 boolean ksh1ShouldBeLast = ksh1.getLabel() == null
91 || ksh1.getLabel().toString().isEmpty();
92 boolean ksh2ShouldBeLast = ksh2.getLabel() == null
93 || ksh2.getLabel().toString().isEmpty();
94 if (ksh1ShouldBeLast && ksh2ShouldBeLast) {
95 return 0;
96 }
97 if (ksh1ShouldBeLast) {
98 return 1;
99 }
100 if (ksh2ShouldBeLast) {
101 return -1;
102 }
103 return (ksh1.getLabel().toString()).compareToIgnoreCase(
104 ksh2.getLabel().toString());
105 }
106 };
Clara Bayarri75e09792015-07-29 16:20:40 +0100107
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100108 private Dialog mKeyboardShortcutsDialog;
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800109 private KeyCharacterMap mKeyCharacterMap;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100110
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000111 public KeyboardShortcuts(Context context) {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000112 this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_Material_Light);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100113 this.mPackageManager = AppGlobals.getPackageManager();
Clara Bayarrib999af52016-04-06 16:02:35 +0100114 loadResources(context);
115 }
116
117 private void loadResources(Context context) {
118 mSpecialCharacterNames.put(
119 KeyEvent.KEYCODE_HOME, context.getString(R.string.keyboard_key_home));
120 mSpecialCharacterNames.put(
121 KeyEvent.KEYCODE_BACK, context.getString(R.string.keyboard_key_back));
122 mSpecialCharacterNames.put(
123 KeyEvent.KEYCODE_DPAD_UP, context.getString(R.string.keyboard_key_dpad_up));
124 mSpecialCharacterNames.put(
125 KeyEvent.KEYCODE_DPAD_DOWN, context.getString(R.string.keyboard_key_dpad_down));
126 mSpecialCharacterNames.put(
127 KeyEvent.KEYCODE_DPAD_LEFT, context.getString(R.string.keyboard_key_dpad_left));
128 mSpecialCharacterNames.put(
129 KeyEvent.KEYCODE_DPAD_RIGHT, context.getString(R.string.keyboard_key_dpad_right));
130 mSpecialCharacterNames.put(
131 KeyEvent.KEYCODE_DPAD_CENTER, context.getString(R.string.keyboard_key_dpad_center));
132 mSpecialCharacterNames.put(KeyEvent.KEYCODE_PERIOD, ".");
133 mSpecialCharacterNames.put(
134 KeyEvent.KEYCODE_TAB, context.getString(R.string.keyboard_key_tab));
135 mSpecialCharacterNames.put(
136 KeyEvent.KEYCODE_SPACE, context.getString(R.string.keyboard_key_space));
137 mSpecialCharacterNames.put(
138 KeyEvent.KEYCODE_ENTER, context.getString(R.string.keyboard_key_enter));
139 mSpecialCharacterNames.put(
140 KeyEvent.KEYCODE_DEL, context.getString(R.string.keyboard_key_backspace));
141 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE,
142 context.getString(R.string.keyboard_key_media_play_pause));
143 mSpecialCharacterNames.put(
144 KeyEvent.KEYCODE_MEDIA_STOP, context.getString(R.string.keyboard_key_media_stop));
145 mSpecialCharacterNames.put(
146 KeyEvent.KEYCODE_MEDIA_NEXT, context.getString(R.string.keyboard_key_media_next));
147 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PREVIOUS,
148 context.getString(R.string.keyboard_key_media_previous));
149 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_REWIND,
150 context.getString(R.string.keyboard_key_media_rewind));
151 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD,
152 context.getString(R.string.keyboard_key_media_fast_forward));
153 mSpecialCharacterNames.put(
154 KeyEvent.KEYCODE_PAGE_UP, context.getString(R.string.keyboard_key_page_up));
155 mSpecialCharacterNames.put(
156 KeyEvent.KEYCODE_PAGE_DOWN, context.getString(R.string.keyboard_key_page_down));
157 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_A,
158 context.getString(R.string.keyboard_key_button_template, "A"));
159 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_B,
160 context.getString(R.string.keyboard_key_button_template, "B"));
161 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_C,
162 context.getString(R.string.keyboard_key_button_template, "C"));
163 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_X,
164 context.getString(R.string.keyboard_key_button_template, "X"));
165 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Y,
166 context.getString(R.string.keyboard_key_button_template, "Y"));
167 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Z,
168 context.getString(R.string.keyboard_key_button_template, "Z"));
169 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L1,
170 context.getString(R.string.keyboard_key_button_template, "L1"));
171 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R1,
172 context.getString(R.string.keyboard_key_button_template, "R1"));
173 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L2,
174 context.getString(R.string.keyboard_key_button_template, "L2"));
175 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R2,
176 context.getString(R.string.keyboard_key_button_template, "R2"));
177 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_START,
178 context.getString(R.string.keyboard_key_button_template, "Start"));
179 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_SELECT,
180 context.getString(R.string.keyboard_key_button_template, "Select"));
181 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_MODE,
182 context.getString(R.string.keyboard_key_button_template, "Mode"));
183 mSpecialCharacterNames.put(
184 KeyEvent.KEYCODE_FORWARD_DEL, context.getString(R.string.keyboard_key_forward_del));
185 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ESCAPE, "Esc");
186 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SYSRQ, "SysRq");
187 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BREAK, "Break");
188 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SCROLL_LOCK, "Scroll Lock");
189 mSpecialCharacterNames.put(
190 KeyEvent.KEYCODE_MOVE_HOME, context.getString(R.string.keyboard_key_move_home));
191 mSpecialCharacterNames.put(
192 KeyEvent.KEYCODE_MOVE_END, context.getString(R.string.keyboard_key_move_end));
193 mSpecialCharacterNames.put(
194 KeyEvent.KEYCODE_INSERT, context.getString(R.string.keyboard_key_insert));
195 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F1, "F1");
196 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F2, "F2");
197 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F3, "F3");
198 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F4, "F4");
199 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F5, "F5");
200 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F6, "F6");
201 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F7, "F7");
202 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F8, "F8");
203 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F9, "F9");
204 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F10, "F10");
205 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F11, "F11");
206 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F12, "F12");
207 mSpecialCharacterNames.put(
208 KeyEvent.KEYCODE_NUM_LOCK, context.getString(R.string.keyboard_key_num_lock));
209 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_0,
210 context.getString(R.string.keyboard_key_numpad_template, "0"));
211 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_1,
212 context.getString(R.string.keyboard_key_numpad_template, "1"));
213 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_2,
214 context.getString(R.string.keyboard_key_numpad_template, "2"));
215 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_3,
216 context.getString(R.string.keyboard_key_numpad_template, "3"));
217 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_4,
218 context.getString(R.string.keyboard_key_numpad_template, "4"));
219 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_5,
220 context.getString(R.string.keyboard_key_numpad_template, "5"));
221 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_6,
222 context.getString(R.string.keyboard_key_numpad_template, "6"));
223 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_7,
224 context.getString(R.string.keyboard_key_numpad_template, "7"));
225 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_8,
226 context.getString(R.string.keyboard_key_numpad_template, "8"));
227 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_9,
228 context.getString(R.string.keyboard_key_numpad_template, "9"));
229 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DIVIDE,
230 context.getString(R.string.keyboard_key_numpad_template, "/"));
231 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_MULTIPLY,
232 context.getString(R.string.keyboard_key_numpad_template, "*"));
233 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_SUBTRACT,
234 context.getString(R.string.keyboard_key_numpad_template, "-"));
235 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ADD,
236 context.getString(R.string.keyboard_key_numpad_template, "+"));
237 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DOT,
238 context.getString(R.string.keyboard_key_numpad_template, "."));
239 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_COMMA,
240 context.getString(R.string.keyboard_key_numpad_template, ","));
241 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ENTER,
242 context.getString(R.string.keyboard_key_numpad_template,
243 context.getString(R.string.keyboard_key_enter)));
244 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_EQUALS,
245 context.getString(R.string.keyboard_key_numpad_template, "="));
246 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN,
247 context.getString(R.string.keyboard_key_numpad_template, "("));
248 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN,
249 context.getString(R.string.keyboard_key_numpad_template, ")"));
250 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ZENKAKU_HANKAKU, "半角/全角");
251 mSpecialCharacterNames.put(KeyEvent.KEYCODE_EISU, "英数");
252 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MUHENKAN, "無変換");
253 mSpecialCharacterNames.put(KeyEvent.KEYCODE_HENKAN, "変換");
254 mSpecialCharacterNames.put(KeyEvent.KEYCODE_KATAKANA_HIRAGANA, "かな");
255
256 mModifierNames.put(KeyEvent.META_META_ON, "Meta");
257 mModifierNames.put(KeyEvent.META_CTRL_ON, "Ctrl");
258 mModifierNames.put(KeyEvent.META_ALT_ON, "Alt");
259 mModifierNames.put(KeyEvent.META_SHIFT_ON, "Shift");
260 mModifierNames.put(KeyEvent.META_SYM_ON, "Sym");
261 mModifierNames.put(KeyEvent.META_FUNCTION_ON, "Fn");
262
263 mSpecialCharacterDrawables.put(
264 KeyEvent.KEYCODE_DEL, context.getDrawable(R.drawable.ic_ksh_key_backspace));
265 mSpecialCharacterDrawables.put(
266 KeyEvent.KEYCODE_ENTER, context.getDrawable(R.drawable.ic_ksh_key_enter));
267 mSpecialCharacterDrawables.put(
268 KeyEvent.KEYCODE_DPAD_UP, context.getDrawable(R.drawable.ic_ksh_key_up));
269 mSpecialCharacterDrawables.put(
270 KeyEvent.KEYCODE_DPAD_RIGHT, context.getDrawable(R.drawable.ic_ksh_key_right));
271 mSpecialCharacterDrawables.put(
272 KeyEvent.KEYCODE_DPAD_DOWN, context.getDrawable(R.drawable.ic_ksh_key_down));
273 mSpecialCharacterDrawables.put(
274 KeyEvent.KEYCODE_DPAD_LEFT, context.getDrawable(R.drawable.ic_ksh_key_left));
275
276 mModifierDrawables.put(
277 KeyEvent.META_META_ON, context.getDrawable(R.drawable.ic_ksh_key_meta));
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000278 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100279
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800280 public void toggleKeyboardShortcuts(int deviceId) {
Clara Bayarri03f19552016-04-06 10:59:52 +0100281 retrieveKeyCharacterMap(deviceId);
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100282 if (mKeyboardShortcutsDialog == null) {
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000283 Recents.getSystemServices().requestKeyboardShortcuts(mContext,
Clara Bayarri75e09792015-07-29 16:20:40 +0100284 new KeyboardShortcutsReceiver() {
285 @Override
286 public void onKeyboardShortcutsReceived(
287 final List<KeyboardShortcutGroup> result) {
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100288 result.add(getSystemShortcuts());
289 final KeyboardShortcutGroup appShortcuts = getDefaultApplicationShortcuts();
290 if (appShortcuts != null) {
291 result.add(appShortcuts);
292 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000293 showKeyboardShortcutsDialog(result);
Clara Bayarri75e09792015-07-29 16:20:40 +0100294 }
Clara Bayarrifcd7e802016-03-10 12:58:18 +0000295 }, deviceId);
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100296 } else {
297 dismissKeyboardShortcutsDialog();
298 }
299 }
300
Clara Bayarri03f19552016-04-06 10:59:52 +0100301 /**
302 * Retrieves a {@link KeyCharacterMap} and assigns it to mKeyCharacterMap. If the given id is an
303 * existing device, that device's map is used. Otherwise, it checks first all available devices
304 * and if there is a full keyboard it uses that map, otherwise falls back to the Virtual
305 * Keyboard with its default map.
306 */
307 private void retrieveKeyCharacterMap(int deviceId) {
308 final InputManager inputManager = InputManager.getInstance();
309 if (deviceId != -1) {
310 final InputDevice inputDevice = inputManager.getInputDevice(deviceId);
311 if (inputDevice != null) {
312 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
313 return;
314 }
315 }
316 final int[] deviceIds = inputManager.getInputDeviceIds();
317 for (int i = 0; i < deviceIds.length; ++i) {
318 final InputDevice inputDevice = inputManager.getInputDevice(deviceIds[i]);
319 // -1 is the Virtual Keyboard, with the default key map. Use that one only as last
320 // resort.
321 if (inputDevice.getId() != -1 && inputDevice.isFullKeyboard()) {
322 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
323 return;
324 }
325 }
326 final InputDevice inputDevice = inputManager.getInputDevice(-1);
327 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
328 }
329
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100330 public void dismissKeyboardShortcutsDialog() {
331 if (mKeyboardShortcutsDialog != null) {
332 mKeyboardShortcutsDialog.dismiss();
333 mKeyboardShortcutsDialog = null;
334 }
335 }
336
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100337 private KeyboardShortcutGroup getSystemShortcuts() {
338 final KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup(
339 mContext.getString(R.string.keyboard_shortcut_group_system), true);
340 systemGroup.addItem(new KeyboardShortcutInfo(
341 mContext.getString(R.string.keyboard_shortcut_group_system_home),
342 KeyEvent.KEYCODE_ENTER,
343 KeyEvent.META_META_ON));
344 systemGroup.addItem(new KeyboardShortcutInfo(
345 mContext.getString(R.string.keyboard_shortcut_group_system_back),
346 KeyEvent.KEYCODE_DEL,
347 KeyEvent.META_META_ON));
348 systemGroup.addItem(new KeyboardShortcutInfo(
349 mContext.getString(R.string.keyboard_shortcut_group_system_recents),
350 KeyEvent.KEYCODE_TAB,
351 KeyEvent.META_ALT_ON));
352 systemGroup.addItem(new KeyboardShortcutInfo(
353 mContext.getString(
354 R.string.keyboard_shortcut_group_system_notifications),
355 KeyEvent.KEYCODE_N,
356 KeyEvent.META_META_ON));
357 systemGroup.addItem(new KeyboardShortcutInfo(
358 mContext.getString(
359 R.string.keyboard_shortcut_group_system_shortcuts_helper),
360 KeyEvent.KEYCODE_SLASH,
361 KeyEvent.META_META_ON));
362 systemGroup.addItem(new KeyboardShortcutInfo(
363 mContext.getString(
364 R.string.keyboard_shortcut_group_system_switch_input),
365 KeyEvent.KEYCODE_SPACE,
366 KeyEvent.META_META_ON));
367 return systemGroup;
368 }
369
370 private KeyboardShortcutGroup getDefaultApplicationShortcuts() {
371 final int userId = mContext.getUserId();
Andrei Stingaceanu9cfcafc2016-04-12 11:07:39 +0100372 List<KeyboardShortcutInfo> keyboardShortcutInfoAppItems = new ArrayList<>();
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100373
374 // Assist.
375 final AssistUtils assistUtils = new AssistUtils(mContext);
376 final ComponentName assistComponent = assistUtils.getAssistComponentForUser(userId);
377 PackageInfo assistPackageInfo = null;
378 try {
379 assistPackageInfo = mPackageManager.getPackageInfo(
380 assistComponent.getPackageName(), 0, userId);
381 } catch (RemoteException e) {
382 Log.e(TAG, "PackageManagerService is dead");
383 }
384
385 if (assistPackageInfo != null) {
386 final Icon assistIcon = Icon.createWithResource(
387 assistPackageInfo.applicationInfo.packageName,
388 assistPackageInfo.applicationInfo.icon);
389
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100390 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100391 mContext.getString(R.string.keyboard_shortcut_group_applications_assist),
392 assistIcon,
393 KeyEvent.KEYCODE_UNKNOWN,
394 KeyEvent.META_META_ON));
395 }
396
397 // Browser.
398 final Icon browserIcon = getIconForIntentCategory(Intent.CATEGORY_APP_BROWSER, userId);
399 if (browserIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100400 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100401 mContext.getString(R.string.keyboard_shortcut_group_applications_browser),
402 browserIcon,
403 KeyEvent.KEYCODE_B,
404 KeyEvent.META_META_ON));
405 }
406
407
408 // Contacts.
409 final Icon contactsIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CONTACTS, userId);
410 if (contactsIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100411 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100412 mContext.getString(R.string.keyboard_shortcut_group_applications_contacts),
413 contactsIcon,
414 KeyEvent.KEYCODE_C,
415 KeyEvent.META_META_ON));
416 }
417
418 // Email.
419 final Icon emailIcon = getIconForIntentCategory(Intent.CATEGORY_APP_EMAIL, userId);
420 if (emailIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100421 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100422 mContext.getString(R.string.keyboard_shortcut_group_applications_email),
423 emailIcon,
424 KeyEvent.KEYCODE_E,
425 KeyEvent.META_META_ON));
426 }
427
428 // Messaging.
429 final Icon messagingIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MESSAGING, userId);
430 if (messagingIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100431 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100432 mContext.getString(R.string.keyboard_shortcut_group_applications_im),
433 messagingIcon,
434 KeyEvent.KEYCODE_T,
435 KeyEvent.META_META_ON));
436 }
437
438 // Music.
439 final Icon musicIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MUSIC, userId);
440 if (musicIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100441 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100442 mContext.getString(R.string.keyboard_shortcut_group_applications_music),
443 musicIcon,
444 KeyEvent.KEYCODE_P,
445 KeyEvent.META_META_ON));
446 }
447
448 // Calendar.
449 final Icon calendarIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CALENDAR, userId);
450 if (calendarIcon != null) {
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100451 keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo(
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100452 mContext.getString(R.string.keyboard_shortcut_group_applications_calendar),
453 calendarIcon,
454 KeyEvent.KEYCODE_L,
455 KeyEvent.META_META_ON));
456 }
457
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100458 final int itemsSize = keyboardShortcutInfoAppItems.size();
459 if (itemsSize == 0) {
460 return null;
461 }
462
463 // Sorts by label, case insensitive with nulls and/or empty labels last.
464 Collections.sort(keyboardShortcutInfoAppItems, mApplicationItemsComparator);
465 return new KeyboardShortcutGroup(
466 mContext.getString(R.string.keyboard_shortcut_group_applications),
467 keyboardShortcutInfoAppItems,
468 true);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100469 }
470
471 private Icon getIconForIntentCategory(String intentCategory, int userId) {
472 final Intent intent = new Intent(Intent.ACTION_MAIN);
473 intent.addCategory(intentCategory);
474
475 final PackageInfo packageInfo = getPackageInfoForIntent(intent, userId);
476 if (packageInfo != null && packageInfo.applicationInfo.icon != 0) {
477 return Icon.createWithResource(
478 packageInfo.applicationInfo.packageName,
479 packageInfo.applicationInfo.icon);
480 }
481 return null;
482 }
483
484 private PackageInfo getPackageInfoForIntent(Intent intent, int userId) {
485 try {
486 ResolveInfo handler;
487 handler = mPackageManager.resolveIntent(
488 intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()), 0, userId);
489 if (handler == null || handler.activityInfo == null) {
490 return null;
491 }
492 return mPackageManager.getPackageInfo(handler.activityInfo.packageName, 0, userId);
493 } catch (RemoteException e) {
494 Log.e(TAG, "PackageManagerService is dead", e);
495 return null;
496 }
497 }
498
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000499 private void showKeyboardShortcutsDialog(
500 final List<KeyboardShortcutGroup> keyboardShortcutGroups) {
501 // Need to post on the main thread.
502 mHandler.post(new Runnable() {
503 @Override
504 public void run() {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000505 handleShowKeyboardShortcuts(keyboardShortcutGroups);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000506 }
507 });
508 }
509
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000510 private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) {
511 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
512 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
513 LAYOUT_INFLATER_SERVICE);
514 final View keyboardShortcutsView = inflater.inflate(
515 R.layout.keyboard_shortcuts_view, null);
516 populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById(
517 R.id.keyboard_shortcuts_container), keyboardShortcutGroups);
518 dialogBuilder.setView(keyboardShortcutsView);
Andrei Stingaceanu56e44e42016-04-08 15:07:15 +0100519 dialogBuilder.setPositiveButton(R.string.quick_settings_done, mDialogCloseListener);
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000520 mKeyboardShortcutsDialog = dialogBuilder.create();
521 mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true);
522 Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow();
523 keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG);
524 mKeyboardShortcutsDialog.show();
525 }
526
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000527 private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout,
528 List<KeyboardShortcutGroup> keyboardShortcutGroups) {
529 LayoutInflater inflater = LayoutInflater.from(mContext);
530 final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size();
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100531 TextView shortcutsKeyView = (TextView) inflater.inflate(
532 R.layout.keyboard_shortcuts_key_view, null, false);
533 shortcutsKeyView.measure(
534 View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
535 final int shortcutKeyTextItemMinWidth = shortcutsKeyView.getMeasuredHeight();
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100536 // Needed to be able to scale the image items to the same height as the text items.
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100537 final int shortcutKeyIconItemHeightWidth = shortcutsKeyView.getMeasuredHeight()
538 - shortcutsKeyView.getPaddingTop()
539 - shortcutsKeyView.getPaddingBottom();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000540 for (int i = 0; i < keyboardShortcutGroupsSize; i++) {
541 KeyboardShortcutGroup group = keyboardShortcutGroups.get(i);
542 TextView categoryTitle = (TextView) inflater.inflate(
543 R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false);
544 categoryTitle.setText(group.getLabel());
545 categoryTitle.setTextColor(group.isSystemGroup()
546 ? mContext.getColor(R.color.ksh_system_group_color)
547 : mContext.getColor(R.color.ksh_application_group_color));
548 keyboardShortcutsLayout.addView(categoryTitle);
549
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000550 LinearLayout shortcutContainer = (LinearLayout) inflater.inflate(
551 R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000552 final int itemsSize = group.getItems().size();
553 for (int j = 0; j < itemsSize; j++) {
554 KeyboardShortcutInfo info = group.getItems().get(j);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100555 List<StringOrDrawable> shortcutKeys = getHumanReadableShortcutKeys(info);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800556 if (shortcutKeys == null) {
557 // Ignore shortcuts we can't display keys for.
558 Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping.");
559 continue;
560 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000561 View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item,
562 shortcutContainer, false);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100563
564 if (info.getIcon() != null) {
565 ImageView shortcutIcon = (ImageView) shortcutView
566 .findViewById(R.id.keyboard_shortcuts_icon);
567 shortcutIcon.setImageIcon(info.getIcon());
568 shortcutIcon.setVisibility(View.VISIBLE);
569 }
570
571 TextView shortcutKeyword = (TextView) shortcutView
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000572 .findViewById(R.id.keyboard_shortcuts_keyword);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100573 shortcutKeyword.setText(info.getLabel());
574 if (info.getIcon() != null) {
575 RelativeLayout.LayoutParams lp =
576 (RelativeLayout.LayoutParams) shortcutKeyword.getLayoutParams();
577 lp.removeRule(RelativeLayout.ALIGN_PARENT_START);
578 shortcutKeyword.setLayoutParams(lp);
579 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000580
Andrei Stingaceanu844927d2016-02-16 14:31:58 +0000581 ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000582 .findViewById(R.id.keyboard_shortcuts_item_container);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000583 final int shortcutKeysSize = shortcutKeys.size();
584 for (int k = 0; k < shortcutKeysSize; k++) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100585 StringOrDrawable shortcutRepresentation = shortcutKeys.get(k);
586 if (shortcutRepresentation.drawable != null) {
587 ImageView shortcutKeyIconView = (ImageView) inflater.inflate(
588 R.layout.keyboard_shortcuts_key_icon_view, shortcutItemsContainer,
589 false);
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100590 Bitmap bitmap = Bitmap.createBitmap(shortcutKeyIconItemHeightWidth,
591 shortcutKeyIconItemHeightWidth, Bitmap.Config.ARGB_8888);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100592 Canvas canvas = new Canvas(bitmap);
593 shortcutRepresentation.drawable.setBounds(0, 0, canvas.getWidth(),
594 canvas.getHeight());
595 shortcutRepresentation.drawable.draw(canvas);
596 shortcutKeyIconView.setImageBitmap(bitmap);
597 shortcutItemsContainer.addView(shortcutKeyIconView);
598 } else if (shortcutRepresentation.string != null) {
599 TextView shortcutKeyTextView = (TextView) inflater.inflate(
600 R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer,
601 false);
Andrei Stingaceanua02965e2016-04-08 16:42:02 +0100602 shortcutKeyTextView.setMinimumWidth(shortcutKeyTextItemMinWidth);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100603 shortcutKeyTextView.setText(shortcutRepresentation.string);
604 shortcutItemsContainer.addView(shortcutKeyTextView);
605 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000606 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000607 shortcutContainer.addView(shortcutView);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000608 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000609 keyboardShortcutsLayout.addView(shortcutContainer);
610 if (i < keyboardShortcutGroupsSize - 1) {
611 View separator = inflater.inflate(
612 R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout,
613 false);
614 keyboardShortcutsLayout.addView(separator);
615 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000616 }
617 }
618
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100619 private List<StringOrDrawable> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) {
620 List<StringOrDrawable> shortcutKeys = getHumanReadableModifiers(info);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800621 if (shortcutKeys == null) {
622 return null;
623 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100624 String displayLabelString = null;
625 Drawable displayLabelDrawable = null;
Clara Bayarri1d648a12016-03-23 17:09:02 +0000626 if (info.getBaseCharacter() > Character.MIN_VALUE) {
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800627 displayLabelString = String.valueOf(info.getBaseCharacter());
Clara Bayarrib999af52016-04-06 16:02:35 +0100628 } else if (mSpecialCharacterDrawables.get(info.getKeycode()) != null) {
629 displayLabelDrawable = mSpecialCharacterDrawables.get(info.getKeycode());
630 } else if (mSpecialCharacterNames.get(info.getKeycode()) != null) {
631 displayLabelString = mSpecialCharacterNames.get(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800632 } else {
Clara Bayarri1d648a12016-03-23 17:09:02 +0000633 // Special case for shortcuts with no base key or keycode.
634 if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) {
635 return shortcutKeys;
636 }
Clara Bayarri03f19552016-04-06 10:59:52 +0100637 char displayLabel = mKeyCharacterMap.getDisplayLabel(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800638 if (displayLabel != 0) {
639 displayLabelString = String.valueOf(displayLabel);
640 } else {
641 return null;
642 }
643 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100644
645 if (displayLabelDrawable != null) {
646 shortcutKeys.add(new StringOrDrawable(displayLabelDrawable));
647 } else if (displayLabelString != null) {
648 shortcutKeys.add(new StringOrDrawable(displayLabelString.toUpperCase()));
649 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000650 return shortcutKeys;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100651 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800652
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100653 private List<StringOrDrawable> getHumanReadableModifiers(KeyboardShortcutInfo info) {
654 final List<StringOrDrawable> shortcutKeys = new ArrayList<>();
Clara Bayarrib9057df2016-03-02 11:37:09 -0800655 int modifiers = info.getModifiers();
656 if (modifiers == 0) {
657 return shortcutKeys;
658 }
Clara Bayarrib999af52016-04-06 16:02:35 +0100659 for(int i = 0; i < mModifierNames.size(); ++i) {
660 final int supportedModifier = mModifierNames.keyAt(i);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800661 if ((modifiers & supportedModifier) != 0) {
Clara Bayarrib999af52016-04-06 16:02:35 +0100662 if (mModifierDrawables.get(supportedModifier) != null) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100663 shortcutKeys.add(new StringOrDrawable(
Clara Bayarrib999af52016-04-06 16:02:35 +0100664 mModifierDrawables.get(supportedModifier)));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100665 } else {
666 shortcutKeys.add(new StringOrDrawable(
Clara Bayarrib999af52016-04-06 16:02:35 +0100667 mModifierNames.get(supportedModifier).toUpperCase()));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100668 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800669 modifiers &= ~supportedModifier;
670 }
671 }
672 if (modifiers != 0) {
673 // Remaining unsupported modifiers, don't show anything.
674 return null;
675 }
676 return shortcutKeys;
677 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100678
679 private static final class StringOrDrawable {
680 public String string;
681 public Drawable drawable;
682
683 public StringOrDrawable(String string) {
684 this.string = string;
685 }
686
687 public StringOrDrawable(Drawable drawable) {
688 this.drawable = drawable;
689 }
690 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100691}