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