Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.shortcut; |
| 18 | |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 19 | import static android.app.ActivityTaskManager.SPLIT_SCREEN_CREATE_MODE_BOTTOM_OR_RIGHT; |
| 20 | import static android.app.ActivityTaskManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT; |
Winson Chung | 5fa3975 | 2017-10-04 14:50:15 -0700 | [diff] [blame] | 21 | |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 22 | import android.content.Context; |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 23 | import android.content.res.Configuration; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 24 | import android.os.RemoteException; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 25 | import android.view.IWindowManager; |
| 26 | import android.view.KeyEvent; |
| 27 | import android.view.WindowManagerGlobal; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 28 | |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 29 | import com.android.internal.policy.DividerSnapAlgorithm; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 30 | import com.android.systemui.SystemUI; |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 31 | import com.android.systemui.recents.Recents; |
| 32 | import com.android.systemui.stackdivider.Divider; |
| 33 | import com.android.systemui.stackdivider.DividerView; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 34 | |
Dave Mankoff | 613c7c6 | 2019-11-04 11:46:36 -0500 | [diff] [blame] | 35 | import javax.inject.Inject; |
| 36 | import javax.inject.Singleton; |
| 37 | |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 38 | /** |
| 39 | * Dispatches shortcut to System UI components |
| 40 | */ |
Dave Mankoff | 613c7c6 | 2019-11-04 11:46:36 -0500 | [diff] [blame] | 41 | @Singleton |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 42 | public class ShortcutKeyDispatcher extends SystemUI |
| 43 | implements ShortcutKeyServiceProxy.Callbacks { |
| 44 | |
| 45 | private static final String TAG = "ShortcutKeyDispatcher"; |
Dave Mankoff | 613c7c6 | 2019-11-04 11:46:36 -0500 | [diff] [blame] | 46 | private final Divider mDivider; |
Dave Mankoff | 6c64d1f | 2019-11-07 17:27:50 -0500 | [diff] [blame] | 47 | private final Recents mRecents; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 48 | |
| 49 | private ShortcutKeyServiceProxy mShortcutKeyServiceProxy = new ShortcutKeyServiceProxy(this); |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 50 | private IWindowManager mWindowManagerService = WindowManagerGlobal.getWindowManagerService(); |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 51 | |
| 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 Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 57 | 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 Mankoff | 613c7c6 | 2019-11-04 11:46:36 -0500 | [diff] [blame] | 60 | @Inject |
Dave Mankoff | 6c64d1f | 2019-11-07 17:27:50 -0500 | [diff] [blame] | 61 | public ShortcutKeyDispatcher(Context context, Divider divider, Recents recents) { |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 62 | super(context); |
Dave Mankoff | 613c7c6 | 2019-11-04 11:46:36 -0500 | [diff] [blame] | 63 | mDivider = divider; |
Dave Mankoff | 6c64d1f | 2019-11-07 17:27:50 -0500 | [diff] [blame] | 64 | mRecents = recents; |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 67 | /** |
| 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 Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 73 | mWindowManagerService.registerShortcutKey(shortcutCode, mShortcutKeyServiceProxy); |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 74 | } catch (RemoteException e) { |
| 75 | // Do nothing |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @Override |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 80 | 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 Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 87 | |
| 88 | @Override |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 89 | public void start() { |
| 90 | registerShortcutKey(SC_DOCK_LEFT); |
| 91 | registerShortcutKey(SC_DOCK_RIGHT); |
| 92 | } |
| 93 | |
| 94 | private void handleDockKey(long shortcutCode) { |
Evan Rosky | 90d5b6d | 2020-05-13 19:39:05 -0700 | [diff] [blame] | 95 | if (mDivider == null || !mDivider.isDividerVisible()) { |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 96 | // 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 Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 115 | } |