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