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.InsetsController;
31 import android.view.InsetsState;
32 import android.view.SurfaceControl;
33 import android.view.WindowManager;
34 import android.view.WindowManagerGlobal;
35 import android.view.animation.Interpolator;
36 
37 import com.android.systemui.shared.recents.view.AppTransitionAnimationSpecsFuture;
38 import com.android.systemui.shared.recents.view.RecentsTransition;
39 
40 public class WindowManagerWrapper {
41 
42     private static final String TAG = "WindowManagerWrapper";
43 
44     public static final int TRANSIT_UNSET = WindowManager.TRANSIT_OLD_UNSET;
45     public static final int TRANSIT_NONE = WindowManager.TRANSIT_OLD_NONE;
46     public static final int TRANSIT_ACTIVITY_OPEN = WindowManager.TRANSIT_OLD_ACTIVITY_OPEN;
47     public static final int TRANSIT_ACTIVITY_CLOSE = WindowManager.TRANSIT_OLD_ACTIVITY_CLOSE;
48     public static final int TRANSIT_TASK_OPEN = WindowManager.TRANSIT_OLD_TASK_OPEN;
49     public static final int TRANSIT_TASK_CLOSE = WindowManager.TRANSIT_OLD_TASK_CLOSE;
50     public static final int TRANSIT_TASK_TO_FRONT = WindowManager.TRANSIT_OLD_TASK_TO_FRONT;
51     public static final int TRANSIT_TASK_TO_BACK = WindowManager.TRANSIT_OLD_TASK_TO_BACK;
52     public static final int TRANSIT_WALLPAPER_CLOSE = WindowManager.TRANSIT_OLD_WALLPAPER_CLOSE;
53     public static final int TRANSIT_WALLPAPER_OPEN = WindowManager.TRANSIT_OLD_WALLPAPER_OPEN;
54     public static final int TRANSIT_WALLPAPER_INTRA_OPEN =
55             WindowManager.TRANSIT_OLD_WALLPAPER_INTRA_OPEN;
56     public static final int TRANSIT_WALLPAPER_INTRA_CLOSE =
57             WindowManager.TRANSIT_OLD_WALLPAPER_INTRA_CLOSE;
58     public static final int TRANSIT_TASK_OPEN_BEHIND = WindowManager.TRANSIT_OLD_TASK_OPEN_BEHIND;
59     public static final int TRANSIT_ACTIVITY_RELAUNCH = WindowManager.TRANSIT_OLD_ACTIVITY_RELAUNCH;
60     public static final int TRANSIT_KEYGUARD_GOING_AWAY =
61             WindowManager.TRANSIT_OLD_KEYGUARD_GOING_AWAY;
62     public static final int TRANSIT_KEYGUARD_GOING_AWAY_ON_WALLPAPER =
63             WindowManager.TRANSIT_OLD_KEYGUARD_GOING_AWAY_ON_WALLPAPER;
64     public static final int TRANSIT_KEYGUARD_OCCLUDE = WindowManager.TRANSIT_OLD_KEYGUARD_OCCLUDE;
65     public static final int TRANSIT_KEYGUARD_UNOCCLUDE =
66             WindowManager.TRANSIT_OLD_KEYGUARD_UNOCCLUDE;
67 
68     public static final int NAV_BAR_POS_INVALID = NAV_BAR_INVALID;
69     public static final int NAV_BAR_POS_LEFT = NAV_BAR_LEFT;
70     public static final int NAV_BAR_POS_RIGHT = NAV_BAR_RIGHT;
71     public static final int NAV_BAR_POS_BOTTOM = NAV_BAR_BOTTOM;
72 
73     public static final int ACTIVITY_TYPE_STANDARD = WindowConfiguration.ACTIVITY_TYPE_STANDARD;
74 
75     public static final int WINDOWING_MODE_UNDEFINED = WindowConfiguration.WINDOWING_MODE_UNDEFINED;
76     public static final int WINDOWING_MODE_FULLSCREEN =
77             WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
78     public static final int WINDOWING_MODE_MULTI_WINDOW =
79             WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW;
80 
81     public static final int WINDOWING_MODE_SPLIT_SCREEN_PRIMARY =
82             WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
83     public static final int WINDOWING_MODE_SPLIT_SCREEN_SECONDARY =
84             WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
85     public static final int WINDOWING_MODE_FREEFORM = WindowConfiguration.WINDOWING_MODE_FREEFORM;
86 
87     public static final int ITYPE_EXTRA_NAVIGATION_BAR = InsetsState.ITYPE_EXTRA_NAVIGATION_BAR;
88     public static final int ITYPE_LEFT_TAPPABLE_ELEMENT = InsetsState.ITYPE_LEFT_TAPPABLE_ELEMENT;
89     public static final int ITYPE_TOP_TAPPABLE_ELEMENT = InsetsState.ITYPE_TOP_TAPPABLE_ELEMENT;
90     public static final int ITYPE_RIGHT_TAPPABLE_ELEMENT = InsetsState.ITYPE_RIGHT_TAPPABLE_ELEMENT;
91     public static final int ITYPE_BOTTOM_TAPPABLE_ELEMENT =
92             InsetsState.ITYPE_BOTTOM_TAPPABLE_ELEMENT;
93 
94     public static final int ANIMATION_DURATION_RESIZE = InsetsController.ANIMATION_DURATION_RESIZE;
95     public static final Interpolator RESIZE_INTERPOLATOR = InsetsController.RESIZE_INTERPOLATOR;
96 
97     private static final WindowManagerWrapper sInstance = new WindowManagerWrapper();
98 
getInstance()99     public static WindowManagerWrapper getInstance() {
100         return sInstance;
101     }
102 
103 
104     /**
105      * Sets {@param providesInsetsTypes} as the inset types provided by {@param params}.
106      * @param params The window layout params.
107      * @param providesInsetsTypes The inset types we would like this layout params to provide.
108      */
setProvidesInsetsTypes(WindowManager.LayoutParams params, int[] providesInsetsTypes)109     public void setProvidesInsetsTypes(WindowManager.LayoutParams params,
110             int[] providesInsetsTypes) {
111         params.providesInsetsTypes = providesInsetsTypes;
112     }
113 
114     /**
115      *  Sets if app requested fixed orientation should be ignored for given displayId.
116      */
setIgnoreOrientationRequest(int displayId, boolean ignoreOrientationRequest)117     public void setIgnoreOrientationRequest(int displayId, boolean ignoreOrientationRequest) {
118         try {
119             WindowManagerGlobal.getWindowManagerService().setIgnoreOrientationRequest(
120                     displayId, ignoreOrientationRequest);
121         } catch (RemoteException e) {
122             Log.e(TAG, "Failed to setIgnoreOrientationRequest()", e);
123         }
124     }
125 
126     /**
127      * @return the stable insets for the primary display.
128      */
getStableInsets(Rect outStableInsets)129     public void getStableInsets(Rect outStableInsets) {
130         try {
131             WindowManagerGlobal.getWindowManagerService().getStableInsets(DEFAULT_DISPLAY,
132                     outStableInsets);
133         } catch (RemoteException e) {
134             Log.e(TAG, "Failed to get stable insets", e);
135         }
136     }
137 
138     /**
139      * Overrides a pending app transition.
140      */
overridePendingAppTransitionMultiThumbFuture( AppTransitionAnimationSpecsFuture animationSpecFuture, Runnable animStartedCallback, Handler animStartedCallbackHandler, boolean scaleUp, int displayId)141     public void overridePendingAppTransitionMultiThumbFuture(
142             AppTransitionAnimationSpecsFuture animationSpecFuture, Runnable animStartedCallback,
143             Handler animStartedCallbackHandler, boolean scaleUp, int displayId) {
144         try {
145             WindowManagerGlobal.getWindowManagerService()
146                     .overridePendingAppTransitionMultiThumbFuture(animationSpecFuture.getFuture(),
147                             RecentsTransition.wrapStartedListener(animStartedCallbackHandler,
148                                     animStartedCallback), scaleUp, displayId);
149         } catch (RemoteException e) {
150             Log.w(TAG, "Failed to override pending app transition (multi-thumbnail future): ", e);
151         }
152     }
153 
overridePendingAppTransitionRemote( RemoteAnimationAdapterCompat remoteAnimationAdapter, int displayId)154     public void overridePendingAppTransitionRemote(
155             RemoteAnimationAdapterCompat remoteAnimationAdapter, int displayId) {
156         try {
157             WindowManagerGlobal.getWindowManagerService().overridePendingAppTransitionRemote(
158                     remoteAnimationAdapter.getWrapped(), displayId);
159         } catch (RemoteException e) {
160             Log.w(TAG, "Failed to override pending app transition (remote): ", e);
161         }
162     }
163 
164     /**
165      * Enable or disable haptic feedback on the navigation bar buttons.
166      */
setNavBarVirtualKeyHapticFeedbackEnabled(boolean enabled)167     public void setNavBarVirtualKeyHapticFeedbackEnabled(boolean enabled) {
168         try {
169             WindowManagerGlobal.getWindowManagerService()
170                     .setNavBarVirtualKeyHapticFeedbackEnabled(enabled);
171         } catch (RemoteException e) {
172             Log.w(TAG, "Failed to enable or disable navigation bar button haptics: ", e);
173         }
174     }
175 
setRecentsVisibility(boolean visible)176     public void setRecentsVisibility(boolean visible) {
177         try {
178             WindowManagerGlobal.getWindowManagerService().setRecentsVisibility(visible);
179         } catch (RemoteException e) {
180             Log.w(TAG, "Failed to set recents visibility");
181         }
182     }
183 
184     @Deprecated
setPipVisibility(final boolean visible)185     public void setPipVisibility(final boolean visible) {
186         // To be removed
187     }
188 
189     /**
190      * @param displayId the id of display to check if there is a software navigation bar.
191      *
192      * @return whether there is a soft nav bar on specific display.
193      */
hasSoftNavigationBar(int displayId)194     public boolean hasSoftNavigationBar(int displayId) {
195         try {
196             return WindowManagerGlobal.getWindowManagerService().hasNavigationBar(displayId);
197         } catch (RemoteException e) {
198             return false;
199         }
200     }
201 
202     /**
203      * @return The side of the screen where navigation bar is positioned.
204      * @see #NAV_BAR_POS_RIGHT
205      * @see #NAV_BAR_POS_LEFT
206      * @see #NAV_BAR_POS_BOTTOM
207      * @see #NAV_BAR_POS_INVALID
208      */
getNavBarPosition(int displayId)209     public int getNavBarPosition(int displayId) {
210         try {
211             return WindowManagerGlobal.getWindowManagerService().getNavBarPosition(displayId);
212         } catch (RemoteException e) {
213             Log.w(TAG, "Failed to get nav bar position");
214         }
215         return NAV_BAR_POS_INVALID;
216     }
217 
218     /**
219      * Mirrors a specified display. The SurfaceControl returned is the root of the mirrored
220      * hierarchy.
221      *
222      * @param displayId The id of the display to mirror
223      * @return The SurfaceControl for the root of the mirrored hierarchy.
224      */
mirrorDisplay(final int displayId)225     public SurfaceControl mirrorDisplay(final int displayId) {
226         try {
227             SurfaceControl outSurfaceControl = new SurfaceControl();
228             WindowManagerGlobal.getWindowManagerService().mirrorDisplay(displayId,
229                     outSurfaceControl);
230             return outSurfaceControl;
231         } catch (RemoteException e) {
232             Log.e(TAG, "Unable to reach window manager", e);
233         }
234         return null;
235     }
236 }
237