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