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.ActivityTaskManager.SPLIT_SCREEN_CREATE_MODE_BOTTOM_OR_RIGHT;
20 import static android.app.ActivityTaskManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT;
21 
22 import android.content.Context;
23 import android.content.res.Configuration;
24 import android.os.RemoteException;
25 import android.view.IWindowManager;
26 import android.view.KeyEvent;
27 import android.view.WindowManagerGlobal;
28 
29 import com.android.internal.policy.DividerSnapAlgorithm;
30 import com.android.systemui.SystemUI;
31 import com.android.systemui.recents.Recents;
32 import com.android.systemui.stackdivider.Divider;
33 import com.android.systemui.stackdivider.DividerView;
34 
35 import javax.inject.Inject;
36 import javax.inject.Singleton;
37 
38 /**
39  * Dispatches shortcut to System UI components
40  */
41 @Singleton
42 public class ShortcutKeyDispatcher extends SystemUI
43         implements ShortcutKeyServiceProxy.Callbacks {
44 
45     private static final String TAG = "ShortcutKeyDispatcher";
46     private final Divider mDivider;
47     private final Recents mRecents;
48 
49     private ShortcutKeyServiceProxy mShortcutKeyServiceProxy = new ShortcutKeyServiceProxy(this);
50     private IWindowManager mWindowManagerService = WindowManagerGlobal.getWindowManagerService();
51 
52     protected final long META_MASK = ((long) KeyEvent.META_META_ON) << Integer.SIZE;
53     protected final long ALT_MASK = ((long) KeyEvent.META_ALT_ON) << Integer.SIZE;
54     protected final long CTRL_MASK = ((long) KeyEvent.META_CTRL_ON) << Integer.SIZE;
55     protected final long SHIFT_MASK = ((long) KeyEvent.META_SHIFT_ON) << Integer.SIZE;
56 
57     protected final long SC_DOCK_LEFT = META_MASK | KeyEvent.KEYCODE_LEFT_BRACKET;
58     protected final long SC_DOCK_RIGHT = META_MASK | KeyEvent.KEYCODE_RIGHT_BRACKET;
59 
60     @Inject
ShortcutKeyDispatcher(Context context, Divider divider, Recents recents)61     public ShortcutKeyDispatcher(Context context, Divider divider, Recents recents) {
62         super(context);
63         mDivider = divider;
64         mRecents = recents;
65     }
66 
67     /**
68      * Registers a shortcut key to window manager.
69      * @param shortcutCode packed representation of shortcut key code and meta information
70      */
registerShortcutKey(long shortcutCode)71     public void registerShortcutKey(long shortcutCode) {
72         try {
73             mWindowManagerService.registerShortcutKey(shortcutCode, mShortcutKeyServiceProxy);
74         } catch (RemoteException e) {
75             // Do nothing
76         }
77     }
78 
79     @Override
onShortcutKeyPressed(long shortcutCode)80     public void onShortcutKeyPressed(long shortcutCode) {
81         int orientation = mContext.getResources().getConfiguration().orientation;
82         if ((shortcutCode == SC_DOCK_LEFT || shortcutCode == SC_DOCK_RIGHT)
83                 && orientation == Configuration.ORIENTATION_LANDSCAPE) {
84             handleDockKey(shortcutCode);
85         }
86     }
87 
88     @Override
start()89     public void start() {
90         registerShortcutKey(SC_DOCK_LEFT);
91         registerShortcutKey(SC_DOCK_RIGHT);
92     }
93 
handleDockKey(long shortcutCode)94     private void handleDockKey(long shortcutCode) {
95         if (mDivider == null || !mDivider.isDividerVisible()) {
96             // Split the screen
97             mRecents.splitPrimaryTask((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 = mDivider.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     }
115 }
116