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; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 61 | |
| 62 | public InsetsSourceConsumer(@InternalInsetType int type, InsetsState state, |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 63 | Supplier<Transaction> transactionSupplier, InsetsController controller) { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 64 | mType = type; |
| 65 | mState = state; |
| 66 | mTransactionSupplier = transactionSupplier; |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 67 | mController = controller; |
Jorim Jaggi | d89efeb | 2019-01-22 17:48:34 +0100 | [diff] [blame] | 68 | mVisible = InsetsState.getDefaultVisibility(type); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | public void setControl(@Nullable InsetsSourceControl control) { |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 72 | if (mSourceControl == control) { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 73 | return; |
| 74 | } |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 75 | mSourceControl = control; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 76 | applyHiddenToControl(); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 77 | if (applyLocalVisibilityOverride()) { |
| 78 | mController.notifyVisibilityChanged(); |
| 79 | } |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 80 | if (mSourceControl == null) { |
| 81 | mController.notifyControlRevoked(this); |
| 82 | } |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @VisibleForTesting |
| 86 | public InsetsSourceControl getControl() { |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 87 | return mSourceControl; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | int getType() { |
| 91 | return mType; |
| 92 | } |
| 93 | |
| 94 | @VisibleForTesting |
| 95 | public void show() { |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 96 | setVisible(true); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @VisibleForTesting |
| 100 | public void hide() { |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 101 | setVisible(false); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 104 | /** |
| 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 Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 114 | boolean applyLocalVisibilityOverride() { |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 115 | |
| 116 | // If we don't have control, we are not able to change the visibility. |
| 117 | if (mSourceControl == null) { |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 118 | return false; |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 119 | } |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 120 | if (mState.getSource(mType).isVisible() == mVisible) { |
| 121 | return false; |
| 122 | } |
| 123 | mState.getSource(mType).setVisible(mVisible); |
| 124 | return true; |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 127 | @VisibleForTesting |
| 128 | public boolean isVisible() { |
| 129 | return mVisible; |
| 130 | } |
| 131 | |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 132 | /** |
| 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 Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 151 | private void setVisible(boolean visible) { |
| 152 | if (mVisible == visible) { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 153 | return; |
| 154 | } |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 155 | mVisible = visible; |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 156 | applyLocalVisibilityOverride(); |
| 157 | mController.notifyVisibilityChanged(); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | private void applyHiddenToControl() { |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 161 | if (mSourceControl == null) { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 165 | final Transaction t = mTransactionSupplier.get(); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 166 | if (mVisible) { |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 167 | t.show(mSourceControl.getLeash()); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 168 | } else { |
| 169 | t.hide(mSourceControl.getLeash()); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 170 | } |
| 171 | t.apply(); |
| 172 | } |
| 173 | } |