Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar; |
| 18 | |
| 19 | import android.app.AlertDialog; |
| 20 | import android.app.Dialog; |
| 21 | import android.content.Context; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 22 | import android.content.DialogInterface; |
| 23 | import android.content.DialogInterface.OnClickListener; |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 24 | import android.hardware.input.InputManager; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 25 | import android.os.Handler; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 26 | import android.os.Looper; |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | import android.util.SparseArray; |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 29 | import android.view.ContextThemeWrapper; |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 30 | import android.view.InputDevice; |
| 31 | import android.view.KeyCharacterMap; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 32 | import android.view.KeyEvent; |
| 33 | import android.view.KeyboardShortcutGroup; |
| 34 | import android.view.KeyboardShortcutInfo; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 35 | import android.view.LayoutInflater; |
| 36 | import android.view.View; |
Andrei Stingaceanu | 844927d | 2016-02-16 14:31:58 +0000 | [diff] [blame] | 37 | import android.view.ViewGroup; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 38 | import android.view.Window; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 39 | import android.view.WindowManager.KeyboardShortcutsReceiver; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 40 | import android.widget.LinearLayout; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 41 | import android.widget.TextView; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 42 | |
| 43 | import com.android.systemui.R; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 44 | import com.android.systemui.recents.Recents; |
| 45 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 46 | import java.util.ArrayList; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 47 | import java.util.List; |
| 48 | |
| 49 | import static android.content.Context.LAYOUT_INFLATER_SERVICE; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 50 | import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Contains functionality for handling keyboard shortcuts. |
| 54 | */ |
| 55 | public class KeyboardShortcuts { |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 56 | private static final String TAG = KeyboardShortcuts.class.getSimpleName(); |
| 57 | |
| 58 | private static final SparseArray<String> SPECIAL_CHARACTER_NAMES = new SparseArray<>(); |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 59 | private static final SparseArray<String> MODIFIER_NAMES = new SparseArray<>(); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 60 | |
| 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 Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 199 | |
| 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 Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 206 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 207 | |
| 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 Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 215 | |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 216 | private Dialog mKeyboardShortcutsDialog; |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 217 | private KeyCharacterMap mKeyCharacterMap; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 218 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 219 | public KeyboardShortcuts(Context context) { |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 220 | this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_Material_Light); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 221 | if (SPECIAL_CHARACTER_NAMES.size() == 0) { |
| 222 | loadSpecialCharacterNames(context); |
| 223 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 224 | } |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 225 | |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 226 | public void toggleKeyboardShortcuts(int deviceId) { |
| 227 | InputDevice inputDevice = InputManager.getInstance().getInputDevice(deviceId); |
| 228 | if (inputDevice != null) { |
| 229 | mKeyCharacterMap = inputDevice.getKeyCharacterMap(); |
| 230 | } |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 231 | if (mKeyboardShortcutsDialog == null) { |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 232 | Recents.getSystemServices().requestKeyboardShortcuts(mContext, |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 233 | new KeyboardShortcutsReceiver() { |
| 234 | @Override |
| 235 | public void onKeyboardShortcutsReceived( |
| 236 | final List<KeyboardShortcutGroup> result) { |
| 237 | KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup( |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 238 | mContext.getString(R.string.keyboard_shortcut_group_system), true); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 239 | systemGroup.addItem(new KeyboardShortcutInfo( |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 240 | mContext.getString(R.string.keyboard_shortcut_group_system_home), |
| 241 | KeyEvent.KEYCODE_ENTER, KeyEvent.META_META_ON)); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 242 | systemGroup.addItem(new KeyboardShortcutInfo( |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 243 | mContext.getString(R.string.keyboard_shortcut_group_system_back), |
| 244 | KeyEvent.KEYCODE_DEL, KeyEvent.META_META_ON)); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 245 | systemGroup.addItem(new KeyboardShortcutInfo( |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 246 | mContext.getString(R.string.keyboard_shortcut_group_system_recents), |
| 247 | KeyEvent.KEYCODE_TAB, KeyEvent.META_ALT_ON)); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 248 | result.add(systemGroup); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 249 | showKeyboardShortcutsDialog(result); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 250 | } |
Clara Bayarri | fcd7e80 | 2016-03-10 12:58:18 +0000 | [diff] [blame] | 251 | }, deviceId); |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 252 | } else { |
| 253 | dismissKeyboardShortcutsDialog(); |
| 254 | } |
| 255 | } |
| 256 | |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 257 | public void dismissKeyboardShortcutsDialog() { |
| 258 | if (mKeyboardShortcutsDialog != null) { |
| 259 | mKeyboardShortcutsDialog.dismiss(); |
| 260 | mKeyboardShortcutsDialog = null; |
| 261 | } |
| 262 | } |
| 263 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 264 | private void showKeyboardShortcutsDialog( |
| 265 | final List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 266 | // Need to post on the main thread. |
| 267 | mHandler.post(new Runnable() { |
| 268 | @Override |
| 269 | public void run() { |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 270 | handleShowKeyboardShortcuts(keyboardShortcutGroups); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 271 | } |
| 272 | }); |
| 273 | } |
| 274 | |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 275 | private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 276 | AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext); |
| 277 | LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( |
| 278 | LAYOUT_INFLATER_SERVICE); |
| 279 | final View keyboardShortcutsView = inflater.inflate( |
| 280 | R.layout.keyboard_shortcuts_view, null); |
| 281 | populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById( |
| 282 | R.id.keyboard_shortcuts_container), keyboardShortcutGroups); |
| 283 | dialogBuilder.setView(keyboardShortcutsView); |
| 284 | dialogBuilder.setPositiveButton(R.string.quick_settings_done, dialogCloseListener); |
| 285 | mKeyboardShortcutsDialog = dialogBuilder.create(); |
| 286 | mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true); |
| 287 | Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow(); |
| 288 | keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG); |
| 289 | mKeyboardShortcutsDialog.show(); |
| 290 | } |
| 291 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 292 | private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout, |
| 293 | List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 294 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 295 | final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size(); |
| 296 | for (int i = 0; i < keyboardShortcutGroupsSize; i++) { |
| 297 | KeyboardShortcutGroup group = keyboardShortcutGroups.get(i); |
| 298 | TextView categoryTitle = (TextView) inflater.inflate( |
| 299 | R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false); |
| 300 | categoryTitle.setText(group.getLabel()); |
| 301 | categoryTitle.setTextColor(group.isSystemGroup() |
| 302 | ? mContext.getColor(R.color.ksh_system_group_color) |
| 303 | : mContext.getColor(R.color.ksh_application_group_color)); |
| 304 | keyboardShortcutsLayout.addView(categoryTitle); |
| 305 | |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 306 | LinearLayout shortcutContainer = (LinearLayout) inflater.inflate( |
| 307 | R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 308 | final int itemsSize = group.getItems().size(); |
| 309 | for (int j = 0; j < itemsSize; j++) { |
| 310 | KeyboardShortcutInfo info = group.getItems().get(j); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 311 | if (info.getKeycode() != KeyEvent.KEYCODE_UNKNOWN |
| 312 | && !KeyCharacterMap.deviceHasKey(info.getKeycode())) { |
| 313 | // The user can't achieve this shortcut, so skipping. |
| 314 | Log.w(TAG, "Keyboard Shortcut contains key not on device, skipping."); |
| 315 | continue; |
| 316 | } |
| 317 | List<String> shortcutKeys = getHumanReadableShortcutKeys(info); |
| 318 | if (shortcutKeys == null) { |
| 319 | // Ignore shortcuts we can't display keys for. |
| 320 | Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping."); |
| 321 | continue; |
| 322 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 323 | View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item, |
| 324 | shortcutContainer, false); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 325 | TextView textView = (TextView) shortcutView |
| 326 | .findViewById(R.id.keyboard_shortcuts_keyword); |
| 327 | textView.setText(info.getLabel()); |
| 328 | |
Andrei Stingaceanu | 844927d | 2016-02-16 14:31:58 +0000 | [diff] [blame] | 329 | ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 330 | .findViewById(R.id.keyboard_shortcuts_item_container); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 331 | final int shortcutKeysSize = shortcutKeys.size(); |
| 332 | for (int k = 0; k < shortcutKeysSize; k++) { |
| 333 | String shortcutKey = shortcutKeys.get(k); |
| 334 | TextView shortcutKeyView = (TextView) inflater.inflate( |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 335 | R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer, false); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 336 | shortcutKeyView.setText(shortcutKey); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 337 | shortcutItemsContainer.addView(shortcutKeyView); |
| 338 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 339 | shortcutContainer.addView(shortcutView); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 340 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 341 | keyboardShortcutsLayout.addView(shortcutContainer); |
| 342 | if (i < keyboardShortcutGroupsSize - 1) { |
| 343 | View separator = inflater.inflate( |
| 344 | R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout, |
| 345 | false); |
| 346 | keyboardShortcutsLayout.addView(separator); |
| 347 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | private List<String> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) { |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 352 | List<String> shortcutKeys = getHumanReadableModifiers(info); |
| 353 | if (shortcutKeys == null) { |
| 354 | return null; |
| 355 | } |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 356 | String displayLabelString; |
| 357 | if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) { |
| 358 | displayLabelString = String.valueOf(info.getBaseCharacter()); |
| 359 | } else if (SPECIAL_CHARACTER_NAMES.get(info.getKeycode()) != null) { |
| 360 | displayLabelString = SPECIAL_CHARACTER_NAMES.get(info.getKeycode()); |
| 361 | } else { |
| 362 | // TODO: Have a generic map for when we don't have the device's. |
| 363 | char displayLabel = mKeyCharacterMap == null |
| 364 | ? 0 : mKeyCharacterMap.getDisplayLabel(info.getKeycode()); |
| 365 | if (displayLabel != 0) { |
| 366 | displayLabelString = String.valueOf(displayLabel); |
| 367 | } else { |
| 368 | return null; |
| 369 | } |
| 370 | } |
| 371 | shortcutKeys.add(displayLabelString.toUpperCase()); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 372 | return shortcutKeys; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 373 | } |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 374 | |
| 375 | private List<String> getHumanReadableModifiers(KeyboardShortcutInfo info) { |
| 376 | final List<String> shortcutKeys = new ArrayList<>(); |
| 377 | int modifiers = info.getModifiers(); |
| 378 | if (modifiers == 0) { |
| 379 | return shortcutKeys; |
| 380 | } |
| 381 | for(int i = 0; i < MODIFIER_NAMES.size(); ++i) { |
| 382 | final int supportedModifier = MODIFIER_NAMES.keyAt(i); |
| 383 | if ((modifiers & supportedModifier) != 0) { |
| 384 | shortcutKeys.add(MODIFIER_NAMES.get(supportedModifier).toUpperCase()); |
| 385 | modifiers &= ~supportedModifier; |
| 386 | } |
| 387 | } |
| 388 | if (modifiers != 0) { |
| 389 | // Remaining unsupported modifiers, don't show anything. |
| 390 | return null; |
| 391 | } |
| 392 | return shortcutKeys; |
| 393 | } |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 394 | } |