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.server.wm;
18 
19 import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
20 import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
21 
22 import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_APP_TRANSITIONS;
23 import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_WALLPAPER;
24 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
25 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
26 
27 import android.annotation.Nullable;
28 import android.os.Bundle;
29 import android.os.IBinder;
30 import android.os.RemoteException;
31 import android.view.DisplayInfo;
32 import android.view.ViewGroup;
33 import android.view.WindowManager;
34 import android.view.animation.Animation;
35 
36 import com.android.internal.protolog.common.ProtoLog;
37 
38 import java.util.function.Consumer;
39 
40 /**
41  * A token that represents a set of wallpaper windows.
42  */
43 class WallpaperWindowToken extends WindowToken {
44 
45     private static final String TAG = TAG_WITH_CLASS_NAME ? "WallpaperWindowToken" : TAG_WM;
46 
47     private boolean mVisibleRequested = false;
48 
WallpaperWindowToken(WindowManagerService service, IBinder token, boolean explicit, DisplayContent dc, boolean ownerCanManageAppTokens)49     WallpaperWindowToken(WindowManagerService service, IBinder token, boolean explicit,
50             DisplayContent dc, boolean ownerCanManageAppTokens) {
51         this(service, token, explicit, dc, ownerCanManageAppTokens, null /* options */);
52     }
53 
WallpaperWindowToken(WindowManagerService service, IBinder token, boolean explicit, DisplayContent dc, boolean ownerCanManageAppTokens, @Nullable Bundle options)54     WallpaperWindowToken(WindowManagerService service, IBinder token, boolean explicit,
55             DisplayContent dc, boolean ownerCanManageAppTokens, @Nullable Bundle options) {
56         super(service, token, TYPE_WALLPAPER, explicit, dc, ownerCanManageAppTokens,
57                 false /* roundedCornerOverlay */, false /* fromClientToken */, options);
58         dc.mWallpaperController.addWallpaperToken(this);
59         setWindowingMode(WINDOWING_MODE_FULLSCREEN);
60     }
61 
62     @Override
asWallpaperToken()63     WallpaperWindowToken asWallpaperToken() {
64         return this;
65     }
66 
67     @Override
setExiting(boolean animateExit)68     void setExiting(boolean animateExit) {
69         super.setExiting(animateExit);
70         mDisplayContent.mWallpaperController.removeWallpaperToken(this);
71     }
72 
sendWindowWallpaperCommand( String action, int x, int y, int z, Bundle extras, boolean sync)73     void sendWindowWallpaperCommand(
74             String action, int x, int y, int z, Bundle extras, boolean sync) {
75         for (int wallpaperNdx = mChildren.size() - 1; wallpaperNdx >= 0; wallpaperNdx--) {
76             final WindowState wallpaper = mChildren.get(wallpaperNdx);
77             try {
78                 wallpaper.mClient.dispatchWallpaperCommand(action, x, y, z, extras, sync);
79                 // We only want to be synchronous with one wallpaper.
80                 sync = false;
81             } catch (RemoteException e) {
82             }
83         }
84     }
85 
updateWallpaperOffset(boolean sync)86     void updateWallpaperOffset(boolean sync) {
87         final WallpaperController wallpaperController = mDisplayContent.mWallpaperController;
88         for (int wallpaperNdx = mChildren.size() - 1; wallpaperNdx >= 0; wallpaperNdx--) {
89             final WindowState wallpaper = mChildren.get(wallpaperNdx);
90             if (wallpaperController.updateWallpaperOffset(wallpaper, sync)) {
91                 // We only want to be synchronous with one wallpaper.
92                 sync = false;
93             }
94         }
95     }
96 
97     /**
98      * Starts {@param anim} on all children.
99      */
startAnimation(Animation anim)100     void startAnimation(Animation anim) {
101         for (int ndx = mChildren.size() - 1; ndx >= 0; ndx--) {
102             final WindowState windowState = mChildren.get(ndx);
103             windowState.startAnimation(anim);
104         }
105     }
106 
updateWallpaperWindows(boolean visible)107     void updateWallpaperWindows(boolean visible) {
108         if (isVisible() != visible) {
109             ProtoLog.d(WM_DEBUG_WALLPAPER, "Wallpaper token %s visible=%b",
110                     token, visible);
111             setVisibility(visible);
112         }
113         final WallpaperController wallpaperController = mDisplayContent.mWallpaperController;
114         if (mTransitionController.isShellTransitionsEnabled()) {
115             return;
116         }
117 
118         final WindowState wallpaperTarget = wallpaperController.getWallpaperTarget();
119 
120         if (visible && wallpaperTarget != null) {
121             final RecentsAnimationController recentsAnimationController =
122                     mWmService.getRecentsAnimationController();
123             if (recentsAnimationController != null
124                     && recentsAnimationController.isAnimatingTask(wallpaperTarget.getTask())) {
125                 // If the Recents animation is running, and the wallpaper target is the animating
126                 // task we want the wallpaper to be rotated in the same orientation as the
127                 // RecentsAnimation's target (e.g the launcher)
128                 recentsAnimationController.linkFixedRotationTransformIfNeeded(this);
129             } else if ((wallpaperTarget.mActivityRecord == null
130                     // Ignore invisible activity because it may be moving to background.
131                     || wallpaperTarget.mActivityRecord.mVisibleRequested)
132                     && wallpaperTarget.mToken.hasFixedRotationTransform()) {
133                 // If the wallpaper target has a fixed rotation, we want the wallpaper to follow its
134                 // rotation
135                 linkFixedRotationTransform(wallpaperTarget.mToken);
136             }
137         }
138 
139         setVisible(visible);
140     }
141 
setVisible(boolean visible)142     private void setVisible(boolean visible) {
143         final boolean wasClientVisible = isClientVisible();
144         setClientVisible(visible);
145         if (visible && !wasClientVisible) {
146             for (int i = mChildren.size() - 1; i >= 0; i--) {
147                 final WindowState wallpaper = mChildren.get(i);
148                 wallpaper.requestUpdateWallpaperIfNeeded();
149             }
150         }
151     }
152 
153     /**
154      * Sets the requested visibility of this token. The visibility may not be if this is part of a
155      * transition. In that situation, make sure to call {@link #commitVisibility} when done.
156      */
setVisibility(boolean visible)157     void setVisibility(boolean visible) {
158         // Before setting mVisibleRequested so we can track changes.
159         mTransitionController.collect(this);
160 
161         setVisibleRequested(visible);
162 
163         // If in a transition, defer commits for activities that are going invisible
164         if (!visible && (mTransitionController.inTransition()
165                 || getDisplayContent().mAppTransition.isRunning())) {
166             return;
167         }
168 
169         commitVisibility(visible);
170     }
171 
172     /**
173      * Commits the visibility of this token. This will directly update the visibility without
174      * regard for other state (like being in a transition).
175      */
commitVisibility(boolean visible)176     void commitVisibility(boolean visible) {
177         if (visible == isVisible()) return;
178 
179         ProtoLog.v(WM_DEBUG_APP_TRANSITIONS,
180                 "commitVisibility: %s: visible=%b mVisibleRequested=%b", this,
181                 isVisible(), mVisibleRequested);
182 
183         setVisibleRequested(visible);
184         setVisible(visible);
185     }
186 
187     @Override
adjustWindowParams(WindowState win, WindowManager.LayoutParams attrs)188     void adjustWindowParams(WindowState win, WindowManager.LayoutParams attrs) {
189         if (attrs.height == ViewGroup.LayoutParams.MATCH_PARENT
190                 || attrs.width == ViewGroup.LayoutParams.MATCH_PARENT) {
191             return;
192         }
193 
194         final DisplayInfo displayInfo = win.getDisplayInfo();
195 
196         final float layoutScale = Math.max(
197                 (float) displayInfo.logicalHeight / (float) attrs.height,
198                 (float) displayInfo.logicalWidth / (float) attrs.width);
199         attrs.height = (int) (attrs.height * layoutScale);
200         attrs.width = (int) (attrs.width * layoutScale);
201         attrs.flags |= WindowManager.LayoutParams.FLAG_SCALED;
202     }
203 
hasVisibleNotDrawnWallpaper()204     boolean hasVisibleNotDrawnWallpaper() {
205         if (!isVisible()) return false;
206         for (int j = mChildren.size() - 1; j >= 0; --j) {
207             final WindowState wallpaper = mChildren.get(j);
208             if (!wallpaper.isDrawn() && wallpaper.isVisible()) {
209                 return true;
210             }
211         }
212         return false;
213     }
214 
215     @Override
forAllWallpaperWindows(Consumer<WallpaperWindowToken> callback)216     void forAllWallpaperWindows(Consumer<WallpaperWindowToken> callback) {
217         callback.accept(this);
218     }
219 
220     @Override
fillsParent()221     boolean fillsParent() {
222         return true;
223     }
224 
225     @Override
showWallpaper()226     boolean showWallpaper() {
227         return false;
228     }
229 
setVisibleRequested(boolean visible)230     void setVisibleRequested(boolean visible) {
231         if (mVisibleRequested == visible) return;
232         mVisibleRequested = visible;
233         setInsetsFrozen(!visible);
234     }
235 
236     @Override
isVisibleRequested()237     boolean isVisibleRequested() {
238         return mVisibleRequested;
239     }
240 
241     @Override
isVisible()242     boolean isVisible() {
243         return isClientVisible();
244     }
245 
246     @Override
toString()247     public String toString() {
248         if (stringName == null) {
249             StringBuilder sb = new StringBuilder();
250             sb.append("WallpaperWindowToken{");
251             sb.append(Integer.toHexString(System.identityHashCode(this)));
252             sb.append(" token="); sb.append(token); sb.append('}');
253             stringName = sb.toString();
254         }
255         return stringName;
256     }
257 }
258