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 static android.app.ActivityManager.SPLIT_SCREEN_CREATE_MODE_BOTTOM_OR_RIGHT;
20 import static android.app.ActivityManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT;
21 import static android.os.UserHandle.USER_CURRENT;
22 
23 import static com.android.systemui.statusbar.phone.NavigationBarGestureHelper.DRAG_MODE_NONE;
24 
25 import android.app.ActivityManager;
26 import android.content.res.Configuration;
27 import android.os.RemoteException;
28 import android.util.Log;
29 import android.view.IWindowManager;
30 import android.view.KeyEvent;
31 import android.view.WindowManager;
32 import android.view.WindowManagerGlobal;
33 
34 import com.android.internal.policy.DividerSnapAlgorithm;
35 import com.android.systemui.SystemUI;
36 import com.android.systemui.recents.Recents;
37 import com.android.systemui.recents.misc.SystemServicesProxy;
38 import com.android.systemui.shared.system.ActivityManagerWrapper;
39 import com.android.systemui.stackdivider.Divider;
40 import com.android.systemui.stackdivider.DividerView;
41 import com.android.systemui.statusbar.phone.NavigationBarGestureHelper;
42 
43 import java.util.List;
44 
45 /**
46  * Dispatches shortcut to System UI components
47  */
48 public class ShortcutKeyDispatcher extends SystemUI
49         implements ShortcutKeyServiceProxy.Callbacks {
50 
51     private static final String TAG = "ShortcutKeyDispatcher";
52 
53     private ShortcutKeyServiceProxy mShortcutKeyServiceProxy = new ShortcutKeyServiceProxy(this);
54     private IWindowManager mWindowManagerService = WindowManagerGlobal.getWindowManagerService();
55 
56     protected final long META_MASK = ((long) KeyEvent.META_META_ON) << Integer.SIZE;
57     protected final long ALT_MASK = ((long) KeyEvent.META_ALT_ON) << Integer.SIZE;
58     protected final long CTRL_MASK = ((long) KeyEvent.META_CTRL_ON) << Integer.SIZE;
59     protected final long SHIFT_MASK = ((long) KeyEvent.META_SHIFT_ON) << Integer.SIZE;
60 
61     protected final long SC_DOCK_LEFT = META_MASK | KeyEvent.KEYCODE_LEFT_BRACKET;
62     protected final long SC_DOCK_RIGHT = META_MASK | KeyEvent.KEYCODE_RIGHT_BRACKET;
63 
64     /**
65      * Registers a shortcut key to window manager.
66      * @param shortcutCode packed representation of shortcut key code and meta information
67      */
registerShortcutKey(long shortcutCode)68     public void registerShortcutKey(long shortcutCode) {
69         try {
70             mWindowManagerService.registerShortcutKey(shortcutCode, mShortcutKeyServiceProxy);
71         } catch (RemoteException e) {
72             // Do nothing
73         }
74     }
75 
76     @Override
onShortcutKeyPressed(long shortcutCode)77     public void onShortcutKeyPressed(long shortcutCode) {
78         int orientation = mContext.getResources().getConfiguration().orientation;
79         if ((shortcutCode == SC_DOCK_LEFT || shortcutCode == SC_DOCK_RIGHT)
80                 && orientation == Configuration.ORIENTATION_LANDSCAPE) {
81             handleDockKey(shortcutCode);
82         }
83     }
84 
85     @Override
start()86     public void start() {
87         registerShortcutKey(SC_DOCK_LEFT);
88         registerShortcutKey(SC_DOCK_RIGHT);
89     }
90 
handleDockKey(long shortcutCode)91     private void handleDockKey(long shortcutCode) {
92         try {
93             int dockSide = mWindowManagerService.getDockedStackSide();
94             if (dockSide == WindowManager.DOCKED_INVALID) {
95                 // Split the screen
96                 Recents recents = getComponent(Recents.class);
97                 recents.splitPrimaryTask(DRAG_MODE_NONE, (shortcutCode == SC_DOCK_LEFT)
98                         ? SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT
99                         : SPLIT_SCREEN_CREATE_MODE_BOTTOM_OR_RIGHT, null, -1);
100             } else {
101                 // If there is already a docked window, we respond by resizing the docking pane.
102                 DividerView dividerView = getComponent(Divider.class).getView();
103                 DividerSnapAlgorithm snapAlgorithm = dividerView.getSnapAlgorithm();
104                 int dividerPosition = dividerView.getCurrentPosition();
105                 DividerSnapAlgorithm.SnapTarget currentTarget =
106                         snapAlgorithm.calculateNonDismissingSnapTarget(dividerPosition);
107                 DividerSnapAlgorithm.SnapTarget target = (shortcutCode == SC_DOCK_LEFT)
108                         ? snapAlgorithm.getPreviousTarget(currentTarget)
109                         : snapAlgorithm.getNextTarget(currentTarget);
110                 dividerView.startDragging(true /* animate */, false /* touching */);
111                 dividerView.stopDragging(target.position, 0f, false /* avoidDismissStart */,
112                         true /* logMetrics */);
113             }
114         } catch (RemoteException e) {
115             Log.e(TAG, "handleDockKey() failed.");
116         }
117     }
118 }
119