blob: dc015c69799ef22893dd8daaf5b9d5c58b1f4abd [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<>();
59
60 private static void loadSpecialCharacterNames(Context context) {
61 SPECIAL_CHARACTER_NAMES.put(
62 KeyEvent.KEYCODE_HOME, context.getString(R.string.keyboard_key_home));
63 SPECIAL_CHARACTER_NAMES.put(
64 KeyEvent.KEYCODE_BACK, context.getString(R.string.keyboard_key_back));
65 SPECIAL_CHARACTER_NAMES.put(
66 KeyEvent.KEYCODE_DPAD_UP, context.getString(R.string.keyboard_key_dpad_up));
67 SPECIAL_CHARACTER_NAMES.put(
68 KeyEvent.KEYCODE_DPAD_DOWN, context.getString(R.string.keyboard_key_dpad_down));
69 SPECIAL_CHARACTER_NAMES.put(
70 KeyEvent.KEYCODE_DPAD_LEFT, context.getString(R.string.keyboard_key_dpad_left));
71 SPECIAL_CHARACTER_NAMES.put(
72 KeyEvent.KEYCODE_DPAD_RIGHT, context.getString(R.string.keyboard_key_dpad_right));
73 SPECIAL_CHARACTER_NAMES.put(
74 KeyEvent.KEYCODE_DPAD_CENTER, context.getString(R.string.keyboard_key_dpad_center));
75 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_PERIOD, ".");
76 SPECIAL_CHARACTER_NAMES.put(
77 KeyEvent.KEYCODE_TAB, context.getString(R.string.keyboard_key_tab));
78 SPECIAL_CHARACTER_NAMES.put(
79 KeyEvent.KEYCODE_SPACE, context.getString(R.string.keyboard_key_space));
80 SPECIAL_CHARACTER_NAMES.put(
81 KeyEvent.KEYCODE_ENTER, context.getString(R.string.keyboard_key_enter));
82 SPECIAL_CHARACTER_NAMES.put(
83 KeyEvent.KEYCODE_DEL, context.getString(R.string.keyboard_key_backspace));
84 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE,
85 context.getString(R.string.keyboard_key_media_play_pause));
86 SPECIAL_CHARACTER_NAMES.put(
87 KeyEvent.KEYCODE_MEDIA_STOP, context.getString(R.string.keyboard_key_media_stop));
88 SPECIAL_CHARACTER_NAMES.put(
89 KeyEvent.KEYCODE_MEDIA_NEXT, context.getString(R.string.keyboard_key_media_next));
90 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_MEDIA_PREVIOUS,
91 context.getString(R.string.keyboard_key_media_previous));
92 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_MEDIA_REWIND,
93 context.getString(R.string.keyboard_key_media_rewind));
94 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD,
95 context.getString(R.string.keyboard_key_media_fast_forward));
96 SPECIAL_CHARACTER_NAMES.put(
97 KeyEvent.KEYCODE_PAGE_UP, context.getString(R.string.keyboard_key_page_up));
98 SPECIAL_CHARACTER_NAMES.put(
99 KeyEvent.KEYCODE_PAGE_DOWN, context.getString(R.string.keyboard_key_page_down));
100 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_A,
101 context.getString(R.string.keyboard_key_button_template, "A"));
102 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_B,
103 context.getString(R.string.keyboard_key_button_template, "B"));
104 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_C,
105 context.getString(R.string.keyboard_key_button_template, "C"));
106 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_X,
107 context.getString(R.string.keyboard_key_button_template, "X"));
108 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_Y,
109 context.getString(R.string.keyboard_key_button_template, "Y"));
110 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_Z,
111 context.getString(R.string.keyboard_key_button_template, "Z"));
112 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_L1,
113 context.getString(R.string.keyboard_key_button_template, "L1"));
114 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_R1,
115 context.getString(R.string.keyboard_key_button_template, "R1"));
116 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_L2,
117 context.getString(R.string.keyboard_key_button_template, "L2"));
118 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_R2,
119 context.getString(R.string.keyboard_key_button_template, "R2"));
120 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_START,
121 context.getString(R.string.keyboard_key_button_template, "Start"));
122 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_SELECT,
123 context.getString(R.string.keyboard_key_button_template, "Select"));
124 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BUTTON_MODE,
125 context.getString(R.string.keyboard_key_button_template, "Mode"));
126 SPECIAL_CHARACTER_NAMES.put(
127 KeyEvent.KEYCODE_FORWARD_DEL, context.getString(R.string.keyboard_key_forward_del));
128 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_ESCAPE, "Esc");
129 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_SYSRQ, "SysRq");
130 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_BREAK, "Break");
131 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_SCROLL_LOCK, "Scroll Lock");
132 SPECIAL_CHARACTER_NAMES.put(
133 KeyEvent.KEYCODE_MOVE_HOME, context.getString(R.string.keyboard_key_move_home));
134 SPECIAL_CHARACTER_NAMES.put(
135 KeyEvent.KEYCODE_MOVE_END, context.getString(R.string.keyboard_key_move_end));
136 SPECIAL_CHARACTER_NAMES.put(
137 KeyEvent.KEYCODE_INSERT, context.getString(R.string.keyboard_key_insert));
138 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F1, "F1");
139 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F2, "F2");
140 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F3, "F3");
141 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F4, "F4");
142 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F5, "F5");
143 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F6, "F6");
144 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F7, "F7");
145 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F8, "F8");
146 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F9, "F9");
147 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F10, "F10");
148 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F11, "F11");
149 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_F12, "F12");
150 SPECIAL_CHARACTER_NAMES.put(
151 KeyEvent.KEYCODE_NUM_LOCK, context.getString(R.string.keyboard_key_num_lock));
152 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_0,
153 context.getString(R.string.keyboard_key_numpad_template, "0"));
154 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_1,
155 context.getString(R.string.keyboard_key_numpad_template, "1"));
156 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_2,
157 context.getString(R.string.keyboard_key_numpad_template, "2"));
158 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_3,
159 context.getString(R.string.keyboard_key_numpad_template, "3"));
160 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_4,
161 context.getString(R.string.keyboard_key_numpad_template, "4"));
162 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_5,
163 context.getString(R.string.keyboard_key_numpad_template, "5"));
164 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_6,
165 context.getString(R.string.keyboard_key_numpad_template, "6"));
166 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_7,
167 context.getString(R.string.keyboard_key_numpad_template, "7"));
168 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_8,
169 context.getString(R.string.keyboard_key_numpad_template, "8"));
170 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_9,
171 context.getString(R.string.keyboard_key_numpad_template, "9"));
172 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_DIVIDE,
173 context.getString(R.string.keyboard_key_numpad_template, "/"));
174 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_MULTIPLY,
175 context.getString(R.string.keyboard_key_numpad_template, "*"));
176 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_SUBTRACT,
177 context.getString(R.string.keyboard_key_numpad_template, "-"));
178 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_ADD,
179 context.getString(R.string.keyboard_key_numpad_template, "+"));
180 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_DOT,
181 context.getString(R.string.keyboard_key_numpad_template, "."));
182 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_COMMA,
183 context.getString(R.string.keyboard_key_numpad_template, ","));
184 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_ENTER,
185 context.getString(R.string.keyboard_key_numpad_template,
186 context.getString(R.string.keyboard_key_enter)));
187 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_EQUALS,
188 context.getString(R.string.keyboard_key_numpad_template, "="));
189 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN,
190 context.getString(R.string.keyboard_key_numpad_template, "("));
191 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN,
192 context.getString(R.string.keyboard_key_numpad_template, ")"));
193 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_ZENKAKU_HANKAKU, "半角/全角");
194 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_EISU, "英数");
195 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_MUHENKAN, "無変換");
196 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_HENKAN, "変換");
197 SPECIAL_CHARACTER_NAMES.put(KeyEvent.KEYCODE_KATAKANA_HIRAGANA, "かな");
198 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000199
200 private final Handler mHandler = new Handler(Looper.getMainLooper());
201 private final Context mContext;
202 private final OnClickListener dialogCloseListener = new DialogInterface.OnClickListener() {
203 public void onClick(DialogInterface dialog, int id) {
204 dismissKeyboardShortcutsDialog();
205 }
206 };
Clara Bayarri75e09792015-07-29 16:20:40 +0100207
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100208 private Dialog mKeyboardShortcutsDialog;
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800209 private KeyCharacterMap mKeyCharacterMap;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100210
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000211 public KeyboardShortcuts(Context context) {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000212 this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_Material_Light);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800213 if (SPECIAL_CHARACTER_NAMES.size() == 0) {
214 loadSpecialCharacterNames(context);
215 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000216 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100217
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800218 public void toggleKeyboardShortcuts(int deviceId) {
219 InputDevice inputDevice = InputManager.getInstance().getInputDevice(deviceId);
220 if (inputDevice != null) {
221 mKeyCharacterMap = inputDevice.getKeyCharacterMap();
222 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100223 if (mKeyboardShortcutsDialog == null) {
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000224 Recents.getSystemServices().requestKeyboardShortcuts(mContext,
Clara Bayarri75e09792015-07-29 16:20:40 +0100225 new KeyboardShortcutsReceiver() {
226 @Override
227 public void onKeyboardShortcutsReceived(
228 final List<KeyboardShortcutGroup> result) {
229 KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup(
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000230 mContext.getString(R.string.keyboard_shortcut_group_system), true);
Clara Bayarri75e09792015-07-29 16:20:40 +0100231 systemGroup.addItem(new KeyboardShortcutInfo(
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000232 mContext.getString(R.string.keyboard_shortcut_group_system_home),
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800233 KeyEvent.KEYCODE_ENTER, KeyEvent.META_META_ON));
Clara Bayarri75e09792015-07-29 16:20:40 +0100234 systemGroup.addItem(new KeyboardShortcutInfo(
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000235 mContext.getString(R.string.keyboard_shortcut_group_system_back),
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800236 KeyEvent.KEYCODE_DEL, KeyEvent.META_META_ON));
Clara Bayarri75e09792015-07-29 16:20:40 +0100237 systemGroup.addItem(new KeyboardShortcutInfo(
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000238 mContext.getString(R.string.keyboard_shortcut_group_system_recents),
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800239 KeyEvent.KEYCODE_TAB, KeyEvent.META_ALT_ON));
Clara Bayarri75e09792015-07-29 16:20:40 +0100240 result.add(systemGroup);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000241 showKeyboardShortcutsDialog(result);
Clara Bayarri75e09792015-07-29 16:20:40 +0100242 }
243 });
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100244 } else {
245 dismissKeyboardShortcutsDialog();
246 }
247 }
248
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100249 public void dismissKeyboardShortcutsDialog() {
250 if (mKeyboardShortcutsDialog != null) {
251 mKeyboardShortcutsDialog.dismiss();
252 mKeyboardShortcutsDialog = null;
253 }
254 }
255
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000256 private void showKeyboardShortcutsDialog(
257 final List<KeyboardShortcutGroup> keyboardShortcutGroups) {
258 // Need to post on the main thread.
259 mHandler.post(new Runnable() {
260 @Override
261 public void run() {
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000262 handleShowKeyboardShortcuts(keyboardShortcutGroups);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000263 }
264 });
265 }
266
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000267 private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) {
268 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
269 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
270 LAYOUT_INFLATER_SERVICE);
271 final View keyboardShortcutsView = inflater.inflate(
272 R.layout.keyboard_shortcuts_view, null);
273 populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById(
274 R.id.keyboard_shortcuts_container), keyboardShortcutGroups);
275 dialogBuilder.setView(keyboardShortcutsView);
276 dialogBuilder.setPositiveButton(R.string.quick_settings_done, dialogCloseListener);
277 mKeyboardShortcutsDialog = dialogBuilder.create();
278 mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true);
279 Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow();
280 keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG);
281 mKeyboardShortcutsDialog.show();
282 }
283
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000284 private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout,
285 List<KeyboardShortcutGroup> keyboardShortcutGroups) {
286 LayoutInflater inflater = LayoutInflater.from(mContext);
287 final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size();
288 for (int i = 0; i < keyboardShortcutGroupsSize; i++) {
289 KeyboardShortcutGroup group = keyboardShortcutGroups.get(i);
290 TextView categoryTitle = (TextView) inflater.inflate(
291 R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false);
292 categoryTitle.setText(group.getLabel());
293 categoryTitle.setTextColor(group.isSystemGroup()
294 ? mContext.getColor(R.color.ksh_system_group_color)
295 : mContext.getColor(R.color.ksh_application_group_color));
296 keyboardShortcutsLayout.addView(categoryTitle);
297
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000298 LinearLayout shortcutContainer = (LinearLayout) inflater.inflate(
299 R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000300 final int itemsSize = group.getItems().size();
301 for (int j = 0; j < itemsSize; j++) {
302 KeyboardShortcutInfo info = group.getItems().get(j);
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800303 if (info.getKeycode() != KeyEvent.KEYCODE_UNKNOWN
304 && !KeyCharacterMap.deviceHasKey(info.getKeycode())) {
305 // The user can't achieve this shortcut, so skipping.
306 Log.w(TAG, "Keyboard Shortcut contains key not on device, skipping.");
307 continue;
308 }
309 List<String> shortcutKeys = getHumanReadableShortcutKeys(info);
310 if (shortcutKeys == null) {
311 // Ignore shortcuts we can't display keys for.
312 Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping.");
313 continue;
314 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000315 View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item,
316 shortcutContainer, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000317 TextView textView = (TextView) shortcutView
318 .findViewById(R.id.keyboard_shortcuts_keyword);
319 textView.setText(info.getLabel());
320
Andrei Stingaceanu844927d2016-02-16 14:31:58 +0000321 ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000322 .findViewById(R.id.keyboard_shortcuts_item_container);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000323 final int shortcutKeysSize = shortcutKeys.size();
324 for (int k = 0; k < shortcutKeysSize; k++) {
325 String shortcutKey = shortcutKeys.get(k);
326 TextView shortcutKeyView = (TextView) inflater.inflate(
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000327 R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer, false);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000328 shortcutKeyView.setText(shortcutKey);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000329 shortcutItemsContainer.addView(shortcutKeyView);
330 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000331 shortcutContainer.addView(shortcutView);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000332 }
Andrei Stingaceanu2b909e92016-02-03 17:52:00 +0000333 keyboardShortcutsLayout.addView(shortcutContainer);
334 if (i < keyboardShortcutGroupsSize - 1) {
335 View separator = inflater.inflate(
336 R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout,
337 false);
338 keyboardShortcutsLayout.addView(separator);
339 }
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000340 }
341 }
342
343 private List<String> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) {
344 // TODO: fix the shortcuts. Find or build an util which can produce human readable
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800345 // names of the modifiers.
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000346 List<String> shortcutKeys = new ArrayList<>();
347 shortcutKeys.add(KeyEvent.metaStateToString(info.getModifiers()).toUpperCase());
Clara Bayarri4e850ff2016-03-02 11:12:32 -0800348 String displayLabelString;
349 if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) {
350 displayLabelString = String.valueOf(info.getBaseCharacter());
351 } else if (SPECIAL_CHARACTER_NAMES.get(info.getKeycode()) != null) {
352 displayLabelString = SPECIAL_CHARACTER_NAMES.get(info.getKeycode());
353 } else {
354 // TODO: Have a generic map for when we don't have the device's.
355 char displayLabel = mKeyCharacterMap == null
356 ? 0 : mKeyCharacterMap.getDisplayLabel(info.getKeycode());
357 if (displayLabel != 0) {
358 displayLabelString = String.valueOf(displayLabel);
359 } else {
360 return null;
361 }
362 }
363 shortcutKeys.add(displayLabelString.toUpperCase());
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000364 return shortcutKeys;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100365 }
366}