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; |
| 22 | import android.graphics.drawable.ColorDrawable; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame^] | 23 | import android.os.Handler; |
| 24 | import android.util.Log; |
| 25 | import android.view.KeyEvent; |
| 26 | import android.view.KeyboardShortcutGroup; |
| 27 | import android.view.KeyboardShortcutInfo; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 28 | import android.view.LayoutInflater; |
| 29 | import android.view.View; |
| 30 | import android.view.Window; |
| 31 | import android.view.WindowManager; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame^] | 32 | import android.view.WindowManager.KeyboardShortcutsReceiver; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 33 | |
| 34 | import com.android.systemui.R; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame^] | 35 | import com.android.systemui.recents.Recents; |
| 36 | |
| 37 | import java.util.List; |
| 38 | |
| 39 | import static android.content.Context.LAYOUT_INFLATER_SERVICE; |
| 40 | import static android.graphics.Color.TRANSPARENT; |
| 41 | import static android.view.Gravity.TOP; |
| 42 | import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG; |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Contains functionality for handling keyboard shortcuts. |
| 46 | */ |
| 47 | public class KeyboardShortcuts { |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame^] | 48 | private static final String TAG = "KeyboardShortcuts"; |
| 49 | |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 50 | private Dialog mKeyboardShortcutsDialog; |
| 51 | |
| 52 | public KeyboardShortcuts() {} |
| 53 | |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame^] | 54 | public void toggleKeyboardShortcuts(final Context context) { |
Andrei Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 55 | if (mKeyboardShortcutsDialog == null) { |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame^] | 56 | 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 Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 77 | } else { |
| 78 | dismissKeyboardShortcutsDialog(); |
| 79 | } |
| 80 | } |
| 81 | |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame^] | 82 | 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 Stingaceanu | 9d9294c | 2015-08-24 17:19:06 +0100 | [diff] [blame] | 109 | 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 | } |