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