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; |
| 62 | import com.android.internal.logging.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; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 65 | import com.android.systemui.recents.Recents; |
| 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); |
| 374 | Recents.getSystemServices().requestKeyboardShortcuts(mContext, |
| 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 | } |
| 384 | showKeyboardShortcutsDialog(result); |
| 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); |
| 436 | PackageInfo assistPackageInfo = null; |
| 437 | try { |
| 438 | assistPackageInfo = mPackageManager.getPackageInfo( |
| 439 | assistComponent.getPackageName(), 0, userId); |
| 440 | } catch (RemoteException e) { |
| 441 | Log.e(TAG, "PackageManagerService is dead"); |
| 442 | } |
| 443 | |
| 444 | if (assistPackageInfo != null) { |
| 445 | final Icon assistIcon = Icon.createWithResource( |
| 446 | assistPackageInfo.applicationInfo.packageName, |
| 447 | assistPackageInfo.applicationInfo.icon); |
| 448 | |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 449 | keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 450 | mContext.getString(R.string.keyboard_shortcut_group_applications_assist), |
| 451 | assistIcon, |
| 452 | KeyEvent.KEYCODE_UNKNOWN, |
| 453 | KeyEvent.META_META_ON)); |
| 454 | } |
| 455 | |
| 456 | // Browser. |
| 457 | final Icon browserIcon = getIconForIntentCategory(Intent.CATEGORY_APP_BROWSER, userId); |
| 458 | if (browserIcon != null) { |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 459 | keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 460 | mContext.getString(R.string.keyboard_shortcut_group_applications_browser), |
| 461 | browserIcon, |
| 462 | KeyEvent.KEYCODE_B, |
| 463 | KeyEvent.META_META_ON)); |
| 464 | } |
| 465 | |
| 466 | |
| 467 | // Contacts. |
| 468 | final Icon contactsIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CONTACTS, userId); |
| 469 | if (contactsIcon != null) { |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 470 | keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 471 | mContext.getString(R.string.keyboard_shortcut_group_applications_contacts), |
| 472 | contactsIcon, |
| 473 | KeyEvent.KEYCODE_C, |
| 474 | KeyEvent.META_META_ON)); |
| 475 | } |
| 476 | |
| 477 | // Email. |
| 478 | final Icon emailIcon = getIconForIntentCategory(Intent.CATEGORY_APP_EMAIL, userId); |
| 479 | if (emailIcon != null) { |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 480 | keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 481 | mContext.getString(R.string.keyboard_shortcut_group_applications_email), |
| 482 | emailIcon, |
| 483 | KeyEvent.KEYCODE_E, |
| 484 | KeyEvent.META_META_ON)); |
| 485 | } |
| 486 | |
| 487 | // Messaging. |
| 488 | final Icon messagingIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MESSAGING, userId); |
| 489 | if (messagingIcon != null) { |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 490 | keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 491 | mContext.getString(R.string.keyboard_shortcut_group_applications_im), |
| 492 | messagingIcon, |
| 493 | KeyEvent.KEYCODE_T, |
| 494 | KeyEvent.META_META_ON)); |
| 495 | } |
| 496 | |
| 497 | // Music. |
| 498 | final Icon musicIcon = getIconForIntentCategory(Intent.CATEGORY_APP_MUSIC, userId); |
| 499 | if (musicIcon != null) { |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 500 | keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 501 | mContext.getString(R.string.keyboard_shortcut_group_applications_music), |
| 502 | musicIcon, |
| 503 | KeyEvent.KEYCODE_P, |
| 504 | KeyEvent.META_META_ON)); |
| 505 | } |
| 506 | |
| 507 | // Calendar. |
| 508 | final Icon calendarIcon = getIconForIntentCategory(Intent.CATEGORY_APP_CALENDAR, userId); |
| 509 | if (calendarIcon != null) { |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 510 | keyboardShortcutInfoAppItems.add(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 511 | mContext.getString(R.string.keyboard_shortcut_group_applications_calendar), |
| 512 | calendarIcon, |
| 513 | KeyEvent.KEYCODE_L, |
| 514 | KeyEvent.META_META_ON)); |
| 515 | } |
| 516 | |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 517 | final int itemsSize = keyboardShortcutInfoAppItems.size(); |
| 518 | if (itemsSize == 0) { |
| 519 | return null; |
| 520 | } |
| 521 | |
| 522 | // Sorts by label, case insensitive with nulls and/or empty labels last. |
| 523 | Collections.sort(keyboardShortcutInfoAppItems, mApplicationItemsComparator); |
| 524 | return new KeyboardShortcutGroup( |
| 525 | mContext.getString(R.string.keyboard_shortcut_group_applications), |
| 526 | keyboardShortcutInfoAppItems, |
| 527 | true); |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | private Icon getIconForIntentCategory(String intentCategory, int userId) { |
| 531 | final Intent intent = new Intent(Intent.ACTION_MAIN); |
| 532 | intent.addCategory(intentCategory); |
| 533 | |
| 534 | final PackageInfo packageInfo = getPackageInfoForIntent(intent, userId); |
| 535 | if (packageInfo != null && packageInfo.applicationInfo.icon != 0) { |
| 536 | return Icon.createWithResource( |
| 537 | packageInfo.applicationInfo.packageName, |
| 538 | packageInfo.applicationInfo.icon); |
| 539 | } |
| 540 | return null; |
| 541 | } |
| 542 | |
| 543 | private PackageInfo getPackageInfoForIntent(Intent intent, int userId) { |
| 544 | try { |
| 545 | ResolveInfo handler; |
| 546 | handler = mPackageManager.resolveIntent( |
| 547 | intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()), 0, userId); |
| 548 | if (handler == null || handler.activityInfo == null) { |
| 549 | return null; |
| 550 | } |
| 551 | return mPackageManager.getPackageInfo(handler.activityInfo.packageName, 0, userId); |
| 552 | } catch (RemoteException e) { |
| 553 | Log.e(TAG, "PackageManagerService is dead", e); |
| 554 | return null; |
| 555 | } |
| 556 | } |
| 557 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 558 | private void showKeyboardShortcutsDialog( |
| 559 | final List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 560 | // Need to post on the main thread. |
| 561 | mHandler.post(new Runnable() { |
| 562 | @Override |
| 563 | public void run() { |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 564 | handleShowKeyboardShortcuts(keyboardShortcutGroups); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 565 | } |
| 566 | }); |
| 567 | } |
| 568 | |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 569 | private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 570 | AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext); |
| 571 | LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( |
| 572 | LAYOUT_INFLATER_SERVICE); |
| 573 | final View keyboardShortcutsView = inflater.inflate( |
| 574 | R.layout.keyboard_shortcuts_view, null); |
| 575 | populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById( |
| 576 | R.id.keyboard_shortcuts_container), keyboardShortcutGroups); |
| 577 | dialogBuilder.setView(keyboardShortcutsView); |
Andrei Stingaceanu | 56e44e4 | 2016-04-08 15:07:15 +0100 | [diff] [blame] | 578 | dialogBuilder.setPositiveButton(R.string.quick_settings_done, mDialogCloseListener); |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 579 | mKeyboardShortcutsDialog = dialogBuilder.create(); |
| 580 | mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true); |
| 581 | Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow(); |
| 582 | keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG); |
| 583 | mKeyboardShortcutsDialog.show(); |
| 584 | } |
| 585 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 586 | private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout, |
| 587 | List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 588 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 589 | final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size(); |
Andrei Stingaceanu | a02965e | 2016-04-08 16:42:02 +0100 | [diff] [blame] | 590 | TextView shortcutsKeyView = (TextView) inflater.inflate( |
| 591 | R.layout.keyboard_shortcuts_key_view, null, false); |
| 592 | shortcutsKeyView.measure( |
| 593 | View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); |
| 594 | final int shortcutKeyTextItemMinWidth = shortcutsKeyView.getMeasuredHeight(); |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 595 | // 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] | 596 | final int shortcutKeyIconItemHeightWidth = shortcutsKeyView.getMeasuredHeight() |
| 597 | - shortcutsKeyView.getPaddingTop() |
| 598 | - shortcutsKeyView.getPaddingBottom(); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 599 | for (int i = 0; i < keyboardShortcutGroupsSize; i++) { |
| 600 | KeyboardShortcutGroup group = keyboardShortcutGroups.get(i); |
| 601 | TextView categoryTitle = (TextView) inflater.inflate( |
| 602 | R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false); |
| 603 | categoryTitle.setText(group.getLabel()); |
| 604 | categoryTitle.setTextColor(group.isSystemGroup() |
Andrew Sapperstein | 5c37344 | 2016-06-12 13:17:16 -0700 | [diff] [blame] | 605 | ? Utils.getColorAccent(mContext) |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 606 | : mContext.getColor(R.color.ksh_application_group_color)); |
| 607 | keyboardShortcutsLayout.addView(categoryTitle); |
| 608 | |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 609 | LinearLayout shortcutContainer = (LinearLayout) inflater.inflate( |
| 610 | R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 611 | final int itemsSize = group.getItems().size(); |
| 612 | for (int j = 0; j < itemsSize; j++) { |
| 613 | KeyboardShortcutInfo info = group.getItems().get(j); |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 614 | List<StringDrawableContainer> shortcutKeys = getHumanReadableShortcutKeys(info); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 615 | if (shortcutKeys == null) { |
| 616 | // Ignore shortcuts we can't display keys for. |
| 617 | Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping."); |
| 618 | continue; |
| 619 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 620 | View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item, |
| 621 | shortcutContainer, false); |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 622 | |
| 623 | if (info.getIcon() != null) { |
| 624 | ImageView shortcutIcon = (ImageView) shortcutView |
| 625 | .findViewById(R.id.keyboard_shortcuts_icon); |
| 626 | shortcutIcon.setImageIcon(info.getIcon()); |
| 627 | shortcutIcon.setVisibility(View.VISIBLE); |
| 628 | } |
| 629 | |
| 630 | TextView shortcutKeyword = (TextView) shortcutView |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 631 | .findViewById(R.id.keyboard_shortcuts_keyword); |
Andrei Stingaceanu | 12e9803 | 2016-04-05 12:22:21 +0100 | [diff] [blame] | 632 | shortcutKeyword.setText(info.getLabel()); |
| 633 | if (info.getIcon() != null) { |
| 634 | RelativeLayout.LayoutParams lp = |
| 635 | (RelativeLayout.LayoutParams) shortcutKeyword.getLayoutParams(); |
| 636 | lp.removeRule(RelativeLayout.ALIGN_PARENT_START); |
| 637 | shortcutKeyword.setLayoutParams(lp); |
| 638 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 639 | |
Andrei Stingaceanu | 844927d | 2016-02-16 14:31:58 +0000 | [diff] [blame] | 640 | ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 641 | .findViewById(R.id.keyboard_shortcuts_item_container); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 642 | final int shortcutKeysSize = shortcutKeys.size(); |
| 643 | for (int k = 0; k < shortcutKeysSize; k++) { |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 644 | StringDrawableContainer shortcutRepresentation = shortcutKeys.get(k); |
| 645 | if (shortcutRepresentation.mDrawable != null) { |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 646 | ImageView shortcutKeyIconView = (ImageView) inflater.inflate( |
| 647 | R.layout.keyboard_shortcuts_key_icon_view, shortcutItemsContainer, |
| 648 | false); |
Andrei Stingaceanu | a02965e | 2016-04-08 16:42:02 +0100 | [diff] [blame] | 649 | Bitmap bitmap = Bitmap.createBitmap(shortcutKeyIconItemHeightWidth, |
| 650 | shortcutKeyIconItemHeightWidth, Bitmap.Config.ARGB_8888); |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 651 | Canvas canvas = new Canvas(bitmap); |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 652 | shortcutRepresentation.mDrawable.setBounds(0, 0, canvas.getWidth(), |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 653 | canvas.getHeight()); |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 654 | shortcutRepresentation.mDrawable.draw(canvas); |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 655 | shortcutKeyIconView.setImageBitmap(bitmap); |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 656 | shortcutKeyIconView.setImportantForAccessibility( |
| 657 | IMPORTANT_FOR_ACCESSIBILITY_YES); |
| 658 | shortcutKeyIconView.setAccessibilityDelegate( |
| 659 | new ShortcutKeyAccessibilityDelegate( |
| 660 | shortcutRepresentation.mString)); |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 661 | shortcutItemsContainer.addView(shortcutKeyIconView); |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 662 | } else if (shortcutRepresentation.mString != null) { |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 663 | TextView shortcutKeyTextView = (TextView) inflater.inflate( |
| 664 | R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer, |
| 665 | false); |
Andrei Stingaceanu | a02965e | 2016-04-08 16:42:02 +0100 | [diff] [blame] | 666 | shortcutKeyTextView.setMinimumWidth(shortcutKeyTextItemMinWidth); |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 667 | shortcutKeyTextView.setText(shortcutRepresentation.mString); |
| 668 | shortcutKeyTextView.setAccessibilityDelegate( |
| 669 | new ShortcutKeyAccessibilityDelegate( |
| 670 | shortcutRepresentation.mString)); |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 671 | shortcutItemsContainer.addView(shortcutKeyTextView); |
| 672 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 673 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 674 | shortcutContainer.addView(shortcutView); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 675 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 676 | keyboardShortcutsLayout.addView(shortcutContainer); |
| 677 | if (i < keyboardShortcutGroupsSize - 1) { |
| 678 | View separator = inflater.inflate( |
| 679 | R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout, |
| 680 | false); |
| 681 | keyboardShortcutsLayout.addView(separator); |
| 682 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 686 | private List<StringDrawableContainer> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) { |
| 687 | List<StringDrawableContainer> shortcutKeys = getHumanReadableModifiers(info); |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 688 | if (shortcutKeys == null) { |
| 689 | return null; |
| 690 | } |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 691 | String shortcutKeyString = null; |
| 692 | Drawable shortcutKeyDrawable = null; |
Clara Bayarri | 1d648a1 | 2016-03-23 17:09:02 +0000 | [diff] [blame] | 693 | if (info.getBaseCharacter() > Character.MIN_VALUE) { |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 694 | shortcutKeyString = String.valueOf(info.getBaseCharacter()); |
Clara Bayarri | b999af5 | 2016-04-06 16:02:35 +0100 | [diff] [blame] | 695 | } else if (mSpecialCharacterDrawables.get(info.getKeycode()) != null) { |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 696 | shortcutKeyDrawable = mSpecialCharacterDrawables.get(info.getKeycode()); |
| 697 | shortcutKeyString = mSpecialCharacterNames.get(info.getKeycode()); |
Clara Bayarri | b999af5 | 2016-04-06 16:02:35 +0100 | [diff] [blame] | 698 | } else if (mSpecialCharacterNames.get(info.getKeycode()) != null) { |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 699 | shortcutKeyString = mSpecialCharacterNames.get(info.getKeycode()); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 700 | } else { |
Clara Bayarri | 1d648a1 | 2016-03-23 17:09:02 +0000 | [diff] [blame] | 701 | // Special case for shortcuts with no base key or keycode. |
| 702 | if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) { |
| 703 | return shortcutKeys; |
| 704 | } |
Clara Bayarri | 03f1955 | 2016-04-06 10:59:52 +0100 | [diff] [blame] | 705 | char displayLabel = mKeyCharacterMap.getDisplayLabel(info.getKeycode()); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 706 | if (displayLabel != 0) { |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 707 | shortcutKeyString = String.valueOf(displayLabel); |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 708 | } else { |
Clara Bayarri | 382c59e | 2016-05-18 12:19:17 +0100 | [diff] [blame] | 709 | displayLabel = mBackupKeyCharacterMap.getDisplayLabel(info.getKeycode()); |
| 710 | if (displayLabel != 0) { |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 711 | shortcutKeyString = String.valueOf(displayLabel); |
Clara Bayarri | 382c59e | 2016-05-18 12:19:17 +0100 | [diff] [blame] | 712 | } else { |
| 713 | return null; |
| 714 | } |
Clara Bayarri | 4e850ff | 2016-03-02 11:12:32 -0800 | [diff] [blame] | 715 | } |
| 716 | } |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 717 | |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 718 | if (shortcutKeyString != null) { |
| 719 | shortcutKeys.add(new StringDrawableContainer(shortcutKeyString, shortcutKeyDrawable)); |
| 720 | } else { |
| 721 | Log.w(TAG, "Keyboard Shortcut does not have a text representation, skipping."); |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 722 | } |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 723 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 724 | return shortcutKeys; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 725 | } |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 726 | |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 727 | private List<StringDrawableContainer> getHumanReadableModifiers(KeyboardShortcutInfo info) { |
| 728 | final List<StringDrawableContainer> shortcutKeys = new ArrayList<>(); |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 729 | int modifiers = info.getModifiers(); |
| 730 | if (modifiers == 0) { |
| 731 | return shortcutKeys; |
| 732 | } |
Clara Bayarri | 7ff3798 | 2016-06-27 13:58:44 +0100 | [diff] [blame^] | 733 | for(int i = 0; i < mModifierList.length; ++i) { |
| 734 | final int supportedModifier = mModifierList[i]; |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 735 | if ((modifiers & supportedModifier) != 0) { |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 736 | shortcutKeys.add(new StringDrawableContainer( |
| 737 | mModifierNames.get(supportedModifier), |
| 738 | mModifierDrawables.get(supportedModifier))); |
Clara Bayarri | b9057df | 2016-03-02 11:37:09 -0800 | [diff] [blame] | 739 | modifiers &= ~supportedModifier; |
| 740 | } |
| 741 | } |
| 742 | if (modifiers != 0) { |
| 743 | // Remaining unsupported modifiers, don't show anything. |
| 744 | return null; |
| 745 | } |
| 746 | return shortcutKeys; |
| 747 | } |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 748 | |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 749 | private final class ShortcutKeyAccessibilityDelegate extends AccessibilityDelegate { |
| 750 | private String mContentDescription; |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 751 | |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 752 | ShortcutKeyAccessibilityDelegate(String contentDescription) { |
| 753 | mContentDescription = contentDescription; |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 754 | } |
| 755 | |
Andrei Stingaceanu | b481701 | 2016-06-13 17:26:39 +0100 | [diff] [blame] | 756 | @Override |
| 757 | public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { |
| 758 | super.onInitializeAccessibilityNodeInfo(host, info); |
| 759 | if (mContentDescription != null) { |
| 760 | info.setContentDescription(mContentDescription.toLowerCase()); |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | private static final class StringDrawableContainer { |
| 766 | @NonNull |
| 767 | public String mString; |
| 768 | @Nullable |
| 769 | public Drawable mDrawable; |
| 770 | |
| 771 | StringDrawableContainer(String string, Drawable drawable) { |
| 772 | mString = string; |
| 773 | mDrawable = drawable; |
Andrei Stingaceanu | d151910 | 2016-03-31 15:53:33 +0100 | [diff] [blame] | 774 | } |
| 775 | } |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 776 | } |