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.shortcut;
18 
19 import android.accessibilityservice.AccessibilityServiceInfo;
20 import android.app.ActivityManager;
21 import android.app.IActivityManager;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.pm.ServiceInfo;
25 import android.content.res.Configuration;
26 import android.os.RemoteException;
27 import android.os.UserHandle;
28 import android.util.ArraySet;
29 import android.util.DisplayMetrics;
30 import android.util.Log;
31 import android.view.IWindowManager;
32 import android.view.KeyEvent;
33 import android.view.WindowManager;
34 import android.view.WindowManagerGlobal;
35 import android.view.accessibility.AccessibilityManager;
36 import com.android.internal.logging.MetricsLogger;
37 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
38 import com.android.internal.policy.DividerSnapAlgorithm;
39 import com.android.settingslib.accessibility.AccessibilityUtils;
40 import com.android.systemui.R;
41 import com.android.systemui.SystemUI;
42 import com.android.systemui.recents.Recents;
43 import com.android.systemui.recents.misc.SystemServicesProxy;
44 import com.android.systemui.stackdivider.Divider;
45 import com.android.systemui.stackdivider.DividerView;
46 import com.android.systemui.statusbar.phone.NavigationBarGestureHelper;
47 
48 import java.util.List;
49 import java.util.Set;
50 
51 /**
52  * Dispatches shortcut to System UI components
53  */
54 public class ShortcutKeyDispatcher extends SystemUI
55         implements ShortcutKeyServiceProxy.Callbacks {
56 
57     private static final String TAG = "ShortcutKeyDispatcher";
58 
59     private ShortcutKeyServiceProxy mShortcutKeyServiceProxy = new ShortcutKeyServiceProxy(this);
60     private IWindowManager mWindowManagerService = WindowManagerGlobal.getWindowManagerService();
61     private IActivityManager mActivityManager = ActivityManager.getService();
62 
63     protected final long META_MASK = ((long) KeyEvent.META_META_ON) << Integer.SIZE;
64     protected final long ALT_MASK = ((long) KeyEvent.META_ALT_ON) << Integer.SIZE;
65     protected final long CTRL_MASK = ((long) KeyEvent.META_CTRL_ON) << Integer.SIZE;
66     protected final long SHIFT_MASK = ((long) KeyEvent.META_SHIFT_ON) << Integer.SIZE;
67 
68     protected final long SC_DOCK_LEFT = META_MASK | KeyEvent.KEYCODE_LEFT_BRACKET;
69     protected final long SC_DOCK_RIGHT = META_MASK | KeyEvent.KEYCODE_RIGHT_BRACKET;
70 
71     /**
72      * Registers a shortcut key to window manager.
73      * @param shortcutCode packed representation of shortcut key code and meta information
74      */
registerShortcutKey(long shortcutCode)75     public void registerShortcutKey(long shortcutCode) {
76         try {
77             mWindowManagerService.registerShortcutKey(shortcutCode, mShortcutKeyServiceProxy);
78         } catch (RemoteException e) {
79             // Do nothing
80         }
81     }
82 
83     @Override
onShortcutKeyPressed(long shortcutCode)84     public void onShortcutKeyPressed(long shortcutCode) {
85         int orientation = mContext.getResources().getConfiguration().orientation;
86         if ((shortcutCode == SC_DOCK_LEFT || shortcutCode == SC_DOCK_RIGHT)
87                 && orientation == Configuration.ORIENTATION_LANDSCAPE) {
88             handleDockKey(shortcutCode);
89         }
90     }
91 
92     @Override
start()93     public void start() {
94         registerShortcutKey(SC_DOCK_LEFT);
95         registerShortcutKey(SC_DOCK_RIGHT);
96     }
97 
handleDockKey(long shortcutCode)98     private void handleDockKey(long shortcutCode) {
99         try {
100             int dockSide = mWindowManagerService.getDockedStackSide();
101             if (dockSide == WindowManager.DOCKED_INVALID) {
102                 // If there is no window docked, we dock the top-most window.
103                 Recents recents = getComponent(Recents.class);
104                 int dockMode = (shortcutCode == SC_DOCK_LEFT)
105                         ? ActivityManager.DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT
106                         : ActivityManager.DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT;
107                 List<ActivityManager.RecentTaskInfo> taskList =
108                         SystemServicesProxy.getInstance(mContext).getRecentTasks(1,
109                                 UserHandle.USER_CURRENT, false, new ArraySet<>());
110                 recents.showRecentApps(
111                         false /* triggeredFromAltTab */,
112                         false /* fromHome */);
113                 if (!taskList.isEmpty()) {
114                     SystemServicesProxy.getInstance(mContext).startTaskInDockedMode(
115                             taskList.get(0).id, dockMode);
116                 }
117             } else {
118                 // If there is already a docked window, we respond by resizing the docking pane.
119                 DividerView dividerView = getComponent(Divider.class).getView();
120                 DividerSnapAlgorithm snapAlgorithm = dividerView.getSnapAlgorithm();
121                 int dividerPosition = dividerView.getCurrentPosition();
122                 DividerSnapAlgorithm.SnapTarget currentTarget =
123                         snapAlgorithm.calculateNonDismissingSnapTarget(dividerPosition);
124                 DividerSnapAlgorithm.SnapTarget target = (shortcutCode == SC_DOCK_LEFT)
125                         ? snapAlgorithm.getPreviousTarget(currentTarget)
126                         : snapAlgorithm.getNextTarget(currentTarget);
127                 dividerView.startDragging(true /* animate */, false /* touching */);
128                 dividerView.stopDragging(target.position, 0f, false /* avoidDismissStart */,
129                         true /* logMetrics */);
130             }
131         } catch (RemoteException e) {
132             Log.e(TAG, "handleDockKey() failed.");
133         }
134     }
135 }
136