Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 19 | import static android.view.InsetsState.ITYPE_IME; |
| 20 | import static android.view.InsetsState.ITYPE_NAVIGATION_BAR; |
| 21 | import static android.view.InsetsState.ITYPE_STATUS_BAR; |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 22 | import static android.view.ViewRootImpl.NEW_INSETS_MODE_FULL; |
| 23 | import static android.view.ViewRootImpl.NEW_INSETS_MODE_IME; |
Jorim Jaggi | 73f3e8a | 2019-01-14 13:06:23 +0100 | [diff] [blame] | 24 | import static android.view.ViewRootImpl.NEW_INSETS_MODE_NONE; |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 25 | import static android.view.ViewRootImpl.sNewInsetsMode; |
Jorim Jaggi | 73f3e8a | 2019-01-14 13:06:23 +0100 | [diff] [blame] | 26 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 27 | import android.annotation.NonNull; |
| 28 | import android.annotation.Nullable; |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 29 | import android.graphics.Point; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 30 | import android.graphics.Rect; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 31 | import android.util.proto.ProtoOutputStream; |
chaviw | 15ad49f | 2019-04-24 15:05:39 -0700 | [diff] [blame] | 32 | import android.view.InsetsSource; |
| 33 | import android.view.InsetsSourceControl; |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 34 | import android.view.InsetsState; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 35 | import android.view.SurfaceControl; |
| 36 | import android.view.SurfaceControl.Transaction; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 37 | |
| 38 | import com.android.internal.util.function.TriConsumer; |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 39 | import com.android.internal.util.function.pooled.PooledLambda; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 40 | import com.android.server.wm.SurfaceAnimator.OnAnimationFinishedCallback; |
| 41 | |
| 42 | import java.io.PrintWriter; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Controller for a specific inset source on the server. It's called provider as it provides the |
| 46 | * {@link InsetsSource} to the client that uses it in {@link InsetsSourceConsumer}. |
| 47 | */ |
| 48 | class InsetsSourceProvider { |
| 49 | |
Tarandeep Singh | 500a38f | 2019-09-26 13:36:40 -0700 | [diff] [blame] | 50 | protected final DisplayContent mDisplayContent; |
| 51 | protected final @NonNull InsetsSource mSource; |
| 52 | protected WindowState mWin; |
| 53 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 54 | private final Rect mTmpRect = new Rect(); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 55 | private final InsetsStateController mStateController; |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 56 | private final InsetsSourceControl mFakeControl; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 57 | private @Nullable InsetsSourceControl mControl; |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 58 | private @Nullable InsetsControlTarget mControlTarget; |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 59 | private @Nullable InsetsControlTarget mFakeControlTarget; |
| 60 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 61 | private @Nullable ControlAdapter mAdapter; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 62 | private TriConsumer<DisplayFrames, WindowState, Rect> mFrameProvider; |
| 63 | |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 64 | /** The visibility override from the current controlling window. */ |
| 65 | private boolean mClientVisible; |
| 66 | |
| 67 | /** |
| 68 | * Whether the window is available and considered visible as in {@link WindowState#isVisible}. |
| 69 | */ |
| 70 | private boolean mServerVisible; |
| 71 | |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 72 | private final boolean mControllable; |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 73 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 74 | InsetsSourceProvider(InsetsSource source, InsetsStateController stateController, |
| 75 | DisplayContent displayContent) { |
Jorim Jaggi | d89efeb | 2019-01-22 17:48:34 +0100 | [diff] [blame] | 76 | mClientVisible = InsetsState.getDefaultVisibility(source.getType()); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 77 | mSource = source; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 78 | mDisplayContent = displayContent; |
| 79 | mStateController = stateController; |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 80 | mFakeControl = new InsetsSourceControl(source.getType(), null /* leash */, |
| 81 | new Point()); |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 82 | |
| 83 | final int type = source.getType(); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 84 | if (type == ITYPE_STATUS_BAR || type == ITYPE_NAVIGATION_BAR) { |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 85 | mControllable = sNewInsetsMode == NEW_INSETS_MODE_FULL; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 86 | } else if (type == ITYPE_IME) { |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 87 | mControllable = sNewInsetsMode >= NEW_INSETS_MODE_IME; |
| 88 | } else { |
| 89 | mControllable = false; |
| 90 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | InsetsSource getSource() { |
| 94 | return mSource; |
| 95 | } |
| 96 | |
| 97 | /** |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 98 | * @return Whether the current flag configuration allows to control this source. |
| 99 | */ |
| 100 | boolean isControllable() { |
| 101 | return mControllable; |
| 102 | } |
| 103 | |
| 104 | /** |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 105 | * Updates the window that currently backs this source. |
| 106 | * |
| 107 | * @param win The window that links to this source. |
| 108 | * @param frameProvider Based on display frame state and the window, calculates the resulting |
| 109 | * frame that should be reported to clients. |
| 110 | */ |
| 111 | void setWindow(@Nullable WindowState win, |
| 112 | @Nullable TriConsumer<DisplayFrames, WindowState, Rect> frameProvider) { |
| 113 | if (mWin != null) { |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 114 | if (mControllable) { |
| 115 | mWin.setControllableInsetProvider(null); |
| 116 | } |
Tarandeep Singh | 5d54e72 | 2019-08-15 13:33:12 -0700 | [diff] [blame] | 117 | // The window may be animating such that we can hand out the leash to the control |
| 118 | // target. Revoke the leash by cancelling the animation to correct the state. |
| 119 | // TODO: Ideally, we should wait for the animation to finish so previous window can |
| 120 | // animate-out as new one animates-in. |
| 121 | mWin.cancelAnimation(); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 122 | } |
| 123 | mWin = win; |
| 124 | mFrameProvider = frameProvider; |
| 125 | if (win == null) { |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 126 | setServerVisible(false); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 127 | mSource.setFrame(new Rect()); |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 128 | } else if (mControllable) { |
| 129 | mWin.setControllableInsetProvider(this); |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 130 | if (mControlTarget != null) { |
| 131 | updateControlForTarget(mControlTarget, true /* force */); |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 132 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 137 | * @return Whether there is a window which backs this source. |
| 138 | */ |
| 139 | boolean hasWindow() { |
| 140 | return mWin != null; |
| 141 | } |
| 142 | |
| 143 | /** |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 144 | * Called when a layout pass has occurred. |
| 145 | */ |
| 146 | void onPostLayout() { |
| 147 | if (mWin == null) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | mTmpRect.set(mWin.getFrameLw()); |
| 152 | if (mFrameProvider != null) { |
| 153 | mFrameProvider.accept(mWin.getDisplayContent().mDisplayFrames, mWin, mTmpRect); |
| 154 | } else { |
| 155 | mTmpRect.inset(mWin.mGivenContentInsets); |
| 156 | } |
| 157 | mSource.setFrame(mTmpRect); |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 158 | if (mControl != null) { |
| 159 | final Rect frame = mWin.getWindowFrames().mFrame; |
| 160 | if (mControl.setSurfacePosition(frame.left, frame.top)) { |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 161 | mStateController.notifyControlChanged(mControlTarget); |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
chaviw | 15ad49f | 2019-04-24 15:05:39 -0700 | [diff] [blame] | 164 | setServerVisible(mWin.wouldBeVisibleIfPolicyIgnored() && mWin.isVisibleByPolicy() |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 165 | && !mWin.mGivenInsetsPending); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 166 | } |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 167 | |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 168 | /** |
| 169 | * @see InsetsStateController#onControlFakeTargetChanged(int, InsetsControlTarget) |
| 170 | */ |
| 171 | void updateControlForFakeTarget(@Nullable InsetsControlTarget fakeTarget) { |
| 172 | if (fakeTarget == mFakeControlTarget) { |
| 173 | return; |
| 174 | } |
| 175 | mFakeControlTarget = fakeTarget; |
| 176 | } |
| 177 | |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 178 | void updateControlForTarget(@Nullable InsetsControlTarget target, boolean force) { |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 179 | if (mWin == null) { |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 180 | mControlTarget = target; |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 181 | return; |
| 182 | } |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 183 | if (target == mControlTarget && !force) { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | if (target == null) { |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 187 | // Cancelling the animation will invoke onAnimationCancelled, resetting all the fields. |
| 188 | mWin.cancelAnimation(); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | mAdapter = new ControlAdapter(); |
Jorim Jaggi | d89efeb | 2019-01-22 17:48:34 +0100 | [diff] [blame] | 192 | setClientVisible(InsetsState.getDefaultVisibility(mSource.getType())); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 193 | mWin.startAnimation(mDisplayContent.getPendingTransaction(), mAdapter, |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 194 | !mClientVisible /* hidden */); |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 195 | mControlTarget = target; |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 196 | mControl = new InsetsSourceControl(mSource.getType(), mAdapter.mCapturedLeash, |
| 197 | new Point(mWin.getWindowFrames().mFrame.left, mWin.getWindowFrames().mFrame.top)); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | boolean onInsetsModified(WindowState caller, InsetsSource modifiedSource) { |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 201 | if (mControlTarget != caller || modifiedSource.isVisible() == mClientVisible) { |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | setClientVisible(modifiedSource.isVisible()); |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | private void setClientVisible(boolean clientVisible) { |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 209 | if (mClientVisible == clientVisible) { |
| 210 | return; |
| 211 | } |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 212 | mClientVisible = clientVisible; |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 213 | mDisplayContent.mWmService.mH.sendMessage(PooledLambda.obtainMessage( |
| 214 | DisplayContent::layoutAndAssignWindowLayersIfNeeded, mDisplayContent)); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 215 | updateVisibility(); |
| 216 | } |
| 217 | |
| 218 | private void setServerVisible(boolean serverVisible) { |
| 219 | mServerVisible = serverVisible; |
| 220 | updateVisibility(); |
| 221 | } |
| 222 | |
| 223 | private void updateVisibility() { |
| 224 | mSource.setVisible(mServerVisible && mClientVisible); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 227 | InsetsSourceControl getControl(InsetsControlTarget target) { |
| 228 | if (target == mControlTarget) { |
| 229 | return mControl; |
| 230 | } |
| 231 | if (target == mFakeControlTarget) { |
| 232 | return mFakeControl; |
| 233 | } |
| 234 | return null; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 237 | InsetsControlTarget getControlTarget() { |
| 238 | return mControlTarget; |
| 239 | } |
| 240 | |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 241 | boolean isClientVisible() { |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 242 | return sNewInsetsMode == NEW_INSETS_MODE_NONE || mClientVisible; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | private class ControlAdapter implements AnimationAdapter { |
| 246 | |
| 247 | private SurfaceControl mCapturedLeash; |
| 248 | |
| 249 | @Override |
| 250 | public boolean getShowWallpaper() { |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | @Override |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 255 | public void startAnimation(SurfaceControl animationLeash, Transaction t, |
| 256 | OnAnimationFinishedCallback finishCallback) { |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 257 | // TODO(b/118118435): We can remove the type check when implementing the transient bar |
| 258 | // animation. |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 259 | if (mSource.getType() == ITYPE_IME) { |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 260 | // TODO: use 0 alpha and remove t.hide() once b/138459974 is fixed. |
| 261 | t.setAlpha(animationLeash, 1 /* alpha */); |
| 262 | t.hide(animationLeash); |
| 263 | } |
Taran Singh | fd0e586 | 2019-10-10 15:15:16 +0200 | [diff] [blame] | 264 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 265 | mCapturedLeash = animationLeash; |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 266 | final Rect frame = mWin.getWindowFrames().mFrame; |
| 267 | t.setPosition(mCapturedLeash, frame.left, frame.top); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public void onAnimationCancelled(SurfaceControl animationLeash) { |
| 272 | if (mAdapter == this) { |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 273 | mStateController.notifyControlRevoked(mControlTarget, InsetsSourceProvider.this); |
Jorim Jaggi | d89efeb | 2019-01-22 17:48:34 +0100 | [diff] [blame] | 274 | setClientVisible(InsetsState.getDefaultVisibility(mSource.getType())); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 275 | mControl = null; |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 276 | mControlTarget = null; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 277 | mAdapter = null; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public long getDurationHint() { |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public long getStatusBarTransitionsStartTime() { |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public void dump(PrintWriter pw, String prefix) { |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public void writeToProto(ProtoOutputStream proto) { |
| 297 | } |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 298 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 299 | } |