blob: 977a77d595599e256e266deff32e3b105a09d489 [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;
Andrei Stingaceanud1519102016-03-31 15:53:33 +010024import android.graphics.Bitmap;
25import android.graphics.Canvas;
26import android.graphics.drawable.Drawable;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080027import android.hardware.input.InputManager;
Clara Bayarri75e09792015-07-29 16:20:40 +010028import android.os.Handler;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000029import android.os.Looper;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080030import android.util.Log;
31import android.util.SparseArray;
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +000032import android.view.ContextThemeWrapper;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080033import android.view.InputDevice;
34import android.view.KeyCharacterMap;
Clara Bayarri75e09792015-07-29 16:20:40 +010035import android.view.KeyEvent;
36import android.view.KeyboardShortcutGroup;
37import android.view.KeyboardShortcutInfo;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010038import android.view.LayoutInflater;
39import android.view.View;
Andrei Stingaceanu844927d2016-02-16 14:31:58 +000040import android.view.ViewGroup;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010041import android.view.Window;
Clara Bayarri75e09792015-07-29 16:20:40 +010042import android.view.WindowManager.KeyboardShortcutsReceiver;
Andrei Stingaceanud1519102016-03-31 15:53:33 +010043import android.widget.ImageView;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000044import android.widget.LinearLayout;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000045import android.widget.TextView;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010046
47import com.android.systemui.R;
Clara Bayarri75e09792015-07-29 16:20:40 +010048import com.android.systemui.recents.Recents;
49
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000050import java.util.ArrayList;
Clara Bayarri75e09792015-07-29 16:20:40 +010051import java.util.List;
52
53import static android.content.Context.LAYOUT_INFLATER_SERVICE;
Clara Bayarri75e09792015-07-29 16:20:40 +010054import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010055
56/**
57 * Contains functionality for handling keyboard shortcuts.
58 */
Andrei Stingaceanud1519102016-03-31 15:53:33 +010059public final class KeyboardShortcuts {
Clara Bayarri4e850ff2016-03-02 11:12:32 -080060 private static final String TAG = KeyboardShortcuts.class.getSimpleName();
Clara Bayarri4e850ff2016-03-02 11:12:32 -080061
Clara Bayarrib999af52016-04-06 16:02:35 +010062 private final SparseArray<String> mSpecialCharacterNames = new SparseArray<>();
63 private final SparseArray<String> mModifierNames = new SparseArray<>();
64 private final SparseArray<Drawable> mSpecialCharacterDrawables = new SparseArray<>();
65 private final SparseArray<Drawable> mModifierDrawables = new SparseArray<>();
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000066
67 private final Handler mHandler = new Handler(Looper.getMainLooper());
68 private final Context mContext;
69 private final OnClickListener dialogCloseListener = new DialogInterface.OnClickListener() {
70 public void onClick(DialogInterface dialog, int id) {
71 dismissKeyboardShortcutsDialog();
72 }
73 };
Clara Bayarri75e09792015-07-29 16:20:40 +010074
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010075 private Dialog mKeyboardShortcutsDialog;
Clara Bayarri4e850ff2016-03-02 11:12:32 -080076 private KeyCharacterMap mKeyCharacterMap;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010077
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000078 public KeyboardShortcuts(Context context) {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +000079 this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_Material_Light);
Clara Bayarrib999af52016-04-06 16:02:35 +010080 loadResources(context);
81 }
82
83 private void loadResources(Context context) {
84 mSpecialCharacterNames.put(
85 KeyEvent.KEYCODE_HOME, context.getString(R.string.keyboard_key_home));
86 mSpecialCharacterNames.put(
87 KeyEvent.KEYCODE_BACK, context.getString(R.string.keyboard_key_back));
88 mSpecialCharacterNames.put(
89 KeyEvent.KEYCODE_DPAD_UP, context.getString(R.string.keyboard_key_dpad_up));
90 mSpecialCharacterNames.put(
91 KeyEvent.KEYCODE_DPAD_DOWN, context.getString(R.string.keyboard_key_dpad_down));
92 mSpecialCharacterNames.put(
93 KeyEvent.KEYCODE_DPAD_LEFT, context.getString(R.string.keyboard_key_dpad_left));
94 mSpecialCharacterNames.put(
95 KeyEvent.KEYCODE_DPAD_RIGHT, context.getString(R.string.keyboard_key_dpad_right));
96 mSpecialCharacterNames.put(
97 KeyEvent.KEYCODE_DPAD_CENTER, context.getString(R.string.keyboard_key_dpad_center));
98 mSpecialCharacterNames.put(KeyEvent.KEYCODE_PERIOD, ".");
99 mSpecialCharacterNames.put(
100 KeyEvent.KEYCODE_TAB, context.getString(R.string.keyboard_key_tab));
101 mSpecialCharacterNames.put(
102 KeyEvent.KEYCODE_SPACE, context.getString(R.string.keyboard_key_space));
103 mSpecialCharacterNames.put(
104 KeyEvent.KEYCODE_ENTER, context.getString(R.string.keyboard_key_enter));
105 mSpecialCharacterNames.put(
106 KeyEvent.KEYCODE_DEL, context.getString(R.string.keyboard_key_backspace));
107 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE,
108 context.getString(R.string.keyboard_key_media_play_pause));
109 mSpecialCharacterNames.put(
110 KeyEvent.KEYCODE_MEDIA_STOP, context.getString(R.string.keyboard_key_media_stop));
111 mSpecialCharacterNames.put(
112 KeyEvent.KEYCODE_MEDIA_NEXT, context.getString(R.string.keyboard_key_media_next));
113 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PREVIOUS,
114 context.getString(R.string.keyboard_key_media_previous));
115 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_REWIND,
116 context.getString(R.string.keyboard_key_media_rewind));
117 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD,
118 context.getString(R.string.keyboard_key_media_fast_forward));
119 mSpecialCharacterNames.put(
120 KeyEvent.KEYCODE_PAGE_UP, context.getString(R.string.keyboard_key_page_up));
121 mSpecialCharacterNames.put(
122 KeyEvent.KEYCODE_PAGE_DOWN, context.getString(R.string.keyboard_key_page_down));
123 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_A,
124 context.getString(R.string.keyboard_key_button_template, "A"));
125 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_B,
126 context.getString(R.string.keyboard_key_button_template, "B"));
127 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_C,
128 context.getString(R.string.keyboard_key_button_template, "C"));
129 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_X,
130 context.getString(R.string.keyboard_key_button_template, "X"));
131 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Y,
132 context.getString(R.string.keyboard_key_button_template, "Y"));
133 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Z,
134 context.getString(R.string.keyboard_key_button_template, "Z"));
135 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L1,
136 context.getString(R.string.keyboard_key_button_template, "L1"));
137 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R1,
138 context.getString(R.string.keyboard_key_button_template, "R1"));
139 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L2,
140 context.getString(R.string.keyboard_key_button_template, "L2"));
141 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R2,
142 context.getString(R.string.keyboard_key_button_template, "R2"));
143 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_START,
144 context.getString(R.string.keyboard_key_button_template, "Start"));
145 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_SELECT,
146 context.getString(R.string.keyboard_key_button_template, "Select"));
147 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_MODE,
148 context.getString(R.string.keyboard_key_button_template, "Mode"));
149 mSpecialCharacterNames.put(
150 KeyEvent.KEYCODE_FORWARD_DEL, context.getString(R.string.keyboard_key_forward_del));
151 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ESCAPE, "Esc");
152 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SYSRQ, "SysRq");
153 mSpecialCharacterNames.put(KeyEvent.KEYCODE_BREAK, "Break");
154 mSpecialCharacterNames.put(KeyEvent.KEYCODE_SCROLL_LOCK, "Scroll Lock");
155 mSpecialCharacterNames.put(
156 KeyEvent.KEYCODE_MOVE_HOME, context.getString(R.string.keyboard_key_move_home));
157 mSpecialCharacterNames.put(
158 KeyEvent.KEYCODE_MOVE_END, context.getString(R.string.keyboard_key_move_end));
159 mSpecialCharacterNames.put(
160 KeyEvent.KEYCODE_INSERT, context.getString(R.string.keyboard_key_insert));
161 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F1, "F1");
162 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F2, "F2");
163 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F3, "F3");
164 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F4, "F4");
165 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F5, "F5");
166 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F6, "F6");
167 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F7, "F7");
168 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F8, "F8");
169 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F9, "F9");
170 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F10, "F10");
171 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F11, "F11");
172 mSpecialCharacterNames.put(KeyEvent.KEYCODE_F12, "F12");
173 mSpecialCharacterNames.put(
174 KeyEvent.KEYCODE_NUM_LOCK, context.getString(R.string.keyboard_key_num_lock));
175 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_0,
176 context.getString(R.string.keyboard_key_numpad_template, "0"));
177 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_1,
178 context.getString(R.string.keyboard_key_numpad_template, "1"));
179 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_2,
180 context.getString(R.string.keyboard_key_numpad_template, "2"));
181 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_3,
182 context.getString(R.string.keyboard_key_numpad_template, "3"));
183 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_4,
184 context.getString(R.string.keyboard_key_numpad_template, "4"));
185 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_5,
186 context.getString(R.string.keyboard_key_numpad_template, "5"));
187 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_6,
188 context.getString(R.string.keyboard_key_numpad_template, "6"));
189 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_7,
190 context.getString(R.string.keyboard_key_numpad_template, "7"));
191 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_8,
192 context.getString(R.string.keyboard_key_numpad_template, "8"));
193 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_9,
194 context.getString(R.string.keyboard_key_numpad_template, "9"));
195 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DIVIDE,
196 context.getString(R.string.keyboard_key_numpad_template, "/"));
197 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_MULTIPLY,
198 context.getString(R.string.keyboard_key_numpad_template, "*"));
199 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_SUBTRACT,
200 context.getString(R.string.keyboard_key_numpad_template, "-"));
201 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ADD,
202 context.getString(R.string.keyboard_key_numpad_template, "+"));
203 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DOT,
204 context.getString(R.string.keyboard_key_numpad_template, "."));
205 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_COMMA,
206 context.getString(R.string.keyboard_key_numpad_template, ","));
207 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ENTER,
208 context.getString(R.string.keyboard_key_numpad_template,
209 context.getString(R.string.keyboard_key_enter)));
210 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_EQUALS,
211 context.getString(R.string.keyboard_key_numpad_template, "="));
212 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN,
213 context.getString(R.string.keyboard_key_numpad_template, "("));
214 mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN,
215 context.getString(R.string.keyboard_key_numpad_template, ")"));
216 mSpecialCharacterNames.put(KeyEvent.KEYCODE_ZENKAKU_HANKAKU, "半角/全角");
217 mSpecialCharacterNames.put(KeyEvent.KEYCODE_EISU, "英数");
218 mSpecialCharacterNames.put(KeyEvent.KEYCODE_MUHENKAN, "無変換");
219 mSpecialCharacterNames.put(KeyEvent.KEYCODE_HENKAN, "変換");
220 mSpecialCharacterNames.put(KeyEvent.KEYCODE_KATAKANA_HIRAGANA, "かな");
221
222 mModifierNames.put(KeyEvent.META_META_ON, "Meta");
223 mModifierNames.put(KeyEvent.META_CTRL_ON, "Ctrl");
224 mModifierNames.put(KeyEvent.META_ALT_ON, "Alt");
225 mModifierNames.put(KeyEvent.META_SHIFT_ON, "Shift");
226 mModifierNames.put(KeyEvent.META_SYM_ON, "Sym");
227 mModifierNames.put(KeyEvent.META_FUNCTION_ON, "Fn");
228
229 mSpecialCharacterDrawables.put(
230 KeyEvent.KEYCODE_DEL, context.getDrawable(R.drawable.ic_ksh_key_backspace));
231 mSpecialCharacterDrawables.put(
232 KeyEvent.KEYCODE_ENTER, context.getDrawable(R.drawable.ic_ksh_key_enter));
233 mSpecialCharacterDrawables.put(
234 KeyEvent.KEYCODE_DPAD_UP, context.getDrawable(R.drawable.ic_ksh_key_up));
235 mSpecialCharacterDrawables.put(
236 KeyEvent.KEYCODE_DPAD_RIGHT, context.getDrawable(R.drawable.ic_ksh_key_right));
237 mSpecialCharacterDrawables.put(
238 KeyEvent.KEYCODE_DPAD_DOWN, context.getDrawable(R.drawable.ic_ksh_key_down));
239 mSpecialCharacterDrawables.put(
240 KeyEvent.KEYCODE_DPAD_LEFT, context.getDrawable(R.drawable.ic_ksh_key_left));
241
242 mModifierDrawables.put(
243 KeyEvent.META_META_ON, context.getDrawable(R.drawable.ic_ksh_key_meta));
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000244 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100245
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800246 public void toggleKeyboardShortcuts(int deviceId) {
247 InputDevice inputDevice = InputManager.getInstance().getInputDevice(deviceId);
248 if (inputDevice != null) {
249 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
250 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100251 if (mKeyboardShortcutsDialog == null) {
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000252 Recents.getSystemServices().requestKeyboardShortcuts(mContext,
Clara Bayarri75e09792015-07-29 16:20:40 +0100253 new KeyboardShortcutsReceiver() {
254 @Override
255 public void onKeyboardShortcutsReceived(
256 final List<KeyboardShortcutGroup> result) {
257 KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup(
Clara Bayarrib9057df2016-03-02 11:37:09 -0800258 mContext.getString(R.string.keyboard_shortcut_group_system), true);
Clara Bayarri75e09792015-07-29 16:20:40 +0100259 systemGroup.addItem(new KeyboardShortcutInfo(
Clara Bayarrib9057df2016-03-02 11:37:09 -0800260 mContext.getString(R.string.keyboard_shortcut_group_system_home),
261 KeyEvent.KEYCODE_ENTER, KeyEvent.META_META_ON));
Clara Bayarri75e09792015-07-29 16:20:40 +0100262 systemGroup.addItem(new KeyboardShortcutInfo(
Clara Bayarrib9057df2016-03-02 11:37:09 -0800263 mContext.getString(R.string.keyboard_shortcut_group_system_back),
264 KeyEvent.KEYCODE_DEL, KeyEvent.META_META_ON));
Clara Bayarri75e09792015-07-29 16:20:40 +0100265 systemGroup.addItem(new KeyboardShortcutInfo(
Clara Bayarrib9057df2016-03-02 11:37:09 -0800266 mContext.getString(R.string.keyboard_shortcut_group_system_recents),
267 KeyEvent.KEYCODE_TAB, KeyEvent.META_ALT_ON));
Clara Bayarri1d648a12016-03-23 17:09:02 +0000268 systemGroup.addItem(new KeyboardShortcutInfo(
269 mContext.getString(
270 R.string.keyboard_shortcut_group_system_notifications),
271 KeyEvent.KEYCODE_N, KeyEvent.META_META_ON));
272 systemGroup.addItem(new KeyboardShortcutInfo(
273 mContext.getString(
274 R.string.keyboard_shortcut_group_system_shortcuts_helper),
275 KeyEvent.KEYCODE_SLASH, KeyEvent.META_META_ON));
276 systemGroup.addItem(new KeyboardShortcutInfo(
277 mContext.getString(
278 R.string.keyboard_shortcut_group_system_switch_input),
279 KeyEvent.KEYCODE_SPACE, KeyEvent.META_META_ON));
Clara Bayarri75e09792015-07-29 16:20:40 +0100280 result.add(systemGroup);
Clara Bayarri1d648a12016-03-23 17:09:02 +0000281
282 KeyboardShortcutGroup applicationGroup = new KeyboardShortcutGroup(
283 mContext.getString(R.string.keyboard_shortcut_group_applications),
284 true);
285 applicationGroup.addItem(new KeyboardShortcutInfo(
286 mContext.getString(
287 R.string.keyboard_shortcut_group_applications_assist),
288 KeyEvent.KEYCODE_UNKNOWN, KeyEvent.META_META_ON));
289 applicationGroup.addItem(new KeyboardShortcutInfo(
290 mContext.getString(
291 R.string.keyboard_shortcut_group_applications_browser),
292 KeyEvent.KEYCODE_B, KeyEvent.META_META_ON));
293 applicationGroup.addItem(new KeyboardShortcutInfo(
294 mContext.getString(
295 R.string.keyboard_shortcut_group_applications_contacts),
296 KeyEvent.KEYCODE_C, KeyEvent.META_META_ON));
297 applicationGroup.addItem(new KeyboardShortcutInfo(
298 mContext.getString(
299 R.string.keyboard_shortcut_group_applications_email),
300 KeyEvent.KEYCODE_E, KeyEvent.META_META_ON));
301 applicationGroup.addItem(new KeyboardShortcutInfo(
302 mContext.getString(
303 R.string.keyboard_shortcut_group_applications_im),
304 KeyEvent.KEYCODE_T, KeyEvent.META_META_ON));
305 applicationGroup.addItem(new KeyboardShortcutInfo(
306 mContext.getString(
307 R.string.keyboard_shortcut_group_applications_music),
308 KeyEvent.KEYCODE_P, KeyEvent.META_META_ON));
309 applicationGroup.addItem(new KeyboardShortcutInfo(
310 mContext.getString(
311 R.string.keyboard_shortcut_group_applications_youtube),
312 KeyEvent.KEYCODE_Y, KeyEvent.META_META_ON));
313 applicationGroup.addItem(new KeyboardShortcutInfo(
314 mContext.getString(
315 R.string.keyboard_shortcut_group_applications_calendar),
316 KeyEvent.KEYCODE_L, KeyEvent.META_META_ON));
317 result.add(applicationGroup);
318
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000319 showKeyboardShortcutsDialog(result);
Clara Bayarri75e09792015-07-29 16:20:40 +0100320 }
Clara Bayarrifcd7e802016-03-10 12:58:18 +0000321 }, deviceId);
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100322 } else {
323 dismissKeyboardShortcutsDialog();
324 }
325 }
326
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100327 public void dismissKeyboardShortcutsDialog() {
328 if (mKeyboardShortcutsDialog != null) {
329 mKeyboardShortcutsDialog.dismiss();
330 mKeyboardShortcutsDialog = null;
331 }
332 }
333
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000334 private void showKeyboardShortcutsDialog(
335 final List<KeyboardShortcutGroup> keyboardShortcutGroups) {
336 // Need to post on the main thread.
337 mHandler.post(new Runnable() {
338 @Override
339 public void run() {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000340 handleShowKeyboardShortcuts(keyboardShortcutGroups);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000341 }
342 });
343 }
344
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000345 private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) {
346 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
347 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
348 LAYOUT_INFLATER_SERVICE);
349 final View keyboardShortcutsView = inflater.inflate(
350 R.layout.keyboard_shortcuts_view, null);
351 populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById(
352 R.id.keyboard_shortcuts_container), keyboardShortcutGroups);
353 dialogBuilder.setView(keyboardShortcutsView);
354 dialogBuilder.setPositiveButton(R.string.quick_settings_done, dialogCloseListener);
355 mKeyboardShortcutsDialog = dialogBuilder.create();
356 mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true);
357 Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow();
358 keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG);
359 mKeyboardShortcutsDialog.show();
360 }
361
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000362 private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout,
363 List<KeyboardShortcutGroup> keyboardShortcutGroups) {
364 LayoutInflater inflater = LayoutInflater.from(mContext);
365 final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size();
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100366 // Needed to be able to scale the image items to the same height as the text items.
367 final int shortcutTextItemHeight = getShortcutTextItemHeight(inflater);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000368 for (int i = 0; i < keyboardShortcutGroupsSize; i++) {
369 KeyboardShortcutGroup group = keyboardShortcutGroups.get(i);
370 TextView categoryTitle = (TextView) inflater.inflate(
371 R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false);
372 categoryTitle.setText(group.getLabel());
373 categoryTitle.setTextColor(group.isSystemGroup()
374 ? mContext.getColor(R.color.ksh_system_group_color)
375 : mContext.getColor(R.color.ksh_application_group_color));
376 keyboardShortcutsLayout.addView(categoryTitle);
377
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000378 LinearLayout shortcutContainer = (LinearLayout) inflater.inflate(
379 R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000380 final int itemsSize = group.getItems().size();
381 for (int j = 0; j < itemsSize; j++) {
382 KeyboardShortcutInfo info = group.getItems().get(j);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800383 if (info.getKeycode() != KeyEvent.KEYCODE_UNKNOWN
384 && !KeyCharacterMap.deviceHasKey(info.getKeycode())) {
385 // The user can't achieve this shortcut, so skipping.
386 Log.w(TAG, "Keyboard Shortcut contains key not on device, skipping.");
387 continue;
388 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100389 List<StringOrDrawable> shortcutKeys = getHumanReadableShortcutKeys(info);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800390 if (shortcutKeys == null) {
391 // Ignore shortcuts we can't display keys for.
392 Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping.");
393 continue;
394 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000395 View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item,
396 shortcutContainer, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000397 TextView textView = (TextView) shortcutView
398 .findViewById(R.id.keyboard_shortcuts_keyword);
399 textView.setText(info.getLabel());
400
Andrei Stingaceanu844927d2016-02-16 14:31:58 +0000401 ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000402 .findViewById(R.id.keyboard_shortcuts_item_container);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000403 final int shortcutKeysSize = shortcutKeys.size();
404 for (int k = 0; k < shortcutKeysSize; k++) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100405 StringOrDrawable shortcutRepresentation = shortcutKeys.get(k);
406 if (shortcutRepresentation.drawable != null) {
407 ImageView shortcutKeyIconView = (ImageView) inflater.inflate(
408 R.layout.keyboard_shortcuts_key_icon_view, shortcutItemsContainer,
409 false);
410 Bitmap bitmap = Bitmap.createBitmap(shortcutTextItemHeight,
411 shortcutTextItemHeight, Bitmap.Config.ARGB_8888);
412 Canvas canvas = new Canvas(bitmap);
413 shortcutRepresentation.drawable.setBounds(0, 0, canvas.getWidth(),
414 canvas.getHeight());
415 shortcutRepresentation.drawable.draw(canvas);
416 shortcutKeyIconView.setImageBitmap(bitmap);
417 shortcutItemsContainer.addView(shortcutKeyIconView);
418 } else if (shortcutRepresentation.string != null) {
419 TextView shortcutKeyTextView = (TextView) inflater.inflate(
420 R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer,
421 false);
422 shortcutKeyTextView.setText(shortcutRepresentation.string);
423 shortcutItemsContainer.addView(shortcutKeyTextView);
424 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000425 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000426 shortcutContainer.addView(shortcutView);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000427 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000428 keyboardShortcutsLayout.addView(shortcutContainer);
429 if (i < keyboardShortcutGroupsSize - 1) {
430 View separator = inflater.inflate(
431 R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout,
432 false);
433 keyboardShortcutsLayout.addView(separator);
434 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000435 }
436 }
437
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100438 private int getShortcutTextItemHeight(LayoutInflater inflater) {
439 TextView shortcutKeyTextView = (TextView) inflater.inflate(
440 R.layout.keyboard_shortcuts_key_view, null, false);
441 shortcutKeyTextView.measure(
442 View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
443 return shortcutKeyTextView.getMeasuredHeight()
444 - shortcutKeyTextView.getPaddingTop()
445 - shortcutKeyTextView.getPaddingBottom();
446 }
447
448 private List<StringOrDrawable> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) {
449 List<StringOrDrawable> shortcutKeys = getHumanReadableModifiers(info);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800450 if (shortcutKeys == null) {
451 return null;
452 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100453 String displayLabelString = null;
454 Drawable displayLabelDrawable = null;
Clara Bayarri1d648a12016-03-23 17:09:02 +0000455 if (info.getBaseCharacter() > Character.MIN_VALUE) {
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800456 displayLabelString = String.valueOf(info.getBaseCharacter());
Clara Bayarrib999af52016-04-06 16:02:35 +0100457 } else if (mSpecialCharacterDrawables.get(info.getKeycode()) != null) {
458 displayLabelDrawable = mSpecialCharacterDrawables.get(info.getKeycode());
459 } else if (mSpecialCharacterNames.get(info.getKeycode()) != null) {
460 displayLabelString = mSpecialCharacterNames.get(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800461 } else {
Clara Bayarri1d648a12016-03-23 17:09:02 +0000462 // Special case for shortcuts with no base key or keycode.
463 if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) {
464 return shortcutKeys;
465 }
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800466 // TODO: Have a generic map for when we don't have the device's.
467 char displayLabel = mKeyCharacterMap == null
468 ? 0 : mKeyCharacterMap.getDisplayLabel(info.getKeycode());
469 if (displayLabel != 0) {
470 displayLabelString = String.valueOf(displayLabel);
471 } else {
472 return null;
473 }
474 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100475
476 if (displayLabelDrawable != null) {
477 shortcutKeys.add(new StringOrDrawable(displayLabelDrawable));
478 } else if (displayLabelString != null) {
479 shortcutKeys.add(new StringOrDrawable(displayLabelString.toUpperCase()));
480 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000481 return shortcutKeys;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100482 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800483
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100484 private List<StringOrDrawable> getHumanReadableModifiers(KeyboardShortcutInfo info) {
485 final List<StringOrDrawable> shortcutKeys = new ArrayList<>();
Clara Bayarrib9057df2016-03-02 11:37:09 -0800486 int modifiers = info.getModifiers();
487 if (modifiers == 0) {
488 return shortcutKeys;
489 }
Clara Bayarrib999af52016-04-06 16:02:35 +0100490 for(int i = 0; i < mModifierNames.size(); ++i) {
491 final int supportedModifier = mModifierNames.keyAt(i);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800492 if ((modifiers & supportedModifier) != 0) {
Clara Bayarrib999af52016-04-06 16:02:35 +0100493 if (mModifierDrawables.get(supportedModifier) != null) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100494 shortcutKeys.add(new StringOrDrawable(
Clara Bayarrib999af52016-04-06 16:02:35 +0100495 mModifierDrawables.get(supportedModifier)));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100496 } else {
497 shortcutKeys.add(new StringOrDrawable(
Clara Bayarrib999af52016-04-06 16:02:35 +0100498 mModifierNames.get(supportedModifier).toUpperCase()));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100499 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800500 modifiers &= ~supportedModifier;
501 }
502 }
503 if (modifiers != 0) {
504 // Remaining unsupported modifiers, don't show anything.
505 return null;
506 }
507 return shortcutKeys;
508 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100509
510 private static final class StringOrDrawable {
511 public String string;
512 public Drawable drawable;
513
514 public StringOrDrawable(String string) {
515 this.string = string;
516 }
517
518 public StringOrDrawable(Drawable drawable) {
519 this.drawable = drawable;
520 }
521 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100522}