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 | |
Winson Chung | 8a16890 | 2020-03-12 22:39:22 -0700 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW; |
Tiger Huang | 0f5347e | 2020-03-06 17:09:41 +0800 | [diff] [blame] | 20 | import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED; |
Tiger Huang | a1663403 | 2020-02-05 17:10:03 +0800 | [diff] [blame] | 21 | import static android.view.InsetsState.ITYPE_CAPTION_BAR; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 22 | import static android.view.InsetsState.ITYPE_IME; |
Charles Chen | b8070fb | 2020-02-24 15:42:59 +0800 | [diff] [blame] | 23 | import static android.view.InsetsState.ITYPE_INVALID; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 24 | import static android.view.InsetsState.ITYPE_NAVIGATION_BAR; |
| 25 | import static android.view.InsetsState.ITYPE_STATUS_BAR; |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 26 | import static android.view.ViewRootImpl.NEW_INSETS_MODE_FULL; |
| 27 | import static android.view.ViewRootImpl.sNewInsetsMode; |
Charles Chen | b8070fb | 2020-02-24 15:42:59 +0800 | [diff] [blame] | 28 | import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD; |
| 29 | import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR; |
| 30 | import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 31 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 32 | import android.annotation.NonNull; |
| 33 | import android.annotation.Nullable; |
Tiger Huang | 0f5347e | 2020-03-06 17:09:41 +0800 | [diff] [blame] | 34 | import android.app.WindowConfiguration; |
| 35 | import android.app.WindowConfiguration.WindowingMode; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 36 | import android.util.ArrayMap; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 37 | import android.util.ArraySet; |
| 38 | import android.util.SparseArray; |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 39 | import android.view.InsetsSource; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 40 | import android.view.InsetsSourceControl; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 41 | import android.view.InsetsState; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 42 | import android.view.InsetsState.InternalInsetsType; |
Charles Chen | b8070fb | 2020-02-24 15:42:59 +0800 | [diff] [blame] | 43 | import android.view.WindowManager; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 44 | |
| 45 | import java.io.PrintWriter; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 46 | import java.util.ArrayList; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 47 | import java.util.function.Consumer; |
| 48 | |
| 49 | /** |
| 50 | * Manages global window inset state in the system represented by {@link InsetsState}. |
| 51 | */ |
| 52 | class InsetsStateController { |
| 53 | |
| 54 | private final InsetsState mLastState = new InsetsState(); |
| 55 | private final InsetsState mState = new InsetsState(); |
| 56 | private final DisplayContent mDisplayContent; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 57 | |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 58 | private final ArrayMap<Integer, InsetsSourceProvider> mProviders = new ArrayMap<>(); |
| 59 | private final ArrayMap<InsetsControlTarget, ArrayList<Integer>> mControlTargetTypeMap = |
| 60 | new ArrayMap<>(); |
| 61 | private final SparseArray<InsetsControlTarget> mTypeControlTargetMap = new SparseArray<>(); |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 62 | |
| 63 | /** @see #onControlFakeTargetChanged */ |
| 64 | private final SparseArray<InsetsControlTarget> mTypeFakeControlTargetMap = new SparseArray<>(); |
| 65 | |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 66 | private final ArraySet<InsetsControlTarget> mPendingControlChanged = new ArraySet<>(); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 67 | |
| 68 | private final Consumer<WindowState> mDispatchInsetsChanged = w -> { |
| 69 | if (w.isVisible()) { |
| 70 | w.notifyInsetsChanged(); |
| 71 | } |
| 72 | }; |
Tiger Huang | b9510ef | 2020-03-03 23:03:39 +0800 | [diff] [blame] | 73 | private final InsetsControlTarget mEmptyImeControlTarget = new InsetsControlTarget() { }; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 74 | |
| 75 | InsetsStateController(DisplayContent displayContent) { |
| 76 | mDisplayContent = displayContent; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * When dispatching window state to the client, we'll need to exclude the source that represents |
Tiger Huang | 0f5347e | 2020-03-06 17:09:41 +0800 | [diff] [blame] | 81 | * the window that is being dispatched. We also need to exclude certain types of insets source |
| 82 | * for client within specific windowing modes. |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 83 | * |
| 84 | * @param target The client we dispatch the state to. |
| 85 | * @return The state stripped of the necessary information. |
| 86 | */ |
Charles Chen | b8070fb | 2020-02-24 15:42:59 +0800 | [diff] [blame] | 87 | InsetsState getInsetsForDispatch(@NonNull WindowState target) { |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 88 | final InsetsSourceProvider provider = target.getControllableInsetProvider(); |
Tiger Huang | 0f5347e | 2020-03-06 17:09:41 +0800 | [diff] [blame] | 89 | final @InternalInsetsType int type = provider != null |
| 90 | ? provider.getSource().getType() : ITYPE_INVALID; |
Tiger Huang | 57e2e1c | 2020-03-13 22:54:36 +0800 | [diff] [blame] | 91 | return getInsetsForDispatchInner(type, target.getWindowingMode(), target.isAlwaysOnTop(), |
| 92 | isAboveIme(target)); |
Charles Chen | b8070fb | 2020-02-24 15:42:59 +0800 | [diff] [blame] | 93 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 94 | |
Charles Chen | b8070fb | 2020-02-24 15:42:59 +0800 | [diff] [blame] | 95 | InsetsState getInsetsForWindowMetrics(@NonNull WindowManager.LayoutParams attrs) { |
| 96 | final @InternalInsetsType int type = getInsetsTypeForWindowType(attrs.type); |
Tiger Huang | 0f5347e | 2020-03-06 17:09:41 +0800 | [diff] [blame] | 97 | final WindowToken token = mDisplayContent.getWindowToken(attrs.token); |
| 98 | final @WindowingMode int windowingMode = token != null |
| 99 | ? token.getWindowingMode() : WINDOWING_MODE_UNDEFINED; |
Tiger Huang | 57e2e1c | 2020-03-13 22:54:36 +0800 | [diff] [blame] | 100 | final boolean alwaysOnTop = token != null && token.isAlwaysOnTop(); |
| 101 | return getInsetsForDispatchInner(type, windowingMode, alwaysOnTop, isAboveIme(token)); |
| 102 | } |
| 103 | |
| 104 | private boolean isAboveIme(WindowContainer target) { |
| 105 | final WindowState imeWindow = mDisplayContent.mInputMethodWindow; |
| 106 | if (target == null || imeWindow == null) { |
| 107 | return false; |
| 108 | } |
| 109 | if (target instanceof WindowState) { |
| 110 | final WindowState win = (WindowState) target; |
| 111 | return win.needsRelativeLayeringToIme() || !win.mBehindIme; |
| 112 | } |
| 113 | return false; |
Charles Chen | b8070fb | 2020-02-24 15:42:59 +0800 | [diff] [blame] | 114 | } |
| 115 | |
Tiger Huang | 0f5347e | 2020-03-06 17:09:41 +0800 | [diff] [blame] | 116 | private static @InternalInsetsType int getInsetsTypeForWindowType(int type) { |
Tiger Huang | 57e2e1c | 2020-03-13 22:54:36 +0800 | [diff] [blame] | 117 | switch (type) { |
Charles Chen | b8070fb | 2020-02-24 15:42:59 +0800 | [diff] [blame] | 118 | case TYPE_STATUS_BAR: |
| 119 | return ITYPE_STATUS_BAR; |
| 120 | case TYPE_NAVIGATION_BAR: |
| 121 | return ITYPE_NAVIGATION_BAR; |
| 122 | case TYPE_INPUT_METHOD: |
| 123 | return ITYPE_IME; |
| 124 | default: |
| 125 | return ITYPE_INVALID; |
| 126 | } |
| 127 | } |
| 128 | |
Tiger Huang | 0f5347e | 2020-03-06 17:09:41 +0800 | [diff] [blame] | 129 | /** @see #getInsetsForDispatch */ |
Tiger Huang | 57e2e1c | 2020-03-13 22:54:36 +0800 | [diff] [blame] | 130 | private InsetsState getInsetsForDispatchInner(@InternalInsetsType int type, |
| 131 | @WindowingMode int windowingMode, boolean isAlwaysOnTop, boolean aboveIme) { |
Tiger Huang | 0f5347e | 2020-03-06 17:09:41 +0800 | [diff] [blame] | 132 | InsetsState state = mState; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 133 | |
Tiger Huang | 0f5347e | 2020-03-06 17:09:41 +0800 | [diff] [blame] | 134 | if (type != ITYPE_INVALID) { |
| 135 | state = new InsetsState(state); |
| 136 | state.removeSource(type); |
Tiger Huang | a1663403 | 2020-02-05 17:10:03 +0800 | [diff] [blame] | 137 | |
Tiger Huang | 0f5347e | 2020-03-06 17:09:41 +0800 | [diff] [blame] | 138 | // Navigation bar doesn't get influenced by anything else |
| 139 | if (type == ITYPE_NAVIGATION_BAR) { |
| 140 | state.removeSource(ITYPE_IME); |
| 141 | state.removeSource(ITYPE_STATUS_BAR); |
| 142 | state.removeSource(ITYPE_CAPTION_BAR); |
| 143 | } |
Jorim Jaggi | 9b4f420 | 2020-01-28 17:05:06 +0100 | [diff] [blame] | 144 | |
Tiger Huang | 0f5347e | 2020-03-06 17:09:41 +0800 | [diff] [blame] | 145 | // Status bar doesn't get influenced by caption bar |
| 146 | if (type == ITYPE_STATUS_BAR) { |
| 147 | state.removeSource(ITYPE_CAPTION_BAR); |
| 148 | } |
| 149 | |
| 150 | // IME needs different frames for certain cases (e.g. navigation bar in gesture nav). |
| 151 | if (type == ITYPE_IME) { |
| 152 | for (int i = mProviders.size() - 1; i >= 0; i--) { |
| 153 | InsetsSourceProvider otherProvider = mProviders.valueAt(i); |
| 154 | if (otherProvider.overridesImeFrame()) { |
| 155 | InsetsSource override = |
| 156 | new InsetsSource( |
| 157 | state.getSource(otherProvider.getSource().getType())); |
| 158 | override.setFrame(otherProvider.getImeOverrideFrame()); |
| 159 | state.addSource(override); |
| 160 | } |
Jorim Jaggi | 9b4f420 | 2020-01-28 17:05:06 +0100 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
Winson Chung | 8a16890 | 2020-03-12 22:39:22 -0700 | [diff] [blame] | 165 | if (WindowConfiguration.isFloating(windowingMode) |
| 166 | || (windowingMode == WINDOWING_MODE_MULTI_WINDOW && isAlwaysOnTop)) { |
Tiger Huang | 0f5347e | 2020-03-06 17:09:41 +0800 | [diff] [blame] | 167 | state = new InsetsState(state); |
| 168 | state.removeSource(ITYPE_STATUS_BAR); |
| 169 | state.removeSource(ITYPE_NAVIGATION_BAR); |
| 170 | } |
| 171 | |
Tiger Huang | 57e2e1c | 2020-03-13 22:54:36 +0800 | [diff] [blame] | 172 | if (aboveIme) { |
| 173 | state = new InsetsState(state); |
Tiger Huang | a70fd87 | 2020-04-15 05:25:16 +0800 | [diff] [blame] | 174 | state.setSourceVisible(ITYPE_IME, false); |
Tiger Huang | 57e2e1c | 2020-03-13 22:54:36 +0800 | [diff] [blame] | 175 | } |
| 176 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 177 | return state; |
| 178 | } |
| 179 | |
Evan Rosky | 8d782e0 | 2019-10-14 15:43:53 -0700 | [diff] [blame] | 180 | InsetsState getRawInsetsState() { |
| 181 | return mState; |
| 182 | } |
| 183 | |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 184 | @Nullable InsetsSourceControl[] getControlsForDispatch(InsetsControlTarget target) { |
| 185 | ArrayList<Integer> controlled = mControlTargetTypeMap.get(target); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 186 | if (controlled == null) { |
| 187 | return null; |
| 188 | } |
| 189 | final int size = controlled.size(); |
| 190 | final InsetsSourceControl[] result = new InsetsSourceControl[size]; |
| 191 | for (int i = 0; i < size; i++) { |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 192 | result[i] = mProviders.get(controlled.get(i)).getControl(target); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 193 | } |
| 194 | return result; |
| 195 | } |
| 196 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 197 | /** |
| 198 | * @return The provider of a specific type. |
| 199 | */ |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 200 | InsetsSourceProvider getSourceProvider(@InternalInsetsType int type) { |
| 201 | if (type == ITYPE_IME) { |
Tarandeep Singh | 500a38f | 2019-09-26 13:36:40 -0700 | [diff] [blame] | 202 | return mProviders.computeIfAbsent(type, |
| 203 | key -> new ImeInsetsSourceProvider( |
| 204 | mState.getSource(key), this, mDisplayContent)); |
| 205 | } else { |
| 206 | return mProviders.computeIfAbsent(type, |
| 207 | key -> new InsetsSourceProvider(mState.getSource(key), this, mDisplayContent)); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | ImeInsetsSourceProvider getImeSourceProvider() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 212 | return (ImeInsetsSourceProvider) getSourceProvider(ITYPE_IME); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 216 | * @return The provider of a specific type or null if we don't have it. |
| 217 | */ |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 218 | @Nullable InsetsSourceProvider peekSourceProvider(@InternalInsetsType int type) { |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 219 | return mProviders.get(type); |
| 220 | } |
| 221 | |
| 222 | /** |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 223 | * Called when a layout pass has occurred. |
| 224 | */ |
| 225 | void onPostLayout() { |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 226 | mState.setDisplayFrame(mDisplayContent.getBounds()); |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 227 | for (int i = mProviders.size() - 1; i >= 0; i--) { |
| 228 | mProviders.valueAt(i).onPostLayout(); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 229 | } |
Tiger Huang | 53c8eb8 | 2020-03-31 18:22:05 +0800 | [diff] [blame] | 230 | final ArrayList<WindowState> winInsetsChanged = mDisplayContent.mWinInsetsChanged; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 231 | if (!mLastState.equals(mState)) { |
| 232 | mLastState.set(mState, true /* copySources */); |
| 233 | notifyInsetsChanged(); |
Tiger Huang | 53c8eb8 | 2020-03-31 18:22:05 +0800 | [diff] [blame] | 234 | } else { |
| 235 | // The global insets state has not changed but there might be windows whose conditions |
| 236 | // (e.g., z-order) have changed. They can affect the insets states that we dispatch to |
| 237 | // the clients. |
| 238 | for (int i = winInsetsChanged.size() - 1; i >= 0; i--) { |
| 239 | winInsetsChanged.get(i).notifyInsetsChanged(); |
| 240 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 241 | } |
Tiger Huang | 53c8eb8 | 2020-03-31 18:22:05 +0800 | [diff] [blame] | 242 | winInsetsChanged.clear(); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 243 | } |
| 244 | |
Evan Rosky | 8d782e0 | 2019-10-14 15:43:53 -0700 | [diff] [blame] | 245 | void onInsetsModified(InsetsControlTarget windowState, InsetsState state) { |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 246 | boolean changed = false; |
| 247 | for (int i = state.getSourcesCount() - 1; i >= 0; i--) { |
| 248 | final InsetsSource source = state.sourceAt(i); |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 249 | final InsetsSourceProvider provider = mProviders.get(source.getType()); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 250 | if (provider == null) { |
| 251 | continue; |
| 252 | } |
| 253 | changed |= provider.onInsetsModified(windowState, source); |
| 254 | } |
| 255 | if (changed) { |
| 256 | notifyInsetsChanged(); |
Tiger Huang | dd6db47 | 2020-02-13 21:38:46 +0800 | [diff] [blame] | 257 | mDisplayContent.getDisplayPolicy().updateSystemUiVisibilityLw(); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Riddle Hsu | c717576 | 2020-01-10 21:03:48 +0800 | [diff] [blame] | 261 | /** |
| 262 | * Computes insets state of the insets provider window in the display frames. |
| 263 | * |
| 264 | * @param state The output state. |
| 265 | * @param win The owner window of insets provider. |
| 266 | * @param displayFrames The display frames to create insets source. |
| 267 | * @param windowFrames The specified frames to represent the owner window. |
| 268 | */ |
| 269 | void computeSimulatedState(InsetsState state, WindowState win, DisplayFrames displayFrames, |
| 270 | WindowFrames windowFrames) { |
| 271 | for (int i = mProviders.size() - 1; i >= 0; i--) { |
| 272 | final InsetsSourceProvider provider = mProviders.valueAt(i); |
| 273 | if (provider.mWin == win) { |
| 274 | state.addSource(provider.createSimulatedSource(displayFrames, windowFrames)); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 279 | boolean isFakeTarget(@InternalInsetsType int type, InsetsControlTarget target) { |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 280 | return mTypeFakeControlTargetMap.get(type) == target; |
| 281 | } |
| 282 | |
Tarandeep Singh | a659018 | 2020-01-24 16:05:18 -0800 | [diff] [blame] | 283 | void onImeControlTargetChanged(@Nullable InsetsControlTarget imeTarget) { |
Jorim Jaggi | f768ea5 | 2020-01-30 23:45:23 +0100 | [diff] [blame] | 284 | |
| 285 | // Make sure that we always have a control target for the IME, even if the IME target is |
| 286 | // null. Otherwise there is no leash that will hide it and IME becomes "randomly" visible. |
| 287 | onControlChanged(ITYPE_IME, imeTarget != null ? imeTarget : mEmptyImeControlTarget); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 288 | notifyPendingInsetsControlChanged(); |
| 289 | } |
| 290 | |
| 291 | /** |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 292 | * Called when the focused window that is able to control the system bars changes. |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 293 | * |
Tiger Huang | a1663403 | 2020-02-05 17:10:03 +0800 | [diff] [blame] | 294 | * @param statusControlling The target that is now able to control the status bar appearance |
| 295 | * and visibility. |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 296 | * @param navControlling The target that is now able to control the nav bar appearance |
| 297 | * and visibility. |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 298 | */ |
Tiger Huang | a1663403 | 2020-02-05 17:10:03 +0800 | [diff] [blame] | 299 | void onBarControlTargetChanged(@Nullable InsetsControlTarget statusControlling, |
| 300 | @Nullable InsetsControlTarget fakeStatusControlling, |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 301 | @Nullable InsetsControlTarget navControlling, |
| 302 | @Nullable InsetsControlTarget fakeNavControlling) { |
Tiger Huang | a1663403 | 2020-02-05 17:10:03 +0800 | [diff] [blame] | 303 | onControlChanged(ITYPE_STATUS_BAR, statusControlling); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 304 | onControlChanged(ITYPE_NAVIGATION_BAR, navControlling); |
Tiger Huang | a1663403 | 2020-02-05 17:10:03 +0800 | [diff] [blame] | 305 | onControlFakeTargetChanged(ITYPE_STATUS_BAR, fakeStatusControlling); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 306 | onControlFakeTargetChanged(ITYPE_NAVIGATION_BAR, fakeNavControlling); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 307 | notifyPendingInsetsControlChanged(); |
| 308 | } |
| 309 | |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 310 | void notifyControlRevoked(@NonNull InsetsControlTarget previousControlTarget, |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 311 | InsetsSourceProvider provider) { |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 312 | removeFromControlMaps(previousControlTarget, provider.getSource().getType(), |
| 313 | false /* fake */); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 316 | private void onControlChanged(@InternalInsetsType int type, |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 317 | @Nullable InsetsControlTarget target) { |
| 318 | final InsetsControlTarget previous = mTypeControlTargetMap.get(type); |
| 319 | if (target == previous) { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 320 | return; |
| 321 | } |
Tiger Huang | 969c608 | 2019-12-24 20:08:57 +0800 | [diff] [blame] | 322 | final InsetsSourceProvider provider = mProviders.get(type); |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 323 | if (provider == null) { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 324 | return; |
| 325 | } |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 326 | if (!provider.isControllable()) { |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 327 | return; |
| 328 | } |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 329 | provider.updateControlForTarget(target, false /* force */); |
Tiger Huang | 969c608 | 2019-12-24 20:08:57 +0800 | [diff] [blame] | 330 | target = provider.getControlTarget(); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 331 | if (previous != null) { |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 332 | removeFromControlMaps(previous, type, false /* fake */); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 333 | mPendingControlChanged.add(previous); |
| 334 | } |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 335 | if (target != null) { |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 336 | addToControlMaps(target, type, false /* fake */); |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 337 | mPendingControlChanged.add(target); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 341 | /** |
| 342 | * The fake target saved here will be used to pretend to the app that it's still under control |
| 343 | * of the bars while it's not really, but we still need to find out the apps intentions around |
| 344 | * showing/hiding. For example, when the transient bars are showing, and the fake target |
| 345 | * requests to show system bars, the transient state will be aborted. |
| 346 | */ |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 347 | void onControlFakeTargetChanged(@InternalInsetsType int type, |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 348 | @Nullable InsetsControlTarget fakeTarget) { |
| 349 | if (sNewInsetsMode != NEW_INSETS_MODE_FULL) { |
| 350 | return; |
| 351 | } |
| 352 | final InsetsControlTarget previous = mTypeFakeControlTargetMap.get(type); |
| 353 | if (fakeTarget == previous) { |
| 354 | return; |
| 355 | } |
| 356 | final InsetsSourceProvider provider = mProviders.get(type); |
| 357 | if (provider == null) { |
| 358 | return; |
| 359 | } |
| 360 | provider.updateControlForFakeTarget(fakeTarget); |
| 361 | if (previous != null) { |
| 362 | removeFromControlMaps(previous, type, true /* fake */); |
| 363 | mPendingControlChanged.add(previous); |
| 364 | } |
| 365 | if (fakeTarget != null) { |
| 366 | addToControlMaps(fakeTarget, type, true /* fake */); |
| 367 | mPendingControlChanged.add(fakeTarget); |
| 368 | } |
| 369 | } |
| 370 | |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 371 | private void removeFromControlMaps(@NonNull InsetsControlTarget target, |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 372 | @InternalInsetsType int type, boolean fake) { |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 373 | final ArrayList<Integer> array = mControlTargetTypeMap.get(target); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 374 | if (array == null) { |
| 375 | return; |
| 376 | } |
| 377 | array.remove((Integer) type); |
| 378 | if (array.isEmpty()) { |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 379 | mControlTargetTypeMap.remove(target); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 380 | } |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 381 | if (fake) { |
| 382 | mTypeFakeControlTargetMap.remove(type); |
| 383 | } else { |
| 384 | mTypeControlTargetMap.remove(type); |
| 385 | } |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 386 | } |
| 387 | |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 388 | private void addToControlMaps(@NonNull InsetsControlTarget target, |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 389 | @InternalInsetsType int type, boolean fake) { |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 390 | final ArrayList<Integer> array = mControlTargetTypeMap.computeIfAbsent(target, |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 391 | key -> new ArrayList<>()); |
| 392 | array.add(type); |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 393 | if (fake) { |
| 394 | mTypeFakeControlTargetMap.put(type, target); |
| 395 | } else { |
| 396 | mTypeControlTargetMap.put(type, target); |
| 397 | } |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 398 | } |
| 399 | |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 400 | void notifyControlChanged(InsetsControlTarget target) { |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 401 | mPendingControlChanged.add(target); |
| 402 | notifyPendingInsetsControlChanged(); |
| 403 | } |
| 404 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 405 | private void notifyPendingInsetsControlChanged() { |
Jorim Jaggi | f86eb49 | 2019-01-09 17:37:08 +0100 | [diff] [blame] | 406 | if (mPendingControlChanged.isEmpty()) { |
| 407 | return; |
| 408 | } |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 409 | mDisplayContent.mWmService.mAnimator.addAfterPrepareSurfacesRunnable(() -> { |
| 410 | for (int i = mPendingControlChanged.size() - 1; i >= 0; i--) { |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 411 | final InsetsControlTarget controlTarget = mPendingControlChanged.valueAt(i); |
| 412 | controlTarget.notifyInsetsControlChanged(); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 413 | } |
| 414 | mPendingControlChanged.clear(); |
| 415 | }); |
| 416 | } |
| 417 | |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 418 | void notifyInsetsChanged() { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 419 | mDisplayContent.forAllWindows(mDispatchInsetsChanged, true /* traverseTopToBottom */); |
Evan Rosky | 8d782e0 | 2019-10-14 15:43:53 -0700 | [diff] [blame] | 420 | if (mDisplayContent.mRemoteInsetsControlTarget != null) { |
| 421 | mDisplayContent.mRemoteInsetsControlTarget.notifyInsetsChanged(); |
| 422 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | void dump(String prefix, PrintWriter pw) { |
| 426 | pw.println(prefix + "WindowInsetsStateController"); |
| 427 | mState.dump(prefix + " ", pw); |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 428 | pw.println(prefix + " " + "Control map:"); |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 429 | for (int i = mTypeControlTargetMap.size() - 1; i >= 0; i--) { |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 430 | pw.print(prefix + " "); |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 431 | pw.println(InsetsState.typeToString(mTypeControlTargetMap.keyAt(i)) + " -> " |
| 432 | + mTypeControlTargetMap.valueAt(i)); |
Jorim Jaggi | cfd6f3b | 2018-11-07 15:30:18 +0100 | [diff] [blame] | 433 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 434 | } |
| 435 | } |