blob: 332573449e18a495a3e9c2bd185c3369f78e16bd [file] [log] [blame]
Jorim Jaggib6030952018-10-23 18:31:52 +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 android.view;
18
Tarandeep Singhb9538cd2020-02-20 17:51:18 -080019import static android.view.InsetsController.AnimationType;
Jorim Jaggi3182ef12020-01-30 00:16:18 +010020import static android.view.InsetsState.toPublicType;
Jorim Jaggi1f2c7eb2020-01-08 00:07:13 +010021
Tarandeep Singh46d59f02019-01-29 18:09:15 -080022import android.annotation.IntDef;
Jorim Jaggib6030952018-10-23 18:31:52 +020023import android.annotation.Nullable;
Tiger Huang332793b2019-10-29 23:21:27 +080024import android.view.InsetsState.InternalInsetsType;
Tarandeep Singh22f2b4c2019-01-10 19:41:30 -080025import android.view.SurfaceControl.Transaction;
Jorim Jaggi3182ef12020-01-30 00:16:18 +010026import android.view.WindowInsets.Type.InsetsType;
Jorim Jaggib6030952018-10-23 18:31:52 +020027
28import com.android.internal.annotations.VisibleForTesting;
29
Tarandeep Singh46d59f02019-01-29 18:09:15 -080030import java.lang.annotation.Retention;
31import java.lang.annotation.RetentionPolicy;
Jorim Jaggib6030952018-10-23 18:31:52 +020032import java.util.function.Supplier;
33
34/**
35 * Controls the visibility and animations of a single window insets source.
36 * @hide
37 */
38public class InsetsSourceConsumer {
39
Tarandeep Singh46d59f02019-01-29 18:09:15 -080040 @Retention(RetentionPolicy.SOURCE)
Jorim Jaggid7f10ed2020-01-08 21:41:55 +010041 @IntDef(value = {ShowResult.SHOW_IMMEDIATELY, ShowResult.IME_SHOW_DELAYED, ShowResult.IME_SHOW_FAILED})
Tarandeep Singh46d59f02019-01-29 18:09:15 -080042 @interface ShowResult {
43 /**
44 * Window type is ready to be shown, will be shown immidiately.
45 */
46 int SHOW_IMMEDIATELY = 0;
47 /**
48 * Result will be delayed. Window needs to be prepared or request is not from controller.
49 * Request will be delegated to controller and may or may not be shown.
50 */
Jorim Jaggid7f10ed2020-01-08 21:41:55 +010051 int IME_SHOW_DELAYED = 1;
Tarandeep Singh46d59f02019-01-29 18:09:15 -080052 /**
53 * Window will not be shown because one of the conditions couldn't be met.
54 * (e.g. in IME's case, when no editor is focused.)
55 */
Jorim Jaggid7f10ed2020-01-08 21:41:55 +010056 int IME_SHOW_FAILED = 2;
Tarandeep Singh46d59f02019-01-29 18:09:15 -080057 }
58
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -080059 protected final InsetsController mController;
Jorim Jaggia51168a2019-12-27 15:17:44 +010060 protected boolean mRequestedVisible;
Tarandeep Singhb9538cd2020-02-20 17:51:18 -080061 protected final InsetsState mState;
62 protected final @InternalInsetsType int mType;
63
Jorim Jaggib6030952018-10-23 18:31:52 +020064 private final Supplier<Transaction> mTransactionSupplier;
Jorim Jaggic8d60382018-10-31 17:06:06 +010065 private @Nullable InsetsSourceControl mSourceControl;
Tarandeep Singh92d2dd32019-08-07 14:45:01 -070066 private boolean mHasWindowFocus;
Jorim Jaggib6030952018-10-23 18:31:52 +020067
Tiger Huang332793b2019-10-29 23:21:27 +080068 public InsetsSourceConsumer(@InternalInsetsType int type, InsetsState state,
Jorim Jaggic8d60382018-10-31 17:06:06 +010069 Supplier<Transaction> transactionSupplier, InsetsController controller) {
Jorim Jaggib6030952018-10-23 18:31:52 +020070 mType = type;
71 mState = state;
72 mTransactionSupplier = transactionSupplier;
Jorim Jaggic8d60382018-10-31 17:06:06 +010073 mController = controller;
Jorim Jaggia51168a2019-12-27 15:17:44 +010074 mRequestedVisible = InsetsState.getDefaultVisibility(type);
Jorim Jaggib6030952018-10-23 18:31:52 +020075 }
76
Jorim Jaggi3182ef12020-01-30 00:16:18 +010077 /**
78 * Updates the control delivered from the server.
79
80 * @param showTypes An integer array with a single entry that determines which types a show
81 * animation should be run after setting the control.
82 * @param hideTypes An integer array with a single entry that determines which types a hide
83 * animation should be run after setting the control.
84 */
85 public void setControl(@Nullable InsetsSourceControl control,
86 @InsetsType int[] showTypes, @InsetsType int[] hideTypes) {
Jorim Jaggic8d60382018-10-31 17:06:06 +010087 if (mSourceControl == control) {
Jorim Jaggib6030952018-10-23 18:31:52 +020088 return;
89 }
Rob Carred48db32020-03-05 13:31:01 -080090 final InsetsSourceControl lastControl = mSourceControl;
Jorim Jaggic8d60382018-10-31 17:06:06 +010091 mSourceControl = control;
Jorim Jaggi3182ef12020-01-30 00:16:18 +010092
93 // We are loosing control
94 if (mSourceControl == null) {
95 mController.notifyControlRevoked(this);
96
97 // Restore server visibility.
98 mState.getSource(getType()).setVisible(
99 mController.getLastDispatchedState().getSource(getType()).isVisible());
100 applyLocalVisibilityOverride();
Rob Carred48db32020-03-05 13:31:01 -0800101 } else {
102 // We are gaining control, and need to run an animation since previous state
103 // didn't match
104 if (mRequestedVisible != mState.getSource(mType).isVisible()) {
105 if (mRequestedVisible) {
106 showTypes[0] |= toPublicType(getType());
107 } else {
108 hideTypes[0] |= toPublicType(getType());
109 }
Jorim Jaggi3182ef12020-01-30 00:16:18 +0100110 } else {
Rob Carred48db32020-03-05 13:31:01 -0800111 // We are gaining control, but don't need to run an animation.
112 // However make sure that the leash visibility is still up to date.
113 if (applyLocalVisibilityOverride()) {
114 mController.notifyVisibilityChanged();
115 }
116 applyHiddenToControl();
Jorim Jaggi3182ef12020-01-30 00:16:18 +0100117 }
Jorim Jaggi3182ef12020-01-30 00:16:18 +0100118 }
Rob Carred48db32020-03-05 13:31:01 -0800119 if (lastControl != null) {
Jorim Jaggi6d5c8012020-02-28 01:40:27 +0100120 lastControl.release(mController::releaseSurfaceControlFromRt);
Jorim Jaggie35c0592018-11-06 16:21:08 +0100121 }
Jorim Jaggib6030952018-10-23 18:31:52 +0200122 }
123
124 @VisibleForTesting
125 public InsetsSourceControl getControl() {
Jorim Jaggic8d60382018-10-31 17:06:06 +0100126 return mSourceControl;
Jorim Jaggib6030952018-10-23 18:31:52 +0200127 }
128
129 int getType() {
130 return mType;
131 }
132
133 @VisibleForTesting
Jorim Jaggi3182ef12020-01-30 00:16:18 +0100134 public void show(boolean fromIme) {
Jorim Jaggia51168a2019-12-27 15:17:44 +0100135 setRequestedVisible(true);
Jorim Jaggib6030952018-10-23 18:31:52 +0200136 }
137
138 @VisibleForTesting
139 public void hide() {
Jorim Jaggia51168a2019-12-27 15:17:44 +0100140 setRequestedVisible(false);
Jorim Jaggib6030952018-10-23 18:31:52 +0200141 }
142
Tarandeep Singhb9538cd2020-02-20 17:51:18 -0800143 void hide(boolean animationFinished, @AnimationType int animationType) {
Tarandeep Singh94c9a832020-02-03 14:55:30 -0800144 hide();
145 }
146
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -0800147 /**
148 * Called when current window gains focus
149 */
Tarandeep Singh92d2dd32019-08-07 14:45:01 -0700150 public void onWindowFocusGained() {
151 mHasWindowFocus = true;
152 }
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -0800153
154 /**
155 * Called when current window loses focus.
156 */
Tarandeep Singh92d2dd32019-08-07 14:45:01 -0700157 public void onWindowFocusLost() {
158 mHasWindowFocus = false;
159 }
160
161 boolean hasWindowFocus() {
162 return mHasWindowFocus;
163 }
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -0800164
Jorim Jaggie35c0592018-11-06 16:21:08 +0100165 boolean applyLocalVisibilityOverride() {
Jorim Jaggi580aef52020-02-26 18:28:28 +0100166 InsetsSource source = mState.peekSource(mType);
167 final boolean isVisible = source != null && source.isVisible();
Tiger Huang41ae5e82020-01-17 18:17:20 +0800168 final boolean hasControl = mSourceControl != null;
169
170 // We still need to let the legacy app know the visibility change even if we don't have the
171 // control.
172 mController.updateCompatSysUiVisibility(
173 mType, hasControl ? mRequestedVisible : isVisible, hasControl);
Jorim Jaggic8d60382018-10-31 17:06:06 +0100174
175 // If we don't have control, we are not able to change the visibility.
Tiger Huang41ae5e82020-01-17 18:17:20 +0800176 if (!hasControl) {
Jorim Jaggie35c0592018-11-06 16:21:08 +0100177 return false;
Jorim Jaggic8d60382018-10-31 17:06:06 +0100178 }
Tiger Huang41ae5e82020-01-17 18:17:20 +0800179 if (isVisible == mRequestedVisible) {
Jorim Jaggie35c0592018-11-06 16:21:08 +0100180 return false;
181 }
Jorim Jaggia51168a2019-12-27 15:17:44 +0100182 mState.getSource(mType).setVisible(mRequestedVisible);
Jorim Jaggie35c0592018-11-06 16:21:08 +0100183 return true;
Jorim Jaggic8d60382018-10-31 17:06:06 +0100184 }
185
Tarandeep Singh22f2b4c2019-01-10 19:41:30 -0800186 @VisibleForTesting
Jorim Jaggia51168a2019-12-27 15:17:44 +0100187 public boolean isRequestedVisible() {
188 return mRequestedVisible;
Tarandeep Singh22f2b4c2019-01-10 19:41:30 -0800189 }
190
Tarandeep Singh46d59f02019-01-29 18:09:15 -0800191 /**
192 * Request to show current window type.
193 *
194 * @param fromController {@code true} if request is coming from controller.
195 * (e.g. in IME case, controller is
196 * {@link android.inputmethodservice.InputMethodService}).
197 * @return @see {@link ShowResult}.
198 */
Jorim Jaggid7f10ed2020-01-08 21:41:55 +0100199 @VisibleForTesting
200 public @ShowResult int requestShow(boolean fromController) {
Tarandeep Singh46d59f02019-01-29 18:09:15 -0800201 return ShowResult.SHOW_IMMEDIATELY;
202 }
203
204 /**
205 * Notify listeners that window is now hidden.
206 */
207 void notifyHidden() {
208 // no-op for types that always return ShowResult#SHOW_IMMEDIATELY.
209 }
210
Jorim Jaggia51168a2019-12-27 15:17:44 +0100211 /**
Tarandeep Singh94c9a832020-02-03 14:55:30 -0800212 * Remove surface on which this consumer type is drawn.
213 */
214 public void removeSurface() {
215 // no-op for types that always return ShowResult#SHOW_IMMEDIATELY.
216 }
217
218 /**
Jorim Jaggia51168a2019-12-27 15:17:44 +0100219 * Sets requested visibility from the client, regardless of whether we are able to control it at
220 * the moment.
221 */
222 private void setRequestedVisible(boolean requestedVisible) {
Jorim Jaggia51168a2019-12-27 15:17:44 +0100223 mRequestedVisible = requestedVisible;
Jorim Jaggi3182ef12020-01-30 00:16:18 +0100224 if (applyLocalVisibilityOverride()) {
225 mController.notifyVisibilityChanged();
226 }
Jorim Jaggib6030952018-10-23 18:31:52 +0200227 }
228
229 private void applyHiddenToControl() {
Jorim Jaggi026ed752020-01-29 00:30:24 +0100230 if (mSourceControl == null || mSourceControl.getLeash() == null) {
Jorim Jaggib6030952018-10-23 18:31:52 +0200231 return;
232 }
233
Jorim Jaggib6030952018-10-23 18:31:52 +0200234 final Transaction t = mTransactionSupplier.get();
Jorim Jaggia51168a2019-12-27 15:17:44 +0100235 if (mRequestedVisible) {
Jorim Jaggic8d60382018-10-31 17:06:06 +0100236 t.show(mSourceControl.getLeash());
Jorim Jaggie35c0592018-11-06 16:21:08 +0100237 } else {
238 t.hide(mSourceControl.getLeash());
Jorim Jaggib6030952018-10-23 18:31:52 +0200239 }
240 t.apply();
241 }
242}