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 | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 21 | import android.view.InsetsState.InternalInsetType; |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 22 | import android.view.SurfaceControl.Transaction; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 23 | |
| 24 | import com.android.internal.annotations.VisibleForTesting; |
| 25 | |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 26 | import java.lang.annotation.Retention; |
| 27 | import java.lang.annotation.RetentionPolicy; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 28 | import java.util.function.Supplier; |
| 29 | |
| 30 | /** |
| 31 | * Controls the visibility and animations of a single window insets source. |
| 32 | * @hide |
| 33 | */ |
| 34 | public class InsetsSourceConsumer { |
| 35 | |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 36 | @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 Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 55 | protected final InsetsController mController; |
| 56 | protected boolean mVisible; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 57 | private final Supplier<Transaction> mTransactionSupplier; |
| 58 | private final @InternalInsetType int mType; |
| 59 | private final InsetsState mState; |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 60 | private @Nullable InsetsSourceControl mSourceControl; |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 61 | private boolean mHasWindowFocus; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 62 | |
| 63 | public InsetsSourceConsumer(@InternalInsetType int type, InsetsState state, |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 64 | Supplier<Transaction> transactionSupplier, InsetsController controller) { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 65 | mType = type; |
| 66 | mState = state; |
| 67 | mTransactionSupplier = transactionSupplier; |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 68 | mController = controller; |
Jorim Jaggi | d89efeb | 2019-01-22 17:48:34 +0100 | [diff] [blame] | 69 | mVisible = InsetsState.getDefaultVisibility(type); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | public void setControl(@Nullable InsetsSourceControl control) { |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 73 | if (mSourceControl == control) { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 74 | return; |
| 75 | } |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 76 | mSourceControl = control; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 77 | applyHiddenToControl(); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 78 | if (applyLocalVisibilityOverride()) { |
| 79 | mController.notifyVisibilityChanged(); |
| 80 | } |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 81 | if (mSourceControl == null) { |
| 82 | mController.notifyControlRevoked(this); |
| 83 | } |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @VisibleForTesting |
| 87 | public InsetsSourceControl getControl() { |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 88 | return mSourceControl; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | int getType() { |
| 92 | return mType; |
| 93 | } |
| 94 | |
| 95 | @VisibleForTesting |
| 96 | public void show() { |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 97 | setVisible(true); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @VisibleForTesting |
| 101 | public void hide() { |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 102 | setVisible(false); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 105 | /** |
| 106 | * Called when current window gains focus |
| 107 | */ |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 108 | public void onWindowFocusGained() { |
| 109 | mHasWindowFocus = true; |
| 110 | } |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * Called when current window loses focus. |
| 114 | */ |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 115 | public void onWindowFocusLost() { |
| 116 | mHasWindowFocus = false; |
| 117 | } |
| 118 | |
| 119 | boolean hasWindowFocus() { |
| 120 | return mHasWindowFocus; |
| 121 | } |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 122 | |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 123 | boolean applyLocalVisibilityOverride() { |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 124 | |
| 125 | // If we don't have control, we are not able to change the visibility. |
| 126 | if (mSourceControl == null) { |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 127 | return false; |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 128 | } |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 129 | if (mState.getSource(mType).isVisible() == mVisible) { |
| 130 | return false; |
| 131 | } |
| 132 | mState.getSource(mType).setVisible(mVisible); |
| 133 | return true; |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 136 | @VisibleForTesting |
| 137 | public boolean isVisible() { |
| 138 | return mVisible; |
| 139 | } |
| 140 | |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 141 | /** |
| 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 Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 160 | private void setVisible(boolean visible) { |
| 161 | if (mVisible == visible) { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 162 | return; |
| 163 | } |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 164 | mVisible = visible; |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 165 | applyLocalVisibilityOverride(); |
| 166 | mController.notifyVisibilityChanged(); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | private void applyHiddenToControl() { |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 170 | if (mSourceControl == null || mSourceControl.getLeash() == null) { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 174 | final Transaction t = mTransactionSupplier.get(); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 175 | if (mVisible) { |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 176 | t.show(mSourceControl.getLeash()); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 177 | } else { |
| 178 | t.hide(mSourceControl.getLeash()); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 179 | } |
| 180 | t.apply(); |
| 181 | } |
| 182 | } |