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