Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view; |
| 18 | |
Tarandeep Singh | b9538cd | 2020-02-20 17:51:18 -0800 | [diff] [blame] | 19 | import static android.view.InsetsController.AnimationType; |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 20 | import static android.view.InsetsState.toPublicType; |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 21 | |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 22 | import android.annotation.IntDef; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 24 | import android.view.InsetsState.InternalInsetsType; |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 25 | import android.view.SurfaceControl.Transaction; |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 26 | import android.view.WindowInsets.Type.InsetsType; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 27 | |
| 28 | import com.android.internal.annotations.VisibleForTesting; |
| 29 | |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 30 | import java.lang.annotation.Retention; |
| 31 | import java.lang.annotation.RetentionPolicy; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 32 | import java.util.function.Supplier; |
| 33 | |
| 34 | /** |
| 35 | * Controls the visibility and animations of a single window insets source. |
| 36 | * @hide |
| 37 | */ |
| 38 | public class InsetsSourceConsumer { |
| 39 | |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 40 | @Retention(RetentionPolicy.SOURCE) |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 41 | @IntDef(value = {ShowResult.SHOW_IMMEDIATELY, ShowResult.IME_SHOW_DELAYED, ShowResult.IME_SHOW_FAILED}) |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 42 | @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 Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 51 | int IME_SHOW_DELAYED = 1; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 52 | /** |
| 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 Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 56 | int IME_SHOW_FAILED = 2; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 59 | protected final InsetsController mController; |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 60 | protected boolean mRequestedVisible; |
Tarandeep Singh | b9538cd | 2020-02-20 17:51:18 -0800 | [diff] [blame] | 61 | protected final InsetsState mState; |
| 62 | protected final @InternalInsetsType int mType; |
| 63 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 64 | private final Supplier<Transaction> mTransactionSupplier; |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 65 | private @Nullable InsetsSourceControl mSourceControl; |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 66 | private boolean mHasWindowFocus; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 67 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 68 | public InsetsSourceConsumer(@InternalInsetsType int type, InsetsState state, |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 69 | Supplier<Transaction> transactionSupplier, InsetsController controller) { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 70 | mType = type; |
| 71 | mState = state; |
| 72 | mTransactionSupplier = transactionSupplier; |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 73 | mController = controller; |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 74 | mRequestedVisible = InsetsState.getDefaultVisibility(type); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 77 | /** |
| 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 Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 87 | if (mSourceControl == control) { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 88 | return; |
| 89 | } |
Rob Carr | ed48db3 | 2020-03-05 13:31:01 -0800 | [diff] [blame] | 90 | final InsetsSourceControl lastControl = mSourceControl; |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 91 | mSourceControl = control; |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 92 | |
| 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 Carr | ed48db3 | 2020-03-05 13:31:01 -0800 | [diff] [blame] | 101 | } 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 Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 110 | } else { |
Rob Carr | ed48db3 | 2020-03-05 13:31:01 -0800 | [diff] [blame] | 111 | // 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 Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 117 | } |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 118 | } |
Rob Carr | ed48db3 | 2020-03-05 13:31:01 -0800 | [diff] [blame] | 119 | if (lastControl != null) { |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 120 | lastControl.release(mController::releaseSurfaceControlFromRt); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 121 | } |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @VisibleForTesting |
| 125 | public InsetsSourceControl getControl() { |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 126 | return mSourceControl; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | int getType() { |
| 130 | return mType; |
| 131 | } |
| 132 | |
| 133 | @VisibleForTesting |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 134 | public void show(boolean fromIme) { |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 135 | setRequestedVisible(true); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @VisibleForTesting |
| 139 | public void hide() { |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 140 | setRequestedVisible(false); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Tarandeep Singh | b9538cd | 2020-02-20 17:51:18 -0800 | [diff] [blame] | 143 | void hide(boolean animationFinished, @AnimationType int animationType) { |
Tarandeep Singh | 94c9a83 | 2020-02-03 14:55:30 -0800 | [diff] [blame] | 144 | hide(); |
| 145 | } |
| 146 | |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 147 | /** |
| 148 | * Called when current window gains focus |
| 149 | */ |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 150 | public void onWindowFocusGained() { |
| 151 | mHasWindowFocus = true; |
| 152 | } |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * Called when current window loses focus. |
| 156 | */ |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 157 | public void onWindowFocusLost() { |
| 158 | mHasWindowFocus = false; |
| 159 | } |
| 160 | |
| 161 | boolean hasWindowFocus() { |
| 162 | return mHasWindowFocus; |
| 163 | } |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 164 | |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 165 | boolean applyLocalVisibilityOverride() { |
Jorim Jaggi | 580aef5 | 2020-02-26 18:28:28 +0100 | [diff] [blame] | 166 | InsetsSource source = mState.peekSource(mType); |
| 167 | final boolean isVisible = source != null && source.isVisible(); |
Tiger Huang | 41ae5e8 | 2020-01-17 18:17:20 +0800 | [diff] [blame] | 168 | 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 Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 174 | |
| 175 | // If we don't have control, we are not able to change the visibility. |
Tiger Huang | 41ae5e8 | 2020-01-17 18:17:20 +0800 | [diff] [blame] | 176 | if (!hasControl) { |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 177 | return false; |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 178 | } |
Tiger Huang | 41ae5e8 | 2020-01-17 18:17:20 +0800 | [diff] [blame] | 179 | if (isVisible == mRequestedVisible) { |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 180 | return false; |
| 181 | } |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 182 | mState.getSource(mType).setVisible(mRequestedVisible); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 183 | return true; |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 186 | @VisibleForTesting |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 187 | public boolean isRequestedVisible() { |
| 188 | return mRequestedVisible; |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 191 | /** |
| 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 Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 199 | @VisibleForTesting |
| 200 | public @ShowResult int requestShow(boolean fromController) { |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 201 | 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 Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 211 | /** |
Tarandeep Singh | 94c9a83 | 2020-02-03 14:55:30 -0800 | [diff] [blame] | 212 | * 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 Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 219 | * 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 Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 223 | mRequestedVisible = requestedVisible; |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 224 | if (applyLocalVisibilityOverride()) { |
| 225 | mController.notifyVisibilityChanged(); |
| 226 | } |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | private void applyHiddenToControl() { |
Jorim Jaggi | 026ed75 | 2020-01-29 00:30:24 +0100 | [diff] [blame] | 230 | if (mSourceControl == null || mSourceControl.getLeash() == null) { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 234 | final Transaction t = mTransactionSupplier.get(); |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 235 | if (mRequestedVisible) { |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 236 | t.show(mSourceControl.getLeash()); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 237 | } else { |
| 238 | t.hide(mSourceControl.getLeash()); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 239 | } |
| 240 | t.apply(); |
| 241 | } |
| 242 | } |