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.accessibilityservice.AccessibilityServiceInfo; 20 import android.app.ActivityManager; 21 import android.app.ActivityManagerNative; 22 import android.app.IActivityManager; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.pm.ServiceInfo; 26 import android.content.res.Configuration; 27 import android.os.RemoteException; 28 import android.util.DisplayMetrics; 29 import android.util.Log; 30 import android.view.IWindowManager; 31 import android.view.KeyEvent; 32 import android.view.WindowManager; 33 import android.view.WindowManagerGlobal; 34 import android.view.accessibility.AccessibilityManager; 35 import com.android.internal.logging.MetricsLogger; 36 import com.android.internal.logging.MetricsProto.MetricsEvent; 37 import com.android.internal.policy.DividerSnapAlgorithm; 38 import com.android.settingslib.accessibility.AccessibilityUtils; 39 import com.android.systemui.R; 40 import com.android.systemui.SystemUI; 41 import com.android.systemui.recents.Recents; 42 import com.android.systemui.stackdivider.Divider; 43 import com.android.systemui.stackdivider.DividerView; 44 import com.android.systemui.statusbar.phone.NavigationBarGestureHelper; 45 46 import java.util.List; 47 import java.util.Set; 48 49 /** 50 * Dispatches shortcut to System UI components 51 */ 52 public class ShortcutKeyDispatcher extends SystemUI 53 implements ShortcutKeyServiceProxy.Callbacks { 54 55 private static final String TAG = "ShortcutKeyDispatcher"; 56 57 private ShortcutKeyServiceProxy mShortcutKeyServiceProxy = new ShortcutKeyServiceProxy(this); 58 private IWindowManager mWindowManagerService = WindowManagerGlobal.getWindowManagerService(); 59 private IActivityManager mActivityManager = ActivityManagerNative.getDefault(); 60 61 protected final long META_MASK = ((long) KeyEvent.META_META_ON) << Integer.SIZE; 62 protected final long ALT_MASK = ((long) KeyEvent.META_ALT_ON) << Integer.SIZE; 63 protected final long CTRL_MASK = ((long) KeyEvent.META_CTRL_ON) << Integer.SIZE; 64 protected final long SHIFT_MASK = ((long) KeyEvent.META_SHIFT_ON) << Integer.SIZE; 65 66 protected final long SC_DOCK_LEFT = META_MASK | KeyEvent.KEYCODE_LEFT_BRACKET; 67 protected final long SC_DOCK_RIGHT = META_MASK | KeyEvent.KEYCODE_RIGHT_BRACKET; 68 69 /** 70 * Registers a shortcut key to window manager. 71 * @param shortcutCode packed representation of shortcut key code and meta information 72 */ registerShortcutKey(long shortcutCode)73 public void registerShortcutKey(long shortcutCode) { 74 try { 75 mWindowManagerService.registerShortcutKey(shortcutCode, mShortcutKeyServiceProxy); 76 } catch (RemoteException e) { 77 // Do nothing 78 } 79 } 80 81 @Override onShortcutKeyPressed(long shortcutCode)82 public void onShortcutKeyPressed(long shortcutCode) { 83 int orientation = mContext.getResources().getConfiguration().orientation; 84 if ((shortcutCode == SC_DOCK_LEFT || shortcutCode == SC_DOCK_RIGHT) 85 && orientation == Configuration.ORIENTATION_LANDSCAPE) { 86 handleDockKey(shortcutCode); 87 } 88 } 89 90 @Override start()91 public void start() { 92 registerShortcutKey(SC_DOCK_LEFT); 93 registerShortcutKey(SC_DOCK_RIGHT); 94 } 95 handleDockKey(long shortcutCode)96 private void handleDockKey(long shortcutCode) { 97 try { 98 int dockSide = mWindowManagerService.getDockedStackSide(); 99 if (dockSide == WindowManager.DOCKED_INVALID) { 100 // If there is no window docked, we dock the top-most window. 101 Recents recents = getComponent(Recents.class); 102 int dockMode = (shortcutCode == SC_DOCK_LEFT) 103 ? ActivityManager.DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT 104 : ActivityManager.DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT; 105 recents.dockTopTask(NavigationBarGestureHelper.DRAG_MODE_NONE, dockMode, null, 106 MetricsEvent.WINDOW_DOCK_SHORTCUTS); 107 } else { 108 // If there is already a docked window, we respond by resizing the docking pane. 109 DividerView dividerView = getComponent(Divider.class).getView(); 110 DividerSnapAlgorithm snapAlgorithm = dividerView.getSnapAlgorithm(); 111 int dividerPosition = dividerView.getCurrentPosition(); 112 DividerSnapAlgorithm.SnapTarget currentTarget = 113 snapAlgorithm.calculateNonDismissingSnapTarget(dividerPosition); 114 int increment = (shortcutCode == SC_DOCK_LEFT) ? -1 : 1; 115 DividerSnapAlgorithm.SnapTarget target = snapAlgorithm.cycleNonDismissTarget( 116 currentTarget, increment); 117 dividerView.startDragging(true /* animate */, false /* touching */); 118 dividerView.stopDragging(target.position, 0f, true /* avoidDismissStart */, 119 true /* logMetrics */); 120 } 121 } catch (RemoteException e) { 122 Log.e(TAG, "handleDockKey() failed."); 123 } 124 } 125 } 126