blob: 2b365dc743e99db2ed9a2789b9a9894de69f4f05 [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;
20import android.app.Dialog;
21import android.content.Context;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000022import android.content.DialogInterface;
23import android.content.DialogInterface.OnClickListener;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080024import android.hardware.input.InputManager;
Clara Bayarri75e09792015-07-29 16:20:40 +010025import android.os.Handler;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000026import android.os.Looper;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080027import android.util.Log;
28import android.util.SparseArray;
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +000029import android.view.ContextThemeWrapper;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080030import android.view.InputDevice;
31import android.view.KeyCharacterMap;
Clara Bayarri75e09792015-07-29 16:20:40 +010032import android.view.KeyEvent;
33import android.view.KeyboardShortcutGroup;
34import android.view.KeyboardShortcutInfo;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010035import android.view.LayoutInflater;
36import android.view.View;
Andrei Stingaceanu844927d2016-02-16 14:31:58 +000037import android.view.ViewGroup;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010038import android.view.Window;
Clara Bayarri75e09792015-07-29 16:20:40 +010039import android.view.WindowManager.KeyboardShortcutsReceiver;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000040import android.widget.LinearLayout;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000041import android.widget.TextView;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010042
43import com.android.systemui.R;
Clara Bayarri75e09792015-07-29 16:20:40 +010044import com.android.systemui.recents.Recents;
45
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000046import java.util.ArrayList;
Clara Bayarri75e09792015-07-29 16:20:40 +010047import java.util.List;
48
49import static android.content.Context.LAYOUT_INFLATER_SERVICE;
Clara Bayarri75e09792015-07-29 16:20:40 +010050import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010051
52/**
53 * Contains functionality for handling keyboard shortcuts.
54 */
55public class KeyboardShortcuts {
Clara Bayarri4e850ff2016-03-02 11:12:32 -080056 private static final String TAG = KeyboardShortcuts.class.getSimpleName();
57
58 private static final SparseArray<String> SPECIAL_CHARACTER_NAMES = new SparseArray<>();
Clara Bayarrib9057df2016-03-02 11:37:09 -080059 private static final SparseArray<String> MODIFIER_NAMES = new SparseArray<>();
Clara Bayarri4e850ff2016-03-02 11:12:32 -080060
61 private static void loadSpecialCharacterNames(Context context) {
62 SPECIAL_CHARACTER_NAMES.put(
63 KeyEvent.KEYCODE_HOME, context.getString(R.string.keyboard_key_home));
64 SPECIAL_CHARACTER_NAMES.put(
65 KeyEvent.KEYCODE_BACK, context.getString(R.string.keyboard_key_back));
66 SPECIAL_CHARACTER_NAMES.put(
67 KeyEvent.KEYCODE_DPAD_UP, context.getString(R.string.keyboard_key_dpad_up));
68 SPECIAL_CHARACTER_NAMES.put(
69 KeyEvent.KEYCODE_DPAD_DOWN, context.getString(R.string.keyboard_key_dpad_down));
70 SPECIAL_CHARACTER_NAMES.put(
71 KeyEvent.KEYCODE_DPAD_LEFT, context.getString(R.string.keyboard_key_dpad_left));
72 SPECIAL_CHARACTER_NAMES.put(
73 KeyEvent.KEYCODE_DPAD_RIGHT, context.getString(R.string.keyboard_key_dpad_right));
74 SPECIAL_CHARACTER_NAMES.put(
75 KeyEvent.KEYCODE_DPAD_CENTER, context.getString(R.string.keyboard_key_dpad_center));
76 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_PERIOD, ".");
77 SPECIAL_CHARACTER_NAMES.put(
78 KeyEvent.KEYCODE_TAB, context.getString(R.string.keyboard_key_tab));
79 SPECIAL_CHARACTER_NAMES.put(
80 KeyEvent.KEYCODE_SPACE, context.getString(R.string.keyboard_key_space));
81 SPECIAL_CHARACTER_NAMES.put(
82 KeyEvent.KEYCODE_ENTER, context.getString(R.string.keyboard_key_enter));
83 SPECIAL_CHARACTER_NAMES.put(
84 KeyEvent.KEYCODE_DEL, context.getString(R.string.keyboard_key_backspace));
85 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE,
86 context.getString(R.string.keyboard_key_media_play_pause));
87 SPECIAL_CHARACTER_NAMES.put(
88 KeyEvent.KEYCODE_MEDIA_STOP, context.getString(R.string.keyboard_key_media_stop));
89 SPECIAL_CHARACTER_NAMES.put(
90 KeyEvent.KEYCODE_MEDIA_NEXT, context.getString(R.string.keyboard_key_media_next));
91 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_MEDIA_PREVIOUS,
92 context.getString(R.string.keyboard_key_media_previous));
93 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_MEDIA_REWIND,
94 context.getString(R.string.keyboard_key_media_rewind));
95 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD,
96 context.getString(R.string.keyboard_key_media_fast_forward));
97 SPECIAL_CHARACTER_NAMES.put(
98 KeyEvent.KEYCODE_PAGE_UP, context.getString(R.string.keyboard_key_page_up));
99 SPECIAL_CHARACTER_NAMES.put(
100 KeyEvent.KEYCODE_PAGE_DOWN, context.getString(R.string.keyboard_key_page_down));
101 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_A,
102 context.getString(R.string.keyboard_key_button_template, "A"));
103 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_B,
104 context.getString(R.string.keyboard_key_button_template, "B"));
105 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_C,
106 context.getString(R.string.keyboard_key_button_template, "C"));
107 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_X,
108 context.getString(R.string.keyboard_key_button_template, "X"));
109 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_Y,
110 context.getString(R.string.keyboard_key_button_template, "Y"));
111 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_Z,
112 context.getString(R.string.keyboard_key_button_template, "Z"));
113 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_L1,
114 context.getString(R.string.keyboard_key_button_template, "L1"));
115 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_R1,
116 context.getString(R.string.keyboard_key_button_template, "R1"));
117 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_L2,
118 context.getString(R.string.keyboard_key_button_template, "L2"));
119 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_R2,
120 context.getString(R.string.keyboard_key_button_template, "R2"));
121 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_START,
122 context.getString(R.string.keyboard_key_button_template, "Start"));
123 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_SELECT,
124 context.getString(R.string.keyboard_key_button_template, "Select"));
125 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_MODE,
126 context.getString(R.string.keyboard_key_button_template, "Mode"));
127 SPECIAL_CHARACTER_NAMES.put(
128 KeyEvent.KEYCODE_FORWARD_DEL, context.getString(R.string.keyboard_key_forward_del));
129 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_ESCAPE, "Esc");
130 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_SYSRQ, "SysRq");
131 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BREAK, "Break");
132 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_SCROLL_LOCK, "Scroll Lock");
133 SPECIAL_CHARACTER_NAMES.put(
134 KeyEvent.KEYCODE_MOVE_HOME, context.getString(R.string.keyboard_key_move_home));
135 SPECIAL_CHARACTER_NAMES.put(
136 KeyEvent.KEYCODE_MOVE_END, context.getString(R.string.keyboard_key_move_end));
137 SPECIAL_CHARACTER_NAMES.put(
138 KeyEvent.KEYCODE_INSERT, context.getString(R.string.keyboard_key_insert));
139 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F1, "F1");
140 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F2, "F2");
141 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F3, "F3");
142 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F4, "F4");
143 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F5, "F5");
144 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F6, "F6");
145 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F7, "F7");
146 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F8, "F8");
147 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F9, "F9");
148 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F10, "F10");
149 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F11, "F11");
150 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F12, "F12");
151 SPECIAL_CHARACTER_NAMES.put(
152 KeyEvent.KEYCODE_NUM_LOCK, context.getString(R.string.keyboard_key_num_lock));
153 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_0,
154 context.getString(R.string.keyboard_key_numpad_template, "0"));
155 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_1,
156 context.getString(R.string.keyboard_key_numpad_template, "1"));
157 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_2,
158 context.getString(R.string.keyboard_key_numpad_template, "2"));
159 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_3,
160 context.getString(R.string.keyboard_key_numpad_template, "3"));
161 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_4,
162 context.getString(R.string.keyboard_key_numpad_template, "4"));
163 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_5,
164 context.getString(R.string.keyboard_key_numpad_template, "5"));
165 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_6,
166 context.getString(R.string.keyboard_key_numpad_template, "6"));
167 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_7,
168 context.getString(R.string.keyboard_key_numpad_template, "7"));
169 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_8,
170 context.getString(R.string.keyboard_key_numpad_template, "8"));
171 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_9,
172 context.getString(R.string.keyboard_key_numpad_template, "9"));
173 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_DIVIDE,
174 context.getString(R.string.keyboard_key_numpad_template, "/"));
175 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_MULTIPLY,
176 context.getString(R.string.keyboard_key_numpad_template, "*"));
177 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_SUBTRACT,
178 context.getString(R.string.keyboard_key_numpad_template, "-"));
179 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_ADD,
180 context.getString(R.string.keyboard_key_numpad_template, "+"));
181 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_DOT,
182 context.getString(R.string.keyboard_key_numpad_template, "."));
183 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_COMMA,
184 context.getString(R.string.keyboard_key_numpad_template, ","));
185 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_ENTER,
186 context.getString(R.string.keyboard_key_numpad_template,
187 context.getString(R.string.keyboard_key_enter)));
188 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_EQUALS,
189 context.getString(R.string.keyboard_key_numpad_template, "="));
190 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN,
191 context.getString(R.string.keyboard_key_numpad_template, "("));
192 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN,
193 context.getString(R.string.keyboard_key_numpad_template, ")"));
194 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_ZENKAKU_HANKAKU, "半角/全角");
195 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_EISU, "英数");
196 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_MUHENKAN, "無変換");
197 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_HENKAN, "変換");
198 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_KATAKANA_HIRAGANA, "かな");
Clara Bayarrib9057df2016-03-02 11:37:09 -0800199
200 MODIFIER_NAMES.put(KeyEvent.META_META_ON, "Meta");
201 MODIFIER_NAMES.put(KeyEvent.META_CTRL_ON, "Ctrl");
202 MODIFIER_NAMES.put(KeyEvent.META_ALT_ON, "Alt");
203 MODIFIER_NAMES.put(KeyEvent.META_SHIFT_ON, "Shift");
204 MODIFIER_NAMES.put(KeyEvent.META_SYM_ON, "Sym");
205 MODIFIER_NAMES.put(KeyEvent.META_FUNCTION_ON, "Fn");
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800206 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000207
208 private final Handler mHandler = new Handler(Looper.getMainLooper());
209 private final Context mContext;
210 private final OnClickListener dialogCloseListener = new DialogInterface.OnClickListener() {
211 public void onClick(DialogInterface dialog, int id) {
212 dismissKeyboardShortcutsDialog();
213 }
214 };
Clara Bayarri75e09792015-07-29 16:20:40 +0100215
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100216 private Dialog mKeyboardShortcutsDialog;
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800217 private KeyCharacterMap mKeyCharacterMap;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100218
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000219 public KeyboardShortcuts(Context context) {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000220 this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_Material_Light);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800221 if (SPECIAL_CHARACTER_NAMES.size() == 0) {
222 loadSpecialCharacterNames(context);
223 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000224 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100225
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800226 public void toggleKeyboardShortcuts(int deviceId) {
227 InputDevice inputDevice = InputManager.getInstance().getInputDevice(deviceId);
228 if (inputDevice != null) {
229 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
230 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100231 if (mKeyboardShortcutsDialog == null) {
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000232 Recents.getSystemServices().requestKeyboardShortcuts(mContext,
Clara Bayarri75e09792015-07-29 16:20:40 +0100233 new KeyboardShortcutsReceiver() {
234 @Override
235 public void onKeyboardShortcutsReceived(
236 final List<KeyboardShortcutGroup> result) {
237 KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup(
Clara Bayarrib9057df2016-03-02 11:37:09 -0800238 mContext.getString(R.string.keyboard_shortcut_group_system), true);
Clara Bayarri75e09792015-07-29 16:20:40 +0100239 systemGroup.addItem(new KeyboardShortcutInfo(
Clara Bayarrib9057df2016-03-02 11:37:09 -0800240 mContext.getString(R.string.keyboard_shortcut_group_system_home),
241 KeyEvent.KEYCODE_ENTER, KeyEvent.META_META_ON));
Clara Bayarri75e09792015-07-29 16:20:40 +0100242 systemGroup.addItem(new KeyboardShortcutInfo(
Clara Bayarrib9057df2016-03-02 11:37:09 -0800243 mContext.getString(R.string.keyboard_shortcut_group_system_back),
244 KeyEvent.KEYCODE_DEL, KeyEvent.META_META_ON));
Clara Bayarri75e09792015-07-29 16:20:40 +0100245 systemGroup.addItem(new KeyboardShortcutInfo(
Clara Bayarrib9057df2016-03-02 11:37:09 -0800246 mContext.getString(R.string.keyboard_shortcut_group_system_recents),
247 KeyEvent.KEYCODE_TAB, KeyEvent.META_ALT_ON));
Clara Bayarri1d648a12016-03-23 17:09:02 +0000248 systemGroup.addItem(new KeyboardShortcutInfo(
249 mContext.getString(
250 R.string.keyboard_shortcut_group_system_notifications),
251 KeyEvent.KEYCODE_N, KeyEvent.META_META_ON));
252 systemGroup.addItem(new KeyboardShortcutInfo(
253 mContext.getString(
254 R.string.keyboard_shortcut_group_system_shortcuts_helper),
255 KeyEvent.KEYCODE_SLASH, KeyEvent.META_META_ON));
256 systemGroup.addItem(new KeyboardShortcutInfo(
257 mContext.getString(
258 R.string.keyboard_shortcut_group_system_switch_input),
259 KeyEvent.KEYCODE_SPACE, KeyEvent.META_META_ON));
Clara Bayarri75e09792015-07-29 16:20:40 +0100260 result.add(systemGroup);
Clara Bayarri1d648a12016-03-23 17:09:02 +0000261
262 KeyboardShortcutGroup applicationGroup = new KeyboardShortcutGroup(
263 mContext.getString(R.string.keyboard_shortcut_group_applications),
264 true);
265 applicationGroup.addItem(new KeyboardShortcutInfo(
266 mContext.getString(
267 R.string.keyboard_shortcut_group_applications_assist),
268 KeyEvent.KEYCODE_UNKNOWN, KeyEvent.META_META_ON));
269 applicationGroup.addItem(new KeyboardShortcutInfo(
270 mContext.getString(
271 R.string.keyboard_shortcut_group_applications_browser),
272 KeyEvent.KEYCODE_B, KeyEvent.META_META_ON));
273 applicationGroup.addItem(new KeyboardShortcutInfo(
274 mContext.getString(
275 R.string.keyboard_shortcut_group_applications_contacts),
276 KeyEvent.KEYCODE_C, KeyEvent.META_META_ON));
277 applicationGroup.addItem(new KeyboardShortcutInfo(
278 mContext.getString(
279 R.string.keyboard_shortcut_group_applications_email),
280 KeyEvent.KEYCODE_E, KeyEvent.META_META_ON));
281 applicationGroup.addItem(new KeyboardShortcutInfo(
282 mContext.getString(
283 R.string.keyboard_shortcut_group_applications_im),
284 KeyEvent.KEYCODE_T, KeyEvent.META_META_ON));
285 applicationGroup.addItem(new KeyboardShortcutInfo(
286 mContext.getString(
287 R.string.keyboard_shortcut_group_applications_music),
288 KeyEvent.KEYCODE_P, KeyEvent.META_META_ON));
289 applicationGroup.addItem(new KeyboardShortcutInfo(
290 mContext.getString(
291 R.string.keyboard_shortcut_group_applications_youtube),
292 KeyEvent.KEYCODE_Y, KeyEvent.META_META_ON));
293 applicationGroup.addItem(new KeyboardShortcutInfo(
294 mContext.getString(
295 R.string.keyboard_shortcut_group_applications_calendar),
296 KeyEvent.KEYCODE_L, KeyEvent.META_META_ON));
297 result.add(applicationGroup);
298
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000299 showKeyboardShortcutsDialog(result);
Clara Bayarri75e09792015-07-29 16:20:40 +0100300 }
Clara Bayarrifcd7e802016-03-10 12:58:18 +0000301 }, deviceId);
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100302 } else {
303 dismissKeyboardShortcutsDialog();
304 }
305 }
306
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100307 public void dismissKeyboardShortcutsDialog() {
308 if (mKeyboardShortcutsDialog != null) {
309 mKeyboardShortcutsDialog.dismiss();
310 mKeyboardShortcutsDialog = null;
311 }
312 }
313
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000314 private void showKeyboardShortcutsDialog(
315 final List<KeyboardShortcutGroup> keyboardShortcutGroups) {
316 // Need to post on the main thread.
317 mHandler.post(new Runnable() {
318 @Override
319 public void run() {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000320 handleShowKeyboardShortcuts(keyboardShortcutGroups);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000321 }
322 });
323 }
324
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000325 private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) {
326 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
327 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
328 LAYOUT_INFLATER_SERVICE);
329 final View keyboardShortcutsView = inflater.inflate(
330 R.layout.keyboard_shortcuts_view, null);
331 populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById(
332 R.id.keyboard_shortcuts_container), keyboardShortcutGroups);
333 dialogBuilder.setView(keyboardShortcutsView);
334 dialogBuilder.setPositiveButton(R.string.quick_settings_done, dialogCloseListener);
335 mKeyboardShortcutsDialog = dialogBuilder.create();
336 mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true);
337 Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow();
338 keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG);
339 mKeyboardShortcutsDialog.show();
340 }
341
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000342 private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout,
343 List<KeyboardShortcutGroup> keyboardShortcutGroups) {
344 LayoutInflater inflater = LayoutInflater.from(mContext);
345 final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size();
346 for (int i = 0; i < keyboardShortcutGroupsSize; i++) {
347 KeyboardShortcutGroup group = keyboardShortcutGroups.get(i);
348 TextView categoryTitle = (TextView) inflater.inflate(
349 R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false);
350 categoryTitle.setText(group.getLabel());
351 categoryTitle.setTextColor(group.isSystemGroup()
352 ? mContext.getColor(R.color.ksh_system_group_color)
353 : mContext.getColor(R.color.ksh_application_group_color));
354 keyboardShortcutsLayout.addView(categoryTitle);
355
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000356 LinearLayout shortcutContainer = (LinearLayout) inflater.inflate(
357 R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000358 final int itemsSize = group.getItems().size();
359 for (int j = 0; j < itemsSize; j++) {
360 KeyboardShortcutInfo info = group.getItems().get(j);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800361 if (info.getKeycode() != KeyEvent.KEYCODE_UNKNOWN
362 && !KeyCharacterMap.deviceHasKey(info.getKeycode())) {
363 // The user can't achieve this shortcut, so skipping.
364 Log.w(TAG, "Keyboard Shortcut contains key not on device, skipping.");
365 continue;
366 }
367 List<String> shortcutKeys = getHumanReadableShortcutKeys(info);
368 if (shortcutKeys == null) {
369 // Ignore shortcuts we can't display keys for.
370 Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping.");
371 continue;
372 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000373 View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item,
374 shortcutContainer, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000375 TextView textView = (TextView) shortcutView
376 .findViewById(R.id.keyboard_shortcuts_keyword);
377 textView.setText(info.getLabel());
378
Andrei Stingaceanu844927d2016-02-16 14:31:58 +0000379 ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000380 .findViewById(R.id.keyboard_shortcuts_item_container);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000381 final int shortcutKeysSize = shortcutKeys.size();
382 for (int k = 0; k < shortcutKeysSize; k++) {
383 String shortcutKey = shortcutKeys.get(k);
384 TextView shortcutKeyView = (TextView) inflater.inflate(
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000385 R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000386 shortcutKeyView.setText(shortcutKey);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000387 shortcutItemsContainer.addView(shortcutKeyView);
388 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000389 shortcutContainer.addView(shortcutView);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000390 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000391 keyboardShortcutsLayout.addView(shortcutContainer);
392 if (i < keyboardShortcutGroupsSize - 1) {
393 View separator = inflater.inflate(
394 R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout,
395 false);
396 keyboardShortcutsLayout.addView(separator);
397 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000398 }
399 }
400
401 private List<String> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) {
Clara Bayarrib9057df2016-03-02 11:37:09 -0800402 List<String> shortcutKeys = getHumanReadableModifiers(info);
403 if (shortcutKeys == null) {
404 return null;
405 }
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800406 String displayLabelString;
Clara Bayarri1d648a12016-03-23 17:09:02 +0000407 if (info.getBaseCharacter() > Character.MIN_VALUE) {
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800408 displayLabelString = String.valueOf(info.getBaseCharacter());
409 } else if (SPECIAL_CHARACTER_NAMES.get(info.getKeycode()) != null) {
410 displayLabelString = SPECIAL_CHARACTER_NAMES.get(info.getKeycode());
411 } else {
Clara Bayarri1d648a12016-03-23 17:09:02 +0000412 // Special case for shortcuts with no base key or keycode.
413 if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) {
414 return shortcutKeys;
415 }
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800416 // TODO: Have a generic map for when we don't have the device's.
417 char displayLabel = mKeyCharacterMap == null
418 ? 0 : mKeyCharacterMap.getDisplayLabel(info.getKeycode());
419 if (displayLabel != 0) {
420 displayLabelString = String.valueOf(displayLabel);
421 } else {
422 return null;
423 }
424 }
425 shortcutKeys.add(displayLabelString.toUpperCase());
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000426 return shortcutKeys;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100427 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800428
429 private List<String> getHumanReadableModifiers(KeyboardShortcutInfo info) {
430 final List<String> shortcutKeys = new ArrayList<>();
431 int modifiers = info.getModifiers();
432 if (modifiers == 0) {
433 return shortcutKeys;
434 }
435 for(int i = 0; i < MODIFIER_NAMES.size(); ++i) {
436 final int supportedModifier = MODIFIER_NAMES.keyAt(i);
437 if ((modifiers & supportedModifier) != 0) {
438 shortcutKeys.add(MODIFIER_NAMES.get(supportedModifier).toUpperCase());
439 modifiers &= ~supportedModifier;
440 }
441 }
442 if (modifiers != 0) {
443 // Remaining unsupported modifiers, don't show anything.
444 return null;
445 }
446 return shortcutKeys;
447 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100448}