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 | 1d648a1 | 2016-03-23 17:09:02 +0000 | [diff] [blame^] | 248 | systemGroup.addItem(new KeyboardShortcutInfo( |
| 249 | mContext.getString( |
| 250 | R.string.keyboard_shortcut_group_system_notifications), |
| 251 | KeyEvent.KEYCODE_N, KeyEvent.META_META_ON)); |
| 252 | systemGroup.addItem(new KeyboardShortcutInfo( |
| 253 | mContext.getString( |
| 254 | R.string.keyboard_shortcut_group_system_shortcuts_helper), |
| 255 | KeyEvent.KEYCODE_SLASH, KeyEvent.META_META_ON)); |
| 256 | systemGroup.addItem(new KeyboardShortcutInfo( |
| 257 | mContext.getString( |
| 258 | R.string.keyboard_shortcut_group_system_switch_input), |
| 259 | KeyEvent.KEYCODE_SPACE, KeyEvent.META_META_ON)); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 260 | result.add(systemGroup); |
Clara Bayarri | 1d648a1 | 2016-03-23 17:09:02 +0000 | [diff] [blame^] | 261 | |
| 262 | KeyboardShortcutGroup applicationGroup = new KeyboardShortcutGroup( |
| 263 | mContext.getString(R.string.keyboard_shortcut_group_applications), |
| 264 | true); |
| 265 | applicationGroup.addItem(new KeyboardShortcutInfo( |
| 266 | mContext.getString( |
| 267 | R.string.keyboard_shortcut_group_applications_assist), |
| 268 | KeyEvent.KEYCODE_UNKNOWN, KeyEvent.META_META_ON)); |
| 269 | applicationGroup.addItem(new KeyboardShortcutInfo( |
| 270 | mContext.getString( |
| 271 | R.string.keyboard_shortcut_group_applications_browser), |
| 272 | KeyEvent.KEYCODE_B, KeyEvent.META_META_ON)); |
| 273 | applicationGroup.addItem(new KeyboardShortcutInfo( |
| 274 | mContext.getString( |
| 275 | R.string.keyboard_shortcut_group_applications_contacts), |
| 276 | KeyEvent.KEYCODE_C, KeyEvent.META_META_ON)); |
| 277 | applicationGroup.addItem(new KeyboardShortcutInfo( |
| 278 | mContext.getString( |
| 279 | R.string.keyboard_shortcut_group_applications_email), |
| 280 | KeyEvent.KEYCODE_E, KeyEvent.META_META_ON)); |
| 281 | applicationGroup.addItem(new KeyboardShortcutInfo( |
| 282 | mContext.getString( |
| 283 | R.string.keyboard_shortcut_group_applications_im), |
| 284 | KeyEvent.KEYCODE_T, KeyEvent.META_META_ON)); |
| 285 | applicationGroup.addItem(new KeyboardShortcutInfo( |
| 286 | mContext.getString( |
| 287 | R.string.keyboard_shortcut_group_applications_music), |
| 288 | KeyEvent.KEYCODE_P, KeyEvent.META_META_ON)); |
| 289 | applicationGroup.addItem(new KeyboardShortcutInfo( |
| 290 | mContext.getString( |
| 291 | R.string.keyboard_shortcut_group_applications_youtube), |
| 292 | KeyEvent.KEYCODE_Y, KeyEvent.META_META_ON)); |
| 293 | applicationGroup.addItem(new KeyboardShortcutInfo( |
| 294 | mContext.getString( |
| 295 | R.string.keyboard_shortcut_group_applications_calendar), |
| 296 | KeyEvent.KEYCODE_L, KeyEvent.META_META_ON)); |
| 297 | result.add(applicationGroup); |
| 298 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 299 | showKeyboardShortcutsDialog(result); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 300 | } |
Clara Bayarri | fcd7e80 | 2016-03-10 12:58:18 +0000 | [diff] [blame] | 301 | }, deviceId); |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 302 | } else { |
| 303 | dismissKeyboardShortcutsDialog(); |
| 304 | } |
| 305 | } |
| 306 | |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 307 | public void dismissKeyboardShortcutsDialog() { |
| 308 | if (mKeyboardShortcutsDialog != null) { |
| 309 | mKeyboardShortcutsDialog.dismiss(); |
| 310 | mKeyboardShortcutsDialog = null; |
| 311 | } |
| 312 | } |
| 313 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 314 | private void showKeyboardShortcutsDialog( |
| 315 | final List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 316 | // Need to post on the main thread. |
| 317 | mHandler.post(new Runnable() { |
| 318 | @Override |
| 319 | public void run() { |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 320 | handleShowKeyboardShortcuts(keyboardShortcutGroups); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 321 | } |
| 322 | }); |
| 323 | } |
| 324 | |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 325 | private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 326 | AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext); |
| 327 | LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( |
| 328 | LAYOUT_INFLATER_SERVICE); |
| 329 | final View keyboardShortcutsView = inflater.inflate( |
| 330 | R.layout.keyboard_shortcuts_view, null); |
| 331 | populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById( |
| 332 | R.id.keyboard_shortcuts_container), keyboardShortcutGroups); |
| 333 | dialogBuilder.setView(keyboardShortcutsView); |
| 334 | dialogBuilder.setPositiveButton(R.string.quick_settings_done, dialogCloseListener); |
| 335 | mKeyboardShortcutsDialog = dialogBuilder.create(); |
| 336 | mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true); |
| 337 | Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow(); |
| 338 | keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG); |
| 339 | mKeyboardShortcutsDialog.show(); |
| 340 | } |
| 341 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 342 | private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout, |
| 343 | List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 344 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 345 | final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size(); |
| 346 | for (int i = 0; i < keyboardShortcutGroupsSize; i++) { |
| 347 | KeyboardShortcutGroup group = keyboardShortcutGroups.get(i); |
| 348 | TextView categoryTitle = (TextView) inflater.inflate( |
| 349 | R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false); |
| 350 | categoryTitle.setText(group.getLabel()); |
| 351 | categoryTitle.setTextColor(group.isSystemGroup() |
| 352 | ? mContext.getColor(R.color.ksh_system_group_color) |
| 353 | : mContext.getColor(R.color.ksh_application_group_color)); |
| 354 | keyboardShortcutsLayout.addView(categoryTitle); |
| 355 | |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 356 | LinearLayout shortcutContainer = (LinearLayout) inflater.inflate( |
| 357 | R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 358 | final int itemsSize = group.getItems().size(); |
| 359 | for (int j = 0; j < itemsSize; j++) { |
| 360 | KeyboardShortcutInfo info = group.getItems().get(j); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 361 | if (info.getKeycode() != KeyEvent.KEYCODE_UNKNOWN |
| 362 | && !KeyCharacterMap.deviceHasKey(info.getKeycode())) { |
| 363 | // The user can't achieve this shortcut, so skipping. |
| 364 | Log.w(TAG, "Keyboard Shortcut contains key not on device, skipping."); |
| 365 | continue; |
| 366 | } |
| 367 | List<String> shortcutKeys = getHumanReadableShortcutKeys(info); |
| 368 | if (shortcutKeys == null) { |
| 369 | // Ignore shortcuts we can't display keys for. |
| 370 | Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping."); |
| 371 | continue; |
| 372 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 373 | View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item, |
| 374 | shortcutContainer, false); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 375 | TextView textView = (TextView) shortcutView |
| 376 | .findViewById(R.id.keyboard_shortcuts_keyword); |
| 377 | textView.setText(info.getLabel()); |
| 378 | |
Andrei Stingaceanu | 844927d | 2016-02-16 14:31:58 +0000 | [diff] [blame] | 379 | ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 380 | .findViewById(R.id.keyboard_shortcuts_item_container); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 381 | final int shortcutKeysSize = shortcutKeys.size(); |
| 382 | for (int k = 0; k < shortcutKeysSize; k++) { |
| 383 | String shortcutKey = shortcutKeys.get(k); |
| 384 | TextView shortcutKeyView = (TextView) inflater.inflate( |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 385 | R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer, false); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 386 | shortcutKeyView.setText(shortcutKey); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 387 | shortcutItemsContainer.addView(shortcutKeyView); |
| 388 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 389 | shortcutContainer.addView(shortcutView); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 390 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 391 | keyboardShortcutsLayout.addView(shortcutContainer); |
| 392 | if (i < keyboardShortcutGroupsSize - 1) { |
| 393 | View separator = inflater.inflate( |
| 394 | R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout, |
| 395 | false); |
| 396 | keyboardShortcutsLayout.addView(separator); |
| 397 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | private List<String> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) { |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 402 | List<String> shortcutKeys = getHumanReadableModifiers(info); |
| 403 | if (shortcutKeys == null) { |
| 404 | return null; |
| 405 | } |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 406 | String displayLabelString; |
Clara Bayarri | 1d648a1 | 2016-03-23 17:09:02 +0000 | [diff] [blame^] | 407 | if (info.getBaseCharacter() > Character.MIN_VALUE) { |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 408 | displayLabelString = String.valueOf(info.getBaseCharacter()); |
| 409 | } else if (SPECIAL_CHARACTER_NAMES.get(info.getKeycode()) != null) { |
| 410 | displayLabelString = SPECIAL_CHARACTER_NAMES.get(info.getKeycode()); |
| 411 | } else { |
Clara Bayarri | 1d648a1 | 2016-03-23 17:09:02 +0000 | [diff] [blame^] | 412 | // Special case for shortcuts with no base key or keycode. |
| 413 | if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) { |
| 414 | return shortcutKeys; |
| 415 | } |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 416 | // TODO: Have a generic map for when we don't have the device's. |
| 417 | char displayLabel = mKeyCharacterMap == null |
| 418 | ? 0 : mKeyCharacterMap.getDisplayLabel(info.getKeycode()); |
| 419 | if (displayLabel != 0) { |
| 420 | displayLabelString = String.valueOf(displayLabel); |
| 421 | } else { |
| 422 | return null; |
| 423 | } |
| 424 | } |
| 425 | shortcutKeys.add(displayLabelString.toUpperCase()); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 426 | return shortcutKeys; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 427 | } |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 428 | |
| 429 | private List<String> getHumanReadableModifiers(KeyboardShortcutInfo info) { |
| 430 | final List<String> shortcutKeys = new ArrayList<>(); |
| 431 | int modifiers = info.getModifiers(); |
| 432 | if (modifiers == 0) { |
| 433 | return shortcutKeys; |
| 434 | } |
| 435 | for(int i = 0; i < MODIFIER_NAMES.size(); ++i) { |
| 436 | final int supportedModifier = MODIFIER_NAMES.keyAt(i); |
| 437 | if ((modifiers & supportedModifier) != 0) { |
| 438 | shortcutKeys.add(MODIFIER_NAMES.get(supportedModifier).toUpperCase()); |
| 439 | modifiers &= ~supportedModifier; |
| 440 | } |
| 441 | } |
| 442 | if (modifiers != 0) { |
| 443 | // Remaining unsupported modifiers, don't show anything. |
| 444 | return null; |
| 445 | } |
| 446 | return shortcutKeys; |
| 447 | } |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 448 | } |