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; |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 20 | import android.app.AppGlobals; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 21 | import android.app.Dialog; |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 22 | import android.content.ComponentName; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 23 | import android.content.Context; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 24 | import android.content.DialogInterface; |
| 25 | import android.content.DialogInterface.OnClickListener; |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 26 | import android.content.Intent; |
| 27 | import android.content.pm.IPackageManager; |
| 28 | import android.content.pm.PackageInfo; |
| 29 | import android.content.pm.ResolveInfo; |
| 30 | import android.graphics.drawable.Icon; |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 31 | import android.graphics.Bitmap; |
| 32 | import android.graphics.Canvas; |
| 33 | import android.graphics.drawable.Drawable; |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 34 | import android.hardware.input.InputManager; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 35 | import android.os.Handler; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 36 | import android.os.Looper; |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 37 | import android.os.RemoteException; |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 38 | import android.util.Log; |
| 39 | import android.util.SparseArray; |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 40 | import android.view.ContextThemeWrapper; |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 41 | import android.view.InputDevice; |
| 42 | import android.view.KeyCharacterMap; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 43 | import android.view.KeyEvent; |
| 44 | import android.view.KeyboardShortcutGroup; |
| 45 | import android.view.KeyboardShortcutInfo; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 46 | import android.view.LayoutInflater; |
| 47 | import android.view.View; |
Andrei Stingaceanu | 844927d | 2016-02-16 14:31:58 +0000 | [diff] [blame] | 48 | import android.view.ViewGroup; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 49 | import android.view.Window; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 50 | import android.view.WindowManager.KeyboardShortcutsReceiver; |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 51 | import android.widget.ImageView; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 52 | import android.widget.LinearLayout; |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 53 | import android.widget.RelativeLayout; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 54 | import android.widget.TextView; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 55 | |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 56 | import com.android.internal.app.AssistUtils; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 57 | import com.android.systemui.R; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 58 | import com.android.systemui.recents.Recents; |
| 59 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 60 | import java.util.ArrayList; |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 61 | import java.util.Collections; |
| 62 | import java.util.Comparator; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 63 | import java.util.List; |
| 64 | |
| 65 | import static android.content.Context.LAYOUT_INFLATER_SERVICE; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 66 | import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Contains functionality for handling keyboard shortcuts. |
| 70 | */ |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 71 | public final class KeyboardShortcuts { |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 72 | private static final String TAG = KeyboardShortcuts.class.getSimpleName(); |
Clara Bayarri | b999af5 | 2016-04-06 16:02:35 +0100 | [diff] [blame] | 73 | private final SparseArray<String> mSpecialCharacterNames = new SparseArray<>(); |
| 74 | private final SparseArray<String> mModifierNames = new SparseArray<>(); |
| 75 | private final SparseArray<Drawable> mSpecialCharacterDrawables = new SparseArray<>(); |
| 76 | private final SparseArray<Drawable> mModifierDrawables = new SparseArray<>(); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 77 | |
| 78 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
| 79 | private final Context mContext; |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 80 | private final IPackageManager mPackageManager; |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 81 | private final OnClickListener mDialogCloseListener = new DialogInterface.OnClickListener() { |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 82 | public void onClick(DialogInterface dialog, int id) { |
| 83 | dismissKeyboardShortcutsDialog(); |
| 84 | } |
| 85 | }; |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 86 | private final Comparator<KeyboardShortcutInfo> mApplicationItemsComparator = |
| 87 | new Comparator<KeyboardShortcutInfo>() { |
| 88 | @Override |
| 89 | public int compare(KeyboardShortcutInfo ksh1, KeyboardShortcutInfo ksh2) { |
| 90 | boolean ksh1ShouldBeLast = ksh1.getLabel() == null |
| 91 | || ksh1.getLabel().toString().isEmpty(); |
| 92 | boolean ksh2ShouldBeLast = ksh2.getLabel() == null |
| 93 | || ksh2.getLabel().toString().isEmpty(); |
| 94 | if (ksh1ShouldBeLast && ksh2ShouldBeLast) { |
| 95 | return 0; |
| 96 | } |
| 97 | if (ksh1ShouldBeLast) { |
| 98 | return 1; |
| 99 | } |
| 100 | if (ksh2ShouldBeLast) { |
| 101 | return -1; |
| 102 | } |
| 103 | return (ksh1.getLabel().toString()).compareToIgnoreCase( |
| 104 | ksh2.getLabel().toString()); |
| 105 | } |
| 106 | }; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 107 | |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 108 | private Dialog mKeyboardShortcutsDialog; |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 109 | private KeyCharacterMap mKeyCharacterMap; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 110 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 111 | public KeyboardShortcuts(Context context) { |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 112 | this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_Material_Light); |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 113 | this.mPackageManager = AppGlobals.getPackageManager(); |
Clara Bayarri | b999af5 | 2016-04-06 16:02:35 +0100 | [diff] [blame] | 114 | loadResources(context); |
| 115 | } |
| 116 | |
| 117 | private void loadResources(Context context) { |
| 118 | mSpecialCharacterNames.put( |
| 119 | KeyEvent.KEYCODE_HOME, context.getString(R.string.keyboard_key_home)); |
| 120 | mSpecialCharacterNames.put( |
| 121 | KeyEvent.KEYCODE_BACK, context.getString(R.string.keyboard_key_back)); |
| 122 | mSpecialCharacterNames.put( |
| 123 | KeyEvent.KEYCODE_DPAD_UP, context.getString(R.string.keyboard_key_dpad_up)); |
| 124 | mSpecialCharacterNames.put( |
| 125 | KeyEvent.KEYCODE_DPAD_DOWN, context.getString(R.string.keyboard_key_dpad_down)); |
| 126 | mSpecialCharacterNames.put( |
| 127 | KeyEvent.KEYCODE_DPAD_LEFT, context.getString(R.string.keyboard_key_dpad_left)); |
| 128 | mSpecialCharacterNames.put( |
| 129 | KeyEvent.KEYCODE_DPAD_RIGHT, context.getString(R.string.keyboard_key_dpad_right)); |
| 130 | mSpecialCharacterNames.put( |
| 131 | KeyEvent.KEYCODE_DPAD_CENTER, context.getString(R.string.keyboard_key_dpad_center)); |
| 132 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_PERIOD, "."); |
| 133 | mSpecialCharacterNames.put( |
| 134 | KeyEvent.KEYCODE_TAB, context.getString(R.string.keyboard_key_tab)); |
| 135 | mSpecialCharacterNames.put( |
| 136 | KeyEvent.KEYCODE_SPACE, context.getString(R.string.keyboard_key_space)); |
| 137 | mSpecialCharacterNames.put( |
| 138 | KeyEvent.KEYCODE_ENTER, context.getString(R.string.keyboard_key_enter)); |
| 139 | mSpecialCharacterNames.put( |
| 140 | KeyEvent.KEYCODE_DEL, context.getString(R.string.keyboard_key_backspace)); |
| 141 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, |
| 142 | context.getString(R.string.keyboard_key_media_play_pause)); |
| 143 | mSpecialCharacterNames.put( |
| 144 | KeyEvent.KEYCODE_MEDIA_STOP, context.getString(R.string.keyboard_key_media_stop)); |
| 145 | mSpecialCharacterNames.put( |
| 146 | KeyEvent.KEYCODE_MEDIA_NEXT, context.getString(R.string.keyboard_key_media_next)); |
| 147 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_PREVIOUS, |
| 148 | context.getString(R.string.keyboard_key_media_previous)); |
| 149 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_REWIND, |
| 150 | context.getString(R.string.keyboard_key_media_rewind)); |
| 151 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD, |
| 152 | context.getString(R.string.keyboard_key_media_fast_forward)); |
| 153 | mSpecialCharacterNames.put( |
| 154 | KeyEvent.KEYCODE_PAGE_UP, context.getString(R.string.keyboard_key_page_up)); |
| 155 | mSpecialCharacterNames.put( |
| 156 | KeyEvent.KEYCODE_PAGE_DOWN, context.getString(R.string.keyboard_key_page_down)); |
| 157 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_A, |
| 158 | context.getString(R.string.keyboard_key_button_template, "A")); |
| 159 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_B, |
| 160 | context.getString(R.string.keyboard_key_button_template, "B")); |
| 161 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_C, |
| 162 | context.getString(R.string.keyboard_key_button_template, "C")); |
| 163 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_X, |
| 164 | context.getString(R.string.keyboard_key_button_template, "X")); |
| 165 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Y, |
| 166 | context.getString(R.string.keyboard_key_button_template, "Y")); |
| 167 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_Z, |
| 168 | context.getString(R.string.keyboard_key_button_template, "Z")); |
| 169 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L1, |
| 170 | context.getString(R.string.keyboard_key_button_template, "L1")); |
| 171 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R1, |
| 172 | context.getString(R.string.keyboard_key_button_template, "R1")); |
| 173 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_L2, |
| 174 | context.getString(R.string.keyboard_key_button_template, "L2")); |
| 175 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_R2, |
| 176 | context.getString(R.string.keyboard_key_button_template, "R2")); |
| 177 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_START, |
| 178 | context.getString(R.string.keyboard_key_button_template, "Start")); |
| 179 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_SELECT, |
| 180 | context.getString(R.string.keyboard_key_button_template, "Select")); |
| 181 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BUTTON_MODE, |
| 182 | context.getString(R.string.keyboard_key_button_template, "Mode")); |
| 183 | mSpecialCharacterNames.put( |
| 184 | KeyEvent.KEYCODE_FORWARD_DEL, context.getString(R.string.keyboard_key_forward_del)); |
| 185 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_ESCAPE, "Esc"); |
| 186 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_SYSRQ, "SysRq"); |
| 187 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_BREAK, "Break"); |
| 188 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_SCROLL_LOCK, "Scroll Lock"); |
| 189 | mSpecialCharacterNames.put( |
| 190 | KeyEvent.KEYCODE_MOVE_HOME, context.getString(R.string.keyboard_key_move_home)); |
| 191 | mSpecialCharacterNames.put( |
| 192 | KeyEvent.KEYCODE_MOVE_END, context.getString(R.string.keyboard_key_move_end)); |
| 193 | mSpecialCharacterNames.put( |
| 194 | KeyEvent.KEYCODE_INSERT, context.getString(R.string.keyboard_key_insert)); |
| 195 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F1, "F1"); |
| 196 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F2, "F2"); |
| 197 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F3, "F3"); |
| 198 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F4, "F4"); |
| 199 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F5, "F5"); |
| 200 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F6, "F6"); |
| 201 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F7, "F7"); |
| 202 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F8, "F8"); |
| 203 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F9, "F9"); |
| 204 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F10, "F10"); |
| 205 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F11, "F11"); |
| 206 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_F12, "F12"); |
| 207 | mSpecialCharacterNames.put( |
| 208 | KeyEvent.KEYCODE_NUM_LOCK, context.getString(R.string.keyboard_key_num_lock)); |
| 209 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_0, |
| 210 | context.getString(R.string.keyboard_key_numpad_template, "0")); |
| 211 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_1, |
| 212 | context.getString(R.string.keyboard_key_numpad_template, "1")); |
| 213 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_2, |
| 214 | context.getString(R.string.keyboard_key_numpad_template, "2")); |
| 215 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_3, |
| 216 | context.getString(R.string.keyboard_key_numpad_template, "3")); |
| 217 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_4, |
| 218 | context.getString(R.string.keyboard_key_numpad_template, "4")); |
| 219 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_5, |
| 220 | context.getString(R.string.keyboard_key_numpad_template, "5")); |
| 221 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_6, |
| 222 | context.getString(R.string.keyboard_key_numpad_template, "6")); |
| 223 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_7, |
| 224 | context.getString(R.string.keyboard_key_numpad_template, "7")); |
| 225 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_8, |
| 226 | context.getString(R.string.keyboard_key_numpad_template, "8")); |
| 227 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_9, |
| 228 | context.getString(R.string.keyboard_key_numpad_template, "9")); |
| 229 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DIVIDE, |
| 230 | context.getString(R.string.keyboard_key_numpad_template, "/")); |
| 231 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_MULTIPLY, |
| 232 | context.getString(R.string.keyboard_key_numpad_template, "*")); |
| 233 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_SUBTRACT, |
| 234 | context.getString(R.string.keyboard_key_numpad_template, "-")); |
| 235 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ADD, |
| 236 | context.getString(R.string.keyboard_key_numpad_template, "+")); |
| 237 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_DOT, |
| 238 | context.getString(R.string.keyboard_key_numpad_template, ".")); |
| 239 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_COMMA, |
| 240 | context.getString(R.string.keyboard_key_numpad_template, ",")); |
| 241 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_ENTER, |
| 242 | context.getString(R.string.keyboard_key_numpad_template, |
| 243 | context.getString(R.string.keyboard_key_enter))); |
| 244 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_EQUALS, |
| 245 | context.getString(R.string.keyboard_key_numpad_template, "=")); |
| 246 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_LEFT_PAREN, |
| 247 | context.getString(R.string.keyboard_key_numpad_template, "(")); |
| 248 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_NUMPAD_RIGHT_PAREN, |
| 249 | context.getString(R.string.keyboard_key_numpad_template, ")")); |
| 250 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_ZENKAKU_HANKAKU, "半角/全角"); |
| 251 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_EISU, "英数"); |
| 252 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_MUHENKAN, "無変換"); |
| 253 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_HENKAN, "変換"); |
| 254 | mSpecialCharacterNames.put(KeyEvent.KEYCODE_KATAKANA_HIRAGANA, "かな"); |
| 255 | |
| 256 | mModifierNames.put(KeyEvent.META_META_ON, "Meta"); |
| 257 | mModifierNames.put(KeyEvent.META_CTRL_ON, "Ctrl"); |
| 258 | mModifierNames.put(KeyEvent.META_ALT_ON, "Alt"); |
| 259 | mModifierNames.put(KeyEvent.META_SHIFT_ON, "Shift"); |
| 260 | mModifierNames.put(KeyEvent.META_SYM_ON, "Sym"); |
| 261 | mModifierNames.put(KeyEvent.META_FUNCTION_ON, "Fn"); |
| 262 | |
| 263 | mSpecialCharacterDrawables.put( |
| 264 | KeyEvent.KEYCODE_DEL, context.getDrawable(R.drawable.ic_ksh_key_backspace)); |
| 265 | mSpecialCharacterDrawables.put( |
| 266 | KeyEvent.KEYCODE_ENTER, context.getDrawable(R.drawable.ic_ksh_key_enter)); |
| 267 | mSpecialCharacterDrawables.put( |
| 268 | KeyEvent.KEYCODE_DPAD_UP, context.getDrawable(R.drawable.ic_ksh_key_up)); |
| 269 | mSpecialCharacterDrawables.put( |
| 270 | KeyEvent.KEYCODE_DPAD_RIGHT, context.getDrawable(R.drawable.ic_ksh_key_right)); |
| 271 | mSpecialCharacterDrawables.put( |
| 272 | KeyEvent.KEYCODE_DPAD_DOWN, context.getDrawable(R.drawable.ic_ksh_key_down)); |
| 273 | mSpecialCharacterDrawables.put( |
| 274 | KeyEvent.KEYCODE_DPAD_LEFT, context.getDrawable(R.drawable.ic_ksh_key_left)); |
| 275 | |
| 276 | mModifierDrawables.put( |
| 277 | KeyEvent.META_META_ON, context.getDrawable(R.drawable.ic_ksh_key_meta)); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 278 | } |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 279 | |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 280 | public void toggleKeyboardShortcuts(int deviceId) { |
Clara Bayarri | 03f1955 | 2016-04-06 10:59:52 +0100 | [diff] [blame] | 281 | retrieveKeyCharacterMap(deviceId); |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 282 | if (mKeyboardShortcutsDialog == null) { |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 283 | Recents.getSystemServices().requestKeyboardShortcuts(mContext, |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 284 | new KeyboardShortcutsReceiver() { |
| 285 | @Override |
| 286 | public void onKeyboardShortcutsReceived( |
| 287 | final List<KeyboardShortcutGroup> result) { |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 288 | result.add(getSystemShortcuts()); |
| 289 | final KeyboardShortcutGroup appShortcuts = getDefaultApplicationShortcuts(); |
| 290 | if (appShortcuts != null) { |
| 291 | result.add(appShortcuts); |
| 292 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 293 | showKeyboardShortcutsDialog(result); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 294 | } |
Clara Bayarri | fcd7e80 | 2016-03-10 12:58:18 +0000 | [diff] [blame] | 295 | }, deviceId); |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 296 | } else { |
| 297 | dismissKeyboardShortcutsDialog(); |
| 298 | } |
| 299 | } |
| 300 | |
Clara Bayarri | 03f1955 | 2016-04-06 10:59:52 +0100 | [diff] [blame] | 301 | /** |
| 302 | * Retrieves a {@link KeyCharacterMap} and assigns it to mKeyCharacterMap. If the given id is an |
| 303 | * existing device, that device's map is used. Otherwise, it checks first all available devices |
| 304 | * and if there is a full keyboard it uses that map, otherwise falls back to the Virtual |
| 305 | * Keyboard with its default map. |
| 306 | */ |
| 307 | private void retrieveKeyCharacterMap(int deviceId) { |
| 308 | final InputManager inputManager = InputManager.getInstance(); |
| 309 | if (deviceId != -1) { |
| 310 | final InputDevice inputDevice = inputManager.getInputDevice(deviceId); |
| 311 | if (inputDevice != null) { |
| 312 | mKeyCharacterMap = inputDevice.getKeyCharacterMap(); |
| 313 | return; |
| 314 | } |
| 315 | } |
| 316 | final int[] deviceIds = inputManager.getInputDeviceIds(); |
| 317 | for (int i = 0; i < deviceIds.length; ++i) { |
| 318 | final InputDevice inputDevice = inputManager.getInputDevice(deviceIds[i]); |
| 319 | // -1 is the Virtual Keyboard, with the default key map. Use that one only as last |
| 320 | // resort. |
| 321 | if (inputDevice.getId() != -1 && inputDevice.isFullKeyboard()) { |
| 322 | mKeyCharacterMap = inputDevice.getKeyCharacterMap(); |
| 323 | return; |
| 324 | } |
| 325 | } |
| 326 | final InputDevice inputDevice = inputManager.getInputDevice(-1); |
| 327 | mKeyCharacterMap = inputDevice.getKeyCharacterMap(); |
| 328 | } |
| 329 | |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 330 | public void dismissKeyboardShortcutsDialog() { |
| 331 | if (mKeyboardShortcutsDialog != null) { |
| 332 | mKeyboardShortcutsDialog.dismiss(); |
| 333 | mKeyboardShortcutsDialog = null; |
| 334 | } |
| 335 | } |
| 336 | |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 337 | private KeyboardShortcutGroup getSystemShortcuts() { |
| 338 | final KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup( |
| 339 | mContext.getString(R.string.keyboard_shortcut_group_system), true); |
| 340 | systemGroup.addItem(new KeyboardShortcutInfo( |
| 341 | mContext.getString(R.string.keyboard_shortcut_group_system_home), |
| 342 | KeyEvent.KEYCODE_ENTER, |
| 343 | KeyEvent.META_META_ON)); |
| 344 | systemGroup.addItem(new KeyboardShortcutInfo( |
| 345 | mContext.getString(R.string.keyboard_shortcut_group_system_back), |
| 346 | KeyEvent.KEYCODE_DEL, |
| 347 | KeyEvent.META_META_ON)); |
| 348 | systemGroup.addItem(new KeyboardShortcutInfo( |
| 349 | mContext.getString(R.string.keyboard_shortcut_group_system_recents), |
| 350 | KeyEvent.KEYCODE_TAB, |
| 351 | KeyEvent.META_ALT_ON)); |
| 352 | systemGroup.addItem(new KeyboardShortcutInfo( |
| 353 | mContext.getString( |
| 354 | R.string.keyboard_shortcut_group_system_notifications), |
| 355 | KeyEvent.KEYCODE_N, |
| 356 | KeyEvent.META_META_ON)); |
| 357 | systemGroup.addItem(new KeyboardShortcutInfo( |
| 358 | mContext.getString( |
| 359 | R.string.keyboard_shortcut_group_system_shortcuts_helper), |
| 360 | KeyEvent.KEYCODE_SLASH, |
| 361 | KeyEvent.META_META_ON)); |
| 362 | systemGroup.addItem(new KeyboardShortcutInfo( |
| 363 | mContext.getString( |
| 364 | R.string.keyboard_shortcut_group_system_switch_input), |
| 365 | KeyEvent.KEYCODE_SPACE, |
| 366 | KeyEvent.META_META_ON)); |
| 367 | return systemGroup; |
| 368 | } |
| 369 | |
| 370 | private KeyboardShortcutGroup getDefaultApplicationShortcuts() { |
| 371 | final int userId = mContext.getUserId(); |
Andrei Stingaceanu | 9cfcafc | 2016-04-12 11:07:39 +0100 | [diff] [blame^] | 372 | List<KeyboardShortcutInfo> keyboardShortcutInfoAppItems = new ArrayList<>(); |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 373 | |
| 374 | // Assist. |
| 375 | final AssistUtils assistUtils = new AssistUtils(mContext); |
| 376 | final ComponentName assistComponent = assistUtils.getAssistComponentForUser(userId); |
| 377 | PackageInfo assistPackageInfo = null; |
| 378 | try { |
| 379 | assistPackageInfo = mPackageManager.getPackageInfo( |
| 380 | assistComponent.getPackageName(), 0, userId); |
| 381 | } catch (RemoteException e) { |
| 382 | Log.e(TAG, "PackageManagerService is dead"); |
| 383 | } |
| 384 | |
| 385 | if (assistPackageInfo != null) { |
| 386 | final Icon assistIcon = Icon.createWithResource( |
| 387 | assistPackageInfo.applicationInfo.packageName, |
| 388 | assistPackageInfo.applicationInfo.icon); |
| 389 | |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 390 | keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 391 | mContext.getString(R.string.keyboard_shortcut_group_applications_assist), |
| 392 | assistIcon, |
| 393 | KeyEvent.KEYCODE_UNKNOWN, |
| 394 | KeyEvent.META_META_ON)); |
| 395 | } |
| 396 | |
| 397 | // Browser. |
| 398 | final Icon browserIcon = getIconForIntentCategory(Intent.CATEGORY_APP_BROWSER, userId); |
| 399 | if (browserIcon != null) { |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 400 | keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 401 | mContext.getString(R.string.keyboard_shortcut_group_applications_browser), |
| 402 | browserIcon, |
| 403 | KeyEvent.KEYCODE_B, |
| 404 | KeyEvent.META_META_ON)); |
| 405 | } |
| 406 | |
| 407 | |
| 408 | // Contacts. |
| 409 | final Icon contactsIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CONTACTS, userId); |
| 410 | if (contactsIcon != null) { |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 411 | keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 412 | mContext.getString(R.string.keyboard_shortcut_group_applications_contacts), |
| 413 | contactsIcon, |
| 414 | KeyEvent.KEYCODE_C, |
| 415 | KeyEvent.META_META_ON)); |
| 416 | } |
| 417 | |
| 418 | // Email. |
| 419 | final Icon emailIcon = getIconForIntentCategory(Intent.CATEGORY_APP_EMAIL, userId); |
| 420 | if (emailIcon != null) { |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 421 | keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 422 | mContext.getString(R.string.keyboard_shortcut_group_applications_email), |
| 423 | emailIcon, |
| 424 | KeyEvent.KEYCODE_E, |
| 425 | KeyEvent.META_META_ON)); |
| 426 | } |
| 427 | |
| 428 | // Messaging. |
| 429 | final Icon messagingIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MESSAGING, userId); |
| 430 | if (messagingIcon != null) { |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 431 | keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 432 | mContext.getString(R.string.keyboard_shortcut_group_applications_im), |
| 433 | messagingIcon, |
| 434 | KeyEvent.KEYCODE_T, |
| 435 | KeyEvent.META_META_ON)); |
| 436 | } |
| 437 | |
| 438 | // Music. |
| 439 | final Icon musicIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MUSIC, userId); |
| 440 | if (musicIcon != null) { |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 441 | keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 442 | mContext.getString(R.string.keyboard_shortcut_group_applications_music), |
| 443 | musicIcon, |
| 444 | KeyEvent.KEYCODE_P, |
| 445 | KeyEvent.META_META_ON)); |
| 446 | } |
| 447 | |
| 448 | // Calendar. |
| 449 | final Icon calendarIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CALENDAR, userId); |
| 450 | if (calendarIcon != null) { |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 451 | keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 452 | mContext.getString(R.string.keyboard_shortcut_group_applications_calendar), |
| 453 | calendarIcon, |
| 454 | KeyEvent.KEYCODE_L, |
| 455 | KeyEvent.META_META_ON)); |
| 456 | } |
| 457 | |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 458 | final int itemsSize = keyboardShortcutInfoAppItems.size(); |
| 459 | if (itemsSize == 0) { |
| 460 | return null; |
| 461 | } |
| 462 | |
| 463 | // Sorts by label, case insensitive with nulls and/or empty labels last. |
| 464 | Collections.sort(keyboardShortcutInfoAppItems, mApplicationItemsComparator); |
| 465 | return new KeyboardShortcutGroup( |
| 466 | mContext.getString(R.string.keyboard_shortcut_group_applications), |
| 467 | keyboardShortcutInfoAppItems, |
| 468 | true); |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | private Icon getIconForIntentCategory(String intentCategory, int userId) { |
| 472 | final Intent intent = new Intent(Intent.ACTION_MAIN); |
| 473 | intent.addCategory(intentCategory); |
| 474 | |
| 475 | final PackageInfo packageInfo = getPackageInfoForIntent(intent, userId); |
| 476 | if (packageInfo != null && packageInfo.applicationInfo.icon != 0) { |
| 477 | return Icon.createWithResource( |
| 478 | packageInfo.applicationInfo.packageName, |
| 479 | packageInfo.applicationInfo.icon); |
| 480 | } |
| 481 | return null; |
| 482 | } |
| 483 | |
| 484 | private PackageInfo getPackageInfoForIntent(Intent intent, int userId) { |
| 485 | try { |
| 486 | ResolveInfo handler; |
| 487 | handler = mPackageManager.resolveIntent( |
| 488 | intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()), 0, userId); |
| 489 | if (handler == null || handler.activityInfo == null) { |
| 490 | return null; |
| 491 | } |
| 492 | return mPackageManager.getPackageInfo(handler.activityInfo.packageName, 0, userId); |
| 493 | } catch (RemoteException e) { |
| 494 | Log.e(TAG, "PackageManagerService is dead", e); |
| 495 | return null; |
| 496 | } |
| 497 | } |
| 498 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 499 | private void showKeyboardShortcutsDialog( |
| 500 | final List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 501 | // Need to post on the main thread. |
| 502 | mHandler.post(new Runnable() { |
| 503 | @Override |
| 504 | public void run() { |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 505 | handleShowKeyboardShortcuts(keyboardShortcutGroups); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 506 | } |
| 507 | }); |
| 508 | } |
| 509 | |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 510 | private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 511 | AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext); |
| 512 | LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( |
| 513 | LAYOUT_INFLATER_SERVICE); |
| 514 | final View keyboardShortcutsView = inflater.inflate( |
| 515 | R.layout.keyboard_shortcuts_view, null); |
| 516 | populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById( |
| 517 | R.id.keyboard_shortcuts_container), keyboardShortcutGroups); |
| 518 | dialogBuilder.setView(keyboardShortcutsView); |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 519 | dialogBuilder.setPositiveButton(R.string.quick_settings_done, mDialogCloseListener); |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 520 | mKeyboardShortcutsDialog = dialogBuilder.create(); |
| 521 | mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true); |
| 522 | Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow(); |
| 523 | keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG); |
| 524 | mKeyboardShortcutsDialog.show(); |
| 525 | } |
| 526 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 527 | private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout, |
| 528 | List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 529 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 530 | final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size(); |
Andrei Stingaceanu | a02965e | 2016-04-08 16:42:02 +0100 | [diff] [blame] | 531 | TextView shortcutsKeyView = (TextView) inflater.inflate( |
| 532 | R.layout.keyboard_shortcuts_key_view, null, false); |
| 533 | shortcutsKeyView.measure( |
| 534 | View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); |
| 535 | final int shortcutKeyTextItemMinWidth = shortcutsKeyView.getMeasuredHeight(); |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 536 | // Needed to be able to scale the image items to the same height as the text items. |
Andrei Stingaceanu | a02965e | 2016-04-08 16:42:02 +0100 | [diff] [blame] | 537 | final int shortcutKeyIconItemHeightWidth = shortcutsKeyView.getMeasuredHeight() |
| 538 | - shortcutsKeyView.getPaddingTop() |
| 539 | - shortcutsKeyView.getPaddingBottom(); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 540 | for (int i = 0; i < keyboardShortcutGroupsSize; i++) { |
| 541 | KeyboardShortcutGroup group = keyboardShortcutGroups.get(i); |
| 542 | TextView categoryTitle = (TextView) inflater.inflate( |
| 543 | R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false); |
| 544 | categoryTitle.setText(group.getLabel()); |
| 545 | categoryTitle.setTextColor(group.isSystemGroup() |
| 546 | ? mContext.getColor(R.color.ksh_system_group_color) |
| 547 | : mContext.getColor(R.color.ksh_application_group_color)); |
| 548 | keyboardShortcutsLayout.addView(categoryTitle); |
| 549 | |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 550 | LinearLayout shortcutContainer = (LinearLayout) inflater.inflate( |
| 551 | R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 552 | final int itemsSize = group.getItems().size(); |
| 553 | for (int j = 0; j < itemsSize; j++) { |
| 554 | KeyboardShortcutInfo info = group.getItems().get(j); |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 555 | List<StringOrDrawable> shortcutKeys = getHumanReadableShortcutKeys(info); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 556 | if (shortcutKeys == null) { |
| 557 | // Ignore shortcuts we can't display keys for. |
| 558 | Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping."); |
| 559 | continue; |
| 560 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 561 | View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item, |
| 562 | shortcutContainer, false); |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 563 | |
| 564 | if (info.getIcon() != null) { |
| 565 | ImageView shortcutIcon = (ImageView) shortcutView |
| 566 | .findViewById(R.id.keyboard_shortcuts_icon); |
| 567 | shortcutIcon.setImageIcon(info.getIcon()); |
| 568 | shortcutIcon.setVisibility(View.VISIBLE); |
| 569 | } |
| 570 | |
| 571 | TextView shortcutKeyword = (TextView) shortcutView |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 572 | .findViewById(R.id.keyboard_shortcuts_keyword); |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 573 | shortcutKeyword.setText(info.getLabel()); |
| 574 | if (info.getIcon() != null) { |
| 575 | RelativeLayout.LayoutParams lp = |
| 576 | (RelativeLayout.LayoutParams) shortcutKeyword.getLayoutParams(); |
| 577 | lp.removeRule(RelativeLayout.ALIGN_PARENT_START); |
| 578 | shortcutKeyword.setLayoutParams(lp); |
| 579 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 580 | |
Andrei Stingaceanu | 844927d | 2016-02-16 14:31:58 +0000 | [diff] [blame] | 581 | ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 582 | .findViewById(R.id.keyboard_shortcuts_item_container); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 583 | final int shortcutKeysSize = shortcutKeys.size(); |
| 584 | for (int k = 0; k < shortcutKeysSize; k++) { |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 585 | StringOrDrawable shortcutRepresentation = shortcutKeys.get(k); |
| 586 | if (shortcutRepresentation.drawable != null) { |
| 587 | ImageView shortcutKeyIconView = (ImageView) inflater.inflate( |
| 588 | R.layout.keyboard_shortcuts_key_icon_view, shortcutItemsContainer, |
| 589 | false); |
Andrei Stingaceanu | a02965e | 2016-04-08 16:42:02 +0100 | [diff] [blame] | 590 | Bitmap bitmap = Bitmap.createBitmap(shortcutKeyIconItemHeightWidth, |
| 591 | shortcutKeyIconItemHeightWidth, Bitmap.Config.ARGB_8888); |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 592 | Canvas canvas = new Canvas(bitmap); |
| 593 | shortcutRepresentation.drawable.setBounds(0, 0, canvas.getWidth(), |
| 594 | canvas.getHeight()); |
| 595 | shortcutRepresentation.drawable.draw(canvas); |
| 596 | shortcutKeyIconView.setImageBitmap(bitmap); |
| 597 | shortcutItemsContainer.addView(shortcutKeyIconView); |
| 598 | } else if (shortcutRepresentation.string != null) { |
| 599 | TextView shortcutKeyTextView = (TextView) inflater.inflate( |
| 600 | R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer, |
| 601 | false); |
Andrei Stingaceanu | a02965e | 2016-04-08 16:42:02 +0100 | [diff] [blame] | 602 | shortcutKeyTextView.setMinimumWidth(shortcutKeyTextItemMinWidth); |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 603 | shortcutKeyTextView.setText(shortcutRepresentation.string); |
| 604 | shortcutItemsContainer.addView(shortcutKeyTextView); |
| 605 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 606 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 607 | shortcutContainer.addView(shortcutView); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 608 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 609 | keyboardShortcutsLayout.addView(shortcutContainer); |
| 610 | if (i < keyboardShortcutGroupsSize - 1) { |
| 611 | View separator = inflater.inflate( |
| 612 | R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout, |
| 613 | false); |
| 614 | keyboardShortcutsLayout.addView(separator); |
| 615 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 619 | private List<StringOrDrawable> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) { |
| 620 | List<StringOrDrawable> shortcutKeys = getHumanReadableModifiers(info); |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 621 | if (shortcutKeys == null) { |
| 622 | return null; |
| 623 | } |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 624 | String displayLabelString = null; |
| 625 | Drawable displayLabelDrawable = null; |
Clara Bayarri | 1d648a1 | 2016-03-23 17:09:02 +0000 | [diff] [blame] | 626 | if (info.getBaseCharacter() > Character.MIN_VALUE) { |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 627 | displayLabelString = String.valueOf(info.getBaseCharacter()); |
Clara Bayarri | b999af5 | 2016-04-06 16:02:35 +0100 | [diff] [blame] | 628 | } else if (mSpecialCharacterDrawables.get(info.getKeycode()) != null) { |
| 629 | displayLabelDrawable = mSpecialCharacterDrawables.get(info.getKeycode()); |
| 630 | } else if (mSpecialCharacterNames.get(info.getKeycode()) != null) { |
| 631 | displayLabelString = mSpecialCharacterNames.get(info.getKeycode()); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 632 | } else { |
Clara Bayarri | 1d648a1 | 2016-03-23 17:09:02 +0000 | [diff] [blame] | 633 | // Special case for shortcuts with no base key or keycode. |
| 634 | if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) { |
| 635 | return shortcutKeys; |
| 636 | } |
Clara Bayarri | 03f1955 | 2016-04-06 10:59:52 +0100 | [diff] [blame] | 637 | char displayLabel = mKeyCharacterMap.getDisplayLabel(info.getKeycode()); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 638 | if (displayLabel != 0) { |
| 639 | displayLabelString = String.valueOf(displayLabel); |
| 640 | } else { |
| 641 | return null; |
| 642 | } |
| 643 | } |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 644 | |
| 645 | if (displayLabelDrawable != null) { |
| 646 | shortcutKeys.add(new StringOrDrawable(displayLabelDrawable)); |
| 647 | } else if (displayLabelString != null) { |
| 648 | shortcutKeys.add(new StringOrDrawable(displayLabelString.toUpperCase())); |
| 649 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 650 | return shortcutKeys; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 651 | } |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 652 | |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 653 | private List<StringOrDrawable> getHumanReadableModifiers(KeyboardShortcutInfo info) { |
| 654 | final List<StringOrDrawable> shortcutKeys = new ArrayList<>(); |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 655 | int modifiers = info.getModifiers(); |
| 656 | if (modifiers == 0) { |
| 657 | return shortcutKeys; |
| 658 | } |
Clara Bayarri | b999af5 | 2016-04-06 16:02:35 +0100 | [diff] [blame] | 659 | for(int i = 0; i < mModifierNames.size(); ++i) { |
| 660 | final int supportedModifier = mModifierNames.keyAt(i); |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 661 | if ((modifiers & supportedModifier) != 0) { |
Clara Bayarri | b999af5 | 2016-04-06 16:02:35 +0100 | [diff] [blame] | 662 | if (mModifierDrawables.get(supportedModifier) != null) { |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 663 | shortcutKeys.add(new StringOrDrawable( |
Clara Bayarri | b999af5 | 2016-04-06 16:02:35 +0100 | [diff] [blame] | 664 | mModifierDrawables.get(supportedModifier))); |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 665 | } else { |
| 666 | shortcutKeys.add(new StringOrDrawable( |
Clara Bayarri | b999af5 | 2016-04-06 16:02:35 +0100 | [diff] [blame] | 667 | mModifierNames.get(supportedModifier).toUpperCase())); |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 668 | } |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 669 | modifiers &= ~supportedModifier; |
| 670 | } |
| 671 | } |
| 672 | if (modifiers != 0) { |
| 673 | // Remaining unsupported modifiers, don't show anything. |
| 674 | return null; |
| 675 | } |
| 676 | return shortcutKeys; |
| 677 | } |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 678 | |
| 679 | private static final class StringOrDrawable { |
| 680 | public String string; |
| 681 | public Drawable drawable; |
| 682 | |
| 683 | public StringOrDrawable(String string) { |
| 684 | this.string = string; |
| 685 | } |
| 686 | |
| 687 | public StringOrDrawable(Drawable drawable) { |
| 688 | this.drawable = drawable; |
| 689 | } |
| 690 | } |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 691 | } |