blob: da798848f7e8334703079b1ead46fdc89e436332 [file] [log] [blame]
Muyuan Li94ce94e2016-02-24 16:20:54 -08001/*
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
17package com.android.systemui.shortcut;
18
Matthew Ngbf155872017-10-27 15:24:39 -070019import static android.app.ActivityManager.SPLIT_SCREEN_CREATE_MODE_BOTTOM_OR_RIGHT;
20import static android.app.ActivityManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT;
Winson Chung5fa39752017-10-04 14:50:15 -070021import static android.os.UserHandle.USER_CURRENT;
22
Winson Chung42ce9002017-11-17 13:04:07 -080023import static com.android.systemui.statusbar.phone.NavigationBarGestureHelper.DRAG_MODE_NONE;
24
Muyuan Lia2129992016-03-03 18:30:39 -080025import android.app.ActivityManager;
Muyuan Lia2129992016-03-03 18:30:39 -080026import android.content.res.Configuration;
Muyuan Li94ce94e2016-02-24 16:20:54 -080027import android.os.RemoteException;
28import android.util.Log;
29import android.view.IWindowManager;
30import android.view.KeyEvent;
Muyuan Lia2129992016-03-03 18:30:39 -080031import android.view.WindowManager;
Muyuan Li94ce94e2016-02-24 16:20:54 -080032import android.view.WindowManagerGlobal;
Winson Chung6519c1b2017-10-13 17:12:56 -070033
Muyuan Lia2129992016-03-03 18:30:39 -080034import com.android.internal.policy.DividerSnapAlgorithm;
Muyuan Li94ce94e2016-02-24 16:20:54 -080035import com.android.systemui.SystemUI;
Muyuan Lia2129992016-03-03 18:30:39 -080036import com.android.systemui.recents.Recents;
Muyuan Li5ef619f2016-07-22 15:47:51 -070037import com.android.systemui.recents.misc.SystemServicesProxy;
Winson Chung6519c1b2017-10-13 17:12:56 -070038import com.android.systemui.shared.system.ActivityManagerWrapper;
Muyuan Lia2129992016-03-03 18:30:39 -080039import com.android.systemui.stackdivider.Divider;
40import com.android.systemui.stackdivider.DividerView;
Winson Chung42ce9002017-11-17 13:04:07 -080041import com.android.systemui.statusbar.phone.NavigationBarGestureHelper;
Muyuan Li94ce94e2016-02-24 16:20:54 -080042
43import java.util.List;
Muyuan Li94ce94e2016-02-24 16:20:54 -080044
45/**
46 * Dispatches shortcut to System UI components
47 */
48public class ShortcutKeyDispatcher extends SystemUI
49 implements ShortcutKeyServiceProxy.Callbacks {
50
51 private static final String TAG = "ShortcutKeyDispatcher";
52
53 private ShortcutKeyServiceProxy mShortcutKeyServiceProxy = new ShortcutKeyServiceProxy(this);
Muyuan Lia2129992016-03-03 18:30:39 -080054 private IWindowManager mWindowManagerService = WindowManagerGlobal.getWindowManagerService();
Muyuan Li94ce94e2016-02-24 16:20:54 -080055
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
Muyuan Lia2129992016-03-03 18:30:39 -080061 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
Muyuan Li94ce94e2016-02-24 16:20:54 -080064 /**
65 * Registers a shortcut key to window manager.
66 * @param shortcutCode packed representation of shortcut key code and meta information
67 */
68 public void registerShortcutKey(long shortcutCode) {
69 try {
Muyuan Lia2129992016-03-03 18:30:39 -080070 mWindowManagerService.registerShortcutKey(shortcutCode, mShortcutKeyServiceProxy);
Muyuan Li94ce94e2016-02-24 16:20:54 -080071 } catch (RemoteException e) {
72 // Do nothing
73 }
74 }
75
76 @Override
Muyuan Lia2129992016-03-03 18:30:39 -080077 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 }
Muyuan Li94ce94e2016-02-24 16:20:54 -080084
85 @Override
Muyuan Lia2129992016-03-03 18:30:39 -080086 public void start() {
87 registerShortcutKey(SC_DOCK_LEFT);
88 registerShortcutKey(SC_DOCK_RIGHT);
89 }
90
91 private void handleDockKey(long shortcutCode) {
92 try {
93 int dockSide = mWindowManagerService.getDockedStackSide();
94 if (dockSide == WindowManager.DOCKED_INVALID) {
Winson Chung42ce9002017-11-17 13:04:07 -080095 // Split the screen
Muyuan Lia2129992016-03-03 18:30:39 -080096 Recents recents = getComponent(Recents.class);
Winson Chung42ce9002017-11-17 13:04:07 -080097 recents.splitPrimaryTask(DRAG_MODE_NONE, (shortcutCode == SC_DOCK_LEFT)
Matthew Ngbf155872017-10-27 15:24:39 -070098 ? SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT
Winson Chung42ce9002017-11-17 13:04:07 -080099 : SPLIT_SCREEN_CREATE_MODE_BOTTOM_OR_RIGHT, null, -1);
Muyuan Lia2129992016-03-03 18:30:39 -0800100 } 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);
Muyuan Li5ef619f2016-07-22 15:47:51 -0700107 DividerSnapAlgorithm.SnapTarget target = (shortcutCode == SC_DOCK_LEFT)
108 ? snapAlgorithm.getPreviousTarget(currentTarget)
109 : snapAlgorithm.getNextTarget(currentTarget);
Muyuan Lia2129992016-03-03 18:30:39 -0800110 dividerView.startDragging(true /* animate */, false /* touching */);
Muyuan Li5ef619f2016-07-22 15:47:51 -0700111 dividerView.stopDragging(target.position, 0f, false /* avoidDismissStart */,
Jorim Jaggi29379ec2016-04-11 23:43:42 -0700112 true /* logMetrics */);
Muyuan Lia2129992016-03-03 18:30:39 -0800113 }
114 } catch (RemoteException e) {
115 Log.e(TAG, "handleDockKey() failed.");
116 }
117 }
Muyuan Li94ce94e2016-02-24 16:20:54 -0800118}