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