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