blob: df9791d1bd37d58f17d9520ee82985659cd1efdd [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
37/**
38 * Dispatches shortcut to System UI components
39 */
40public class ShortcutKeyDispatcher extends SystemUI
41 implements ShortcutKeyServiceProxy.Callbacks {
42
43 private static final String TAG = "ShortcutKeyDispatcher";
44
45 private ShortcutKeyServiceProxy mShortcutKeyServiceProxy = new ShortcutKeyServiceProxy(this);
Muyuan Lia2129992016-03-03 18:30:39 -080046 private IWindowManager mWindowManagerService = WindowManagerGlobal.getWindowManagerService();
Muyuan Li94ce94e2016-02-24 16:20:54 -080047
48 protected final long META_MASK = ((long) KeyEvent.META_META_ON) << Integer.SIZE;
49 protected final long ALT_MASK = ((long) KeyEvent.META_ALT_ON) << Integer.SIZE;
50 protected final long CTRL_MASK = ((long) KeyEvent.META_CTRL_ON) << Integer.SIZE;
51 protected final long SHIFT_MASK = ((long) KeyEvent.META_SHIFT_ON) << Integer.SIZE;
52
Muyuan Lia2129992016-03-03 18:30:39 -080053 protected final long SC_DOCK_LEFT = META_MASK | KeyEvent.KEYCODE_LEFT_BRACKET;
54 protected final long SC_DOCK_RIGHT = META_MASK | KeyEvent.KEYCODE_RIGHT_BRACKET;
55
Dave Mankoffa5d8a392019-10-10 12:21:09 -040056 public ShortcutKeyDispatcher(Context context) {
57 super(context);
58 }
59
Muyuan Li94ce94e2016-02-24 16:20:54 -080060 /**
61 * Registers a shortcut key to window manager.
62 * @param shortcutCode packed representation of shortcut key code and meta information
63 */
64 public void registerShortcutKey(long shortcutCode) {
65 try {
Muyuan Lia2129992016-03-03 18:30:39 -080066 mWindowManagerService.registerShortcutKey(shortcutCode, mShortcutKeyServiceProxy);
Muyuan Li94ce94e2016-02-24 16:20:54 -080067 } catch (RemoteException e) {
68 // Do nothing
69 }
70 }
71
72 @Override
Muyuan Lia2129992016-03-03 18:30:39 -080073 public void onShortcutKeyPressed(long shortcutCode) {
74 int orientation = mContext.getResources().getConfiguration().orientation;
75 if ((shortcutCode == SC_DOCK_LEFT || shortcutCode == SC_DOCK_RIGHT)
76 && orientation == Configuration.ORIENTATION_LANDSCAPE) {
77 handleDockKey(shortcutCode);
78 }
79 }
Muyuan Li94ce94e2016-02-24 16:20:54 -080080
81 @Override
Muyuan Lia2129992016-03-03 18:30:39 -080082 public void start() {
83 registerShortcutKey(SC_DOCK_LEFT);
84 registerShortcutKey(SC_DOCK_RIGHT);
85 }
86
87 private void handleDockKey(long shortcutCode) {
88 try {
89 int dockSide = mWindowManagerService.getDockedStackSide();
90 if (dockSide == WindowManager.DOCKED_INVALID) {
Winson Chung42ce9002017-11-17 13:04:07 -080091 // Split the screen
Muyuan Lia2129992016-03-03 18:30:39 -080092 Recents recents = getComponent(Recents.class);
Winson Chung67f5c8b2018-09-24 12:09:19 -070093 recents.splitPrimaryTask((shortcutCode == SC_DOCK_LEFT)
Matthew Ngbf155872017-10-27 15:24:39 -070094 ? SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT
Winson Chung42ce9002017-11-17 13:04:07 -080095 : SPLIT_SCREEN_CREATE_MODE_BOTTOM_OR_RIGHT, null, -1);
Muyuan Lia2129992016-03-03 18:30:39 -080096 } else {
97 // If there is already a docked window, we respond by resizing the docking pane.
98 DividerView dividerView = getComponent(Divider.class).getView();
99 DividerSnapAlgorithm snapAlgorithm = dividerView.getSnapAlgorithm();
100 int dividerPosition = dividerView.getCurrentPosition();
101 DividerSnapAlgorithm.SnapTarget currentTarget =
102 snapAlgorithm.calculateNonDismissingSnapTarget(dividerPosition);
Muyuan Li5ef619f2016-07-22 15:47:51 -0700103 DividerSnapAlgorithm.SnapTarget target = (shortcutCode == SC_DOCK_LEFT)
104 ? snapAlgorithm.getPreviousTarget(currentTarget)
105 : snapAlgorithm.getNextTarget(currentTarget);
Muyuan Lia2129992016-03-03 18:30:39 -0800106 dividerView.startDragging(true /* animate */, false /* touching */);
Muyuan Li5ef619f2016-07-22 15:47:51 -0700107 dividerView.stopDragging(target.position, 0f, false /* avoidDismissStart */,
Jorim Jaggi29379ec2016-04-11 23:43:42 -0700108 true /* logMetrics */);
Muyuan Lia2129992016-03-03 18:30:39 -0800109 }
110 } catch (RemoteException e) {
111 Log.e(TAG, "handleDockKey() failed.");
112 }
113 }
Muyuan Li94ce94e2016-02-24 16:20:54 -0800114}