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 | |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 19 | import static android.view.InsetsState.TYPE_IME; |
| 20 | import static android.view.InsetsState.TYPE_NAVIGATION_BAR; |
| 21 | import static android.view.InsetsState.TYPE_TOP_BAR; |
| 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; |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 32 | import android.view.InsetsState; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 33 | import android.view.SurfaceControl; |
| 34 | import android.view.SurfaceControl.Transaction; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 35 | import android.view.InsetsSource; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 36 | import android.view.InsetsSourceControl; |
Jorim Jaggi | 956f124 | 2019-01-06 22:34:13 +0100 | [diff] [blame] | 37 | import android.view.ViewRootImpl; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 38 | |
| 39 | import com.android.internal.util.function.TriConsumer; |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 40 | import com.android.internal.util.function.pooled.PooledLambda; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 41 | import com.android.server.wm.SurfaceAnimator.OnAnimationFinishedCallback; |
| 42 | |
| 43 | import java.io.PrintWriter; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Controller for a specific inset source on the server. It's called provider as it provides the |
| 47 | * {@link InsetsSource} to the client that uses it in {@link InsetsSourceConsumer}. |
| 48 | */ |
| 49 | class InsetsSourceProvider { |
| 50 | |
| 51 | private final Rect mTmpRect = new Rect(); |
| 52 | private final @NonNull InsetsSource mSource; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 53 | private final DisplayContent mDisplayContent; |
| 54 | private final InsetsStateController mStateController; |
| 55 | private @Nullable InsetsSourceControl mControl; |
| 56 | private @Nullable WindowState mControllingWin; |
| 57 | private @Nullable ControlAdapter mAdapter; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 58 | private WindowState mWin; |
| 59 | private TriConsumer<DisplayFrames, WindowState, Rect> mFrameProvider; |
| 60 | |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 61 | /** The visibility override from the current controlling window. */ |
| 62 | private boolean mClientVisible; |
| 63 | |
| 64 | /** |
| 65 | * Whether the window is available and considered visible as in {@link WindowState#isVisible}. |
| 66 | */ |
| 67 | private boolean mServerVisible; |
| 68 | |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 69 | private final boolean mControllable; |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 70 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 71 | InsetsSourceProvider(InsetsSource source, InsetsStateController stateController, |
| 72 | DisplayContent displayContent) { |
Jorim Jaggi | d89efeb | 2019-01-22 17:48:34 +0100 | [diff] [blame] | 73 | mClientVisible = InsetsState.getDefaultVisibility(source.getType()); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 74 | mSource = source; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 75 | mDisplayContent = displayContent; |
| 76 | mStateController = stateController; |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 77 | |
| 78 | final int type = source.getType(); |
| 79 | if (type == TYPE_TOP_BAR || type == TYPE_NAVIGATION_BAR) { |
| 80 | mControllable = sNewInsetsMode == NEW_INSETS_MODE_FULL; |
| 81 | } else if (type == TYPE_IME) { |
| 82 | mControllable = sNewInsetsMode >= NEW_INSETS_MODE_IME; |
| 83 | } else { |
| 84 | mControllable = false; |
| 85 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | InsetsSource getSource() { |
| 89 | return mSource; |
| 90 | } |
| 91 | |
| 92 | /** |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 93 | * @return Whether the current flag configuration allows to control this source. |
| 94 | */ |
| 95 | boolean isControllable() { |
| 96 | return mControllable; |
| 97 | } |
| 98 | |
| 99 | /** |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 100 | * Updates the window that currently backs this source. |
| 101 | * |
| 102 | * @param win The window that links to this source. |
| 103 | * @param frameProvider Based on display frame state and the window, calculates the resulting |
| 104 | * frame that should be reported to clients. |
| 105 | */ |
| 106 | void setWindow(@Nullable WindowState win, |
| 107 | @Nullable TriConsumer<DisplayFrames, WindowState, Rect> frameProvider) { |
| 108 | if (mWin != null) { |
| 109 | mWin.setInsetProvider(null); |
| 110 | } |
| 111 | mWin = win; |
| 112 | mFrameProvider = frameProvider; |
| 113 | if (win == null) { |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 114 | setServerVisible(false); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 115 | mSource.setFrame(new Rect()); |
| 116 | } else { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 117 | mWin.setInsetProvider(this); |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 118 | if (mControllingWin != null) { |
| 119 | updateControlForTarget(mControllingWin, true /* force */); |
| 120 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Called when a layout pass has occurred. |
| 126 | */ |
| 127 | void onPostLayout() { |
| 128 | if (mWin == null) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | mTmpRect.set(mWin.getFrameLw()); |
| 133 | if (mFrameProvider != null) { |
| 134 | mFrameProvider.accept(mWin.getDisplayContent().mDisplayFrames, mWin, mTmpRect); |
| 135 | } else { |
| 136 | mTmpRect.inset(mWin.mGivenContentInsets); |
| 137 | } |
| 138 | mSource.setFrame(mTmpRect); |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame^] | 139 | if (mControl != null) { |
| 140 | final Rect frame = mWin.getWindowFrames().mFrame; |
| 141 | if (mControl.setSurfacePosition(frame.left, frame.top)) { |
| 142 | mStateController.notifyControlChanged(mControllingWin); |
| 143 | } |
| 144 | } |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 145 | setServerVisible(mWin.wouldBeVisibleIfPolicyIgnored() && mWin.mPolicyVisibility |
| 146 | && !mWin.mGivenInsetsPending); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 147 | } |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 148 | |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 149 | void updateControlForTarget(@Nullable WindowState target, boolean force) { |
| 150 | if (mWin == null) { |
| 151 | mControllingWin = target; |
| 152 | return; |
| 153 | } |
| 154 | if (target == mControllingWin && !force) { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | if (target == null) { |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 158 | // Cancelling the animation will invoke onAnimationCancelled, resetting all the fields. |
| 159 | mWin.cancelAnimation(); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | mAdapter = new ControlAdapter(); |
Jorim Jaggi | d89efeb | 2019-01-22 17:48:34 +0100 | [diff] [blame] | 163 | setClientVisible(InsetsState.getDefaultVisibility(mSource.getType())); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 164 | mWin.startAnimation(mDisplayContent.getPendingTransaction(), mAdapter, |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 165 | !mClientVisible /* hidden */); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 166 | mControllingWin = target; |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame^] | 167 | mControl = new InsetsSourceControl(mSource.getType(), mAdapter.mCapturedLeash, |
| 168 | new Point(mWin.getWindowFrames().mFrame.left, mWin.getWindowFrames().mFrame.top)); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | boolean onInsetsModified(WindowState caller, InsetsSource modifiedSource) { |
| 172 | if (mControllingWin != caller || modifiedSource.isVisible() == mClientVisible) { |
| 173 | return false; |
| 174 | } |
| 175 | setClientVisible(modifiedSource.isVisible()); |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | private void setClientVisible(boolean clientVisible) { |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 180 | if (mClientVisible == clientVisible) { |
| 181 | return; |
| 182 | } |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 183 | mClientVisible = clientVisible; |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 184 | mDisplayContent.mWmService.mH.sendMessage(PooledLambda.obtainMessage( |
| 185 | DisplayContent::layoutAndAssignWindowLayersIfNeeded, mDisplayContent)); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 186 | updateVisibility(); |
| 187 | } |
| 188 | |
| 189 | private void setServerVisible(boolean serverVisible) { |
| 190 | mServerVisible = serverVisible; |
| 191 | updateVisibility(); |
| 192 | } |
| 193 | |
| 194 | private void updateVisibility() { |
| 195 | mSource.setVisible(mServerVisible && mClientVisible); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | InsetsSourceControl getControl() { |
| 199 | return mControl; |
| 200 | } |
| 201 | |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 202 | boolean isClientVisible() { |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 203 | return sNewInsetsMode == NEW_INSETS_MODE_NONE || mClientVisible; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | private class ControlAdapter implements AnimationAdapter { |
| 207 | |
| 208 | private SurfaceControl mCapturedLeash; |
| 209 | |
| 210 | @Override |
| 211 | public boolean getShowWallpaper() { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public int getBackgroundColor() { |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void startAnimation(SurfaceControl animationLeash, Transaction t, |
| 222 | OnAnimationFinishedCallback finishCallback) { |
| 223 | mCapturedLeash = animationLeash; |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame^] | 224 | final Rect frame = mWin.getWindowFrames().mFrame; |
| 225 | t.setPosition(mCapturedLeash, frame.left, frame.top); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public void onAnimationCancelled(SurfaceControl animationLeash) { |
| 230 | if (mAdapter == this) { |
| 231 | mStateController.notifyControlRevoked(mControllingWin, InsetsSourceProvider.this); |
Jorim Jaggi | d89efeb | 2019-01-22 17:48:34 +0100 | [diff] [blame] | 232 | setClientVisible(InsetsState.getDefaultVisibility(mSource.getType())); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 233 | mControl = null; |
| 234 | mControllingWin = null; |
| 235 | mAdapter = null; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public long getDurationHint() { |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public long getStatusBarTransitionsStartTime() { |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public void dump(PrintWriter pw, String prefix) { |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public void writeToProto(ProtoOutputStream proto) { |
| 255 | } |
| 256 | }; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 257 | } |