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