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; |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.Canvas; |
| 26 | import android.graphics.drawable.Drawable; |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 27 | import android.hardware.input.InputManager; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 28 | import android.os.Handler; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 29 | import android.os.Looper; |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 30 | import android.util.Log; |
| 31 | import android.util.SparseArray; |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 32 | import android.view.ContextThemeWrapper; |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 33 | import android.view.InputDevice; |
| 34 | import android.view.KeyCharacterMap; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 35 | import android.view.KeyEvent; |
| 36 | import android.view.KeyboardShortcutGroup; |
| 37 | import android.view.KeyboardShortcutInfo; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 38 | import android.view.LayoutInflater; |
| 39 | import android.view.View; |
Andrei Stingaceanu | 844927d | 2016-02-16 14:31:58 +0000 | [diff] [blame] | 40 | import android.view.ViewGroup; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 41 | import android.view.Window; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 42 | import android.view.WindowManager.KeyboardShortcutsReceiver; |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 43 | import android.widget.ImageView; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 44 | import android.widget.LinearLayout; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 45 | import android.widget.TextView; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 46 | |
| 47 | import com.android.systemui.R; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 48 | import com.android.systemui.recents.Recents; |
| 49 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 50 | import java.util.ArrayList; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 51 | import java.util.List; |
| 52 | |
| 53 | import static android.content.Context.LAYOUT_INFLATER_SERVICE; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 54 | import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Contains functionality for handling keyboard shortcuts. |
| 58 | */ |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 59 | public final class KeyboardShortcuts { |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 60 | private static final String TAG = KeyboardShortcuts.class.getSimpleName(); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 61 | |
Clara Bayarri | b999af5 | 2016-04-06 16:02:35 +0100 | [diff] [blame^] | 62 | private final SparseArray<String> mSpecialCharacterNames = new SparseArray<>(); |
| 63 | private final SparseArray<String> mModifierNames = new SparseArray<>(); |
| 64 | private final SparseArray<Drawable> mSpecialCharacterDrawables = new SparseArray<>(); |
| 65 | private final SparseArray<Drawable> mModifierDrawables = new SparseArray<>(); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 66 | |
| 67 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
| 68 | private final Context mContext; |
| 69 | private final OnClickListener dialogCloseListener = new DialogInterface.OnClickListener() { |
| 70 | public void onClick(DialogInterface dialog, int id) { |
| 71 | dismissKeyboardShortcutsDialog(); |
| 72 | } |
| 73 | }; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 74 | |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 75 | private Dialog mKeyboardShortcutsDialog; |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 76 | private KeyCharacterMap mKeyCharacterMap; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 77 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 78 | public KeyboardShortcuts(Context context) { |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 79 | this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_Material_Light); |
Clara Bayarri | b999af5 | 2016-04-06 16:02:35 +0100 | [diff] [blame^] | 80 | loadResources(context); |
| 81 | } |
| 82 | |
| 83 | private void loadResources(Context context) { |
| 84 | mSpecialCharacterNames.put( |
| 85 | KeyEvent.KEYCODE_HOME, context.getString(R.string.keyboard_key_home)); |
| 86 | mSpecialCharacterNames.put( |
| 87 | KeyEvent.KEYCODE_BACK, context.getString(R.string.keyboard_key_back)); |
| 88 | mSpecialCharacterNames.put( |
| 89 | KeyEvent.KEYCODE_DPAD_UP, context.getString(R.string.keyboard_key_dpad_up)); |
| 90 | mSpecialCharacterNames.put( |
| 91 | KeyEvent.KEYCODE_DPAD_DOWN, context.getString(R.string.keyboard_key_dpad_down)); |
| 92 | mSpecialCharacterNames.put( |
| 93 | KeyEvent.KEYCODE_DPAD_LEFT, context.getString(R.string.keyboard_key_dpad_left)); |
| 94 | mSpecialCharacterNames.put( |
| 95 | KeyEvent.KEYCODE_DPAD_RIGHT, context.getString(R.string.keyboard_key_dpad_right)); |
| 96 | mSpecialCharacterNames.put( |
| 97 | KeyEvent.KEYCODE_DPAD_CENTER, context.getString(R.string.keyboard_key_dpad_center)); |
| 98 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_PERIOD, "."); |
| 99 | mSpecialCharacterNames.put( |
| 100 | KeyEvent.KEYCODE_TAB, context.getString(R.string.keyboard_key_tab)); |
| 101 | mSpecialCharacterNames.put( |
| 102 | KeyEvent.KEYCODE_SPACE, context.getString(R.string.keyboard_key_space)); |
| 103 | mSpecialCharacterNames.put( |
| 104 | KeyEvent.KEYCODE_ENTER, context.getString(R.string.keyboard_key_enter)); |
| 105 | mSpecialCharacterNames.put( |
| 106 | KeyEvent.KEYCODE_DEL, context.getString(R.string.keyboard_key_backspace)); |
| 107 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, |
| 108 | context.getString(R.string.keyboard_key_media_play_pause)); |
| 109 | mSpecialCharacterNames.put( |
| 110 | KeyEvent.KEYCODE_MEDIA_STOP, context.getString(R.string.keyboard_key_media_stop)); |
| 111 | mSpecialCharacterNames.put( |
| 112 | KeyEvent.KEYCODE_MEDIA_NEXT, context.getString(R.string.keyboard_key_media_next)); |
| 113 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PREVIOUS, |
| 114 | context.getString(R.string.keyboard_key_media_previous)); |
| 115 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_REWIND, |
| 116 | context.getString(R.string.keyboard_key_media_rewind)); |
| 117 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD, |
| 118 | context.getString(R.string.keyboard_key_media_fast_forward)); |
| 119 | mSpecialCharacterNames.put( |
| 120 | KeyEvent.KEYCODE_PAGE_UP, context.getString(R.string.keyboard_key_page_up)); |
| 121 | mSpecialCharacterNames.put( |
| 122 | KeyEvent.KEYCODE_PAGE_DOWN, context.getString(R.string.keyboard_key_page_down)); |
| 123 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_A, |
| 124 | context.getString(R.string.keyboard_key_button_template, "A")); |
| 125 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_B, |
| 126 | context.getString(R.string.keyboard_key_button_template, "B")); |
| 127 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_C, |
| 128 | context.getString(R.string.keyboard_key_button_template, "C")); |
| 129 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_X, |
| 130 | context.getString(R.string.keyboard_key_button_template, "X")); |
| 131 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Y, |
| 132 | context.getString(R.string.keyboard_key_button_template, "Y")); |
| 133 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Z, |
| 134 | context.getString(R.string.keyboard_key_button_template, "Z")); |
| 135 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L1, |
| 136 | context.getString(R.string.keyboard_key_button_template, "L1")); |
| 137 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R1, |
| 138 | context.getString(R.string.keyboard_key_button_template, "R1")); |
| 139 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L2, |
| 140 | context.getString(R.string.keyboard_key_button_template, "L2")); |
| 141 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R2, |
| 142 | context.getString(R.string.keyboard_key_button_template, "R2")); |
| 143 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_START, |
| 144 | context.getString(R.string.keyboard_key_button_template, "Start")); |
| 145 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_SELECT, |
| 146 | context.getString(R.string.keyboard_key_button_template, "Select")); |
| 147 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_MODE, |
| 148 | context.getString(R.string.keyboard_key_button_template, "Mode")); |
| 149 | mSpecialCharacterNames.put( |
| 150 | KeyEvent.KEYCODE_FORWARD_DEL, context.getString(R.string.keyboard_key_forward_del)); |
| 151 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_ESCAPE, "Esc"); |
| 152 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_SYSRQ, "SysRq"); |
| 153 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BREAK, "Break"); |
| 154 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_SCROLL_LOCK, "Scroll Lock"); |
| 155 | mSpecialCharacterNames.put( |
| 156 | KeyEvent.KEYCODE_MOVE_HOME, context.getString(R.string.keyboard_key_move_home)); |
| 157 | mSpecialCharacterNames.put( |
| 158 | KeyEvent.KEYCODE_MOVE_END, context.getString(R.string.keyboard_key_move_end)); |
| 159 | mSpecialCharacterNames.put( |
| 160 | KeyEvent.KEYCODE_INSERT, context.getString(R.string.keyboard_key_insert)); |
| 161 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F1, "F1"); |
| 162 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F2, "F2"); |
| 163 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F3, "F3"); |
| 164 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F4, "F4"); |
| 165 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F5, "F5"); |
| 166 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F6, "F6"); |
| 167 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F7, "F7"); |
| 168 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F8, "F8"); |
| 169 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F9, "F9"); |
| 170 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F10, "F10"); |
| 171 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F11, "F11"); |
| 172 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F12, "F12"); |
| 173 | mSpecialCharacterNames.put( |
| 174 | KeyEvent.KEYCODE_NUM_LOCK, context.getString(R.string.keyboard_key_num_lock)); |
| 175 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_0, |
| 176 | context.getString(R.string.keyboard_key_numpad_template, "0")); |
| 177 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_1, |
| 178 | context.getString(R.string.keyboard_key_numpad_template, "1")); |
| 179 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_2, |
| 180 | context.getString(R.string.keyboard_key_numpad_template, "2")); |
| 181 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_3, |
| 182 | context.getString(R.string.keyboard_key_numpad_template, "3")); |
| 183 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_4, |
| 184 | context.getString(R.string.keyboard_key_numpad_template, "4")); |
| 185 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_5, |
| 186 | context.getString(R.string.keyboard_key_numpad_template, "5")); |
| 187 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_6, |
| 188 | context.getString(R.string.keyboard_key_numpad_template, "6")); |
| 189 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_7, |
| 190 | context.getString(R.string.keyboard_key_numpad_template, "7")); |
| 191 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_8, |
| 192 | context.getString(R.string.keyboard_key_numpad_template, "8")); |
| 193 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_9, |
| 194 | context.getString(R.string.keyboard_key_numpad_template, "9")); |
| 195 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DIVIDE, |
| 196 | context.getString(R.string.keyboard_key_numpad_template, "/")); |
| 197 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_MULTIPLY, |
| 198 | context.getString(R.string.keyboard_key_numpad_template, "*")); |
| 199 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_SUBTRACT, |
| 200 | context.getString(R.string.keyboard_key_numpad_template, "-")); |
| 201 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ADD, |
| 202 | context.getString(R.string.keyboard_key_numpad_template, "+")); |
| 203 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DOT, |
| 204 | context.getString(R.string.keyboard_key_numpad_template, ".")); |
| 205 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_COMMA, |
| 206 | context.getString(R.string.keyboard_key_numpad_template, ",")); |
| 207 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ENTER, |
| 208 | context.getString(R.string.keyboard_key_numpad_template, |
| 209 | context.getString(R.string.keyboard_key_enter))); |
| 210 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_EQUALS, |
| 211 | context.getString(R.string.keyboard_key_numpad_template, "=")); |
| 212 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN, |
| 213 | context.getString(R.string.keyboard_key_numpad_template, "(")); |
| 214 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN, |
| 215 | context.getString(R.string.keyboard_key_numpad_template, ")")); |
| 216 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_ZENKAKU_HANKAKU, "半角/全角"); |
| 217 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_EISU, "英数"); |
| 218 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_MUHENKAN, "無変換"); |
| 219 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_HENKAN, "変換"); |
| 220 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_KATAKANA_HIRAGANA, "かな"); |
| 221 | |
| 222 | mModifierNames.put(KeyEvent.META_META_ON, "Meta"); |
| 223 | mModifierNames.put(KeyEvent.META_CTRL_ON, "Ctrl"); |
| 224 | mModifierNames.put(KeyEvent.META_ALT_ON, "Alt"); |
| 225 | mModifierNames.put(KeyEvent.META_SHIFT_ON, "Shift"); |
| 226 | mModifierNames.put(KeyEvent.META_SYM_ON, "Sym"); |
| 227 | mModifierNames.put(KeyEvent.META_FUNCTION_ON, "Fn"); |
| 228 | |
| 229 | mSpecialCharacterDrawables.put( |
| 230 | KeyEvent.KEYCODE_DEL, context.getDrawable(R.drawable.ic_ksh_key_backspace)); |
| 231 | mSpecialCharacterDrawables.put( |
| 232 | KeyEvent.KEYCODE_ENTER, context.getDrawable(R.drawable.ic_ksh_key_enter)); |
| 233 | mSpecialCharacterDrawables.put( |
| 234 | KeyEvent.KEYCODE_DPAD_UP, context.getDrawable(R.drawable.ic_ksh_key_up)); |
| 235 | mSpecialCharacterDrawables.put( |
| 236 | KeyEvent.KEYCODE_DPAD_RIGHT, context.getDrawable(R.drawable.ic_ksh_key_right)); |
| 237 | mSpecialCharacterDrawables.put( |
| 238 | KeyEvent.KEYCODE_DPAD_DOWN, context.getDrawable(R.drawable.ic_ksh_key_down)); |
| 239 | mSpecialCharacterDrawables.put( |
| 240 | KeyEvent.KEYCODE_DPAD_LEFT, context.getDrawable(R.drawable.ic_ksh_key_left)); |
| 241 | |
| 242 | mModifierDrawables.put( |
| 243 | KeyEvent.META_META_ON, context.getDrawable(R.drawable.ic_ksh_key_meta)); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 244 | } |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 245 | |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 246 | public void toggleKeyboardShortcuts(int deviceId) { |
| 247 | InputDevice inputDevice = InputManager.getInstance().getInputDevice(deviceId); |
| 248 | if (inputDevice != null) { |
| 249 | mKeyCharacterMap = inputDevice.getKeyCharacterMap(); |
| 250 | } |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 251 | if (mKeyboardShortcutsDialog == null) { |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 252 | Recents.getSystemServices().requestKeyboardShortcuts(mContext, |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 253 | new KeyboardShortcutsReceiver() { |
| 254 | @Override |
| 255 | public void onKeyboardShortcutsReceived( |
| 256 | final List<KeyboardShortcutGroup> result) { |
| 257 | KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup( |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 258 | mContext.getString(R.string.keyboard_shortcut_group_system), true); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 259 | systemGroup.addItem(new KeyboardShortcutInfo( |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 260 | mContext.getString(R.string.keyboard_shortcut_group_system_home), |
| 261 | KeyEvent.KEYCODE_ENTER, KeyEvent.META_META_ON)); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 262 | systemGroup.addItem(new KeyboardShortcutInfo( |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 263 | mContext.getString(R.string.keyboard_shortcut_group_system_back), |
| 264 | KeyEvent.KEYCODE_DEL, KeyEvent.META_META_ON)); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 265 | systemGroup.addItem(new KeyboardShortcutInfo( |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 266 | mContext.getString(R.string.keyboard_shortcut_group_system_recents), |
| 267 | KeyEvent.KEYCODE_TAB, KeyEvent.META_ALT_ON)); |
Clara Bayarri | 1d648a1 | 2016-03-23 17:09:02 +0000 | [diff] [blame] | 268 | systemGroup.addItem(new KeyboardShortcutInfo( |
| 269 | mContext.getString( |
| 270 | R.string.keyboard_shortcut_group_system_notifications), |
| 271 | KeyEvent.KEYCODE_N, KeyEvent.META_META_ON)); |
| 272 | systemGroup.addItem(new KeyboardShortcutInfo( |
| 273 | mContext.getString( |
| 274 | R.string.keyboard_shortcut_group_system_shortcuts_helper), |
| 275 | KeyEvent.KEYCODE_SLASH, KeyEvent.META_META_ON)); |
| 276 | systemGroup.addItem(new KeyboardShortcutInfo( |
| 277 | mContext.getString( |
| 278 | R.string.keyboard_shortcut_group_system_switch_input), |
| 279 | KeyEvent.KEYCODE_SPACE, KeyEvent.META_META_ON)); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 280 | result.add(systemGroup); |
Clara Bayarri | 1d648a1 | 2016-03-23 17:09:02 +0000 | [diff] [blame] | 281 | |
| 282 | KeyboardShortcutGroup applicationGroup = new KeyboardShortcutGroup( |
| 283 | mContext.getString(R.string.keyboard_shortcut_group_applications), |
| 284 | true); |
| 285 | applicationGroup.addItem(new KeyboardShortcutInfo( |
| 286 | mContext.getString( |
| 287 | R.string.keyboard_shortcut_group_applications_assist), |
| 288 | KeyEvent.KEYCODE_UNKNOWN, KeyEvent.META_META_ON)); |
| 289 | applicationGroup.addItem(new KeyboardShortcutInfo( |
| 290 | mContext.getString( |
| 291 | R.string.keyboard_shortcut_group_applications_browser), |
| 292 | KeyEvent.KEYCODE_B, KeyEvent.META_META_ON)); |
| 293 | applicationGroup.addItem(new KeyboardShortcutInfo( |
| 294 | mContext.getString( |
| 295 | R.string.keyboard_shortcut_group_applications_contacts), |
| 296 | KeyEvent.KEYCODE_C, KeyEvent.META_META_ON)); |
| 297 | applicationGroup.addItem(new KeyboardShortcutInfo( |
| 298 | mContext.getString( |
| 299 | R.string.keyboard_shortcut_group_applications_email), |
| 300 | KeyEvent.KEYCODE_E, KeyEvent.META_META_ON)); |
| 301 | applicationGroup.addItem(new KeyboardShortcutInfo( |
| 302 | mContext.getString( |
| 303 | R.string.keyboard_shortcut_group_applications_im), |
| 304 | KeyEvent.KEYCODE_T, KeyEvent.META_META_ON)); |
| 305 | applicationGroup.addItem(new KeyboardShortcutInfo( |
| 306 | mContext.getString( |
| 307 | R.string.keyboard_shortcut_group_applications_music), |
| 308 | KeyEvent.KEYCODE_P, KeyEvent.META_META_ON)); |
| 309 | applicationGroup.addItem(new KeyboardShortcutInfo( |
| 310 | mContext.getString( |
| 311 | R.string.keyboard_shortcut_group_applications_youtube), |
| 312 | KeyEvent.KEYCODE_Y, KeyEvent.META_META_ON)); |
| 313 | applicationGroup.addItem(new KeyboardShortcutInfo( |
| 314 | mContext.getString( |
| 315 | R.string.keyboard_shortcut_group_applications_calendar), |
| 316 | KeyEvent.KEYCODE_L, KeyEvent.META_META_ON)); |
| 317 | result.add(applicationGroup); |
| 318 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 319 | showKeyboardShortcutsDialog(result); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 320 | } |
Clara Bayarri | fcd7e80 | 2016-03-10 12:58:18 +0000 | [diff] [blame] | 321 | }, deviceId); |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 322 | } else { |
| 323 | dismissKeyboardShortcutsDialog(); |
| 324 | } |
| 325 | } |
| 326 | |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 327 | public void dismissKeyboardShortcutsDialog() { |
| 328 | if (mKeyboardShortcutsDialog != null) { |
| 329 | mKeyboardShortcutsDialog.dismiss(); |
| 330 | mKeyboardShortcutsDialog = null; |
| 331 | } |
| 332 | } |
| 333 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 334 | private void showKeyboardShortcutsDialog( |
| 335 | final List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 336 | // Need to post on the main thread. |
| 337 | mHandler.post(new Runnable() { |
| 338 | @Override |
| 339 | public void run() { |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 340 | handleShowKeyboardShortcuts(keyboardShortcutGroups); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 341 | } |
| 342 | }); |
| 343 | } |
| 344 | |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 345 | private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 346 | AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext); |
| 347 | LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( |
| 348 | LAYOUT_INFLATER_SERVICE); |
| 349 | final View keyboardShortcutsView = inflater.inflate( |
| 350 | R.layout.keyboard_shortcuts_view, null); |
| 351 | populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById( |
| 352 | R.id.keyboard_shortcuts_container), keyboardShortcutGroups); |
| 353 | dialogBuilder.setView(keyboardShortcutsView); |
| 354 | dialogBuilder.setPositiveButton(R.string.quick_settings_done, dialogCloseListener); |
| 355 | mKeyboardShortcutsDialog = dialogBuilder.create(); |
| 356 | mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true); |
| 357 | Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow(); |
| 358 | keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG); |
| 359 | mKeyboardShortcutsDialog.show(); |
| 360 | } |
| 361 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 362 | private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout, |
| 363 | List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 364 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 365 | final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size(); |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 366 | // Needed to be able to scale the image items to the same height as the text items. |
| 367 | final int shortcutTextItemHeight = getShortcutTextItemHeight(inflater); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 368 | for (int i = 0; i < keyboardShortcutGroupsSize; i++) { |
| 369 | KeyboardShortcutGroup group = keyboardShortcutGroups.get(i); |
| 370 | TextView categoryTitle = (TextView) inflater.inflate( |
| 371 | R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false); |
| 372 | categoryTitle.setText(group.getLabel()); |
| 373 | categoryTitle.setTextColor(group.isSystemGroup() |
| 374 | ? mContext.getColor(R.color.ksh_system_group_color) |
| 375 | : mContext.getColor(R.color.ksh_application_group_color)); |
| 376 | keyboardShortcutsLayout.addView(categoryTitle); |
| 377 | |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 378 | LinearLayout shortcutContainer = (LinearLayout) inflater.inflate( |
| 379 | R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 380 | final int itemsSize = group.getItems().size(); |
| 381 | for (int j = 0; j < itemsSize; j++) { |
| 382 | KeyboardShortcutInfo info = group.getItems().get(j); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 383 | if (info.getKeycode() != KeyEvent.KEYCODE_UNKNOWN |
| 384 | && !KeyCharacterMap.deviceHasKey(info.getKeycode())) { |
| 385 | // The user can't achieve this shortcut, so skipping. |
| 386 | Log.w(TAG, "Keyboard Shortcut contains key not on device, skipping."); |
| 387 | continue; |
| 388 | } |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 389 | List<StringOrDrawable> shortcutKeys = getHumanReadableShortcutKeys(info); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 390 | if (shortcutKeys == null) { |
| 391 | // Ignore shortcuts we can't display keys for. |
| 392 | Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping."); |
| 393 | continue; |
| 394 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 395 | View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item, |
| 396 | shortcutContainer, false); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 397 | TextView textView = (TextView) shortcutView |
| 398 | .findViewById(R.id.keyboard_shortcuts_keyword); |
| 399 | textView.setText(info.getLabel()); |
| 400 | |
Andrei Stingaceanu | 844927d | 2016-02-16 14:31:58 +0000 | [diff] [blame] | 401 | ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 402 | .findViewById(R.id.keyboard_shortcuts_item_container); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 403 | final int shortcutKeysSize = shortcutKeys.size(); |
| 404 | for (int k = 0; k < shortcutKeysSize; k++) { |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 405 | StringOrDrawable shortcutRepresentation = shortcutKeys.get(k); |
| 406 | if (shortcutRepresentation.drawable != null) { |
| 407 | ImageView shortcutKeyIconView = (ImageView) inflater.inflate( |
| 408 | R.layout.keyboard_shortcuts_key_icon_view, shortcutItemsContainer, |
| 409 | false); |
| 410 | Bitmap bitmap = Bitmap.createBitmap(shortcutTextItemHeight, |
| 411 | shortcutTextItemHeight, Bitmap.Config.ARGB_8888); |
| 412 | Canvas canvas = new Canvas(bitmap); |
| 413 | shortcutRepresentation.drawable.setBounds(0, 0, canvas.getWidth(), |
| 414 | canvas.getHeight()); |
| 415 | shortcutRepresentation.drawable.draw(canvas); |
| 416 | shortcutKeyIconView.setImageBitmap(bitmap); |
| 417 | shortcutItemsContainer.addView(shortcutKeyIconView); |
| 418 | } else if (shortcutRepresentation.string != null) { |
| 419 | TextView shortcutKeyTextView = (TextView) inflater.inflate( |
| 420 | R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer, |
| 421 | false); |
| 422 | shortcutKeyTextView.setText(shortcutRepresentation.string); |
| 423 | shortcutItemsContainer.addView(shortcutKeyTextView); |
| 424 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 425 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 426 | shortcutContainer.addView(shortcutView); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 427 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 428 | keyboardShortcutsLayout.addView(shortcutContainer); |
| 429 | if (i < keyboardShortcutGroupsSize - 1) { |
| 430 | View separator = inflater.inflate( |
| 431 | R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout, |
| 432 | false); |
| 433 | keyboardShortcutsLayout.addView(separator); |
| 434 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 438 | private int getShortcutTextItemHeight(LayoutInflater inflater) { |
| 439 | TextView shortcutKeyTextView = (TextView) inflater.inflate( |
| 440 | R.layout.keyboard_shortcuts_key_view, null, false); |
| 441 | shortcutKeyTextView.measure( |
| 442 | View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); |
| 443 | return shortcutKeyTextView.getMeasuredHeight() |
| 444 | - shortcutKeyTextView.getPaddingTop() |
| 445 | - shortcutKeyTextView.getPaddingBottom(); |
| 446 | } |
| 447 | |
| 448 | private List<StringOrDrawable> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) { |
| 449 | List<StringOrDrawable> shortcutKeys = getHumanReadableModifiers(info); |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 450 | if (shortcutKeys == null) { |
| 451 | return null; |
| 452 | } |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 453 | String displayLabelString = null; |
| 454 | Drawable displayLabelDrawable = null; |
Clara Bayarri | 1d648a1 | 2016-03-23 17:09:02 +0000 | [diff] [blame] | 455 | if (info.getBaseCharacter() > Character.MIN_VALUE) { |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 456 | displayLabelString = String.valueOf(info.getBaseCharacter()); |
Clara Bayarri | b999af5 | 2016-04-06 16:02:35 +0100 | [diff] [blame^] | 457 | } else if (mSpecialCharacterDrawables.get(info.getKeycode()) != null) { |
| 458 | displayLabelDrawable = mSpecialCharacterDrawables.get(info.getKeycode()); |
| 459 | } else if (mSpecialCharacterNames.get(info.getKeycode()) != null) { |
| 460 | displayLabelString = mSpecialCharacterNames.get(info.getKeycode()); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 461 | } else { |
Clara Bayarri | 1d648a1 | 2016-03-23 17:09:02 +0000 | [diff] [blame] | 462 | // Special case for shortcuts with no base key or keycode. |
| 463 | if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) { |
| 464 | return shortcutKeys; |
| 465 | } |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 466 | // TODO: Have a generic map for when we don't have the device's. |
| 467 | char displayLabel = mKeyCharacterMap == null |
| 468 | ? 0 : mKeyCharacterMap.getDisplayLabel(info.getKeycode()); |
| 469 | if (displayLabel != 0) { |
| 470 | displayLabelString = String.valueOf(displayLabel); |
| 471 | } else { |
| 472 | return null; |
| 473 | } |
| 474 | } |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 475 | |
| 476 | if (displayLabelDrawable != null) { |
| 477 | shortcutKeys.add(new StringOrDrawable(displayLabelDrawable)); |
| 478 | } else if (displayLabelString != null) { |
| 479 | shortcutKeys.add(new StringOrDrawable(displayLabelString.toUpperCase())); |
| 480 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 481 | return shortcutKeys; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 482 | } |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 483 | |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 484 | private List<StringOrDrawable> getHumanReadableModifiers(KeyboardShortcutInfo info) { |
| 485 | final List<StringOrDrawable> shortcutKeys = new ArrayList<>(); |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 486 | int modifiers = info.getModifiers(); |
| 487 | if (modifiers == 0) { |
| 488 | return shortcutKeys; |
| 489 | } |
Clara Bayarri | b999af5 | 2016-04-06 16:02:35 +0100 | [diff] [blame^] | 490 | for(int i = 0; i < mModifierNames.size(); ++i) { |
| 491 | final int supportedModifier = mModifierNames.keyAt(i); |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 492 | if ((modifiers & supportedModifier) != 0) { |
Clara Bayarri | b999af5 | 2016-04-06 16:02:35 +0100 | [diff] [blame^] | 493 | if (mModifierDrawables.get(supportedModifier) != null) { |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 494 | shortcutKeys.add(new StringOrDrawable( |
Clara Bayarri | b999af5 | 2016-04-06 16:02:35 +0100 | [diff] [blame^] | 495 | mModifierDrawables.get(supportedModifier))); |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 496 | } else { |
| 497 | shortcutKeys.add(new StringOrDrawable( |
Clara Bayarri | b999af5 | 2016-04-06 16:02:35 +0100 | [diff] [blame^] | 498 | mModifierNames.get(supportedModifier).toUpperCase())); |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 499 | } |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 500 | modifiers &= ~supportedModifier; |
| 501 | } |
| 502 | } |
| 503 | if (modifiers != 0) { |
| 504 | // Remaining unsupported modifiers, don't show anything. |
| 505 | return null; |
| 506 | } |
| 507 | return shortcutKeys; |
| 508 | } |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 509 | |
| 510 | private static final class StringOrDrawable { |
| 511 | public String string; |
| 512 | public Drawable drawable; |
| 513 | |
| 514 | public StringOrDrawable(String string) { |
| 515 | this.string = string; |
| 516 | } |
| 517 | |
| 518 | public StringOrDrawable(Drawable drawable) { |
| 519 | this.drawable = drawable; |
| 520 | } |
| 521 | } |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 522 | } |