blob: 1383463ef72fa789a8f6e85b079536f10a9b167c [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 Singh46d59f02019-01-29 18:09:15 -080019import android.annotation.IntDef;
Jorim Jaggib6030952018-10-23 18:31:52 +020020import android.annotation.Nullable;
Jorim Jaggib6030952018-10-23 18:31:52 +020021import android.view.InsetsState.InternalInsetType;
Tarandeep Singh22f2b4c2019-01-10 19:41:30 -080022import android.view.SurfaceControl.Transaction;
Jorim Jaggib6030952018-10-23 18:31:52 +020023
24import com.android.internal.annotations.VisibleForTesting;
25
Tarandeep Singh46d59f02019-01-29 18:09:15 -080026import java.lang.annotation.Retention;
27import java.lang.annotation.RetentionPolicy;
Jorim Jaggib6030952018-10-23 18:31:52 +020028import java.util.function.Supplier;
29
30/**
31 * Controls the visibility and animations of a single window insets source.
32 * @hide
33 */
34public class InsetsSourceConsumer {
35
Tarandeep Singh46d59f02019-01-29 18:09:15 -080036 @Retention(RetentionPolicy.SOURCE)
37 @IntDef(value = {ShowResult.SHOW_IMMEDIATELY, ShowResult.SHOW_DELAYED, ShowResult.SHOW_FAILED})
38 @interface ShowResult {
39 /**
40 * Window type is ready to be shown, will be shown immidiately.
41 */
42 int SHOW_IMMEDIATELY = 0;
43 /**
44 * Result will be delayed. Window needs to be prepared or request is not from controller.
45 * Request will be delegated to controller and may or may not be shown.
46 */
47 int SHOW_DELAYED = 1;
48 /**
49 * Window will not be shown because one of the conditions couldn't be met.
50 * (e.g. in IME's case, when no editor is focused.)
51 */
52 int SHOW_FAILED = 2;
53 }
54
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -080055 protected final InsetsController mController;
56 protected boolean mVisible;
Jorim Jaggib6030952018-10-23 18:31:52 +020057 private final Supplier<Transaction> mTransactionSupplier;
58 private final @InternalInsetType int mType;
59 private final InsetsState mState;
Jorim Jaggic8d60382018-10-31 17:06:06 +010060 private @Nullable InsetsSourceControl mSourceControl;
Jorim Jaggib6030952018-10-23 18:31:52 +020061
62 public InsetsSourceConsumer(@InternalInsetType int type, InsetsState state,
Jorim Jaggic8d60382018-10-31 17:06:06 +010063 Supplier<Transaction> transactionSupplier, InsetsController controller) {
Jorim Jaggib6030952018-10-23 18:31:52 +020064 mType = type;
65 mState = state;
66 mTransactionSupplier = transactionSupplier;
Jorim Jaggic8d60382018-10-31 17:06:06 +010067 mController = controller;
Jorim Jaggid89efeb2019-01-22 17:48:34 +010068 mVisible = InsetsState.getDefaultVisibility(type);
Jorim Jaggib6030952018-10-23 18:31:52 +020069 }
70
71 public void setControl(@Nullable InsetsSourceControl control) {
Jorim Jaggic8d60382018-10-31 17:06:06 +010072 if (mSourceControl == control) {
Jorim Jaggib6030952018-10-23 18:31:52 +020073 return;
74 }
Jorim Jaggic8d60382018-10-31 17:06:06 +010075 mSourceControl = control;
Jorim Jaggib6030952018-10-23 18:31:52 +020076 applyHiddenToControl();
Jorim Jaggie35c0592018-11-06 16:21:08 +010077 if (applyLocalVisibilityOverride()) {
78 mController.notifyVisibilityChanged();
79 }
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +010080 if (mSourceControl == null) {
81 mController.notifyControlRevoked(this);
82 }
Jorim Jaggib6030952018-10-23 18:31:52 +020083 }
84
85 @VisibleForTesting
86 public InsetsSourceControl getControl() {
Jorim Jaggic8d60382018-10-31 17:06:06 +010087 return mSourceControl;
Jorim Jaggib6030952018-10-23 18:31:52 +020088 }
89
90 int getType() {
91 return mType;
92 }
93
94 @VisibleForTesting
95 public void show() {
Jorim Jaggie35c0592018-11-06 16:21:08 +010096 setVisible(true);
Jorim Jaggib6030952018-10-23 18:31:52 +020097 }
98
99 @VisibleForTesting
100 public void hide() {
Jorim Jaggie35c0592018-11-06 16:21:08 +0100101 setVisible(false);
Jorim Jaggib6030952018-10-23 18:31:52 +0200102 }
103
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -0800104 /**
105 * Called when current window gains focus
106 */
107 public void onWindowFocusGained() {}
108
109 /**
110 * Called when current window loses focus.
111 */
112 public void onWindowFocusLost() {}
113
Jorim Jaggie35c0592018-11-06 16:21:08 +0100114 boolean applyLocalVisibilityOverride() {
Jorim Jaggic8d60382018-10-31 17:06:06 +0100115
116 // If we don't have control, we are not able to change the visibility.
117 if (mSourceControl == null) {
Jorim Jaggie35c0592018-11-06 16:21:08 +0100118 return false;
Jorim Jaggic8d60382018-10-31 17:06:06 +0100119 }
Jorim Jaggie35c0592018-11-06 16:21:08 +0100120 if (mState.getSource(mType).isVisible() == mVisible) {
121 return false;
122 }
123 mState.getSource(mType).setVisible(mVisible);
124 return true;
Jorim Jaggic8d60382018-10-31 17:06:06 +0100125 }
126
Tarandeep Singh22f2b4c2019-01-10 19:41:30 -0800127 @VisibleForTesting
128 public boolean isVisible() {
129 return mVisible;
130 }
131
Tarandeep Singh46d59f02019-01-29 18:09:15 -0800132 /**
133 * Request to show current window type.
134 *
135 * @param fromController {@code true} if request is coming from controller.
136 * (e.g. in IME case, controller is
137 * {@link android.inputmethodservice.InputMethodService}).
138 * @return @see {@link ShowResult}.
139 */
140 @ShowResult int requestShow(boolean fromController) {
141 return ShowResult.SHOW_IMMEDIATELY;
142 }
143
144 /**
145 * Notify listeners that window is now hidden.
146 */
147 void notifyHidden() {
148 // no-op for types that always return ShowResult#SHOW_IMMEDIATELY.
149 }
150
Jorim Jaggie35c0592018-11-06 16:21:08 +0100151 private void setVisible(boolean visible) {
152 if (mVisible == visible) {
Jorim Jaggib6030952018-10-23 18:31:52 +0200153 return;
154 }
Jorim Jaggie35c0592018-11-06 16:21:08 +0100155 mVisible = visible;
Jorim Jaggic8d60382018-10-31 17:06:06 +0100156 applyLocalVisibilityOverride();
157 mController.notifyVisibilityChanged();
Jorim Jaggib6030952018-10-23 18:31:52 +0200158 }
159
160 private void applyHiddenToControl() {
Jorim Jaggic8d60382018-10-31 17:06:06 +0100161 if (mSourceControl == null) {
Jorim Jaggib6030952018-10-23 18:31:52 +0200162 return;
163 }
164
Jorim Jaggib6030952018-10-23 18:31:52 +0200165 final Transaction t = mTransactionSupplier.get();
Jorim Jaggie35c0592018-11-06 16:21:08 +0100166 if (mVisible) {
Jorim Jaggic8d60382018-10-31 17:06:06 +0100167 t.show(mSourceControl.getLeash());
Jorim Jaggie35c0592018-11-06 16:21:08 +0100168 } else {
169 t.hide(mSourceControl.getLeash());
Jorim Jaggib6030952018-10-23 18:31:52 +0200170 }
171 t.apply();
172 }
173}