1 /*
2  * Copyright (C) 2017 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.shared.system;
18 
19 import static android.view.Display.DEFAULT_DISPLAY;
20 import static android.view.WindowManagerPolicyConstants.NAV_BAR_BOTTOM;
21 import static android.view.WindowManagerPolicyConstants.NAV_BAR_INVALID;
22 import static android.view.WindowManagerPolicyConstants.NAV_BAR_LEFT;
23 import static android.view.WindowManagerPolicyConstants.NAV_BAR_RIGHT;
24 
25 import android.app.WindowConfiguration;
26 import android.graphics.Rect;
27 import android.os.Handler;
28 import android.os.RemoteException;
29 import android.util.Log;
30 import android.view.IPinnedStackListener;
31 import android.view.WindowManager;
32 import android.view.WindowManagerGlobal;
33 
34 import com.android.systemui.shared.recents.view.AppTransitionAnimationSpecsFuture;
35 import com.android.systemui.shared.recents.view.RecentsTransition;
36 
37 public class WindowManagerWrapper {
38 
39     private static final String TAG = "WindowManagerWrapper";
40 
41     public static final int TRANSIT_UNSET = WindowManager.TRANSIT_UNSET;
42     public static final int TRANSIT_NONE = WindowManager.TRANSIT_NONE;
43     public static final int TRANSIT_ACTIVITY_OPEN = WindowManager.TRANSIT_ACTIVITY_OPEN;
44     public static final int TRANSIT_ACTIVITY_CLOSE = WindowManager.TRANSIT_ACTIVITY_CLOSE;
45     public static final int TRANSIT_TASK_OPEN = WindowManager.TRANSIT_TASK_OPEN;
46     public static final int TRANSIT_TASK_CLOSE = WindowManager.TRANSIT_TASK_CLOSE;
47     public static final int TRANSIT_TASK_TO_FRONT = WindowManager.TRANSIT_TASK_TO_FRONT;
48     public static final int TRANSIT_TASK_TO_BACK = WindowManager.TRANSIT_TASK_TO_BACK;
49     public static final int TRANSIT_WALLPAPER_CLOSE = WindowManager.TRANSIT_WALLPAPER_CLOSE;
50     public static final int TRANSIT_WALLPAPER_OPEN = WindowManager.TRANSIT_WALLPAPER_OPEN;
51     public static final int TRANSIT_WALLPAPER_INTRA_OPEN =
52             WindowManager.TRANSIT_WALLPAPER_INTRA_OPEN;
53     public static final int TRANSIT_WALLPAPER_INTRA_CLOSE =
54             WindowManager.TRANSIT_WALLPAPER_INTRA_CLOSE;
55     public static final int TRANSIT_TASK_OPEN_BEHIND = WindowManager.TRANSIT_TASK_OPEN_BEHIND;
56     public static final int TRANSIT_TASK_IN_PLACE = WindowManager.TRANSIT_TASK_IN_PLACE;
57     public static final int TRANSIT_ACTIVITY_RELAUNCH = WindowManager.TRANSIT_ACTIVITY_RELAUNCH;
58     public static final int TRANSIT_DOCK_TASK_FROM_RECENTS =
59             WindowManager.TRANSIT_DOCK_TASK_FROM_RECENTS;
60     public static final int TRANSIT_KEYGUARD_GOING_AWAY = WindowManager.TRANSIT_KEYGUARD_GOING_AWAY;
61     public static final int TRANSIT_KEYGUARD_GOING_AWAY_ON_WALLPAPER =
62             WindowManager.TRANSIT_KEYGUARD_GOING_AWAY_ON_WALLPAPER;
63     public static final int TRANSIT_KEYGUARD_OCCLUDE = WindowManager.TRANSIT_KEYGUARD_OCCLUDE;
64     public static final int TRANSIT_KEYGUARD_UNOCCLUDE = WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
65 
66     public static final int NAV_BAR_POS_INVALID = NAV_BAR_INVALID;
67     public static final int NAV_BAR_POS_LEFT = NAV_BAR_LEFT;
68     public static final int NAV_BAR_POS_RIGHT = NAV_BAR_RIGHT;
69     public static final int NAV_BAR_POS_BOTTOM = NAV_BAR_BOTTOM;
70 
71     public static final int ACTIVITY_TYPE_STANDARD = WindowConfiguration.ACTIVITY_TYPE_STANDARD;
72 
73     public static final int WINDOWING_MODE_UNDEFINED = WindowConfiguration.WINDOWING_MODE_UNDEFINED;
74     public static final int WINDOWING_MODE_FULLSCREEN =
75             WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
76     public static final int WINDOWING_MODE_PINNED = WindowConfiguration.WINDOWING_MODE_PINNED;
77     public static final int WINDOWING_MODE_SPLIT_SCREEN_PRIMARY =
78             WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
79     public static final int WINDOWING_MODE_SPLIT_SCREEN_SECONDARY =
80             WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
81     public static final int WINDOWING_MODE_FREEFORM = WindowConfiguration.WINDOWING_MODE_FREEFORM;
82 
83     private static final WindowManagerWrapper sInstance = new WindowManagerWrapper();
84 
85     /**
86      * Forwarder to which we can add multiple pinned stack listeners. Each listener will receive
87      * updates from the window manager service.
88      */
89     private PinnedStackListenerForwarder mPinnedStackListenerForwarder =
90             new PinnedStackListenerForwarder();
91 
getInstance()92     public static WindowManagerWrapper getInstance() {
93         return sInstance;
94     }
95 
96     /**
97      * @return the stable insets for the primary display.
98      */
getStableInsets(Rect outStableInsets)99     public void getStableInsets(Rect outStableInsets) {
100         try {
101             WindowManagerGlobal.getWindowManagerService().getStableInsets(DEFAULT_DISPLAY,
102                     outStableInsets);
103         } catch (RemoteException e) {
104             Log.e(TAG, "Failed to get stable insets", e);
105         }
106     }
107 
108     /**
109      * Overrides a pending app transition.
110      */
overridePendingAppTransitionMultiThumbFuture( AppTransitionAnimationSpecsFuture animationSpecFuture, Runnable animStartedCallback, Handler animStartedCallbackHandler, boolean scaleUp, int displayId)111     public void overridePendingAppTransitionMultiThumbFuture(
112             AppTransitionAnimationSpecsFuture animationSpecFuture, Runnable animStartedCallback,
113             Handler animStartedCallbackHandler, boolean scaleUp, int displayId) {
114         try {
115             WindowManagerGlobal.getWindowManagerService()
116                     .overridePendingAppTransitionMultiThumbFuture(animationSpecFuture.getFuture(),
117                             RecentsTransition.wrapStartedListener(animStartedCallbackHandler,
118                                     animStartedCallback), scaleUp, displayId);
119         } catch (RemoteException e) {
120             Log.w(TAG, "Failed to override pending app transition (multi-thumbnail future): ", e);
121         }
122     }
123 
overridePendingAppTransitionRemote( RemoteAnimationAdapterCompat remoteAnimationAdapter, int displayId)124     public void overridePendingAppTransitionRemote(
125             RemoteAnimationAdapterCompat remoteAnimationAdapter, int displayId) {
126         try {
127             WindowManagerGlobal.getWindowManagerService().overridePendingAppTransitionRemote(
128                     remoteAnimationAdapter.getWrapped(), displayId);
129         } catch (RemoteException e) {
130             Log.w(TAG, "Failed to override pending app transition (remote): ", e);
131         }
132     }
133 
134     /**
135      * Enable or disable haptic feedback on the navigation bar buttons.
136      */
setNavBarVirtualKeyHapticFeedbackEnabled(boolean enabled)137     public void setNavBarVirtualKeyHapticFeedbackEnabled(boolean enabled) {
138         try {
139             WindowManagerGlobal.getWindowManagerService()
140                     .setNavBarVirtualKeyHapticFeedbackEnabled(enabled);
141         } catch (RemoteException e) {
142             Log.w(TAG, "Failed to enable or disable navigation bar button haptics: ", e);
143         }
144     }
145 
setShelfHeight(boolean visible, int shelfHeight)146     public void setShelfHeight(boolean visible, int shelfHeight) {
147         try {
148             WindowManagerGlobal.getWindowManagerService().setShelfHeight(visible, shelfHeight);
149         } catch (RemoteException e) {
150             Log.w(TAG, "Failed to set shelf height");
151         }
152     }
153 
setRecentsVisibility(boolean visible)154     public void setRecentsVisibility(boolean visible) {
155         try {
156             WindowManagerGlobal.getWindowManagerService().setRecentsVisibility(visible);
157         } catch (RemoteException e) {
158             Log.w(TAG, "Failed to set recents visibility");
159         }
160     }
161 
setPipVisibility(final boolean visible)162     public void setPipVisibility(final boolean visible) {
163         try {
164             WindowManagerGlobal.getWindowManagerService().setPipVisibility(visible);
165         } catch (RemoteException e) {
166             Log.e(TAG, "Unable to reach window manager", e);
167         }
168     }
169 
170     /**
171      * @param displayId the id of display to check if there is a software navigation bar.
172      *
173      * @return whether there is a soft nav bar on specific display.
174      */
hasSoftNavigationBar(int displayId)175     public boolean hasSoftNavigationBar(int displayId) {
176         try {
177             return WindowManagerGlobal.getWindowManagerService().hasNavigationBar(displayId);
178         } catch (RemoteException e) {
179             return false;
180         }
181     }
182 
183     /**
184      * @return The side of the screen where navigation bar is positioned.
185      * @see #NAV_BAR_POS_RIGHT
186      * @see #NAV_BAR_POS_LEFT
187      * @see #NAV_BAR_POS_BOTTOM
188      * @see #NAV_BAR_POS_INVALID
189      */
getNavBarPosition(int displayId)190     public int getNavBarPosition(int displayId) {
191         try {
192             return WindowManagerGlobal.getWindowManagerService().getNavBarPosition(displayId);
193         } catch (RemoteException e) {
194             Log.w(TAG, "Failed to get nav bar position");
195         }
196         return NAV_BAR_POS_INVALID;
197     }
198 
199     /**
200      * Registers a docked stack listener with the system.
201      */
registerDockedStackListener(DockedStackListenerCompat listener)202     public void registerDockedStackListener(DockedStackListenerCompat listener) {
203         try {
204             WindowManagerGlobal.getWindowManagerService().registerDockedStackListener(
205                     listener.mListener);
206         } catch (RemoteException e) {
207             Log.w(TAG, "Failed to register docked stack listener");
208         }
209     }
210 
211     /**
212      * Adds a pinned stack listener, which will receive updates from the window manager service
213      * along with any other pinned stack listeners that were added via this method.
214      */
addPinnedStackListener(IPinnedStackListener listener)215     public void addPinnedStackListener(IPinnedStackListener listener) throws RemoteException {
216         mPinnedStackListenerForwarder.addListener(listener);
217         WindowManagerGlobal.getWindowManagerService().registerPinnedStackListener(
218                 DEFAULT_DISPLAY, mPinnedStackListenerForwarder);
219     }
220 
221     /**
222      * Removes a pinned stack listener.
223      */
removePinnedStackListener(IPinnedStackListener listener)224     public void removePinnedStackListener(IPinnedStackListener listener) {
225         mPinnedStackListenerForwarder.removeListener(listener);
226     }
227 }
228