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.systemui.CoreStartable;
27 import com.android.systemui.dagger.SysUISingleton;
28 
29 import javax.inject.Inject;
30 
31 /**
32  * Dispatches shortcut to System UI components
33  */
34 @SysUISingleton
35 public class ShortcutKeyDispatcher implements CoreStartable, ShortcutKeyServiceProxy.Callbacks {
36 
37     private static final String TAG = "ShortcutKeyDispatcher";
38     private final Context mContext;
39 
40     private ShortcutKeyServiceProxy mShortcutKeyServiceProxy = new ShortcutKeyServiceProxy(this);
41     private IWindowManager mWindowManagerService = WindowManagerGlobal.getWindowManagerService();
42 
43     protected final long META_MASK = ((long) KeyEvent.META_META_ON) << Integer.SIZE;
44     protected final long ALT_MASK = ((long) KeyEvent.META_ALT_ON) << Integer.SIZE;
45     protected final long CTRL_MASK = ((long) KeyEvent.META_CTRL_ON) << Integer.SIZE;
46     protected final long SHIFT_MASK = ((long) KeyEvent.META_SHIFT_ON) << Integer.SIZE;
47 
48     protected final long SC_DOCK_LEFT = META_MASK | KeyEvent.KEYCODE_LEFT_BRACKET;
49     protected final long SC_DOCK_RIGHT = META_MASK | KeyEvent.KEYCODE_RIGHT_BRACKET;
50 
51     @Inject
ShortcutKeyDispatcher(Context context)52     public ShortcutKeyDispatcher(Context context) {
53         mContext = context;
54     }
55 
56     /**
57      * Registers a shortcut key to window manager.
58      *
59      * @param shortcutCode packed representation of shortcut key code and meta information
60      */
registerShortcutKey(long shortcutCode)61     public void registerShortcutKey(long shortcutCode) {
62         try {
63             mWindowManagerService.registerShortcutKey(shortcutCode, mShortcutKeyServiceProxy);
64         } catch (RemoteException e) {
65             // Do nothing
66         }
67     }
68 
69     @Override
onShortcutKeyPressed(long shortcutCode)70     public void onShortcutKeyPressed(long shortcutCode) {
71         int orientation = mContext.getResources().getConfiguration().orientation;
72         if ((shortcutCode == SC_DOCK_LEFT || shortcutCode == SC_DOCK_RIGHT)
73                 && orientation == Configuration.ORIENTATION_LANDSCAPE) {
74             handleDockKey(shortcutCode);
75         }
76     }
77 
78     @Override
start()79     public void start() {
80         registerShortcutKey(SC_DOCK_LEFT);
81         registerShortcutKey(SC_DOCK_RIGHT);
82     }
83 
handleDockKey(long shortcutCode)84     private void handleDockKey(long shortcutCode) {
85         // TODO(b/220262470) : implement it with new split screen.
86     }
87 }
88