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<>(); |
| 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 Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 199 | |
| 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 Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 207 | |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 208 | private Dialog mKeyboardShortcutsDialog; |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame^] | 209 | private KeyCharacterMap mKeyCharacterMap; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 210 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 211 | public KeyboardShortcuts(Context context) { |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 212 | this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_Material_Light); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame^] | 213 | if (SPECIAL_CHARACTER_NAMES.size() == 0) { |
| 214 | loadSpecialCharacterNames(context); |
| 215 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 216 | } |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 217 | |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame^] | 218 | public void toggleKeyboardShortcuts(int deviceId) { |
| 219 | InputDevice inputDevice = InputManager.getInstance().getInputDevice(deviceId); |
| 220 | if (inputDevice != null) { |
| 221 | mKeyCharacterMap = inputDevice.getKeyCharacterMap(); |
| 222 | } |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 223 | if (mKeyboardShortcutsDialog == null) { |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 224 | Recents.getSystemServices().requestKeyboardShortcuts(mContext, |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 225 | new KeyboardShortcutsReceiver() { |
| 226 | @Override |
| 227 | public void onKeyboardShortcutsReceived( |
| 228 | final List<KeyboardShortcutGroup> result) { |
| 229 | KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup( |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 230 | mContext.getString(R.string.keyboard_shortcut_group_system), true); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 231 | systemGroup.addItem(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 232 | mContext.getString(R.string.keyboard_shortcut_group_system_home), |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame^] | 233 | KeyEvent.KEYCODE_ENTER, KeyEvent.META_META_ON)); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 234 | systemGroup.addItem(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 235 | mContext.getString(R.string.keyboard_shortcut_group_system_back), |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame^] | 236 | KeyEvent.KEYCODE_DEL, KeyEvent.META_META_ON)); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 237 | systemGroup.addItem(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 238 | mContext.getString(R.string.keyboard_shortcut_group_system_recents), |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame^] | 239 | KeyEvent.KEYCODE_TAB, KeyEvent.META_ALT_ON)); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 240 | result.add(systemGroup); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 241 | showKeyboardShortcutsDialog(result); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 242 | } |
| 243 | }); |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 244 | } else { |
| 245 | dismissKeyboardShortcutsDialog(); |
| 246 | } |
| 247 | } |
| 248 | |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 249 | public void dismissKeyboardShortcutsDialog() { |
| 250 | if (mKeyboardShortcutsDialog != null) { |
| 251 | mKeyboardShortcutsDialog.dismiss(); |
| 252 | mKeyboardShortcutsDialog = null; |
| 253 | } |
| 254 | } |
| 255 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 256 | 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 Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 262 | handleShowKeyboardShortcuts(keyboardShortcutGroups); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 263 | } |
| 264 | }); |
| 265 | } |
| 266 | |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 267 | 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 Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 284 | 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 Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 298 | LinearLayout shortcutContainer = (LinearLayout) inflater.inflate( |
| 299 | R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 300 | final int itemsSize = group.getItems().size(); |
| 301 | for (int j = 0; j < itemsSize; j++) { |
| 302 | KeyboardShortcutInfo info = group.getItems().get(j); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame^] | 303 | 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 Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 315 | View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item, |
| 316 | shortcutContainer, false); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 317 | TextView textView = (TextView) shortcutView |
| 318 | .findViewById(R.id.keyboard_shortcuts_keyword); |
| 319 | textView.setText(info.getLabel()); |
| 320 | |
Andrei Stingaceanu | 844927d | 2016-02-16 14:31:58 +0000 | [diff] [blame] | 321 | ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 322 | .findViewById(R.id.keyboard_shortcuts_item_container); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 323 | 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 Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 327 | R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer, false); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 328 | shortcutKeyView.setText(shortcutKey); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 329 | shortcutItemsContainer.addView(shortcutKeyView); |
| 330 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 331 | shortcutContainer.addView(shortcutView); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 332 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 333 | 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 Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 340 | } |
| 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 Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame^] | 345 | // names of the modifiers. |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 346 | List<String> shortcutKeys = new ArrayList<>(); |
| 347 | shortcutKeys.add(KeyEvent.metaStateToString(info.getModifiers()).toUpperCase()); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame^] | 348 | 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 Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 364 | return shortcutKeys; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 365 | } |
| 366 | } |