blob: b2234d17984e4dde8da1be5f452d314ca1b4653a [file] [log] [blame]
Jorim Jaggif96c90a2018-09-26 16:55:15 +02001/*
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
17package com.android.server.wm;
18
Tiger Huang332793b2019-10-29 23:21:27 +080019import static android.view.InsetsState.ITYPE_IME;
20import static android.view.InsetsState.ITYPE_NAVIGATION_BAR;
21import static android.view.InsetsState.ITYPE_STATUS_BAR;
Jorim Jaggia12ea562019-01-07 17:47:47 +010022import static android.view.ViewRootImpl.NEW_INSETS_MODE_FULL;
23import static android.view.ViewRootImpl.sNewInsetsMode;
Jorim Jaggif96c90a2018-09-26 16:55:15 +020024
Jorim Jaggib6030952018-10-23 18:31:52 +020025import android.annotation.NonNull;
26import android.annotation.Nullable;
Jorim Jaggif96c90a2018-09-26 16:55:15 +020027import android.util.ArrayMap;
Jorim Jaggib6030952018-10-23 18:31:52 +020028import android.util.ArraySet;
29import android.util.SparseArray;
Jorim Jaggie35c0592018-11-06 16:21:08 +010030import android.view.InsetsSource;
Jorim Jaggib6030952018-10-23 18:31:52 +020031import android.view.InsetsSourceControl;
Jorim Jaggif96c90a2018-09-26 16:55:15 +020032import android.view.InsetsState;
Tiger Huang332793b2019-10-29 23:21:27 +080033import android.view.InsetsState.InternalInsetsType;
Jorim Jaggif96c90a2018-09-26 16:55:15 +020034
35import java.io.PrintWriter;
Jorim Jaggib6030952018-10-23 18:31:52 +020036import java.util.ArrayList;
Jorim Jaggif96c90a2018-09-26 16:55:15 +020037import java.util.function.Consumer;
38
39/**
40 * Manages global window inset state in the system represented by {@link InsetsState}.
41 */
42class InsetsStateController {
43
44 private final InsetsState mLastState = new InsetsState();
45 private final InsetsState mState = new InsetsState();
46 private final DisplayContent mDisplayContent;
Jorim Jaggib6030952018-10-23 18:31:52 +020047
Jorim Jaggi28620472019-01-02 23:21:49 +010048 private final ArrayMap<Integer, InsetsSourceProvider> mProviders = new ArrayMap<>();
49 private final ArrayMap<InsetsControlTarget, ArrayList<Integer>> mControlTargetTypeMap =
50 new ArrayMap<>();
51 private final SparseArray<InsetsControlTarget> mTypeControlTargetMap = new SparseArray<>();
Jorim Jaggia12ea562019-01-07 17:47:47 +010052
53 /** @see #onControlFakeTargetChanged */
54 private final SparseArray<InsetsControlTarget> mTypeFakeControlTargetMap = new SparseArray<>();
55
Jorim Jaggi28620472019-01-02 23:21:49 +010056 private final ArraySet<InsetsControlTarget> mPendingControlChanged = new ArraySet<>();
Jorim Jaggif96c90a2018-09-26 16:55:15 +020057
58 private final Consumer<WindowState> mDispatchInsetsChanged = w -> {
59 if (w.isVisible()) {
60 w.notifyInsetsChanged();
61 }
62 };
63
64 InsetsStateController(DisplayContent displayContent) {
65 mDisplayContent = displayContent;
66 }
67
68 /**
69 * When dispatching window state to the client, we'll need to exclude the source that represents
70 * the window that is being dispatched.
71 *
72 * @param target The client we dispatch the state to.
73 * @return The state stripped of the necessary information.
74 */
75 InsetsState getInsetsForDispatch(WindowState target) {
Jorim Jaggi956ca412019-01-07 14:49:14 +010076 final InsetsSourceProvider provider = target.getControllableInsetProvider();
Jorim Jaggif96c90a2018-09-26 16:55:15 +020077 if (provider == null) {
78 return mState;
79 }
80
81 final InsetsState state = new InsetsState();
82 state.set(mState);
83 final int type = provider.getSource().getType();
84 state.removeSource(type);
85
86 // Navigation bar doesn't get influenced by anything else
Tiger Huang332793b2019-10-29 23:21:27 +080087 if (type == ITYPE_NAVIGATION_BAR) {
88 state.removeSource(ITYPE_IME);
89 state.removeSource(ITYPE_STATUS_BAR);
Jorim Jaggif96c90a2018-09-26 16:55:15 +020090 }
91 return state;
92 }
93
Evan Rosky8d782e02019-10-14 15:43:53 -070094 InsetsState getRawInsetsState() {
95 return mState;
96 }
97
Jorim Jaggi28620472019-01-02 23:21:49 +010098 @Nullable InsetsSourceControl[] getControlsForDispatch(InsetsControlTarget target) {
99 ArrayList<Integer> controlled = mControlTargetTypeMap.get(target);
Jorim Jaggib6030952018-10-23 18:31:52 +0200100 if (controlled == null) {
101 return null;
102 }
103 final int size = controlled.size();
104 final InsetsSourceControl[] result = new InsetsSourceControl[size];
105 for (int i = 0; i < size; i++) {
Jorim Jaggia12ea562019-01-07 17:47:47 +0100106 result[i] = mProviders.get(controlled.get(i)).getControl(target);
Jorim Jaggib6030952018-10-23 18:31:52 +0200107 }
108 return result;
109 }
110
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200111 /**
112 * @return The provider of a specific type.
113 */
Tiger Huang332793b2019-10-29 23:21:27 +0800114 InsetsSourceProvider getSourceProvider(@InternalInsetsType int type) {
115 if (type == ITYPE_IME) {
Tarandeep Singh500a38f2019-09-26 13:36:40 -0700116 return mProviders.computeIfAbsent(type,
117 key -> new ImeInsetsSourceProvider(
118 mState.getSource(key), this, mDisplayContent));
119 } else {
120 return mProviders.computeIfAbsent(type,
121 key -> new InsetsSourceProvider(mState.getSource(key), this, mDisplayContent));
122 }
123 }
124
125 ImeInsetsSourceProvider getImeSourceProvider() {
Tiger Huang332793b2019-10-29 23:21:27 +0800126 return (ImeInsetsSourceProvider) getSourceProvider(ITYPE_IME);
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200127 }
128
129 /**
Jorim Jaggi956ca412019-01-07 14:49:14 +0100130 * @return The provider of a specific type or null if we don't have it.
131 */
Tiger Huang332793b2019-10-29 23:21:27 +0800132 @Nullable InsetsSourceProvider peekSourceProvider(@InternalInsetsType int type) {
Jorim Jaggi956ca412019-01-07 14:49:14 +0100133 return mProviders.get(type);
134 }
135
136 /**
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200137 * Called when a layout pass has occurred.
138 */
139 void onPostLayout() {
Tarandeep Singha6f35612019-01-11 19:50:46 -0800140 mState.setDisplayFrame(mDisplayContent.getBounds());
Jorim Jaggi28620472019-01-02 23:21:49 +0100141 for (int i = mProviders.size() - 1; i >= 0; i--) {
142 mProviders.valueAt(i).onPostLayout();
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200143 }
144 if (!mLastState.equals(mState)) {
145 mLastState.set(mState, true /* copySources */);
146 notifyInsetsChanged();
147 }
Tarandeep Singh500a38f2019-09-26 13:36:40 -0700148 getImeSourceProvider().onPostInsetsDispatched();
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200149 }
150
Evan Rosky8d782e02019-10-14 15:43:53 -0700151 void onInsetsModified(InsetsControlTarget windowState, InsetsState state) {
Jorim Jaggie35c0592018-11-06 16:21:08 +0100152 boolean changed = false;
153 for (int i = state.getSourcesCount() - 1; i >= 0; i--) {
154 final InsetsSource source = state.sourceAt(i);
Jorim Jaggi28620472019-01-02 23:21:49 +0100155 final InsetsSourceProvider provider = mProviders.get(source.getType());
Jorim Jaggie35c0592018-11-06 16:21:08 +0100156 if (provider == null) {
157 continue;
158 }
159 changed |= provider.onInsetsModified(windowState, source);
160 }
161 if (changed) {
162 notifyInsetsChanged();
163 }
164 }
165
Tiger Huang332793b2019-10-29 23:21:27 +0800166 boolean isFakeTarget(@InternalInsetsType int type, InsetsControlTarget target) {
Jorim Jaggi956ca412019-01-07 14:49:14 +0100167 return mTypeFakeControlTargetMap.get(type) == target;
168 }
169
Jorim Jaggi28620472019-01-02 23:21:49 +0100170 void onImeTargetChanged(@Nullable InsetsControlTarget imeTarget) {
Tiger Huang332793b2019-10-29 23:21:27 +0800171 onControlChanged(ITYPE_IME, imeTarget);
Jorim Jaggib6030952018-10-23 18:31:52 +0200172 notifyPendingInsetsControlChanged();
173 }
174
175 /**
Jorim Jaggi28620472019-01-02 23:21:49 +0100176 * Called when the focused window that is able to control the system bars changes.
Jorim Jaggib6030952018-10-23 18:31:52 +0200177 *
Jorim Jaggi28620472019-01-02 23:21:49 +0100178 * @param topControlling The target that is now able to control the top bar appearance
179 * and visibility.
180 * @param navControlling The target that is now able to control the nav bar appearance
181 * and visibility.
Jorim Jaggib6030952018-10-23 18:31:52 +0200182 */
Jorim Jaggi28620472019-01-02 23:21:49 +0100183 void onBarControlTargetChanged(@Nullable InsetsControlTarget topControlling,
Jorim Jaggi956ca412019-01-07 14:49:14 +0100184 @Nullable InsetsControlTarget fakeTopControlling,
185 @Nullable InsetsControlTarget navControlling,
186 @Nullable InsetsControlTarget fakeNavControlling) {
Tiger Huang332793b2019-10-29 23:21:27 +0800187 onControlChanged(ITYPE_STATUS_BAR, topControlling);
188 onControlChanged(ITYPE_NAVIGATION_BAR, navControlling);
189 onControlFakeTargetChanged(ITYPE_STATUS_BAR, fakeTopControlling);
190 onControlFakeTargetChanged(ITYPE_NAVIGATION_BAR, fakeNavControlling);
Jorim Jaggib6030952018-10-23 18:31:52 +0200191 notifyPendingInsetsControlChanged();
192 }
193
Jorim Jaggi28620472019-01-02 23:21:49 +0100194 void notifyControlRevoked(@NonNull InsetsControlTarget previousControlTarget,
Jorim Jaggib6030952018-10-23 18:31:52 +0200195 InsetsSourceProvider provider) {
Jorim Jaggia12ea562019-01-07 17:47:47 +0100196 removeFromControlMaps(previousControlTarget, provider.getSource().getType(),
197 false /* fake */);
Jorim Jaggib6030952018-10-23 18:31:52 +0200198 }
199
Tiger Huang332793b2019-10-29 23:21:27 +0800200 private void onControlChanged(@InternalInsetsType int type,
Jorim Jaggi28620472019-01-02 23:21:49 +0100201 @Nullable InsetsControlTarget target) {
202 final InsetsControlTarget previous = mTypeControlTargetMap.get(type);
203 if (target == previous) {
Jorim Jaggib6030952018-10-23 18:31:52 +0200204 return;
205 }
Tiger Huang969c6082019-12-24 20:08:57 +0800206 final InsetsSourceProvider provider = mProviders.get(type);
Jorim Jaggi28620472019-01-02 23:21:49 +0100207 if (provider == null) {
Jorim Jaggib6030952018-10-23 18:31:52 +0200208 return;
209 }
Jorim Jaggi28620472019-01-02 23:21:49 +0100210 if (!provider.isControllable()) {
Jorim Jaggia2759b22019-01-24 13:21:40 +0100211 return;
212 }
Jorim Jaggi28620472019-01-02 23:21:49 +0100213 provider.updateControlForTarget(target, false /* force */);
Tiger Huang969c6082019-12-24 20:08:57 +0800214 target = provider.getControlTarget();
Jorim Jaggib6030952018-10-23 18:31:52 +0200215 if (previous != null) {
Jorim Jaggia12ea562019-01-07 17:47:47 +0100216 removeFromControlMaps(previous, type, false /* fake */);
Jorim Jaggib6030952018-10-23 18:31:52 +0200217 mPendingControlChanged.add(previous);
218 }
Jorim Jaggi28620472019-01-02 23:21:49 +0100219 if (target != null) {
Jorim Jaggia12ea562019-01-07 17:47:47 +0100220 addToControlMaps(target, type, false /* fake */);
Jorim Jaggi28620472019-01-02 23:21:49 +0100221 mPendingControlChanged.add(target);
Jorim Jaggib6030952018-10-23 18:31:52 +0200222 }
223 }
224
Jorim Jaggia12ea562019-01-07 17:47:47 +0100225 /**
226 * The fake target saved here will be used to pretend to the app that it's still under control
227 * of the bars while it's not really, but we still need to find out the apps intentions around
228 * showing/hiding. For example, when the transient bars are showing, and the fake target
229 * requests to show system bars, the transient state will be aborted.
230 */
Tiger Huang332793b2019-10-29 23:21:27 +0800231 void onControlFakeTargetChanged(@InternalInsetsType int type,
Jorim Jaggia12ea562019-01-07 17:47:47 +0100232 @Nullable InsetsControlTarget fakeTarget) {
233 if (sNewInsetsMode != NEW_INSETS_MODE_FULL) {
234 return;
235 }
236 final InsetsControlTarget previous = mTypeFakeControlTargetMap.get(type);
237 if (fakeTarget == previous) {
238 return;
239 }
240 final InsetsSourceProvider provider = mProviders.get(type);
241 if (provider == null) {
242 return;
243 }
244 provider.updateControlForFakeTarget(fakeTarget);
245 if (previous != null) {
246 removeFromControlMaps(previous, type, true /* fake */);
247 mPendingControlChanged.add(previous);
248 }
249 if (fakeTarget != null) {
250 addToControlMaps(fakeTarget, type, true /* fake */);
251 mPendingControlChanged.add(fakeTarget);
252 }
253 }
254
Jorim Jaggi28620472019-01-02 23:21:49 +0100255 private void removeFromControlMaps(@NonNull InsetsControlTarget target,
Tiger Huang332793b2019-10-29 23:21:27 +0800256 @InternalInsetsType int type, boolean fake) {
Jorim Jaggi28620472019-01-02 23:21:49 +0100257 final ArrayList<Integer> array = mControlTargetTypeMap.get(target);
Jorim Jaggib6030952018-10-23 18:31:52 +0200258 if (array == null) {
259 return;
260 }
261 array.remove((Integer) type);
262 if (array.isEmpty()) {
Jorim Jaggi28620472019-01-02 23:21:49 +0100263 mControlTargetTypeMap.remove(target);
Jorim Jaggib6030952018-10-23 18:31:52 +0200264 }
Jorim Jaggia12ea562019-01-07 17:47:47 +0100265 if (fake) {
266 mTypeFakeControlTargetMap.remove(type);
267 } else {
268 mTypeControlTargetMap.remove(type);
269 }
Jorim Jaggib6030952018-10-23 18:31:52 +0200270 }
271
Jorim Jaggi28620472019-01-02 23:21:49 +0100272 private void addToControlMaps(@NonNull InsetsControlTarget target,
Tiger Huang332793b2019-10-29 23:21:27 +0800273 @InternalInsetsType int type, boolean fake) {
Jorim Jaggi28620472019-01-02 23:21:49 +0100274 final ArrayList<Integer> array = mControlTargetTypeMap.computeIfAbsent(target,
Jorim Jaggib6030952018-10-23 18:31:52 +0200275 key -> new ArrayList<>());
276 array.add(type);
Jorim Jaggia12ea562019-01-07 17:47:47 +0100277 if (fake) {
278 mTypeFakeControlTargetMap.put(type, target);
279 } else {
280 mTypeControlTargetMap.put(type, target);
281 }
Jorim Jaggib6030952018-10-23 18:31:52 +0200282 }
283
Jorim Jaggi28620472019-01-02 23:21:49 +0100284 void notifyControlChanged(InsetsControlTarget target) {
Tarandeep Singh215929b2019-01-11 18:24:37 -0800285 mPendingControlChanged.add(target);
286 notifyPendingInsetsControlChanged();
287 }
288
Jorim Jaggib6030952018-10-23 18:31:52 +0200289 private void notifyPendingInsetsControlChanged() {
Jorim Jaggif86eb492019-01-09 17:37:08 +0100290 if (mPendingControlChanged.isEmpty()) {
291 return;
292 }
Jorim Jaggib6030952018-10-23 18:31:52 +0200293 mDisplayContent.mWmService.mAnimator.addAfterPrepareSurfacesRunnable(() -> {
294 for (int i = mPendingControlChanged.size() - 1; i >= 0; i--) {
Jorim Jaggi28620472019-01-02 23:21:49 +0100295 final InsetsControlTarget controlTarget = mPendingControlChanged.valueAt(i);
296 controlTarget.notifyInsetsControlChanged();
Jorim Jaggib6030952018-10-23 18:31:52 +0200297 }
298 mPendingControlChanged.clear();
299 });
300 }
301
Jorim Jaggi956ca412019-01-07 14:49:14 +0100302 void notifyInsetsChanged() {
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200303 mDisplayContent.forAllWindows(mDispatchInsetsChanged, true /* traverseTopToBottom */);
Evan Rosky8d782e02019-10-14 15:43:53 -0700304 if (mDisplayContent.mRemoteInsetsControlTarget != null) {
305 mDisplayContent.mRemoteInsetsControlTarget.notifyInsetsChanged();
306 }
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200307 }
308
309 void dump(String prefix, PrintWriter pw) {
310 pw.println(prefix + "WindowInsetsStateController");
311 mState.dump(prefix + " ", pw);
Jorim Jaggicfd6f3b2018-11-07 15:30:18 +0100312 pw.println(prefix + " " + "Control map:");
Jorim Jaggi28620472019-01-02 23:21:49 +0100313 for (int i = mTypeControlTargetMap.size() - 1; i >= 0; i--) {
Jorim Jaggicfd6f3b2018-11-07 15:30:18 +0100314 pw.print(prefix + " ");
Jorim Jaggi28620472019-01-02 23:21:49 +0100315 pw.println(InsetsState.typeToString(mTypeControlTargetMap.keyAt(i)) + " -> "
316 + mTypeControlTargetMap.valueAt(i));
Jorim Jaggicfd6f3b2018-11-07 15:30:18 +0100317 }
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200318 }
319}