blob: 07675e248906ac41182ded04a59af9fb5cf199a5 [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
Muyuan Lia2129992016-03-03 18:30:39 -080022import android.content.res.Configuration;
Muyuan Li94ce94e2016-02-24 16:20:54 -080023import android.os.RemoteException;
24import android.util.Log;
25import android.view.IWindowManager;
26import android.view.KeyEvent;
Muyuan Lia2129992016-03-03 18:30:39 -080027import android.view.WindowManager;
Muyuan Li94ce94e2016-02-24 16:20:54 -080028import android.view.WindowManagerGlobal;
Gus Prevasab336792018-11-14 13:52:20 -050029
Muyuan Lia2129992016-03-03 18:30:39 -080030import com.android.internal.policy.DividerSnapAlgorithm;
Muyuan Li94ce94e2016-02-24 16:20:54 -080031import com.android.systemui.SystemUI;
Muyuan Lia2129992016-03-03 18:30:39 -080032import com.android.systemui.recents.Recents;
33import com.android.systemui.stackdivider.Divider;
34import com.android.systemui.stackdivider.DividerView;
Muyuan Li94ce94e2016-02-24 16:20:54 -080035
36/**
37 * Dispatches shortcut to System UI components
38 */
39public class ShortcutKeyDispatcher extends SystemUI
40 implements ShortcutKeyServiceProxy.Callbacks {
41
42 private static final String TAG = "ShortcutKeyDispatcher";
43
44 private ShortcutKeyServiceProxy mShortcutKeyServiceProxy = new ShortcutKeyServiceProxy(this);
Muyuan Lia2129992016-03-03 18:30:39 -080045 private IWindowManager mWindowManagerService = WindowManagerGlobal.getWindowManagerService();
Muyuan Li94ce94e2016-02-24 16:20:54 -080046
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
Muyuan Lia2129992016-03-03 18:30:39 -080052 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
Muyuan Li94ce94e2016-02-24 16:20:54 -080055 /**
56 * Registers a shortcut key to window manager.
57 * @param shortcutCode packed representation of shortcut key code and meta information
58 */
59 public void registerShortcutKey(long shortcutCode) {
60 try {
Muyuan Lia2129992016-03-03 18:30:39 -080061 mWindowManagerService.registerShortcutKey(shortcutCode, mShortcutKeyServiceProxy);
Muyuan Li94ce94e2016-02-24 16:20:54 -080062 } catch (RemoteException e) {
63 // Do nothing
64 }
65 }
66
67 @Override
Muyuan Lia2129992016-03-03 18:30:39 -080068 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 }
Muyuan Li94ce94e2016-02-24 16:20:54 -080075
76 @Override
Muyuan Lia2129992016-03-03 18:30:39 -080077 public void start() {
78 registerShortcutKey(SC_DOCK_LEFT);
79 registerShortcutKey(SC_DOCK_RIGHT);
80 }
81
82 private void handleDockKey(long shortcutCode) {
83 try {
84 int dockSide = mWindowManagerService.getDockedStackSide();
85 if (dockSide == WindowManager.DOCKED_INVALID) {
Winson Chung42ce9002017-11-17 13:04:07 -080086 // Split the screen
Muyuan Lia2129992016-03-03 18:30:39 -080087 Recents recents = getComponent(Recents.class);
Winson Chung67f5c8b2018-09-24 12:09:19 -070088 recents.splitPrimaryTask((shortcutCode == SC_DOCK_LEFT)
Matthew Ngbf155872017-10-27 15:24:39 -070089 ? SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT
Winson Chung42ce9002017-11-17 13:04:07 -080090 : SPLIT_SCREEN_CREATE_MODE_BOTTOM_OR_RIGHT, null, -1);
Muyuan Lia2129992016-03-03 18:30:39 -080091 } 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);
Muyuan Li5ef619f2016-07-22 15:47:51 -070098 DividerSnapAlgorithm.SnapTarget target = (shortcutCode == SC_DOCK_LEFT)
99 ? snapAlgorithm.getPreviousTarget(currentTarget)
100 : snapAlgorithm.getNextTarget(currentTarget);
Muyuan Lia2129992016-03-03 18:30:39 -0800101 dividerView.startDragging(true /* animate */, false /* touching */);
Muyuan Li5ef619f2016-07-22 15:47:51 -0700102 dividerView.stopDragging(target.position, 0f, false /* avoidDismissStart */,
Jorim Jaggi29379ec2016-04-11 23:43:42 -0700103 true /* logMetrics */);
Muyuan Lia2129992016-03-03 18:30:39 -0800104 }
105 } catch (RemoteException e) {
106 Log.e(TAG, "handleDockKey() failed.");
107 }
108 }
Muyuan Li94ce94e2016-02-24 16:20:54 -0800109}