blob: 25e9a7aa1d8bfe64856bd28c62f6557820ba54ee [file] [log] [blame]
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +01001/*
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
17package com.android.systemui.statusbar;
18
19import android.app.AlertDialog;
20import android.app.Dialog;
21import android.content.Context;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000022import android.content.DialogInterface;
23import android.content.DialogInterface.OnClickListener;
24import android.graphics.Color;
25import android.graphics.Typeface;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010026import android.graphics.drawable.ColorDrawable;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000027import android.graphics.drawable.Drawable;
Clara Bayarri75e09792015-07-29 16:20:40 +010028import android.os.Handler;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000029import android.os.Looper;
30import android.util.DisplayMetrics;
Clara Bayarri75e09792015-07-29 16:20:40 +010031import android.util.Log;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000032import android.util.TypedValue;
Clara Bayarri75e09792015-07-29 16:20:40 +010033import android.view.KeyEvent;
34import android.view.KeyboardShortcutGroup;
35import android.view.KeyboardShortcutInfo;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010036import android.view.LayoutInflater;
37import android.view.View;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000038import android.view.ViewGroup;
39import android.view.ViewGroup.LayoutParams;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010040import android.view.Window;
Clara Bayarri75e09792015-07-29 16:20:40 +010041import android.view.WindowManager.KeyboardShortcutsReceiver;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000042import android.widget.LinearLayout;
43import android.widget.ScrollView;
44import android.widget.TextView;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010045
46import com.android.systemui.R;
Clara Bayarri75e09792015-07-29 16:20:40 +010047import com.android.systemui.recents.Recents;
48
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000049import java.util.ArrayList;
Clara Bayarri75e09792015-07-29 16:20:40 +010050import java.util.List;
51
52import static android.content.Context.LAYOUT_INFLATER_SERVICE;
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000053import static android.graphics.Color.WHITE;
Clara Bayarri75e09792015-07-29 16:20:40 +010054import static android.view.Gravity.TOP;
55import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010056
57/**
58 * Contains functionality for handling keyboard shortcuts.
59 */
60public class KeyboardShortcuts {
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000061 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 Bayarri75e09792015-07-29 16:20:40 +010072
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010073 private Dialog mKeyboardShortcutsDialog;
74
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000075 public KeyboardShortcuts(Context context) {
76 this.mContext = context;
77 }
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010078
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000079 public void toggleKeyboardShortcuts() {
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010080 if (mKeyboardShortcutsDialog == null) {
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000081 Recents.getSystemServices().requestKeyboardShortcuts(mContext,
Clara Bayarri75e09792015-07-29 16:20:40 +010082 new KeyboardShortcutsReceiver() {
83 @Override
84 public void onKeyboardShortcutsReceived(
85 final List<KeyboardShortcutGroup> result) {
86 KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup(
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000087 mContext.getString(R.string.keyboard_shortcut_group_system), true);
Clara Bayarri75e09792015-07-29 16:20:40 +010088 systemGroup.addItem(new KeyboardShortcutInfo(
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000089 mContext.getString(R.string.keyboard_shortcut_group_system_home),
90 SYSTEM_HOME_BASE_CHARACTER, KeyEvent.META_META_ON));
Clara Bayarri75e09792015-07-29 16:20:40 +010091 systemGroup.addItem(new KeyboardShortcutInfo(
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000092 mContext.getString(R.string.keyboard_shortcut_group_system_back),
93 SYSTEM_BACK_BASE_CHARACTER, KeyEvent.META_META_ON));
Clara Bayarri75e09792015-07-29 16:20:40 +010094 systemGroup.addItem(new KeyboardShortcutInfo(
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000095 mContext.getString(R.string.keyboard_shortcut_group_system_recents),
96 SYSTEM_RECENTS_BASE_CHARACTER, KeyEvent.META_ALT_ON));
Clara Bayarri75e09792015-07-29 16:20:40 +010097 result.add(systemGroup);
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +000098 showKeyboardShortcutsDialog(result);
Clara Bayarri75e09792015-07-29 16:20:40 +010099 }
100 });
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100101 } else {
102 dismissKeyboardShortcutsDialog();
103 }
104 }
105
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100106 public void dismissKeyboardShortcutsDialog() {
107 if (mKeyboardShortcutsDialog != null) {
108 mKeyboardShortcutsDialog.dismiss();
109 mKeyboardShortcutsDialog = null;
110 }
111 }
112
Andrei Stingaceanu8861cb02016-01-20 16:48:30 +0000113 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 Stingaceanu9d9294c2015-08-24 17:19:06 +0100207 }
208}