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