blob: f7f12239c6db03a5ebdbbdeedd6a96ac15d2df6d [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;
Muyuan Li94ce94e2016-02-24 16:20:54 -080025import android.view.IWindowManager;
26import android.view.KeyEvent;
27import android.view.WindowManagerGlobal;
Gus Prevasab336792018-11-14 13:52:20 -050028
Muyuan Lia2129992016-03-03 18:30:39 -080029import com.android.internal.policy.DividerSnapAlgorithm;
Muyuan Li94ce94e2016-02-24 16:20:54 -080030import com.android.systemui.SystemUI;
Muyuan Lia2129992016-03-03 18:30:39 -080031import com.android.systemui.recents.Recents;
32import com.android.systemui.stackdivider.Divider;
33import com.android.systemui.stackdivider.DividerView;
Muyuan Li94ce94e2016-02-24 16:20:54 -080034
Dave Mankoff613c7c62019-11-04 11:46:36 -050035import javax.inject.Inject;
36import javax.inject.Singleton;
37
Muyuan Li94ce94e2016-02-24 16:20:54 -080038/**
39 * Dispatches shortcut to System UI components
40 */
Dave Mankoff613c7c62019-11-04 11:46:36 -050041@Singleton
Muyuan Li94ce94e2016-02-24 16:20:54 -080042public class ShortcutKeyDispatcher extends SystemUI
43 implements ShortcutKeyServiceProxy.Callbacks {
44
45 private static final String TAG = "ShortcutKeyDispatcher";
Dave Mankoff613c7c62019-11-04 11:46:36 -050046 private final Divider mDivider;
Dave Mankoff6c64d1f2019-11-07 17:27:50 -050047 private final Recents mRecents;
Muyuan Li94ce94e2016-02-24 16:20:54 -080048
49 private ShortcutKeyServiceProxy mShortcutKeyServiceProxy = new ShortcutKeyServiceProxy(this);
Muyuan Lia2129992016-03-03 18:30:39 -080050 private IWindowManager mWindowManagerService = WindowManagerGlobal.getWindowManagerService();
Muyuan Li94ce94e2016-02-24 16:20:54 -080051
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
Muyuan Lia2129992016-03-03 18:30:39 -080057 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
Dave Mankoff613c7c62019-11-04 11:46:36 -050060 @Inject
Dave Mankoff6c64d1f2019-11-07 17:27:50 -050061 public ShortcutKeyDispatcher(Context context, Divider divider, Recents recents) {
Dave Mankoffa5d8a392019-10-10 12:21:09 -040062 super(context);
Dave Mankoff613c7c62019-11-04 11:46:36 -050063 mDivider = divider;
Dave Mankoff6c64d1f2019-11-07 17:27:50 -050064 mRecents = recents;
Dave Mankoffa5d8a392019-10-10 12:21:09 -040065 }
66
Muyuan Li94ce94e2016-02-24 16:20:54 -080067 /**
68 * Registers a shortcut key to window manager.
69 * @param shortcutCode packed representation of shortcut key code and meta information
70 */
71 public void registerShortcutKey(long shortcutCode) {
72 try {
Muyuan Lia2129992016-03-03 18:30:39 -080073 mWindowManagerService.registerShortcutKey(shortcutCode, mShortcutKeyServiceProxy);
Muyuan Li94ce94e2016-02-24 16:20:54 -080074 } catch (RemoteException e) {
75 // Do nothing
76 }
77 }
78
79 @Override
Muyuan Lia2129992016-03-03 18:30:39 -080080 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 }
Muyuan Li94ce94e2016-02-24 16:20:54 -080087
88 @Override
Muyuan Lia2129992016-03-03 18:30:39 -080089 public void start() {
90 registerShortcutKey(SC_DOCK_LEFT);
91 registerShortcutKey(SC_DOCK_RIGHT);
92 }
93
94 private void handleDockKey(long shortcutCode) {
Evan Rosky90d5b6d2020-05-13 19:39:05 -070095 if (mDivider == null || !mDivider.isDividerVisible()) {
Evan Roskyaf9f27c2020-02-18 18:58:35 +000096 // 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 */);
Muyuan Lia2129992016-03-03 18:30:39 -0800113 }
114 }
Muyuan Li94ce94e2016-02-24 16:20:54 -0800115}