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 | |
Matthew Ng | bf15587 | 2017-10-27 15:24:39 -0700 | [diff] [blame] | 19 | import static android.app.ActivityManager.SPLIT_SCREEN_CREATE_MODE_BOTTOM_OR_RIGHT; |
| 20 | import static android.app.ActivityManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT; |
Winson Chung | 5fa3975 | 2017-10-04 14:50:15 -0700 | [diff] [blame] | 21 | import static android.os.UserHandle.USER_CURRENT; |
| 22 | |
Winson Chung | 42ce900 | 2017-11-17 13:04:07 -0800 | [diff] [blame] | 23 | import static com.android.systemui.statusbar.phone.NavigationBarGestureHelper.DRAG_MODE_NONE; |
| 24 | |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 25 | import android.app.ActivityManager; |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 26 | import android.content.res.Configuration; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 27 | import android.os.RemoteException; |
| 28 | import android.util.Log; |
| 29 | import android.view.IWindowManager; |
| 30 | import android.view.KeyEvent; |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 31 | import android.view.WindowManager; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 32 | import android.view.WindowManagerGlobal; |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 33 | |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 34 | import com.android.internal.policy.DividerSnapAlgorithm; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 35 | import com.android.systemui.SystemUI; |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 36 | import com.android.systemui.recents.Recents; |
Muyuan Li | 5ef619f | 2016-07-22 15:47:51 -0700 | [diff] [blame] | 37 | import com.android.systemui.recents.misc.SystemServicesProxy; |
Winson Chung | 6519c1b | 2017-10-13 17:12:56 -0700 | [diff] [blame] | 38 | import com.android.systemui.shared.system.ActivityManagerWrapper; |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 39 | import com.android.systemui.stackdivider.Divider; |
| 40 | import com.android.systemui.stackdivider.DividerView; |
Winson Chung | 42ce900 | 2017-11-17 13:04:07 -0800 | [diff] [blame] | 41 | import com.android.systemui.statusbar.phone.NavigationBarGestureHelper; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 42 | |
| 43 | import java.util.List; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Dispatches shortcut to System UI components |
| 47 | */ |
| 48 | public class ShortcutKeyDispatcher extends SystemUI |
| 49 | implements ShortcutKeyServiceProxy.Callbacks { |
| 50 | |
| 51 | private static final String TAG = "ShortcutKeyDispatcher"; |
| 52 | |
| 53 | private ShortcutKeyServiceProxy mShortcutKeyServiceProxy = new ShortcutKeyServiceProxy(this); |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 54 | private IWindowManager mWindowManagerService = WindowManagerGlobal.getWindowManagerService(); |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 55 | |
| 56 | protected final long META_MASK = ((long) KeyEvent.META_META_ON) << Integer.SIZE; |
| 57 | protected final long ALT_MASK = ((long) KeyEvent.META_ALT_ON) << Integer.SIZE; |
| 58 | protected final long CTRL_MASK = ((long) KeyEvent.META_CTRL_ON) << Integer.SIZE; |
| 59 | protected final long SHIFT_MASK = ((long) KeyEvent.META_SHIFT_ON) << Integer.SIZE; |
| 60 | |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 61 | protected final long SC_DOCK_LEFT = META_MASK | KeyEvent.KEYCODE_LEFT_BRACKET; |
| 62 | protected final long SC_DOCK_RIGHT = META_MASK | KeyEvent.KEYCODE_RIGHT_BRACKET; |
| 63 | |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 64 | /** |
| 65 | * Registers a shortcut key to window manager. |
| 66 | * @param shortcutCode packed representation of shortcut key code and meta information |
| 67 | */ |
| 68 | public void registerShortcutKey(long shortcutCode) { |
| 69 | try { |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 70 | mWindowManagerService.registerShortcutKey(shortcutCode, mShortcutKeyServiceProxy); |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 71 | } catch (RemoteException e) { |
| 72 | // Do nothing |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | @Override |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 77 | public void onShortcutKeyPressed(long shortcutCode) { |
| 78 | int orientation = mContext.getResources().getConfiguration().orientation; |
| 79 | if ((shortcutCode == SC_DOCK_LEFT || shortcutCode == SC_DOCK_RIGHT) |
| 80 | && orientation == Configuration.ORIENTATION_LANDSCAPE) { |
| 81 | handleDockKey(shortcutCode); |
| 82 | } |
| 83 | } |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 84 | |
| 85 | @Override |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 86 | public void start() { |
| 87 | registerShortcutKey(SC_DOCK_LEFT); |
| 88 | registerShortcutKey(SC_DOCK_RIGHT); |
| 89 | } |
| 90 | |
| 91 | private void handleDockKey(long shortcutCode) { |
| 92 | try { |
| 93 | int dockSide = mWindowManagerService.getDockedStackSide(); |
| 94 | if (dockSide == WindowManager.DOCKED_INVALID) { |
Winson Chung | 42ce900 | 2017-11-17 13:04:07 -0800 | [diff] [blame] | 95 | // Split the screen |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 96 | Recents recents = getComponent(Recents.class); |
Winson Chung | 42ce900 | 2017-11-17 13:04:07 -0800 | [diff] [blame] | 97 | recents.splitPrimaryTask(DRAG_MODE_NONE, (shortcutCode == SC_DOCK_LEFT) |
Matthew Ng | bf15587 | 2017-10-27 15:24:39 -0700 | [diff] [blame] | 98 | ? SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT |
Winson Chung | 42ce900 | 2017-11-17 13:04:07 -0800 | [diff] [blame] | 99 | : SPLIT_SCREEN_CREATE_MODE_BOTTOM_OR_RIGHT, null, -1); |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 100 | } else { |
| 101 | // If there is already a docked window, we respond by resizing the docking pane. |
| 102 | DividerView dividerView = getComponent(Divider.class).getView(); |
| 103 | DividerSnapAlgorithm snapAlgorithm = dividerView.getSnapAlgorithm(); |
| 104 | int dividerPosition = dividerView.getCurrentPosition(); |
| 105 | DividerSnapAlgorithm.SnapTarget currentTarget = |
| 106 | snapAlgorithm.calculateNonDismissingSnapTarget(dividerPosition); |
Muyuan Li | 5ef619f | 2016-07-22 15:47:51 -0700 | [diff] [blame] | 107 | DividerSnapAlgorithm.SnapTarget target = (shortcutCode == SC_DOCK_LEFT) |
| 108 | ? snapAlgorithm.getPreviousTarget(currentTarget) |
| 109 | : snapAlgorithm.getNextTarget(currentTarget); |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 110 | dividerView.startDragging(true /* animate */, false /* touching */); |
Muyuan Li | 5ef619f | 2016-07-22 15:47:51 -0700 | [diff] [blame] | 111 | dividerView.stopDragging(target.position, 0f, false /* avoidDismissStart */, |
Jorim Jaggi | 29379ec | 2016-04-11 23:43:42 -0700 | [diff] [blame] | 112 | true /* logMetrics */); |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 113 | } |
| 114 | } catch (RemoteException e) { |
| 115 | Log.e(TAG, "handleDockKey() failed."); |
| 116 | } |
| 117 | } |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 118 | } |