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; |
| 20 | import android.app.Dialog; |
| 21 | import android.content.Context; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 22 | import android.content.DialogInterface; |
| 23 | import android.content.DialogInterface.OnClickListener; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 24 | import android.os.Handler; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 25 | import android.os.Looper; |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 26 | import android.view.ContextThemeWrapper; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 27 | import android.view.KeyEvent; |
| 28 | import android.view.KeyboardShortcutGroup; |
| 29 | import android.view.KeyboardShortcutInfo; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 30 | import android.view.LayoutInflater; |
| 31 | import android.view.View; |
Andrei Stingaceanu | 844927d | 2016-02-16 14:31:58 +0000 | [diff] [blame^] | 32 | import android.view.ViewGroup; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 33 | import android.view.Window; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 34 | import android.view.WindowManager.KeyboardShortcutsReceiver; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 35 | import android.widget.LinearLayout; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 36 | import android.widget.TextView; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 37 | |
| 38 | import com.android.systemui.R; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 39 | import com.android.systemui.recents.Recents; |
| 40 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 41 | import java.util.ArrayList; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 42 | import java.util.List; |
| 43 | |
| 44 | import static android.content.Context.LAYOUT_INFLATER_SERVICE; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 45 | import static android.view.Gravity.TOP; |
| 46 | import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Contains functionality for handling keyboard shortcuts. |
| 50 | */ |
| 51 | public class KeyboardShortcuts { |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 52 | private static final char SYSTEM_HOME_BASE_CHARACTER = '\u2386'; |
| 53 | private static final char SYSTEM_BACK_BASE_CHARACTER = '\u007F'; |
| 54 | private static final char SYSTEM_RECENTS_BASE_CHARACTER = '\u0009'; |
| 55 | |
| 56 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
| 57 | private final Context mContext; |
| 58 | private final OnClickListener dialogCloseListener = new DialogInterface.OnClickListener() { |
| 59 | public void onClick(DialogInterface dialog, int id) { |
| 60 | dismissKeyboardShortcutsDialog(); |
| 61 | } |
| 62 | }; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 63 | |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 64 | private Dialog mKeyboardShortcutsDialog; |
| 65 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 66 | public KeyboardShortcuts(Context context) { |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 67 | this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_Material_Light); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 68 | } |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 69 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 70 | public void toggleKeyboardShortcuts() { |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 71 | if (mKeyboardShortcutsDialog == null) { |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 72 | Recents.getSystemServices().requestKeyboardShortcuts(mContext, |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 73 | new KeyboardShortcutsReceiver() { |
| 74 | @Override |
| 75 | public void onKeyboardShortcutsReceived( |
| 76 | final List<KeyboardShortcutGroup> result) { |
| 77 | KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup( |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 78 | mContext.getString(R.string.keyboard_shortcut_group_system), true); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 79 | systemGroup.addItem(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 80 | mContext.getString(R.string.keyboard_shortcut_group_system_home), |
| 81 | SYSTEM_HOME_BASE_CHARACTER, KeyEvent.META_META_ON)); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 82 | systemGroup.addItem(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 83 | mContext.getString(R.string.keyboard_shortcut_group_system_back), |
| 84 | SYSTEM_BACK_BASE_CHARACTER, KeyEvent.META_META_ON)); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 85 | systemGroup.addItem(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 86 | mContext.getString(R.string.keyboard_shortcut_group_system_recents), |
| 87 | SYSTEM_RECENTS_BASE_CHARACTER, KeyEvent.META_ALT_ON)); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 88 | result.add(systemGroup); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 89 | showKeyboardShortcutsDialog(result); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 90 | } |
| 91 | }); |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 92 | } else { |
| 93 | dismissKeyboardShortcutsDialog(); |
| 94 | } |
| 95 | } |
| 96 | |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 97 | public void dismissKeyboardShortcutsDialog() { |
| 98 | if (mKeyboardShortcutsDialog != null) { |
| 99 | mKeyboardShortcutsDialog.dismiss(); |
| 100 | mKeyboardShortcutsDialog = null; |
| 101 | } |
| 102 | } |
| 103 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 104 | private void showKeyboardShortcutsDialog( |
| 105 | final List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 106 | // Need to post on the main thread. |
| 107 | mHandler.post(new Runnable() { |
| 108 | @Override |
| 109 | public void run() { |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 110 | handleShowKeyboardShortcuts(keyboardShortcutGroups); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 111 | } |
| 112 | }); |
| 113 | } |
| 114 | |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 115 | private void handleShowKeyboardShortcuts(List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 116 | AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext); |
| 117 | LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( |
| 118 | LAYOUT_INFLATER_SERVICE); |
| 119 | final View keyboardShortcutsView = inflater.inflate( |
| 120 | R.layout.keyboard_shortcuts_view, null); |
| 121 | populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById( |
| 122 | R.id.keyboard_shortcuts_container), keyboardShortcutGroups); |
| 123 | dialogBuilder.setView(keyboardShortcutsView); |
| 124 | dialogBuilder.setPositiveButton(R.string.quick_settings_done, dialogCloseListener); |
| 125 | mKeyboardShortcutsDialog = dialogBuilder.create(); |
| 126 | mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true); |
| 127 | Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow(); |
| 128 | keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG); |
| 129 | mKeyboardShortcutsDialog.show(); |
| 130 | } |
| 131 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 132 | private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout, |
| 133 | List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 134 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 135 | final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size(); |
| 136 | for (int i = 0; i < keyboardShortcutGroupsSize; i++) { |
| 137 | KeyboardShortcutGroup group = keyboardShortcutGroups.get(i); |
| 138 | TextView categoryTitle = (TextView) inflater.inflate( |
| 139 | R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false); |
| 140 | categoryTitle.setText(group.getLabel()); |
| 141 | categoryTitle.setTextColor(group.isSystemGroup() |
| 142 | ? mContext.getColor(R.color.ksh_system_group_color) |
| 143 | : mContext.getColor(R.color.ksh_application_group_color)); |
| 144 | keyboardShortcutsLayout.addView(categoryTitle); |
| 145 | |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 146 | LinearLayout shortcutContainer = (LinearLayout) inflater.inflate( |
| 147 | R.layout.keyboard_shortcuts_container, keyboardShortcutsLayout, false); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 148 | final int itemsSize = group.getItems().size(); |
| 149 | for (int j = 0; j < itemsSize; j++) { |
| 150 | KeyboardShortcutInfo info = group.getItems().get(j); |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 151 | View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item, |
| 152 | shortcutContainer, false); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 153 | TextView textView = (TextView) shortcutView |
| 154 | .findViewById(R.id.keyboard_shortcuts_keyword); |
| 155 | textView.setText(info.getLabel()); |
| 156 | |
Andrei Stingaceanu | 844927d | 2016-02-16 14:31:58 +0000 | [diff] [blame^] | 157 | ViewGroup shortcutItemsContainer = (ViewGroup) shortcutView |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 158 | .findViewById(R.id.keyboard_shortcuts_item_container); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 159 | List<String> shortcutKeys = getHumanReadableShortcutKeys(info); |
| 160 | final int shortcutKeysSize = shortcutKeys.size(); |
| 161 | for (int k = 0; k < shortcutKeysSize; k++) { |
| 162 | String shortcutKey = shortcutKeys.get(k); |
| 163 | TextView shortcutKeyView = (TextView) inflater.inflate( |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 164 | R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer, false); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 165 | shortcutKeyView.setText(shortcutKey); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 166 | shortcutItemsContainer.addView(shortcutKeyView); |
| 167 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 168 | shortcutContainer.addView(shortcutView); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 169 | } |
Andrei Stingaceanu | 2b909e9 | 2016-02-03 17:52:00 +0000 | [diff] [blame] | 170 | keyboardShortcutsLayout.addView(shortcutContainer); |
| 171 | if (i < keyboardShortcutGroupsSize - 1) { |
| 172 | View separator = inflater.inflate( |
| 173 | R.layout.keyboard_shortcuts_category_separator, keyboardShortcutsLayout, |
| 174 | false); |
| 175 | keyboardShortcutsLayout.addView(separator); |
| 176 | } |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | private List<String> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) { |
| 181 | // TODO: fix the shortcuts. Find or build an util which can produce human readable |
| 182 | // names of the baseCharacter and the modifiers. |
| 183 | List<String> shortcutKeys = new ArrayList<>(); |
| 184 | shortcutKeys.add(KeyEvent.metaStateToString(info.getModifiers()).toUpperCase()); |
| 185 | shortcutKeys.add(Character.getName(info.getBaseCharacter()).toUpperCase()); |
| 186 | return shortcutKeys; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 187 | } |
| 188 | } |