blob: 08d45a746dc41ae87b861a51316646a4c9af2481 [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;
Tarandeep Singh92d2dd32019-08-07 14:45:01 -070061 private boolean mHasWindowFocus;
Jorim Jaggib6030952018-10-23 18:31:52 +020062
63 public InsetsSourceConsumer(@InternalInsetType int type, InsetsState state,
Jorim Jaggic8d60382018-10-31 17:06:06 +010064 Supplier<Transaction> transactionSupplier, InsetsController controller) {
Jorim Jaggib6030952018-10-23 18:31:52 +020065 mType = type;
66 mState = state;
67 mTransactionSupplier = transactionSupplier;
Jorim Jaggic8d60382018-10-31 17:06:06 +010068 mController = controller;
Jorim Jaggid89efeb2019-01-22 17:48:34 +010069 mVisible = InsetsState.getDefaultVisibility(type);
Jorim Jaggib6030952018-10-23 18:31:52 +020070 }
71
72 public void setControl(@Nullable InsetsSourceControl control) {
Jorim Jaggic8d60382018-10-31 17:06:06 +010073 if (mSourceControl == control) {
Jorim Jaggib6030952018-10-23 18:31:52 +020074 return;
75 }
Jorim Jaggic8d60382018-10-31 17:06:06 +010076 mSourceControl = control;
Jorim Jaggib6030952018-10-23 18:31:52 +020077 applyHiddenToControl();
Jorim Jaggie35c0592018-11-06 16:21:08 +010078 if (applyLocalVisibilityOverride()) {
79 mController.notifyVisibilityChanged();
80 }
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +010081 if (mSourceControl == null) {
82 mController.notifyControlRevoked(this);
83 }
Jorim Jaggib6030952018-10-23 18:31:52 +020084 }
85
86 @VisibleForTesting
87 public InsetsSourceControl getControl() {
Jorim Jaggic8d60382018-10-31 17:06:06 +010088 return mSourceControl;
Jorim Jaggib6030952018-10-23 18:31:52 +020089 }
90
91 int getType() {
92 return mType;
93 }
94
95 @VisibleForTesting
96 public void show() {
Jorim Jaggie35c0592018-11-06 16:21:08 +010097 setVisible(true);
Jorim Jaggib6030952018-10-23 18:31:52 +020098 }
99
100 @VisibleForTesting
101 public void hide() {
Jorim Jaggie35c0592018-11-06 16:21:08 +0100102 setVisible(false);
Jorim Jaggib6030952018-10-23 18:31:52 +0200103 }
104
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -0800105 /**
106 * Called when current window gains focus
107 */
Tarandeep Singh92d2dd32019-08-07 14:45:01 -0700108 public void onWindowFocusGained() {
109 mHasWindowFocus = true;
110 }
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -0800111
112 /**
113 * Called when current window loses focus.
114 */
Tarandeep Singh92d2dd32019-08-07 14:45:01 -0700115 public void onWindowFocusLost() {
116 mHasWindowFocus = false;
117 }
118
119 boolean hasWindowFocus() {
120 return mHasWindowFocus;
121 }
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -0800122
Jorim Jaggie35c0592018-11-06 16:21:08 +0100123 boolean applyLocalVisibilityOverride() {
Jorim Jaggic8d60382018-10-31 17:06:06 +0100124
125 // If we don't have control, we are not able to change the visibility.
126 if (mSourceControl == null) {
Jorim Jaggie35c0592018-11-06 16:21:08 +0100127 return false;
Jorim Jaggic8d60382018-10-31 17:06:06 +0100128 }
Jorim Jaggie35c0592018-11-06 16:21:08 +0100129 if (mState.getSource(mType).isVisible() == mVisible) {
130 return false;
131 }
132 mState.getSource(mType).setVisible(mVisible);
133 return true;
Jorim Jaggic8d60382018-10-31 17:06:06 +0100134 }
135
Tarandeep Singh22f2b4c2019-01-10 19:41:30 -0800136 @VisibleForTesting
137 public boolean isVisible() {
138 return mVisible;
139 }
140
Tarandeep Singh46d59f02019-01-29 18:09:15 -0800141 /**
142 * Request to show current window type.
143 *
144 * @param fromController {@code true} if request is coming from controller.
145 * (e.g. in IME case, controller is
146 * {@link android.inputmethodservice.InputMethodService}).
147 * @return @see {@link ShowResult}.
148 */
149 @ShowResult int requestShow(boolean fromController) {
150 return ShowResult.SHOW_IMMEDIATELY;
151 }
152
153 /**
154 * Notify listeners that window is now hidden.
155 */
156 void notifyHidden() {
157 // no-op for types that always return ShowResult#SHOW_IMMEDIATELY.
158 }
159
Jorim Jaggie35c0592018-11-06 16:21:08 +0100160 private void setVisible(boolean visible) {
161 if (mVisible == visible) {
Jorim Jaggib6030952018-10-23 18:31:52 +0200162 return;
163 }
Jorim Jaggie35c0592018-11-06 16:21:08 +0100164 mVisible = visible;
Jorim Jaggic8d60382018-10-31 17:06:06 +0100165 applyLocalVisibilityOverride();
166 mController.notifyVisibilityChanged();
Jorim Jaggib6030952018-10-23 18:31:52 +0200167 }
168
169 private void applyHiddenToControl() {
Jorim Jaggia12ea562019-01-07 17:47:47 +0100170 if (mSourceControl == null || mSourceControl.getLeash() == null) {
Jorim Jaggib6030952018-10-23 18:31:52 +0200171 return;
172 }
173
Jorim Jaggib6030952018-10-23 18:31:52 +0200174 final Transaction t = mTransactionSupplier.get();
Jorim Jaggie35c0592018-11-06 16:21:08 +0100175 if (mVisible) {
Jorim Jaggic8d60382018-10-31 17:06:06 +0100176 t.show(mSourceControl.getLeash());
Jorim Jaggie35c0592018-11-06 16:21:08 +0100177 } else {
178 t.hide(mSourceControl.getLeash());
Jorim Jaggib6030952018-10-23 18:31:52 +0200179 }
180 t.apply();
181 }
182}