blob: e0554242fb29d18459577b7959b5b8217eb967f1 [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
19import static android.view.InsetsState.TYPE_IME;
20import static android.view.InsetsState.TYPE_NAVIGATION_BAR;
21import static android.view.InsetsState.TYPE_TOP_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;
Jorim Jaggi956ca412019-01-07 14:49:14 +010033import android.view.InsetsState.InternalInsetType;
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
87 if (type == TYPE_NAVIGATION_BAR) {
88 state.removeSource(TYPE_IME);
89 state.removeSource(TYPE_TOP_BAR);
90 }
91 return state;
92 }
93
Jorim Jaggi28620472019-01-02 23:21:49 +010094 @Nullable InsetsSourceControl[] getControlsForDispatch(InsetsControlTarget target) {
95 ArrayList<Integer> controlled = mControlTargetTypeMap.get(target);
Jorim Jaggib6030952018-10-23 18:31:52 +020096 if (controlled == null) {
97 return null;
98 }
99 final int size = controlled.size();
100 final InsetsSourceControl[] result = new InsetsSourceControl[size];
101 for (int i = 0; i < size; i++) {
Jorim Jaggia12ea562019-01-07 17:47:47 +0100102 result[i] = mProviders.get(controlled.get(i)).getControl(target);
Jorim Jaggib6030952018-10-23 18:31:52 +0200103 }
104 return result;
105 }
106
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200107 /**
108 * @return The provider of a specific type.
109 */
Jorim Jaggi28620472019-01-02 23:21:49 +0100110 InsetsSourceProvider getSourceProvider(@InternalInsetType int type) {
Tarandeep Singh500a38f2019-09-26 13:36:40 -0700111 if (type == TYPE_IME) {
112 return mProviders.computeIfAbsent(type,
113 key -> new ImeInsetsSourceProvider(
114 mState.getSource(key), this, mDisplayContent));
115 } else {
116 return mProviders.computeIfAbsent(type,
117 key -> new InsetsSourceProvider(mState.getSource(key), this, mDisplayContent));
118 }
119 }
120
121 ImeInsetsSourceProvider getImeSourceProvider() {
122 return (ImeInsetsSourceProvider) getSourceProvider(TYPE_IME);
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200123 }
124
125 /**
Jorim Jaggi956ca412019-01-07 14:49:14 +0100126 * @return The provider of a specific type or null if we don't have it.
127 */
128 @Nullable InsetsSourceProvider peekSourceProvider(@InternalInsetType int type) {
129 return mProviders.get(type);
130 }
131
132 /**
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200133 * Called when a layout pass has occurred.
134 */
135 void onPostLayout() {
Tarandeep Singha6f35612019-01-11 19:50:46 -0800136 mState.setDisplayFrame(mDisplayContent.getBounds());
Jorim Jaggi28620472019-01-02 23:21:49 +0100137 for (int i = mProviders.size() - 1; i >= 0; i--) {
138 mProviders.valueAt(i).onPostLayout();
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200139 }
140 if (!mLastState.equals(mState)) {
141 mLastState.set(mState, true /* copySources */);
142 notifyInsetsChanged();
143 }
Tarandeep Singh500a38f2019-09-26 13:36:40 -0700144 getImeSourceProvider().onPostInsetsDispatched();
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200145 }
146
Jorim Jaggie35c0592018-11-06 16:21:08 +0100147 void onInsetsModified(WindowState windowState, InsetsState state) {
148 boolean changed = false;
149 for (int i = state.getSourcesCount() - 1; i >= 0; i--) {
150 final InsetsSource source = state.sourceAt(i);
Jorim Jaggi28620472019-01-02 23:21:49 +0100151 final InsetsSourceProvider provider = mProviders.get(source.getType());
Jorim Jaggie35c0592018-11-06 16:21:08 +0100152 if (provider == null) {
153 continue;
154 }
155 changed |= provider.onInsetsModified(windowState, source);
156 }
157 if (changed) {
158 notifyInsetsChanged();
159 }
160 }
161
Jorim Jaggi956ca412019-01-07 14:49:14 +0100162 boolean isFakeTarget(@InternalInsetType int type, InsetsControlTarget target) {
163 return mTypeFakeControlTargetMap.get(type) == target;
164 }
165
Jorim Jaggi28620472019-01-02 23:21:49 +0100166 void onImeTargetChanged(@Nullable InsetsControlTarget imeTarget) {
Jorim Jaggib6030952018-10-23 18:31:52 +0200167 onControlChanged(TYPE_IME, imeTarget);
168 notifyPendingInsetsControlChanged();
169 }
170
171 /**
Jorim Jaggi28620472019-01-02 23:21:49 +0100172 * Called when the focused window that is able to control the system bars changes.
Jorim Jaggib6030952018-10-23 18:31:52 +0200173 *
Jorim Jaggi28620472019-01-02 23:21:49 +0100174 * @param topControlling The target that is now able to control the top bar appearance
175 * and visibility.
176 * @param navControlling The target that is now able to control the nav bar appearance
177 * and visibility.
Jorim Jaggib6030952018-10-23 18:31:52 +0200178 */
Jorim Jaggi28620472019-01-02 23:21:49 +0100179 void onBarControlTargetChanged(@Nullable InsetsControlTarget topControlling,
Jorim Jaggi956ca412019-01-07 14:49:14 +0100180 @Nullable InsetsControlTarget fakeTopControlling,
181 @Nullable InsetsControlTarget navControlling,
182 @Nullable InsetsControlTarget fakeNavControlling) {
Jorim Jaggi28620472019-01-02 23:21:49 +0100183 onControlChanged(TYPE_TOP_BAR, topControlling);
184 onControlChanged(TYPE_NAVIGATION_BAR, navControlling);
Jorim Jaggi956ca412019-01-07 14:49:14 +0100185 onControlFakeTargetChanged(TYPE_TOP_BAR, fakeTopControlling);
186 onControlFakeTargetChanged(TYPE_NAVIGATION_BAR, fakeNavControlling);
Jorim Jaggib6030952018-10-23 18:31:52 +0200187 notifyPendingInsetsControlChanged();
188 }
189
Jorim Jaggi28620472019-01-02 23:21:49 +0100190 void notifyControlRevoked(@NonNull InsetsControlTarget previousControlTarget,
Jorim Jaggib6030952018-10-23 18:31:52 +0200191 InsetsSourceProvider provider) {
Jorim Jaggia12ea562019-01-07 17:47:47 +0100192 removeFromControlMaps(previousControlTarget, provider.getSource().getType(),
193 false /* fake */);
Jorim Jaggib6030952018-10-23 18:31:52 +0200194 }
195
Jorim Jaggi28620472019-01-02 23:21:49 +0100196 private void onControlChanged(@InternalInsetType int type,
197 @Nullable InsetsControlTarget target) {
198 final InsetsControlTarget previous = mTypeControlTargetMap.get(type);
199 if (target == previous) {
Jorim Jaggib6030952018-10-23 18:31:52 +0200200 return;
201 }
Jorim Jaggi28620472019-01-02 23:21:49 +0100202 final InsetsSourceProvider provider = getSourceProvider(type);
203 if (provider == null) {
Jorim Jaggib6030952018-10-23 18:31:52 +0200204 return;
205 }
Jorim Jaggi28620472019-01-02 23:21:49 +0100206 if (!provider.isControllable()) {
Jorim Jaggia2759b22019-01-24 13:21:40 +0100207 return;
208 }
Jorim Jaggi28620472019-01-02 23:21:49 +0100209 provider.updateControlForTarget(target, false /* force */);
Jorim Jaggib6030952018-10-23 18:31:52 +0200210 if (previous != null) {
Jorim Jaggia12ea562019-01-07 17:47:47 +0100211 removeFromControlMaps(previous, type, false /* fake */);
Jorim Jaggib6030952018-10-23 18:31:52 +0200212 mPendingControlChanged.add(previous);
213 }
Jorim Jaggi28620472019-01-02 23:21:49 +0100214 if (target != null) {
Jorim Jaggia12ea562019-01-07 17:47:47 +0100215 addToControlMaps(target, type, false /* fake */);
Jorim Jaggi28620472019-01-02 23:21:49 +0100216 mPendingControlChanged.add(target);
Jorim Jaggib6030952018-10-23 18:31:52 +0200217 }
218 }
219
Jorim Jaggia12ea562019-01-07 17:47:47 +0100220 /**
221 * The fake target saved here will be used to pretend to the app that it's still under control
222 * of the bars while it's not really, but we still need to find out the apps intentions around
223 * showing/hiding. For example, when the transient bars are showing, and the fake target
224 * requests to show system bars, the transient state will be aborted.
225 */
226 void onControlFakeTargetChanged(@InternalInsetType int type,
227 @Nullable InsetsControlTarget fakeTarget) {
228 if (sNewInsetsMode != NEW_INSETS_MODE_FULL) {
229 return;
230 }
231 final InsetsControlTarget previous = mTypeFakeControlTargetMap.get(type);
232 if (fakeTarget == previous) {
233 return;
234 }
235 final InsetsSourceProvider provider = mProviders.get(type);
236 if (provider == null) {
237 return;
238 }
239 provider.updateControlForFakeTarget(fakeTarget);
240 if (previous != null) {
241 removeFromControlMaps(previous, type, true /* fake */);
242 mPendingControlChanged.add(previous);
243 }
244 if (fakeTarget != null) {
245 addToControlMaps(fakeTarget, type, true /* fake */);
246 mPendingControlChanged.add(fakeTarget);
247 }
248 }
249
Jorim Jaggi28620472019-01-02 23:21:49 +0100250 private void removeFromControlMaps(@NonNull InsetsControlTarget target,
Jorim Jaggia12ea562019-01-07 17:47:47 +0100251 @InternalInsetType int type, boolean fake) {
Jorim Jaggi28620472019-01-02 23:21:49 +0100252 final ArrayList<Integer> array = mControlTargetTypeMap.get(target);
Jorim Jaggib6030952018-10-23 18:31:52 +0200253 if (array == null) {
254 return;
255 }
256 array.remove((Integer) type);
257 if (array.isEmpty()) {
Jorim Jaggi28620472019-01-02 23:21:49 +0100258 mControlTargetTypeMap.remove(target);
Jorim Jaggib6030952018-10-23 18:31:52 +0200259 }
Jorim Jaggia12ea562019-01-07 17:47:47 +0100260 if (fake) {
261 mTypeFakeControlTargetMap.remove(type);
262 } else {
263 mTypeControlTargetMap.remove(type);
264 }
Jorim Jaggib6030952018-10-23 18:31:52 +0200265 }
266
Jorim Jaggi28620472019-01-02 23:21:49 +0100267 private void addToControlMaps(@NonNull InsetsControlTarget target,
Jorim Jaggia12ea562019-01-07 17:47:47 +0100268 @InternalInsetType int type, boolean fake) {
Jorim Jaggi28620472019-01-02 23:21:49 +0100269 final ArrayList<Integer> array = mControlTargetTypeMap.computeIfAbsent(target,
Jorim Jaggib6030952018-10-23 18:31:52 +0200270 key -> new ArrayList<>());
271 array.add(type);
Jorim Jaggia12ea562019-01-07 17:47:47 +0100272 if (fake) {
273 mTypeFakeControlTargetMap.put(type, target);
274 } else {
275 mTypeControlTargetMap.put(type, target);
276 }
Jorim Jaggib6030952018-10-23 18:31:52 +0200277 }
278
Jorim Jaggi28620472019-01-02 23:21:49 +0100279 void notifyControlChanged(InsetsControlTarget target) {
Tarandeep Singh215929b2019-01-11 18:24:37 -0800280 mPendingControlChanged.add(target);
281 notifyPendingInsetsControlChanged();
282 }
283
Jorim Jaggib6030952018-10-23 18:31:52 +0200284 private void notifyPendingInsetsControlChanged() {
Jorim Jaggif86eb492019-01-09 17:37:08 +0100285 if (mPendingControlChanged.isEmpty()) {
286 return;
287 }
Jorim Jaggib6030952018-10-23 18:31:52 +0200288 mDisplayContent.mWmService.mAnimator.addAfterPrepareSurfacesRunnable(() -> {
289 for (int i = mPendingControlChanged.size() - 1; i >= 0; i--) {
Jorim Jaggi28620472019-01-02 23:21:49 +0100290 final InsetsControlTarget controlTarget = mPendingControlChanged.valueAt(i);
291 controlTarget.notifyInsetsControlChanged();
Jorim Jaggib6030952018-10-23 18:31:52 +0200292 }
293 mPendingControlChanged.clear();
294 });
295 }
296
Jorim Jaggi956ca412019-01-07 14:49:14 +0100297 void notifyInsetsChanged() {
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200298 mDisplayContent.forAllWindows(mDispatchInsetsChanged, true /* traverseTopToBottom */);
299 }
300
301 void dump(String prefix, PrintWriter pw) {
302 pw.println(prefix + "WindowInsetsStateController");
303 mState.dump(prefix + " ", pw);
Jorim Jaggicfd6f3b2018-11-07 15:30:18 +0100304 pw.println(prefix + " " + "Control map:");
Jorim Jaggi28620472019-01-02 23:21:49 +0100305 for (int i = mTypeControlTargetMap.size() - 1; i >= 0; i--) {
Jorim Jaggicfd6f3b2018-11-07 15:30:18 +0100306 pw.print(prefix + " ");
Jorim Jaggi28620472019-01-02 23:21:49 +0100307 pw.println(InsetsState.typeToString(mTypeControlTargetMap.keyAt(i)) + " -> "
308 + mTypeControlTargetMap.valueAt(i));
Jorim Jaggicfd6f3b2018-11-07 15:30:18 +0100309 }
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200310 }
311}