blob: b36fb7e65f7e1d4c72f84660f7f7e1cc09ca7dad [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;
22import android.graphics.drawable.ColorDrawable;
Clara Bayarri75e09792015-07-29 16:20:40 +010023import android.os.Handler;
24import android.util.Log;
25import android.view.KeyEvent;
26import android.view.KeyboardShortcutGroup;
27import android.view.KeyboardShortcutInfo;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010028import android.view.LayoutInflater;
29import android.view.View;
30import android.view.Window;
31import android.view.WindowManager;
Clara Bayarri75e09792015-07-29 16:20:40 +010032import android.view.WindowManager.KeyboardShortcutsReceiver;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010033
34import com.android.systemui.R;
Clara Bayarri75e09792015-07-29 16:20:40 +010035import com.android.systemui.recents.Recents;
36
37import java.util.List;
38
39import static android.content.Context.LAYOUT_INFLATER_SERVICE;
40import static android.graphics.Color.TRANSPARENT;
41import static android.view.Gravity.TOP;
42import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010043
44/**
45 * Contains functionality for handling keyboard shortcuts.
46 */
47public class KeyboardShortcuts {
Clara Bayarri75e09792015-07-29 16:20:40 +010048 private static final String TAG = "KeyboardShortcuts";
49
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010050 private Dialog mKeyboardShortcutsDialog;
51
52 public KeyboardShortcuts() {}
53
Clara Bayarri75e09792015-07-29 16:20:40 +010054 public void toggleKeyboardShortcuts(final Context context) {
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010055 if (mKeyboardShortcutsDialog == null) {
Clara Bayarri75e09792015-07-29 16:20:40 +010056 Recents.getSystemServices().requestKeyboardShortcuts(context,
57 new KeyboardShortcutsReceiver() {
58 @Override
59 public void onKeyboardShortcutsReceived(
60 final List<KeyboardShortcutGroup> result) {
61 KeyboardShortcutGroup systemGroup = new KeyboardShortcutGroup(
62 context.getString(R.string.keyboard_shortcut_group_system));
63 systemGroup.addItem(new KeyboardShortcutInfo(
64 context.getString(R.string.keyboard_shortcut_group_system_home),
65 '\u2386', KeyEvent.META_META_ON));
66 systemGroup.addItem(new KeyboardShortcutInfo(
67 context.getString(R.string.keyboard_shortcut_group_system_back),
68 '\u007F', KeyEvent.META_META_ON));
69 systemGroup.addItem(new KeyboardShortcutInfo(
70 context.getString(R.string.keyboard_shortcut_group_system_recents),
71 '\u0009', KeyEvent.META_ALT_ON));
72 result.add(systemGroup);
73 Log.i(TAG, "Keyboard shortcuts received: " + String.valueOf(result));
74 showKeyboardShortcutsDialog(context);
75 }
76 });
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +010077 } else {
78 dismissKeyboardShortcutsDialog();
79 }
80 }
81
Clara Bayarri75e09792015-07-29 16:20:40 +010082 private void showKeyboardShortcutsDialog(Context context) {
83 // Create dialog.
84 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
85 LayoutInflater inflater = (LayoutInflater) context.getSystemService(
86 LAYOUT_INFLATER_SERVICE);
87 final View keyboardShortcutsView = inflater.inflate(
88 R.layout.keyboard_shortcuts_view, null);
89
90 populateKeyboardShortcuts(keyboardShortcutsView.findViewById(
91 R.id.keyboard_shortcuts_wrapper));
92 dialogBuilder.setView(keyboardShortcutsView);
93 mKeyboardShortcutsDialog = dialogBuilder.create();
94 mKeyboardShortcutsDialog.setCanceledOnTouchOutside(true);
95
96 // Setup window.
97 Window keyboardShortcutsWindow = mKeyboardShortcutsDialog.getWindow();
98 keyboardShortcutsWindow.setType(TYPE_SYSTEM_DIALOG);
99 keyboardShortcutsWindow.setBackgroundDrawable(
100 new ColorDrawable(TRANSPARENT));
101 keyboardShortcutsWindow.setGravity(TOP);
102 keyboardShortcutsView.post(new Runnable() {
103 public void run() {
104 mKeyboardShortcutsDialog.show();
105 }
106 });
107 }
108
Andrei Stingaceanu9d9294c2015-08-24 17:19:06 +0100109 public void dismissKeyboardShortcutsDialog() {
110 if (mKeyboardShortcutsDialog != null) {
111 mKeyboardShortcutsDialog.dismiss();
112 mKeyboardShortcutsDialog = null;
113 }
114 }
115
116 /**
117 * @return {@code true} if the keyboard shortcuts have been successfully populated.
118 */
119 private boolean populateKeyboardShortcuts(View keyboardShortcutsLayout) {
120 // TODO: Populate shortcuts.
121 return true;
122 }
123}