Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 20 | import static android.view.InsetsState.ITYPE_IME; |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 21 | |
Jorim Jaggi | abaa904 | 2020-01-29 15:02:50 +0100 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 23 | import android.inputmethodservice.InputMethodService; |
Adrian Roos | c22eec9 | 2020-06-12 18:48:10 +0200 | [diff] [blame] | 24 | import android.os.IBinder; |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 25 | import android.os.Parcel; |
| 26 | import android.text.TextUtils; |
| 27 | import android.view.SurfaceControl.Transaction; |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 28 | import android.view.inputmethod.EditorInfo; |
| 29 | import android.view.inputmethod.InputMethodManager; |
| 30 | |
| 31 | import com.android.internal.annotations.VisibleForTesting; |
| 32 | |
| 33 | import java.util.Arrays; |
| 34 | import java.util.function.Supplier; |
| 35 | |
| 36 | /** |
| 37 | * Controls the visibility and animations of IME window insets source. |
| 38 | * @hide |
| 39 | */ |
| 40 | public final class ImeInsetsSourceConsumer extends InsetsSourceConsumer { |
| 41 | private EditorInfo mFocusedEditor; |
| 42 | private EditorInfo mPreRenderedEditor; |
| 43 | /** |
| 44 | * Determines if IME would be shown next time IME is pre-rendered for currently focused |
| 45 | * editor {@link #mFocusedEditor} if {@link #isServedEditorRendered} is {@code true}. |
| 46 | */ |
| 47 | private boolean mShowOnNextImeRender; |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 48 | |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 49 | /** |
| 50 | * Tracks whether we have an outstanding request from the IME to show, but weren't able to |
| 51 | * execute it because we didn't have control yet. |
| 52 | */ |
Taran Singh | 9641cfc | 2020-04-09 16:59:10 -0700 | [diff] [blame] | 53 | private boolean mIsRequestedVisibleAwaitingControl; |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 54 | |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 55 | public ImeInsetsSourceConsumer( |
| 56 | InsetsState state, Supplier<Transaction> transactionSupplier, |
| 57 | InsetsController controller) { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 58 | super(ITYPE_IME, state, transactionSupplier, controller); |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | public void onPreRendered(EditorInfo info) { |
| 62 | mPreRenderedEditor = info; |
| 63 | if (mShowOnNextImeRender) { |
| 64 | mShowOnNextImeRender = false; |
| 65 | if (isServedEditorRendered()) { |
| 66 | applyImeVisibility(true /* setVisible */); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public void onServedEditorChanged(EditorInfo info) { |
| 72 | if (isDummyOrEmptyEditor(info)) { |
| 73 | mShowOnNextImeRender = false; |
| 74 | } |
| 75 | mFocusedEditor = info; |
| 76 | } |
| 77 | |
| 78 | public void applyImeVisibility(boolean setVisible) { |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 79 | mController.applyImeVisibility(setVisible); |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void onWindowFocusGained() { |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 84 | super.onWindowFocusGained(); |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 85 | getImm().registerImeConsumer(this); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void onWindowFocusLost() { |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 90 | super.onWindowFocusLost(); |
Louis Chang | 3d4a7d1 | 2019-04-12 16:18:30 +0800 | [diff] [blame] | 91 | getImm().unregisterImeConsumer(this); |
Taran Singh | 9641cfc | 2020-04-09 16:59:10 -0700 | [diff] [blame] | 92 | mIsRequestedVisibleAwaitingControl = false; |
Jorim Jaggi | abaa904 | 2020-01-29 15:02:50 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Tarandeep Singh | 94c9a83 | 2020-02-03 14:55:30 -0800 | [diff] [blame] | 95 | @Override |
Tarandeep Singh | b9538cd | 2020-02-20 17:51:18 -0800 | [diff] [blame] | 96 | void hide(boolean animationFinished, @AnimationType int animationType) { |
Tarandeep Singh | 94c9a83 | 2020-02-03 14:55:30 -0800 | [diff] [blame] | 97 | super.hide(); |
Tarandeep Singh | b9538cd | 2020-02-20 17:51:18 -0800 | [diff] [blame] | 98 | |
Adrian Roos | 3b19ff1 | 2020-04-01 19:48:16 +0200 | [diff] [blame] | 99 | if (animationFinished) { |
Tarandeep Singh | 94c9a83 | 2020-02-03 14:55:30 -0800 | [diff] [blame] | 100 | // remove IME surface as IME has finished hide animation. |
Adrian Roos | 3b19ff1 | 2020-04-01 19:48:16 +0200 | [diff] [blame] | 101 | notifyHidden(); |
Tarandeep Singh | 94c9a83 | 2020-02-03 14:55:30 -0800 | [diff] [blame] | 102 | removeSurface(); |
| 103 | } |
| 104 | } |
| 105 | |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 106 | /** |
| 107 | * Request {@link InputMethodManager} to show the IME. |
| 108 | * @return @see {@link android.view.InsetsSourceConsumer.ShowResult}. |
| 109 | */ |
| 110 | @Override |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 111 | public @ShowResult int requestShow(boolean fromIme) { |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 112 | // TODO: ResultReceiver for IME. |
| 113 | // TODO: Set mShowOnNextImeRender to automatically show IME and guard it with a flag. |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 114 | |
Taran Singh | 9641cfc | 2020-04-09 16:59:10 -0700 | [diff] [blame] | 115 | if (getControl() == null) { |
| 116 | // If control is null, schedule to show IME when control is available. |
| 117 | mIsRequestedVisibleAwaitingControl = true; |
| 118 | } |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 119 | // If we had a request before to show from IME (tracked with mImeRequestedShow), reaching |
| 120 | // this code here means that we now got control, so we can start the animation immediately. |
Tarandeep Singh | b9538cd | 2020-02-20 17:51:18 -0800 | [diff] [blame] | 121 | // If client window is trying to control IME and IME is already visible, it is immediate. |
Taran Singh | 9641cfc | 2020-04-09 16:59:10 -0700 | [diff] [blame] | 122 | if (fromIme || mState.getSource(getType()).isVisible()) { |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 123 | return ShowResult.SHOW_IMMEDIATELY; |
| 124 | } |
| 125 | |
| 126 | return getImm().requestImeShow(null /* resultReceiver */) |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 127 | ? ShowResult.IME_SHOW_DELAYED : ShowResult.IME_SHOW_FAILED; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Notify {@link InputMethodService} that IME window is hidden. |
| 132 | */ |
| 133 | @Override |
| 134 | void notifyHidden() { |
| 135 | getImm().notifyImeHidden(); |
| 136 | } |
| 137 | |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 138 | @Override |
Tarandeep Singh | 94c9a83 | 2020-02-03 14:55:30 -0800 | [diff] [blame] | 139 | public void removeSurface() { |
| 140 | getImm().removeImeSurface(); |
| 141 | } |
| 142 | |
| 143 | @Override |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 144 | public void setControl(@Nullable InsetsSourceControl control, int[] showTypes, |
| 145 | int[] hideTypes) { |
| 146 | super.setControl(control, showTypes, hideTypes); |
Taran Singh | 9641cfc | 2020-04-09 16:59:10 -0700 | [diff] [blame] | 147 | if (control == null && !mIsRequestedVisibleAwaitingControl) { |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 148 | hide(); |
| 149 | } |
| 150 | } |
| 151 | |
Taran Singh | 9641cfc | 2020-04-09 16:59:10 -0700 | [diff] [blame] | 152 | @Override |
| 153 | protected boolean isRequestedVisibleAwaitingControl() { |
Taran Singh | 335ffd0 | 2020-04-20 17:10:41 -0700 | [diff] [blame] | 154 | return mIsRequestedVisibleAwaitingControl || isRequestedVisible(); |
Taran Singh | 9641cfc | 2020-04-09 16:59:10 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Adrian Roos | c22eec9 | 2020-06-12 18:48:10 +0200 | [diff] [blame] | 157 | @Override |
| 158 | public void onPerceptible(boolean perceptible) { |
| 159 | super.onPerceptible(perceptible); |
| 160 | final IBinder window = mController.getHost().getWindowToken(); |
| 161 | if (window != null) { |
| 162 | getImm().reportPerceptible(window, perceptible); |
| 163 | } |
| 164 | } |
| 165 | |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 166 | private boolean isDummyOrEmptyEditor(EditorInfo info) { |
| 167 | // TODO(b/123044812): Handle dummy input gracefully in IME Insets API |
| 168 | return info == null || (info.fieldId <= 0 && info.inputType <= 0); |
| 169 | } |
| 170 | |
| 171 | private boolean isServedEditorRendered() { |
| 172 | if (mFocusedEditor == null || mPreRenderedEditor == null |
| 173 | || isDummyOrEmptyEditor(mFocusedEditor) |
| 174 | || isDummyOrEmptyEditor(mPreRenderedEditor)) { |
| 175 | // No view is focused or ready. |
| 176 | return false; |
| 177 | } |
| 178 | return areEditorsSimilar(mFocusedEditor, mPreRenderedEditor); |
| 179 | } |
| 180 | |
| 181 | @VisibleForTesting |
| 182 | public static boolean areEditorsSimilar(EditorInfo info1, EditorInfo info2) { |
| 183 | // We don't need to compare EditorInfo.fieldId (View#id) since that shouldn't change |
| 184 | // IME views. |
| 185 | boolean areOptionsSimilar = |
| 186 | info1.imeOptions == info2.imeOptions |
| 187 | && info1.inputType == info2.inputType |
| 188 | && TextUtils.equals(info1.packageName, info2.packageName); |
| 189 | areOptionsSimilar &= info1.privateImeOptions != null |
| 190 | ? info1.privateImeOptions.equals(info2.privateImeOptions) : true; |
| 191 | |
| 192 | if (!areOptionsSimilar) { |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | // compare bundle extras. |
| 197 | if ((info1.extras == null && info2.extras == null) || info1.extras == info2.extras) { |
| 198 | return true; |
| 199 | } |
| 200 | if ((info1.extras == null && info2.extras != null) |
| 201 | || (info1.extras == null && info2.extras != null)) { |
| 202 | return false; |
| 203 | } |
| 204 | if (info1.extras.hashCode() == info2.extras.hashCode() |
| 205 | || info1.extras.equals(info1)) { |
| 206 | return true; |
| 207 | } |
| 208 | if (info1.extras.size() != info2.extras.size()) { |
| 209 | return false; |
| 210 | } |
| 211 | if (info1.extras.toString().equals(info2.extras.toString())) { |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | // Compare bytes |
| 216 | Parcel parcel1 = Parcel.obtain(); |
| 217 | info1.extras.writeToParcel(parcel1, 0); |
| 218 | parcel1.setDataPosition(0); |
| 219 | Parcel parcel2 = Parcel.obtain(); |
| 220 | info2.extras.writeToParcel(parcel2, 0); |
| 221 | parcel2.setDataPosition(0); |
| 222 | |
| 223 | return Arrays.equals(parcel1.createByteArray(), parcel2.createByteArray()); |
| 224 | } |
| 225 | |
| 226 | private InputMethodManager getImm() { |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 227 | return mController.getHost().getInputMethodManager(); |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 228 | } |
| 229 | } |