blob: 5ae095421a80941e15a9bbe60fe6f708331af91d [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
Wale Ogunwale65ebd952018-04-25 15:41:44 -070019import static android.app.ActivityTaskManager.SPLIT_SCREEN_CREATE_MODE_BOTTOM_OR_RIGHT;
20import static android.app.ActivityTaskManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT;
Winson Chung5fa39752017-10-04 14:50:15 -070021
Dave Mankoffa5d8a392019-10-10 12:21:09 -040022import android.content.Context;
Muyuan Lia2129992016-03-03 18:30:39 -080023import android.content.res.Configuration;
Muyuan Li94ce94e2016-02-24 16:20:54 -080024import android.os.RemoteException;
25import android.util.Log;
26import android.view.IWindowManager;
27import android.view.KeyEvent;
Muyuan Lia2129992016-03-03 18:30:39 -080028import android.view.WindowManager;
Muyuan Li94ce94e2016-02-24 16:20:54 -080029import android.view.WindowManagerGlobal;
Gus Prevasab336792018-11-14 13:52:20 -050030
Muyuan Lia2129992016-03-03 18:30:39 -080031import com.android.internal.policy.DividerSnapAlgorithm;
Muyuan Li94ce94e2016-02-24 16:20:54 -080032import com.android.systemui.SystemUI;
Muyuan Lia2129992016-03-03 18:30:39 -080033import com.android.systemui.recents.Recents;
34import com.android.systemui.stackdivider.Divider;
35import com.android.systemui.stackdivider.DividerView;
Muyuan Li94ce94e2016-02-24 16:20:54 -080036
Dave Mankoff613c7c62019-11-04 11:46:36 -050037import javax.inject.Inject;
38import javax.inject.Singleton;
39
Muyuan Li94ce94e2016-02-24 16:20:54 -080040/**
41 * Dispatches shortcut to System UI components
42 */
Dave Mankoff613c7c62019-11-04 11:46:36 -050043@Singleton
Muyuan Li94ce94e2016-02-24 16:20:54 -080044public class ShortcutKeyDispatcher extends SystemUI
45 implements ShortcutKeyServiceProxy.Callbacks {
46
47 private static final String TAG = "ShortcutKeyDispatcher";
Dave Mankoff613c7c62019-11-04 11:46:36 -050048 private final Divider mDivider;
Dave Mankoff6c64d1f2019-11-07 17:27:50 -050049 private final Recents mRecents;
Muyuan Li94ce94e2016-02-24 16:20:54 -080050
51 private ShortcutKeyServiceProxy mShortcutKeyServiceProxy = new ShortcutKeyServiceProxy(this);
Muyuan Lia2129992016-03-03 18:30:39 -080052 private IWindowManager mWindowManagerService = WindowManagerGlobal.getWindowManagerService();
Muyuan Li94ce94e2016-02-24 16:20:54 -080053
54 protected final long META_MASK = ((long) KeyEvent.META_META_ON) << Integer.SIZE;
55 protected final long ALT_MASK = ((long) KeyEvent.META_ALT_ON) << Integer.SIZE;
56 protected final long CTRL_MASK = ((long) KeyEvent.META_CTRL_ON) << Integer.SIZE;
57 protected final long SHIFT_MASK = ((long) KeyEvent.META_SHIFT_ON) << Integer.SIZE;
58
Muyuan Lia2129992016-03-03 18:30:39 -080059 protected final long SC_DOCK_LEFT = META_MASK | KeyEvent.KEYCODE_LEFT_BRACKET;
60 protected final long SC_DOCK_RIGHT = META_MASK | KeyEvent.KEYCODE_RIGHT_BRACKET;
61
Dave Mankoff613c7c62019-11-04 11:46:36 -050062 @Inject
Dave Mankoff6c64d1f2019-11-07 17:27:50 -050063 public ShortcutKeyDispatcher(Context context, Divider divider, Recents recents) {
Dave Mankoffa5d8a392019-10-10 12:21:09 -040064 super(context);
Dave Mankoff613c7c62019-11-04 11:46:36 -050065 mDivider = divider;
Dave Mankoff6c64d1f2019-11-07 17:27:50 -050066 mRecents = recents;
Dave Mankoffa5d8a392019-10-10 12:21:09 -040067 }
68
Muyuan Li94ce94e2016-02-24 16:20:54 -080069 /**
70 * Registers a shortcut key to window manager.
71 * @param shortcutCode packed representation of shortcut key code and meta information
72 */
73 public void registerShortcutKey(long shortcutCode) {
74 try {
Muyuan Lia2129992016-03-03 18:30:39 -080075 mWindowManagerService.registerShortcutKey(shortcutCode, mShortcutKeyServiceProxy);
Muyuan Li94ce94e2016-02-24 16:20:54 -080076 } catch (RemoteException e) {
77 // Do nothing
78 }
79 }
80
81 @Override
Muyuan Lia2129992016-03-03 18:30:39 -080082 public void onShortcutKeyPressed(long shortcutCode) {
83 int orientation = mContext.getResources().getConfiguration().orientation;
84 if ((shortcutCode == SC_DOCK_LEFT || shortcutCode == SC_DOCK_RIGHT)
85 && orientation == Configuration.ORIENTATION_LANDSCAPE) {
86 handleDockKey(shortcutCode);
87 }
88 }
Muyuan Li94ce94e2016-02-24 16:20:54 -080089
90 @Override
Muyuan Lia2129992016-03-03 18:30:39 -080091 public void start() {
92 registerShortcutKey(SC_DOCK_LEFT);
93 registerShortcutKey(SC_DOCK_RIGHT);
94 }
95
96 private void handleDockKey(long shortcutCode) {
97 try {
98 int dockSide = mWindowManagerService.getDockedStackSide();
99 if (dockSide == WindowManager.DOCKED_INVALID) {
Winson Chung42ce9002017-11-17 13:04:07 -0800100 // Split the screen
Dave Mankoff6c64d1f2019-11-07 17:27:50 -0500101 mRecents.splitPrimaryTask((shortcutCode == SC_DOCK_LEFT)
Matthew Ngbf155872017-10-27 15:24:39 -0700102 ? SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT
Winson Chung42ce9002017-11-17 13:04:07 -0800103 : SPLIT_SCREEN_CREATE_MODE_BOTTOM_OR_RIGHT, null, -1);
Muyuan Lia2129992016-03-03 18:30:39 -0800104 } else {
105 // If there is already a docked window, we respond by resizing the docking pane.
Dave Mankoff613c7c62019-11-04 11:46:36 -0500106 DividerView dividerView = mDivider.getView();
Muyuan Lia2129992016-03-03 18:30:39 -0800107 DividerSnapAlgorithm snapAlgorithm = dividerView.getSnapAlgorithm();
108 int dividerPosition = dividerView.getCurrentPosition();
109 DividerSnapAlgorithm.SnapTarget currentTarget =
110 snapAlgorithm.calculateNonDismissingSnapTarget(dividerPosition);
Muyuan Li5ef619f2016-07-22 15:47:51 -0700111 DividerSnapAlgorithm.SnapTarget target = (shortcutCode == SC_DOCK_LEFT)
112 ? snapAlgorithm.getPreviousTarget(currentTarget)
113 : snapAlgorithm.getNextTarget(currentTarget);
Muyuan Lia2129992016-03-03 18:30:39 -0800114 dividerView.startDragging(true /* animate */, false /* touching */);
Muyuan Li5ef619f2016-07-22 15:47:51 -0700115 dividerView.stopDragging(target.position, 0f, false /* avoidDismissStart */,
Jorim Jaggi29379ec2016-04-11 23:43:42 -0700116 true /* logMetrics */);
Muyuan Lia2129992016-03-03 18:30:39 -0800117 }
118 } catch (RemoteException e) {
119 Log.e(TAG, "handleDockKey() failed.");
120 }
121 }
Muyuan Li94ce94e2016-02-24 16:20:54 -0800122}