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 | |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 22 | import android.content.res.Configuration; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 23 | import android.os.RemoteException; |
| 24 | import android.util.Log; |
| 25 | import android.view.IWindowManager; |
| 26 | import android.view.KeyEvent; |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 27 | import android.view.WindowManager; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 28 | import android.view.WindowManagerGlobal; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 29 | |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 30 | import com.android.internal.policy.DividerSnapAlgorithm; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 31 | import com.android.systemui.SystemUI; |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 32 | import com.android.systemui.recents.Recents; |
| 33 | import com.android.systemui.stackdivider.Divider; |
| 34 | import com.android.systemui.stackdivider.DividerView; |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Dispatches shortcut to System UI components |
| 38 | */ |
| 39 | public 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 Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 45 | private IWindowManager mWindowManagerService = WindowManagerGlobal.getWindowManagerService(); |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 46 | |
| 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 Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 52 | 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 Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 55 | /** |
| 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 Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 61 | mWindowManagerService.registerShortcutKey(shortcutCode, mShortcutKeyServiceProxy); |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 62 | } catch (RemoteException e) { |
| 63 | // Do nothing |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | @Override |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 68 | 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 Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 75 | |
| 76 | @Override |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 77 | 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 Chung | 42ce900 | 2017-11-17 13:04:07 -0800 | [diff] [blame] | 86 | // Split the screen |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 87 | Recents recents = getComponent(Recents.class); |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 88 | recents.splitPrimaryTask((shortcutCode == SC_DOCK_LEFT) |
Matthew Ng | bf15587 | 2017-10-27 15:24:39 -0700 | [diff] [blame] | 89 | ? SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT |
Winson Chung | 42ce900 | 2017-11-17 13:04:07 -0800 | [diff] [blame] | 90 | : SPLIT_SCREEN_CREATE_MODE_BOTTOM_OR_RIGHT, null, -1); |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 91 | } 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 Li | 5ef619f | 2016-07-22 15:47:51 -0700 | [diff] [blame] | 98 | DividerSnapAlgorithm.SnapTarget target = (shortcutCode == SC_DOCK_LEFT) |
| 99 | ? snapAlgorithm.getPreviousTarget(currentTarget) |
| 100 | : snapAlgorithm.getNextTarget(currentTarget); |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 101 | dividerView.startDragging(true /* animate */, false /* touching */); |
Muyuan Li | 5ef619f | 2016-07-22 15:47:51 -0700 | [diff] [blame] | 102 | dividerView.stopDragging(target.position, 0f, false /* avoidDismissStart */, |
Jorim Jaggi | 29379ec | 2016-04-11 23:43:42 -0700 | [diff] [blame] | 103 | true /* logMetrics */); |
Muyuan Li | a212999 | 2016-03-03 18:30:39 -0800 | [diff] [blame] | 104 | } |
| 105 | } catch (RemoteException e) { |
| 106 | Log.e(TAG, "handleDockKey() failed."); |
| 107 | } |
| 108 | } |
Muyuan Li | 94ce94e | 2016-02-24 16:20:54 -0800 | [diff] [blame] | 109 | } |