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 19 import android.content.Context; 20 import android.content.res.Configuration; 21 import android.os.RemoteException; 22 import android.view.IWindowManager; 23 import android.view.KeyEvent; 24 import android.view.WindowManagerGlobal; 25 26 import com.android.internal.policy.DividerSnapAlgorithm; 27 import com.android.systemui.SystemUI; 28 import com.android.systemui.dagger.SysUISingleton; 29 import com.android.wm.shell.legacysplitscreen.DividerView; 30 import com.android.wm.shell.legacysplitscreen.LegacySplitScreen; 31 32 import java.util.Optional; 33 34 import javax.inject.Inject; 35 36 /** 37 * Dispatches shortcut to System UI components 38 */ 39 @SysUISingleton 40 public class ShortcutKeyDispatcher extends SystemUI 41 implements ShortcutKeyServiceProxy.Callbacks { 42 43 private static final String TAG = "ShortcutKeyDispatcher"; 44 private final Optional<LegacySplitScreen> mSplitScreenOptional; 45 46 private ShortcutKeyServiceProxy mShortcutKeyServiceProxy = new ShortcutKeyServiceProxy(this); 47 private IWindowManager mWindowManagerService = WindowManagerGlobal.getWindowManagerService(); 48 49 protected final long META_MASK = ((long) KeyEvent.META_META_ON) << Integer.SIZE; 50 protected final long ALT_MASK = ((long) KeyEvent.META_ALT_ON) << Integer.SIZE; 51 protected final long CTRL_MASK = ((long) KeyEvent.META_CTRL_ON) << Integer.SIZE; 52 protected final long SHIFT_MASK = ((long) KeyEvent.META_SHIFT_ON) << Integer.SIZE; 53 54 protected final long SC_DOCK_LEFT = META_MASK | KeyEvent.KEYCODE_LEFT_BRACKET; 55 protected final long SC_DOCK_RIGHT = META_MASK | KeyEvent.KEYCODE_RIGHT_BRACKET; 56 57 @Inject ShortcutKeyDispatcher(Context context, Optional<LegacySplitScreen> splitScreenOptional)58 public ShortcutKeyDispatcher(Context context, Optional<LegacySplitScreen> splitScreenOptional) { 59 super(context); 60 mSplitScreenOptional = splitScreenOptional; 61 } 62 63 /** 64 * Registers a shortcut key to window manager. 65 * 66 * @param shortcutCode packed representation of shortcut key code and meta information 67 */ registerShortcutKey(long shortcutCode)68 public void registerShortcutKey(long shortcutCode) { 69 try { 70 mWindowManagerService.registerShortcutKey(shortcutCode, mShortcutKeyServiceProxy); 71 } catch (RemoteException e) { 72 // Do nothing 73 } 74 } 75 76 @Override onShortcutKeyPressed(long shortcutCode)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 } 84 85 @Override start()86 public void start() { 87 registerShortcutKey(SC_DOCK_LEFT); 88 registerShortcutKey(SC_DOCK_RIGHT); 89 } 90 handleDockKey(long shortcutCode)91 private void handleDockKey(long shortcutCode) { 92 mSplitScreenOptional.ifPresent(splitScreen -> { 93 if (splitScreen.isDividerVisible()) { 94 // If there is already a docked window, we respond by resizing the docking pane. 95 DividerView dividerView = splitScreen.getDividerView(); 96 DividerSnapAlgorithm snapAlgorithm = dividerView.getSnapAlgorithm(); 97 int dividerPosition = dividerView.getCurrentPosition(); 98 DividerSnapAlgorithm.SnapTarget currentTarget = 99 snapAlgorithm.calculateNonDismissingSnapTarget(dividerPosition); 100 DividerSnapAlgorithm.SnapTarget target = (shortcutCode == SC_DOCK_LEFT) 101 ? snapAlgorithm.getPreviousTarget(currentTarget) 102 : snapAlgorithm.getNextTarget(currentTarget); 103 dividerView.startDragging(true /* animate */, false /* touching */); 104 dividerView.stopDragging(target.position, 0f, false /* avoidDismissStart */, 105 true /* logMetrics */); 106 return; 107 } else { 108 splitScreen.splitPrimaryTask(); 109 } 110 }); 111 } 112 } 113