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; |
| 25 | import android.util.Log; |
| 26 | import android.view.IWindowManager; |
| 27 | import android.view.KeyEvent; |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 28 | import android.view.WindowManager; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 29 | import android.view.WindowManagerGlobal; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 30 | |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 31 | import com.android.internal.policy.DividerSnapAlgorithm; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 32 | import com.android.systemui.SystemUI; |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 33 | import com.android.systemui.recents.Recents; |
| 34 | import com.android.systemui.stackdivider.Divider; |
| 35 | import com.android.systemui.stackdivider.DividerView; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Dispatches shortcut to System UI components |
| 39 | */ |
| 40 | public 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 Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 46 | private IWindowManager mWindowManagerService = WindowManagerGlobal.getWindowManagerService(); |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 47 | |
| 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 Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 53 | 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 Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 56 | public ShortcutKeyDispatcher(Context context) { |
| 57 | super(context); |
| 58 | } |
| 59 | |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 60 | /** |
| 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 Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 66 | mWindowManagerService.registerShortcutKey(shortcutCode, mShortcutKeyServiceProxy); |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 67 | } catch (RemoteException e) { |
| 68 | // Do nothing |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | @Override |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 73 | 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 Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 80 | |
| 81 | @Override |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 82 | 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 Chung | 42ce900 | 2017-11-17 13:04:07 -0800 | [diff] [blame] | 91 | // Split the screen |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 92 | Recents recents = getComponent(Recents.class); |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 93 | recents.splitPrimaryTask((shortcutCode == SC_DOCK_LEFT) |
Matthew Ng | bf15587 | 2017-10-27 15:24:39 -0700 | [diff] [blame] | 94 | ? SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT |
Winson Chung | 42ce900 | 2017-11-17 13:04:07 -0800 | [diff] [blame] | 95 | : SPLIT_SCREEN_CREATE_MODE_BOTTOM_OR_RIGHT, null, -1); |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 96 | } 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 Li | 5ef619f | 2016-07-22 15:47:51 -0700 | [diff] [blame] | 103 | DividerSnapAlgorithm.SnapTarget target = (shortcutCode == SC_DOCK_LEFT) |
| 104 | ? snapAlgorithm.getPreviousTarget(currentTarget) |
| 105 | : snapAlgorithm.getNextTarget(currentTarget); |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 106 | dividerView.startDragging(true /* animate */, false /* touching */); |
Muyuan Li | 5ef619f | 2016-07-22 15:47:51 -0700 | [diff] [blame] | 107 | dividerView.stopDragging(target.position, 0f, false /* avoidDismissStart */, |
Jorim Jaggi | 29379ec | 2016-04-11 23:43:42 -0700 | [diff] [blame] | 108 | true /* logMetrics */); |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 109 | } |
| 110 | } catch (RemoteException e) { |
| 111 | Log.e(TAG, "handleDockKey() failed."); |
| 112 | } |
| 113 | } |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 114 | } |