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; |
| 24 | import android.graphics.Color; |
| 25 | import android.graphics.Typeface; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 26 | import android.graphics.drawable.ColorDrawable; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 27 | import android.graphics.drawable.Drawable; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 28 | import android.os.Handler; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 29 | import android.os.Looper; |
| 30 | import android.util.DisplayMetrics; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 31 | import android.util.Log; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 32 | import android.util.TypedValue; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 33 | import android.view.KeyEvent; |
| 34 | import android.view.KeyboardShortcutGroup; |
| 35 | import android.view.KeyboardShortcutInfo; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 36 | import android.view.LayoutInflater; |
| 37 | import android.view.View; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 38 | import android.view.ViewGroup; |
| 39 | import android.view.ViewGroup.LayoutParams; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 40 | import android.view.Window; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 41 | import android.view.WindowManager.KeyboardShortcutsReceiver; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 42 | import android.widget.LinearLayout; |
| 43 | import android.widget.ScrollView; |
| 44 | import android.widget.TextView; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 45 | |
| 46 | import com.android.systemui.R; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 47 | import com.android.systemui.recents.Recents; |
| 48 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 49 | import java.util.ArrayList; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 50 | import java.util.List; |
| 51 | |
| 52 | import static android.content.Context.LAYOUT_INFLATER_SERVICE; |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 53 | import static android.graphics.Color.WHITE; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 54 | import static android.view.Gravity.TOP; |
| 55 | import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Contains functionality for handling keyboard shortcuts. |
| 59 | */ |
| 60 | public class KeyboardShortcuts { |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 61 | private static final char SYSTEM_HOME_BASE_CHARACTER = '\u2386'; |
| 62 | private static final char SYSTEM_BACK_BASE_CHARACTER = '\u007F'; |
| 63 | private static final char SYSTEM_RECENTS_BASE_CHARACTER = '\u0009'; |
| 64 | |
| 65 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
| 66 | private final Context mContext; |
| 67 | private final OnClickListener dialogCloseListener = new DialogInterface.OnClickListener() { |
| 68 | public void onClick(DialogInterface dialog, int id) { |
| 69 | dismissKeyboardShortcutsDialog(); |
| 70 | } |
| 71 | }; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 72 | |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 73 | private Dialog mKeyboardShortcutsDialog; |
| 74 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 75 | public KeyboardShortcuts(Context context) { |
| 76 | this.mContext = context; |
| 77 | } |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 78 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 79 | public void toggleKeyboardShortcuts() { |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 80 | if (mKeyboardShortcutsDialog == null) { |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 81 | Recents.getSystemServices().requestKeyboardShortcuts(mContext, |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 82 | new KeyboardShortcutsReceiver() { |
| 83 | @Override |
| 84 | public void onKeyboardShortcutsReceived( |
| 85 | final List<KeyboardShortcutGroup> result) { |
| 86 | KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup( |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 87 | mContext.getString(R.string.keyboard_shortcut_group_system), true); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 88 | systemGroup.addItem(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 89 | mContext.getString(R.string.keyboard_shortcut_group_system_home), |
| 90 | SYSTEM_HOME_BASE_CHARACTER, KeyEvent.META_META_ON)); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 91 | systemGroup.addItem(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 92 | mContext.getString(R.string.keyboard_shortcut_group_system_back), |
| 93 | SYSTEM_BACK_BASE_CHARACTER, KeyEvent.META_META_ON)); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 94 | systemGroup.addItem(new KeyboardShortcutInfo( |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 95 | mContext.getString(R.string.keyboard_shortcut_group_system_recents), |
| 96 | SYSTEM_RECENTS_BASE_CHARACTER, KeyEvent.META_ALT_ON)); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 97 | result.add(systemGroup); |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 98 | showKeyboardShortcutsDialog(result); |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 99 | } |
| 100 | }); |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 101 | } else { |
| 102 | dismissKeyboardShortcutsDialog(); |
| 103 | } |
| 104 | } |
| 105 | |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 106 | public void dismissKeyboardShortcutsDialog() { |
| 107 | if (mKeyboardShortcutsDialog != null) { |
| 108 | mKeyboardShortcutsDialog.dismiss(); |
| 109 | mKeyboardShortcutsDialog = null; |
| 110 | } |
| 111 | } |
| 112 | |
Andrei Stingaceanu | 8861cb0 | 2016-01-20 16:48:30 +0000 | [diff] [blame^] | 113 | private void showKeyboardShortcutsDialog( |
| 114 | final List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 115 | // Need to post on the main thread. |
| 116 | mHandler.post(new Runnable() { |
| 117 | @Override |
| 118 | public void run() { |
| 119 | // TODO: break all this code out into a handleShowKeyboard... |
| 120 | // Might add more things posted; should consider adding a custom handler so |
| 121 | // you can send the keyboardShortcutsGroups as part of the message. |
| 122 | AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext); |
| 123 | LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( |
| 124 | LAYOUT_INFLATER_SERVICE); |
| 125 | final View keyboardShortcutsView = inflater.inflate( |
| 126 | R.layout.keyboard_shortcuts_view, null); |
| 127 | DisplayMetrics dm = mContext.getResources().getDisplayMetrics(); |
| 128 | ScrollView scrollView = (ScrollView) keyboardShortcutsView.findViewById( |
| 129 | R.id.keyboard_shortcuts_scroll_view); |
| 130 | // TODO: find a better way to set the height. |
| 131 | scrollView.setLayoutParams(new LinearLayout.LayoutParams( |
| 132 | LayoutParams.WRAP_CONTENT, |
| 133 | (int) (dm.heightPixels * dm.density))); |
| 134 | |
| 135 | populateKeyboardShortcuts((LinearLayout) keyboardShortcutsView.findViewById( |
| 136 | R.id.keyboard_shortcuts_container), keyboardShortcutGroups); |
| 137 | dialogBuilder.setView(keyboardShortcutsView); |
| 138 | dialogBuilder.setPositiveButton(R.string.quick_settings_done, dialogCloseListener); |
| 139 | mKeyboardShortcutsDialog = dialogBuilder.create(); |
| 140 | mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true); |
| 141 | |
| 142 | // Setup window. |
| 143 | Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow(); |
| 144 | keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG); |
| 145 | keyboardShortcutsWindow.setBackgroundDrawable( |
| 146 | mContext.getDrawable(R.color.ksh_dialog_background_color)); |
| 147 | keyboardShortcutsWindow.setGravity(TOP); |
| 148 | mKeyboardShortcutsDialog.show(); |
| 149 | } |
| 150 | }); |
| 151 | } |
| 152 | |
| 153 | private void populateKeyboardShortcuts(LinearLayout keyboardShortcutsLayout, |
| 154 | List<KeyboardShortcutGroup> keyboardShortcutGroups) { |
| 155 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 156 | final int keyboardShortcutGroupsSize = keyboardShortcutGroups.size(); |
| 157 | for (int i = 0; i < keyboardShortcutGroupsSize; i++) { |
| 158 | KeyboardShortcutGroup group = keyboardShortcutGroups.get(i); |
| 159 | TextView categoryTitle = (TextView) inflater.inflate( |
| 160 | R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false); |
| 161 | categoryTitle.setText(group.getLabel()); |
| 162 | categoryTitle.setTextColor(group.isSystemGroup() |
| 163 | ? mContext.getColor(R.color.ksh_system_group_color) |
| 164 | : mContext.getColor(R.color.ksh_application_group_color)); |
| 165 | keyboardShortcutsLayout.addView(categoryTitle); |
| 166 | |
| 167 | LinearLayout shortcutWrapper = (LinearLayout) inflater.inflate( |
| 168 | R.layout.keyboard_shortcuts_wrapper, null); |
| 169 | final int itemsSize = group.getItems().size(); |
| 170 | for (int j = 0; j < itemsSize; j++) { |
| 171 | KeyboardShortcutInfo info = group.getItems().get(j); |
| 172 | View shortcutView = inflater.inflate(R.layout.keyboard_shortcut_app_item, null); |
| 173 | TextView textView = (TextView) shortcutView |
| 174 | .findViewById(R.id.keyboard_shortcuts_keyword); |
| 175 | textView.setText(info.getLabel()); |
| 176 | |
| 177 | List<String> shortcutKeys = getHumanReadableShortcutKeys(info); |
| 178 | final int shortcutKeysSize = shortcutKeys.size(); |
| 179 | for (int k = 0; k < shortcutKeysSize; k++) { |
| 180 | String shortcutKey = shortcutKeys.get(k); |
| 181 | TextView shortcutKeyView = (TextView) inflater.inflate( |
| 182 | R.layout.keyboard_shortcuts_key_view, null); |
| 183 | shortcutKeyView.setText(shortcutKey); |
| 184 | LinearLayout shortcutItemsContainer = (LinearLayout) shortcutView |
| 185 | .findViewById(R.id.keyboard_shortcuts_item_container); |
| 186 | shortcutItemsContainer.addView(shortcutKeyView); |
| 187 | } |
| 188 | shortcutWrapper.addView(shortcutView); |
| 189 | } |
| 190 | |
| 191 | // TODO: merge container and wrapper into one xml file - wrapper is always a child of |
| 192 | // container. |
| 193 | LinearLayout shortcutsContainer = (LinearLayout) inflater.inflate( |
| 194 | R.layout.keyboard_shortcuts_container, null); |
| 195 | shortcutsContainer.addView(shortcutWrapper); |
| 196 | keyboardShortcutsLayout.addView(shortcutsContainer); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | private List<String> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) { |
| 201 | // TODO: fix the shortcuts. Find or build an util which can produce human readable |
| 202 | // names of the baseCharacter and the modifiers. |
| 203 | List<String> shortcutKeys = new ArrayList<>(); |
| 204 | shortcutKeys.add(KeyEvent.metaStateToString(info.getModifiers()).toUpperCase()); |
| 205 | shortcutKeys.add(Character.getName(info.getBaseCharacter()).toUpperCase()); |
| 206 | return shortcutKeys; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 207 | } |
| 208 | } |