blob: 646efa2b45b0d7c0bdda04acdbe12446410373fe [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) {
Clara Bayarri03f19552016-04-06 10:59:52 +0100247 retrieveKeyCharacterMap(deviceId);
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100248 if (mKeyboardShortcutsDialog == null) {
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000249 Recents.getSystemServices().requestKeyboardShortcuts(mContext,
Clara Bayarri75e09792015-07-29 16:20:40 +0100250 new KeyboardShortcutsReceiver() {
251 @Override
252 public void onKeyboardShortcutsReceived(
253 final List<KeyboardShortcutGroup> result) {
254 KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup(
Clara Bayarrib9057df2016-03-02 11:37:09 -0800255 mContext.getString(R.string.keyboard_shortcut_group_system), true);
Clara Bayarri75e09792015-07-29 16:20:40 +0100256 systemGroup.addItem(new KeyboardShortcutInfo(
Clara Bayarrib9057df2016-03-02 11:37:09 -0800257 mContext.getString(R.string.keyboard_shortcut_group_system_home),
258 KeyEvent.KEYCODE_ENTER, KeyEvent.META_META_ON));
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_back),
261 KeyEvent.KEYCODE_DEL, 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_recents),
264 KeyEvent.KEYCODE_TAB, KeyEvent.META_ALT_ON));
Clara Bayarri1d648a12016-03-23 17:09:02 +0000265 systemGroup.addItem(new KeyboardShortcutInfo(
266 mContext.getString(
267 R.string.keyboard_shortcut_group_system_notifications),
268 KeyEvent.KEYCODE_N, KeyEvent.META_META_ON));
269 systemGroup.addItem(new KeyboardShortcutInfo(
270 mContext.getString(
271 R.string.keyboard_shortcut_group_system_shortcuts_helper),
272 KeyEvent.KEYCODE_SLASH, KeyEvent.META_META_ON));
273 systemGroup.addItem(new KeyboardShortcutInfo(
274 mContext.getString(
275 R.string.keyboard_shortcut_group_system_switch_input),
276 KeyEvent.KEYCODE_SPACE, KeyEvent.META_META_ON));
Clara Bayarri75e09792015-07-29 16:20:40 +0100277 result.add(systemGroup);
Clara Bayarri1d648a12016-03-23 17:09:02 +0000278
279 KeyboardShortcutGroup applicationGroup = new KeyboardShortcutGroup(
280 mContext.getString(R.string.keyboard_shortcut_group_applications),
281 true);
282 applicationGroup.addItem(new KeyboardShortcutInfo(
283 mContext.getString(
284 R.string.keyboard_shortcut_group_applications_assist),
285 KeyEvent.KEYCODE_UNKNOWN, KeyEvent.META_META_ON));
286 applicationGroup.addItem(new KeyboardShortcutInfo(
287 mContext.getString(
288 R.string.keyboard_shortcut_group_applications_browser),
289 KeyEvent.KEYCODE_B, KeyEvent.META_META_ON));
290 applicationGroup.addItem(new KeyboardShortcutInfo(
291 mContext.getString(
292 R.string.keyboard_shortcut_group_applications_contacts),
293 KeyEvent.KEYCODE_C, KeyEvent.META_META_ON));
294 applicationGroup.addItem(new KeyboardShortcutInfo(
295 mContext.getString(
296 R.string.keyboard_shortcut_group_applications_email),
297 KeyEvent.KEYCODE_E, KeyEvent.META_META_ON));
298 applicationGroup.addItem(new KeyboardShortcutInfo(
299 mContext.getString(
300 R.string.keyboard_shortcut_group_applications_im),
301 KeyEvent.KEYCODE_T, KeyEvent.META_META_ON));
302 applicationGroup.addItem(new KeyboardShortcutInfo(
303 mContext.getString(
304 R.string.keyboard_shortcut_group_applications_music),
305 KeyEvent.KEYCODE_P, KeyEvent.META_META_ON));
306 applicationGroup.addItem(new KeyboardShortcutInfo(
307 mContext.getString(
308 R.string.keyboard_shortcut_group_applications_youtube),
309 KeyEvent.KEYCODE_Y, KeyEvent.META_META_ON));
310 applicationGroup.addItem(new KeyboardShortcutInfo(
311 mContext.getString(
312 R.string.keyboard_shortcut_group_applications_calendar),
313 KeyEvent.KEYCODE_L, KeyEvent.META_META_ON));
314 result.add(applicationGroup);
315
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000316 showKeyboardShortcutsDialog(result);
Clara Bayarri75e09792015-07-29 16:20:40 +0100317 }
Clara Bayarrifcd7e802016-03-10 12:58:18 +0000318 }, deviceId);
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100319 } else {
320 dismissKeyboardShortcutsDialog();
321 }
322 }
323
Clara Bayarri03f19552016-04-06 10:59:52 +0100324 /**
325 * Retrieves a {@link KeyCharacterMap} and assigns it to mKeyCharacterMap. If the given id is an
326 * existing device, that device's map is used. Otherwise, it checks first all available devices
327 * and if there is a full keyboard it uses that map, otherwise falls back to the Virtual
328 * Keyboard with its default map.
329 */
330 private void retrieveKeyCharacterMap(int deviceId) {
331 final InputManager inputManager = InputManager.getInstance();
332 if (deviceId != -1) {
333 final InputDevice inputDevice = inputManager.getInputDevice(deviceId);
334 if (inputDevice != null) {
335 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
336 return;
337 }
338 }
339 final int[] deviceIds = inputManager.getInputDeviceIds();
340 for (int i = 0; i < deviceIds.length; ++i) {
341 final InputDevice inputDevice = inputManager.getInputDevice(deviceIds[i]);
342 // -1 is the Virtual Keyboard, with the default key map. Use that one only as last
343 // resort.
344 if (inputDevice.getId() != -1 && inputDevice.isFullKeyboard()) {
345 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
346 return;
347 }
348 }
349 final InputDevice inputDevice = inputManager.getInputDevice(-1);
350 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
351 }
352
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100353 public void dismissKeyboardShortcutsDialog() {
354 if (mKeyboardShortcutsDialog != null) {
355 mKeyboardShortcutsDialog.dismiss();
356 mKeyboardShortcutsDialog = null;
357 }
358 }
359
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000360 private void showKeyboardShortcutsDialog(
361 final List<KeyboardShortcutGroup> keyboardShortcutGroups) {
362 // Need to post on the main thread.
363 mHandler.post(new Runnable() {
364 @Override
365 public void run() {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000366 handleShowKeyboardShortcuts(keyboardShortcutGroups);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000367 }
368 });
369 }
370
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000371 private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) {
372 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
373 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
374 LAYOUT_INFLATER_SERVICE);
375 final View keyboardShortcutsView = inflater.inflate(
376 R.layout.keyboard_shortcuts_view, null);
377 populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById(
378 R.id.keyboard_shortcuts_container), keyboardShortcutGroups);
379 dialogBuilder.setView(keyboardShortcutsView);
380 dialogBuilder.setPositiveButton(R.string.quick_settings_done, dialogCloseListener);
381 mKeyboardShortcutsDialog = dialogBuilder.create();
382 mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true);
383 Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow();
384 keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG);
385 mKeyboardShortcutsDialog.show();
386 }
387
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000388 private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout,
389 List<KeyboardShortcutGroup> keyboardShortcutGroups) {
390 LayoutInflater inflater = LayoutInflater.from(mContext);
391 final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size();
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100392 // Needed to be able to scale the image items to the same height as the text items.
393 final int shortcutTextItemHeight = getShortcutTextItemHeight(inflater);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000394 for (int i = 0; i < keyboardShortcutGroupsSize; i++) {
395 KeyboardShortcutGroup group = keyboardShortcutGroups.get(i);
396 TextView categoryTitle = (TextView) inflater.inflate(
397 R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false);
398 categoryTitle.setText(group.getLabel());
399 categoryTitle.setTextColor(group.isSystemGroup()
400 ? mContext.getColor(R.color.ksh_system_group_color)
401 : mContext.getColor(R.color.ksh_application_group_color));
402 keyboardShortcutsLayout.addView(categoryTitle);
403
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000404 LinearLayout shortcutContainer = (LinearLayout) inflater.inflate(
405 R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000406 final int itemsSize = group.getItems().size();
407 for (int j = 0; j < itemsSize; j++) {
408 KeyboardShortcutInfo info = group.getItems().get(j);
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100409 List<StringOrDrawable> shortcutKeys = getHumanReadableShortcutKeys(info);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800410 if (shortcutKeys == null) {
411 // Ignore shortcuts we can't display keys for.
412 Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping.");
413 continue;
414 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000415 View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item,
416 shortcutContainer, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000417 TextView textView = (TextView) shortcutView
418 .findViewById(R.id.keyboard_shortcuts_keyword);
419 textView.setText(info.getLabel());
420
Andrei Stingaceanu844927d2016-02-16 14:31:58 +0000421 ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000422 .findViewById(R.id.keyboard_shortcuts_item_container);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000423 final int shortcutKeysSize = shortcutKeys.size();
424 for (int k = 0; k < shortcutKeysSize; k++) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100425 StringOrDrawable shortcutRepresentation = shortcutKeys.get(k);
426 if (shortcutRepresentation.drawable != null) {
427 ImageView shortcutKeyIconView = (ImageView) inflater.inflate(
428 R.layout.keyboard_shortcuts_key_icon_view, shortcutItemsContainer,
429 false);
430 Bitmap bitmap = Bitmap.createBitmap(shortcutTextItemHeight,
431 shortcutTextItemHeight, Bitmap.Config.ARGB_8888);
432 Canvas canvas = new Canvas(bitmap);
433 shortcutRepresentation.drawable.setBounds(0, 0, canvas.getWidth(),
434 canvas.getHeight());
435 shortcutRepresentation.drawable.draw(canvas);
436 shortcutKeyIconView.setImageBitmap(bitmap);
437 shortcutItemsContainer.addView(shortcutKeyIconView);
438 } else if (shortcutRepresentation.string != null) {
439 TextView shortcutKeyTextView = (TextView) inflater.inflate(
440 R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer,
441 false);
442 shortcutKeyTextView.setText(shortcutRepresentation.string);
443 shortcutItemsContainer.addView(shortcutKeyTextView);
444 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000445 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000446 shortcutContainer.addView(shortcutView);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000447 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000448 keyboardShortcutsLayout.addView(shortcutContainer);
449 if (i < keyboardShortcutGroupsSize - 1) {
450 View separator = inflater.inflate(
451 R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout,
452 false);
453 keyboardShortcutsLayout.addView(separator);
454 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000455 }
456 }
457
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100458 private int getShortcutTextItemHeight(LayoutInflater inflater) {
459 TextView shortcutKeyTextView = (TextView) inflater.inflate(
460 R.layout.keyboard_shortcuts_key_view, null, false);
461 shortcutKeyTextView.measure(
462 View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
463 return shortcutKeyTextView.getMeasuredHeight()
464 - shortcutKeyTextView.getPaddingTop()
465 - shortcutKeyTextView.getPaddingBottom();
466 }
467
468 private List<StringOrDrawable> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) {
469 List<StringOrDrawable> shortcutKeys = getHumanReadableModifiers(info);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800470 if (shortcutKeys == null) {
471 return null;
472 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100473 String displayLabelString = null;
474 Drawable displayLabelDrawable = null;
Clara Bayarri1d648a12016-03-23 17:09:02 +0000475 if (info.getBaseCharacter() > Character.MIN_VALUE) {
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800476 displayLabelString = String.valueOf(info.getBaseCharacter());
Clara Bayarrib999af52016-04-06 16:02:35 +0100477 } else if (mSpecialCharacterDrawables.get(info.getKeycode()) != null) {
478 displayLabelDrawable = mSpecialCharacterDrawables.get(info.getKeycode());
479 } else if (mSpecialCharacterNames.get(info.getKeycode()) != null) {
480 displayLabelString = mSpecialCharacterNames.get(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800481 } else {
Clara Bayarri1d648a12016-03-23 17:09:02 +0000482 // Special case for shortcuts with no base key or keycode.
483 if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) {
484 return shortcutKeys;
485 }
Clara Bayarri03f19552016-04-06 10:59:52 +0100486 char displayLabel = mKeyCharacterMap.getDisplayLabel(info.getKeycode());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800487 if (displayLabel != 0) {
488 displayLabelString = String.valueOf(displayLabel);
489 } else {
490 return null;
491 }
492 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100493
494 if (displayLabelDrawable != null) {
495 shortcutKeys.add(new StringOrDrawable(displayLabelDrawable));
496 } else if (displayLabelString != null) {
497 shortcutKeys.add(new StringOrDrawable(displayLabelString.toUpperCase()));
498 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000499 return shortcutKeys;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100500 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800501
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100502 private List<StringOrDrawable> getHumanReadableModifiers(KeyboardShortcutInfo info) {
503 final List<StringOrDrawable> shortcutKeys = new ArrayList<>();
Clara Bayarrib9057df2016-03-02 11:37:09 -0800504 int modifiers = info.getModifiers();
505 if (modifiers == 0) {
506 return shortcutKeys;
507 }
Clara Bayarrib999af52016-04-06 16:02:35 +0100508 for(int i = 0; i < mModifierNames.size(); ++i) {
509 final int supportedModifier = mModifierNames.keyAt(i);
Clara Bayarrib9057df2016-03-02 11:37:09 -0800510 if ((modifiers & supportedModifier) != 0) {
Clara Bayarrib999af52016-04-06 16:02:35 +0100511 if (mModifierDrawables.get(supportedModifier) != null) {
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100512 shortcutKeys.add(new StringOrDrawable(
Clara Bayarrib999af52016-04-06 16:02:35 +0100513 mModifierDrawables.get(supportedModifier)));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100514 } else {
515 shortcutKeys.add(new StringOrDrawable(
Clara Bayarrib999af52016-04-06 16:02:35 +0100516 mModifierNames.get(supportedModifier).toUpperCase()));
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100517 }
Clara Bayarrib9057df2016-03-02 11:37:09 -0800518 modifiers &= ~supportedModifier;
519 }
520 }
521 if (modifiers != 0) {
522 // Remaining unsupported modifiers, don't show anything.
523 return null;
524 }
525 return shortcutKeys;
526 }
Andrei Stingaceanud1519102016-03-31 15:53:33 +0100527
528 private static final class StringOrDrawable {
529 public String string;
530 public Drawable drawable;
531
532 public StringOrDrawable(String string) {
533 this.string = string;
534 }
535
536 public StringOrDrawable(Drawable drawable) {
537 this.drawable = drawable;
538 }
539 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100540}