blob: fff14914baf2da3564bbcdefa9e5fcf0ac701120 [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;
Clara Bayarri75e09792015-07-29 16:20:40 +010061import java.util.List;
62
63import static android.content.Context.LAYOUT_INFLATER_SERVICE;
Clara Bayarri75e09792015-07-29 16:20:40 +010064import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010065
66/**
67 * Contains functionality for handling keyboard shortcuts.
68 */
Andrei Stingaceanud1519102016-03-31 15:53:33 +010069public final class KeyboardShortcuts {
Clara Bayarri4e850ff2016-03-02 11:12:32 -080070 private static final String TAG = KeyboardShortcuts.class.getSimpleName();
Clara Bayarrib999af52016-04-06 16:02:35 +010071 private final SparseArray<String> mSpecialCharacterNames = new SparseArray<>();
72 private final SparseArray<String> mModifierNames = new SparseArray<>();
73 private final SparseArray<Drawable> mSpecialCharacterDrawables = new SparseArray<>();
74 private final SparseArray<Drawable> mModifierDrawables = new SparseArray<>();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000075
76 private final Handler mHandler = new Handler(Looper.getMainLooper());
77 private final Context mContext;
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010078 private final IPackageManager mPackageManager;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000079 private final OnClickListener dialogCloseListener = new DialogInterface.OnClickListener() {
80 public void onClick(DialogInterface dialog, int id) {
81 dismissKeyboardShortcutsDialog();
82 }
83 };
Clara Bayarri75e09792015-07-29 16:20:40 +010084
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010085 private Dialog mKeyboardShortcutsDialog;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080086 private KeyCharacterMap mKeyCharacterMap;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010087
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000088 public KeyboardShortcuts(Context context) {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +000089 this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_Material_Light);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +010090 this.mPackageManager = AppGlobals.getPackageManager();
Clara Bayarrib999af52016-04-06 16:02:35 +010091 loadResources(context);
92 }
93
94 private void loadResources(Context context) {
95 mSpecialCharacterNames.put(
96 KeyEvent.KEYCODE_HOME, context.getString(R.string.keyboard_key_home));
97 mSpecialCharacterNames.put(
98 KeyEvent.KEYCODE_BACK, context.getString(R.string.keyboard_key_back));
99 mSpecialCharacterNames.put(
100 KeyEvent.KEYCODE_DPAD_UP, context.getString(R.string.keyboard_key_dpad_up));
101 mSpecialCharacterNames.put(
102 KeyEvent.KEYCODE_DPAD_DOWN, context.getString(R.string.keyboard_key_dpad_down));
103 mSpecialCharacterNames.put(
104 KeyEvent.KEYCODE_DPAD_LEFT, context.getString(R.string.keyboard_key_dpad_left));
105 mSpecialCharacterNames.put(
106 KeyEvent.KEYCODE_DPAD_RIGHT, context.getString(R.string.keyboard_key_dpad_right));
107 mSpecialCharacterNames.put(
108 KeyEvent.KEYCODE_DPAD_CENTER, context.getString(R.string.keyboard_key_dpad_center));
109 mSpecialCharacterNames.put(KeyEvent.KEYCODE_PERIOD, ".");
110 mSpecialCharacterNames.put(
111 KeyEvent.KEYCODE_TAB, context.getString(R.string.keyboard_key_tab));
112 mSpecialCharacterNames.put(
113 KeyEvent.KEYCODE_SPACE, context.getString(R.string.keyboard_key_space));
114 mSpecialCharacterNames.put(
115 KeyEvent.KEYCODE_ENTER, context.getString(R.string.keyboard_key_enter));
116 mSpecialCharacterNames.put(
117 KeyEvent.KEYCODE_DEL, context.getString(R.string.keyboard_key_backspace));
118 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE,
119 context.getString(R.string.keyboard_key_media_play_pause));
120 mSpecialCharacterNames.put(
121 KeyEvent.KEYCODE_MEDIA_STOP, context.getString(R.string.keyboard_key_media_stop));
122 mSpecialCharacterNames.put(
123 KeyEvent.KEYCODE_MEDIA_NEXT, context.getString(R.string.keyboard_key_media_next));
124 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PREVIOUS,
125 context.getString(R.string.keyboard_key_media_previous));
126 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_REWIND,
127 context.getString(R.string.keyboard_key_media_rewind));
128 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD,
129 context.getString(R.string.keyboard_key_media_fast_forward));
130 mSpecialCharacterNames.put(
131 KeyEvent.KEYCODE_PAGE_UP, context.getString(R.string.keyboard_key_page_up));
132 mSpecialCharacterNames.put(
133 KeyEvent.KEYCODE_PAGE_DOWN, context.getString(R.string.keyboard_key_page_down));
134 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_A,
135 context.getString(R.string.keyboard_key_button_template, "A"));
136 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_B,
137 context.getString(R.string.keyboard_key_button_template, "B"));
138 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_C,
139 context.getString(R.string.keyboard_key_button_template, "C"));
140 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_X,
141 context.getString(R.string.keyboard_key_button_template, "X"));
142 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Y,
143 context.getString(R.string.keyboard_key_button_template, "Y"));
144 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Z,
145 context.getString(R.string.keyboard_key_button_template, "Z"));
146 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L1,
147 context.getString(R.string.keyboard_key_button_template, "L1"));
148 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R1,
149 context.getString(R.string.keyboard_key_button_template, "R1"));
150 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L2,
151 context.getString(R.string.keyboard_key_button_template, "L2"));
152 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R2,
153 context.getString(R.string.keyboard_key_button_template, "R2"));
154 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_START,
155 context.getString(R.string.keyboard_key_button_template, "Start"));
156 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_SELECT,
157 context.getString(R.string.keyboard_key_button_template, "Select"));
158 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_MODE,
159 context.getString(R.string.keyboard_key_button_template, "Mode"));
160 mSpecialCharacterNames.put(
161 KeyEvent.KEYCODE_FORWARD_DEL, context.getString(R.string.keyboard_key_forward_del));
162 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ESCAPE, "Esc");
163 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SYSRQ, "SysRq");
164 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BREAK, "Break");
165 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SCROLL_LOCK, "Scroll Lock");
166 mSpecialCharacterNames.put(
167 KeyEvent.KEYCODE_MOVE_HOME, context.getString(R.string.keyboard_key_move_home));
168 mSpecialCharacterNames.put(
169 KeyEvent.KEYCODE_MOVE_END, context.getString(R.string.keyboard_key_move_end));
170 mSpecialCharacterNames.put(
171 KeyEvent.KEYCODE_INSERT, context.getString(R.string.keyboard_key_insert));
172 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F1, "F1");
173 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F2, "F2");
174 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F3, "F3");
175 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F4, "F4");
176 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F5, "F5");
177 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F6, "F6");
178 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F7, "F7");
179 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F8, "F8");
180 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F9, "F9");
181 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F10, "F10");
182 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F11, "F11");
183 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F12, "F12");
184 mSpecialCharacterNames.put(
185 KeyEvent.KEYCODE_NUM_LOCK, context.getString(R.string.keyboard_key_num_lock));
186 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_0,
187 context.getString(R.string.keyboard_key_numpad_template, "0"));
188 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_1,
189 context.getString(R.string.keyboard_key_numpad_template, "1"));
190 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_2,
191 context.getString(R.string.keyboard_key_numpad_template, "2"));
192 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_3,
193 context.getString(R.string.keyboard_key_numpad_template, "3"));
194 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_4,
195 context.getString(R.string.keyboard_key_numpad_template, "4"));
196 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_5,
197 context.getString(R.string.keyboard_key_numpad_template, "5"));
198 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_6,
199 context.getString(R.string.keyboard_key_numpad_template, "6"));
200 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_7,
201 context.getString(R.string.keyboard_key_numpad_template, "7"));
202 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_8,
203 context.getString(R.string.keyboard_key_numpad_template, "8"));
204 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_9,
205 context.getString(R.string.keyboard_key_numpad_template, "9"));
206 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DIVIDE,
207 context.getString(R.string.keyboard_key_numpad_template, "/"));
208 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_MULTIPLY,
209 context.getString(R.string.keyboard_key_numpad_template, "*"));
210 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_SUBTRACT,
211 context.getString(R.string.keyboard_key_numpad_template, "-"));
212 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ADD,
213 context.getString(R.string.keyboard_key_numpad_template, "+"));
214 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DOT,
215 context.getString(R.string.keyboard_key_numpad_template, "."));
216 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_COMMA,
217 context.getString(R.string.keyboard_key_numpad_template, ","));
218 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ENTER,
219 context.getString(R.string.keyboard_key_numpad_template,
220 context.getString(R.string.keyboard_key_enter)));
221 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_EQUALS,
222 context.getString(R.string.keyboard_key_numpad_template, "="));
223 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN,
224 context.getString(R.string.keyboard_key_numpad_template, "("));
225 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN,
226 context.getString(R.string.keyboard_key_numpad_template, ")"));
227 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ZENKAKU_HANKAKU, "半角/全角");
228 mSpecialCharacterNames.put(KeyEvent.KEYCODE_EISU, "英数");
229 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MUHENKAN, "無変換");
230 mSpecialCharacterNames.put(KeyEvent.KEYCODE_HENKAN, "変換");
231 mSpecialCharacterNames.put(KeyEvent.KEYCODE_KATAKANA_HIRAGANA, "かな");
232
233 mModifierNames.put(KeyEvent.META_META_ON, "Meta");
234 mModifierNames.put(KeyEvent.META_CTRL_ON, "Ctrl");
235 mModifierNames.put(KeyEvent.META_ALT_ON, "Alt");
236 mModifierNames.put(KeyEvent.META_SHIFT_ON, "Shift");
237 mModifierNames.put(KeyEvent.META_SYM_ON, "Sym");
238 mModifierNames.put(KeyEvent.META_FUNCTION_ON, "Fn");
239
240 mSpecialCharacterDrawables.put(
241 KeyEvent.KEYCODE_DEL, context.getDrawable(R.drawable.ic_ksh_key_backspace));
242 mSpecialCharacterDrawables.put(
243 KeyEvent.KEYCODE_ENTER, context.getDrawable(R.drawable.ic_ksh_key_enter));
244 mSpecialCharacterDrawables.put(
245 KeyEvent.KEYCODE_DPAD_UP, context.getDrawable(R.drawable.ic_ksh_key_up));
246 mSpecialCharacterDrawables.put(
247 KeyEvent.KEYCODE_DPAD_RIGHT, context.getDrawable(R.drawable.ic_ksh_key_right));
248 mSpecialCharacterDrawables.put(
249 KeyEvent.KEYCODE_DPAD_DOWN, context.getDrawable(R.drawable.ic_ksh_key_down));
250 mSpecialCharacterDrawables.put(
251 KeyEvent.KEYCODE_DPAD_LEFT, context.getDrawable(R.drawable.ic_ksh_key_left));
252
253 mModifierDrawables.put(
254 KeyEvent.META_META_ON, context.getDrawable(R.drawable.ic_ksh_key_meta));
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000255 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100256
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800257 public void toggleKeyboardShortcuts(int deviceId) {
258 InputDevice inputDevice = InputManager.getInstance().getInputDevice(deviceId);
259 if (inputDevice != null) {
260 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
261 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100262 if (mKeyboardShortcutsDialog == null) {
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000263 Recents.getSystemServices().requestKeyboardShortcuts(mContext,
Clara Bayarri75e09792015-07-29 16:20:40 +0100264 new KeyboardShortcutsReceiver() {
265 @Override
266 public void onKeyboardShortcutsReceived(
267 final List<KeyboardShortcutGroup> result) {
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100268 result.add(getSystemShortcuts());
269 final KeyboardShortcutGroup appShortcuts = getDefaultApplicationShortcuts();
270 if (appShortcuts != null) {
271 result.add(appShortcuts);
272 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000273 showKeyboardShortcutsDialog(result);
Clara Bayarri75e09792015-07-29 16:20:40 +0100274 }
Clara Bayarrifcd7e802016-03-10 12:58:18 +0000275 }, deviceId);
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100276 } else {
277 dismissKeyboardShortcutsDialog();
278 }
279 }
280
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100281 public void dismissKeyboardShortcutsDialog() {
282 if (mKeyboardShortcutsDialog != null) {
283 mKeyboardShortcutsDialog.dismiss();
284 mKeyboardShortcutsDialog = null;
285 }
286 }
287
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100288 private KeyboardShortcutGroup getSystemShortcuts() {
289 final KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup(
290 mContext.getString(R.string.keyboard_shortcut_group_system), true);
291 systemGroup.addItem(new KeyboardShortcutInfo(
292 mContext.getString(R.string.keyboard_shortcut_group_system_home),
293 KeyEvent.KEYCODE_ENTER,
294 KeyEvent.META_META_ON));
295 systemGroup.addItem(new KeyboardShortcutInfo(
296 mContext.getString(R.string.keyboard_shortcut_group_system_back),
297 KeyEvent.KEYCODE_DEL,
298 KeyEvent.META_META_ON));
299 systemGroup.addItem(new KeyboardShortcutInfo(
300 mContext.getString(R.string.keyboard_shortcut_group_system_recents),
301 KeyEvent.KEYCODE_TAB,
302 KeyEvent.META_ALT_ON));
303 systemGroup.addItem(new KeyboardShortcutInfo(
304 mContext.getString(
305 R.string.keyboard_shortcut_group_system_notifications),
306 KeyEvent.KEYCODE_N,
307 KeyEvent.META_META_ON));
308 systemGroup.addItem(new KeyboardShortcutInfo(
309 mContext.getString(
310 R.string.keyboard_shortcut_group_system_shortcuts_helper),
311 KeyEvent.KEYCODE_SLASH,
312 KeyEvent.META_META_ON));
313 systemGroup.addItem(new KeyboardShortcutInfo(
314 mContext.getString(
315 R.string.keyboard_shortcut_group_system_switch_input),
316 KeyEvent.KEYCODE_SPACE,
317 KeyEvent.META_META_ON));
318 return systemGroup;
319 }
320
321 private KeyboardShortcutGroup getDefaultApplicationShortcuts() {
322 final int userId = mContext.getUserId();
323 final KeyboardShortcutGroup applicationGroup = new KeyboardShortcutGroup(
324 mContext.getString(R.string.keyboard_shortcut_group_applications),
325 true);
326
327 // Assist.
328 final AssistUtils assistUtils = new AssistUtils(mContext);
329 final ComponentName assistComponent = assistUtils.getAssistComponentForUser(userId);
330 PackageInfo assistPackageInfo = null;
331 try {
332 assistPackageInfo = mPackageManager.getPackageInfo(
333 assistComponent.getPackageName(), 0, userId);
334 } catch (RemoteException e) {
335 Log.e(TAG, "PackageManagerService is dead");
336 }
337
338 if (assistPackageInfo != null) {
339 final Icon assistIcon = Icon.createWithResource(
340 assistPackageInfo.applicationInfo.packageName,
341 assistPackageInfo.applicationInfo.icon);
342
343 applicationGroup.addItem(new KeyboardShortcutInfo(
344 mContext.getString(R.string.keyboard_shortcut_group_applications_assist),
345 assistIcon,
346 KeyEvent.KEYCODE_UNKNOWN,
347 KeyEvent.META_META_ON));
348 }
349
350 // Browser.
351 final Icon browserIcon = getIconForIntentCategory(Intent.CATEGORY_APP_BROWSER, userId);
352 if (browserIcon != null) {
353 applicationGroup.addItem(new KeyboardShortcutInfo(
354 mContext.getString(R.string.keyboard_shortcut_group_applications_browser),
355 browserIcon,
356 KeyEvent.KEYCODE_B,
357 KeyEvent.META_META_ON));
358 }
359
360
361 // Contacts.
362 final Icon contactsIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CONTACTS, userId);
363 if (contactsIcon != null) {
364 applicationGroup.addItem(new KeyboardShortcutInfo(
365 mContext.getString(R.string.keyboard_shortcut_group_applications_contacts),
366 contactsIcon,
367 KeyEvent.KEYCODE_C,
368 KeyEvent.META_META_ON));
369 }
370
371 // Email.
372 final Icon emailIcon = getIconForIntentCategory(Intent.CATEGORY_APP_EMAIL, userId);
373 if (emailIcon != null) {
374 applicationGroup.addItem(new KeyboardShortcutInfo(
375 mContext.getString(R.string.keyboard_shortcut_group_applications_email),
376 emailIcon,
377 KeyEvent.KEYCODE_E,
378 KeyEvent.META_META_ON));
379 }
380
381 // Messaging.
382 final Icon messagingIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MESSAGING, userId);
383 if (messagingIcon != null) {
384 applicationGroup.addItem(new KeyboardShortcutInfo(
385 mContext.getString(R.string.keyboard_shortcut_group_applications_im),
386 messagingIcon,
387 KeyEvent.KEYCODE_T,
388 KeyEvent.META_META_ON));
389 }
390
391 // Music.
392 final Icon musicIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MUSIC, userId);
393 if (musicIcon != null) {
394 applicationGroup.addItem(new KeyboardShortcutInfo(
395 mContext.getString(R.string.keyboard_shortcut_group_applications_music),
396 musicIcon,
397 KeyEvent.KEYCODE_P,
398 KeyEvent.META_META_ON));
399 }
400
401 // Calendar.
402 final Icon calendarIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CALENDAR, userId);
403 if (calendarIcon != null) {
404 applicationGroup.addItem(new KeyboardShortcutInfo(
405 mContext.getString(R.string.keyboard_shortcut_group_applications_calendar),
406 calendarIcon,
407 KeyEvent.KEYCODE_L,
408 KeyEvent.META_META_ON));
409 }
410
411 return applicationGroup.getItems().size() == 0 ? null : applicationGroup;
412 }
413
414 private Icon getIconForIntentCategory(String intentCategory, int userId) {
415 final Intent intent = new Intent(Intent.ACTION_MAIN);
416 intent.addCategory(intentCategory);
417
418 final PackageInfo packageInfo = getPackageInfoForIntent(intent, userId);
419 if (packageInfo != null && packageInfo.applicationInfo.icon != 0) {
420 return Icon.createWithResource(
421 packageInfo.applicationInfo.packageName,
422 packageInfo.applicationInfo.icon);
423 }
424 return null;
425 }
426
427 private PackageInfo getPackageInfoForIntent(Intent intent, int userId) {
428 try {
429 ResolveInfo handler;
430 handler = mPackageManager.resolveIntent(
431 intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()), 0, userId);
432 if (handler == null || handler.activityInfo == null) {
433 return null;
434 }
435 return mPackageManager.getPackageInfo(handler.activityInfo.packageName, 0, userId);
436 } catch (RemoteException e) {
437 Log.e(TAG, "PackageManagerService is dead", e);
438 return null;
439 }
440 }
441
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000442 private void showKeyboardShortcutsDialog(
443 final List<KeyboardShortcutGroup> keyboardShortcutGroups) {
444 // Need to post on the main thread.
445 mHandler.post(new Runnable() {
446 @Override
447 public void run() {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000448 handleShowKeyboardShortcuts(keyboardShortcutGroups);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000449 }
450 });
451 }
452
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000453 private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) {
454 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
455 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
456 LAYOUT_INFLATER_SERVICE);
457 final View keyboardShortcutsView = inflater.inflate(
458 R.layout.keyboard_shortcuts_view, null);
459 populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById(
460 R.id.keyboard_shortcuts_container), keyboardShortcutGroups);
461 dialogBuilder.setView(keyboardShortcutsView);
462 dialogBuilder.setPositiveButton(R.string.quick_settings_done, dialogCloseListener);
463 mKeyboardShortcutsDialog = dialogBuilder.create();
464 mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true);
465 Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow();
466 keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG);
467 mKeyboardShortcutsDialog.show();
468 }
469
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000470 private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout,
471 List<KeyboardShortcutGroup> keyboardShortcutGroups) {
472 LayoutInflater inflater = LayoutInflater.from(mContext);
473 final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size();
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100474 // Needed to be able to scale the image items to the same height as the text items.
475 final int shortcutTextItemHeight = getShortcutTextItemHeight(inflater);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000476 for (int i = 0; i < keyboardShortcutGroupsSize; i++) {
477 KeyboardShortcutGroup group = keyboardShortcutGroups.get(i);
478 TextView categoryTitle = (TextView) inflater.inflate(
479 R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false);
480 categoryTitle.setText(group.getLabel());
481 categoryTitle.setTextColor(group.isSystemGroup()
482 ? mContext.getColor(R.color.ksh_system_group_color)
483 : mContext.getColor(R.color.ksh_application_group_color));
484 keyboardShortcutsLayout.addView(categoryTitle);
485
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000486 LinearLayout shortcutContainer = (LinearLayout) inflater.inflate(
487 R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000488 final int itemsSize = group.getItems().size();
489 for (int j = 0; j < itemsSize; j++) {
490 KeyboardShortcutInfo info = group.getItems().get(j);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800491 if (info.getKeycode() != KeyEvent.KEYCODE_UNKNOWN
492 && !KeyCharacterMap.deviceHasKey(info.getKeycode())) {
493 // The user can't achieve this shortcut, so skipping.
494 Log.w(TAG, "Keyboard Shortcut contains key not on device, skipping.");
495 continue;
496 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100497 List<StringOrDrawable> shortcutKeys = getHumanReadableShortcutKeys(info);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800498 if (shortcutKeys == null) {
499 // Ignore shortcuts we can't display keys for.
500 Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping.");
501 continue;
502 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000503 View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item,
504 shortcutContainer, false);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100505
506 if (info.getIcon() != null) {
507 ImageView shortcutIcon = (ImageView) shortcutView
508 .findViewById(R.id.keyboard_shortcuts_icon);
509 shortcutIcon.setImageIcon(info.getIcon());
510 shortcutIcon.setVisibility(View.VISIBLE);
511 }
512
513 TextView shortcutKeyword = (TextView) shortcutView
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000514 .findViewById(R.id.keyboard_shortcuts_keyword);
Andrei Stingaceanu12e98032016-04-05 12:22:21 +0100515 shortcutKeyword.setText(info.getLabel());
516 if (info.getIcon() != null) {
517 RelativeLayout.LayoutParams lp =
518 (RelativeLayout.LayoutParams) shortcutKeyword.getLayoutParams();
519 lp.removeRule(RelativeLayout.ALIGN_PARENT_START);
520 shortcutKeyword.setLayoutParams(lp);
521 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000522
Andrei Stingaceanu844927d2016-02-16 14:31:58 +0000523 ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000524 .findViewById(R.id.keyboard_shortcuts_item_container);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000525 final int shortcutKeysSize = shortcutKeys.size();
526 for (int k = 0; k < shortcutKeysSize; k++) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100527 StringOrDrawable shortcutRepresentation = shortcutKeys.get(k);
528 if (shortcutRepresentation.drawable != null) {
529 ImageView shortcutKeyIconView = (ImageView) inflater.inflate(
530 R.layout.keyboard_shortcuts_key_icon_view, shortcutItemsContainer,
531 false);
532 Bitmap bitmap = Bitmap.createBitmap(shortcutTextItemHeight,
533 shortcutTextItemHeight, Bitmap.Config.ARGB_8888);
534 Canvas canvas = new Canvas(bitmap);
535 shortcutRepresentation.drawable.setBounds(0, 0, canvas.getWidth(),
536 canvas.getHeight());
537 shortcutRepresentation.drawable.draw(canvas);
538 shortcutKeyIconView.setImageBitmap(bitmap);
539 shortcutItemsContainer.addView(shortcutKeyIconView);
540 } else if (shortcutRepresentation.string != null) {
541 TextView shortcutKeyTextView = (TextView) inflater.inflate(
542 R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer,
543 false);
544 shortcutKeyTextView.setText(shortcutRepresentation.string);
545 shortcutItemsContainer.addView(shortcutKeyTextView);
546 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000547 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000548 shortcutContainer.addView(shortcutView);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000549 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000550 keyboardShortcutsLayout.addView(shortcutContainer);
551 if (i < keyboardShortcutGroupsSize - 1) {
552 View separator = inflater.inflate(
553 R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout,
554 false);
555 keyboardShortcutsLayout.addView(separator);
556 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000557 }
558 }
559
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100560 private int getShortcutTextItemHeight(LayoutInflater inflater) {
561 TextView shortcutKeyTextView = (TextView) inflater.inflate(
562 R.layout.keyboard_shortcuts_key_view, null, false);
563 shortcutKeyTextView.measure(
564 View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
565 return shortcutKeyTextView.getMeasuredHeight()
566 - shortcutKeyTextView.getPaddingTop()
567 - shortcutKeyTextView.getPaddingBottom();
568 }
569
570 private List<StringOrDrawable> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) {
571 List<StringOrDrawable> shortcutKeys = getHumanReadableModifiers(info);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800572 if (shortcutKeys == null) {
573 return null;
574 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100575 String displayLabelString = null;
576 Drawable displayLabelDrawable = null;
Clara Bayarri1d648a12016-03-23 17:09:02 +0000577 if (info.getBaseCharacter() > Character.MIN_VALUE) {
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800578 displayLabelString = String.valueOf(info.getBaseCharacter());
Clara Bayarrib999af52016-04-06 16:02:35 +0100579 } else if (mSpecialCharacterDrawables.get(info.getKeycode()) != null) {
580 displayLabelDrawable = mSpecialCharacterDrawables.get(info.getKeycode());
581 } else if (mSpecialCharacterNames.get(info.getKeycode()) != null) {
582 displayLabelString = mSpecialCharacterNames.get(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800583 } else {
Clara Bayarri1d648a12016-03-23 17:09:02 +0000584 // Special case for shortcuts with no base key or keycode.
585 if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) {
586 return shortcutKeys;
587 }
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800588 // TODO: Have a generic map for when we don't have the device's.
589 char displayLabel = mKeyCharacterMap == null
590 ? 0 : mKeyCharacterMap.getDisplayLabel(info.getKeycode());
591 if (displayLabel != 0) {
592 displayLabelString = String.valueOf(displayLabel);
593 } else {
594 return null;
595 }
596 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100597
598 if (displayLabelDrawable != null) {
599 shortcutKeys.add(new StringOrDrawable(displayLabelDrawable));
600 } else if (displayLabelString != null) {
601 shortcutKeys.add(new StringOrDrawable(displayLabelString.toUpperCase()));
602 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000603 return shortcutKeys;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100604 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800605
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100606 private List<StringOrDrawable> getHumanReadableModifiers(KeyboardShortcutInfo info) {
607 final List<StringOrDrawable> shortcutKeys = new ArrayList<>();
Clara Bayarrib9057df2016-03-02 11:37:09 -0800608 int modifiers = info.getModifiers();
609 if (modifiers == 0) {
610 return shortcutKeys;
611 }
Clara Bayarrib999af52016-04-06 16:02:35 +0100612 for(int i = 0; i < mModifierNames.size(); ++i) {
613 final int supportedModifier = mModifierNames.keyAt(i);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800614 if ((modifiers & supportedModifier) != 0) {
Clara Bayarrib999af52016-04-06 16:02:35 +0100615 if (mModifierDrawables.get(supportedModifier) != null) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100616 shortcutKeys.add(new StringOrDrawable(
Clara Bayarrib999af52016-04-06 16:02:35 +0100617 mModifierDrawables.get(supportedModifier)));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100618 } else {
619 shortcutKeys.add(new StringOrDrawable(
Clara Bayarrib999af52016-04-06 16:02:35 +0100620 mModifierNames.get(supportedModifier).toUpperCase()));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100621 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800622 modifiers &= ~supportedModifier;
623 }
624 }
625 if (modifiers != 0) {
626 // Remaining unsupported modifiers, don't show anything.
627 return null;
628 }
629 return shortcutKeys;
630 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100631
632 private static final class StringOrDrawable {
633 public String string;
634 public Drawable drawable;
635
636 public StringOrDrawable(String string) {
637 this.string = string;
638 }
639
640 public StringOrDrawable(Drawable drawable) {
641 this.drawable = drawable;
642 }
643 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100644}