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.ANIMATION_TYPE_USER; |
| 20 | import static android.view.InsetsController.AnimationType; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 21 | import static android.view.InsetsState.ITYPE_IME; |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 22 | |
Jorim Jaggi | abaa904 | 2020-01-29 15:02:50 +0100 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 24 | import android.inputmethodservice.InputMethodService; |
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 | */ |
| 53 | private boolean mImeRequestedShow; |
| 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); |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 92 | mImeRequestedShow = false; |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Jorim Jaggi | abaa904 | 2020-01-29 15:02:50 +0100 | [diff] [blame] | 95 | @Override |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 96 | public void show(boolean fromIme) { |
| 97 | super.show(fromIme); |
| 98 | if (fromIme) { |
| 99 | mImeRequestedShow = true; |
Jorim Jaggi | abaa904 | 2020-01-29 15:02:50 +0100 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Tarandeep Singh | 94c9a83 | 2020-02-03 14:55:30 -0800 | [diff] [blame] | 103 | @Override |
Tarandeep Singh | b9538cd | 2020-02-20 17:51:18 -0800 | [diff] [blame] | 104 | void hide(boolean animationFinished, @AnimationType int animationType) { |
Tarandeep Singh | 94c9a83 | 2020-02-03 14:55:30 -0800 | [diff] [blame] | 105 | super.hide(); |
Tarandeep Singh | b9538cd | 2020-02-20 17:51:18 -0800 | [diff] [blame] | 106 | |
| 107 | if (!animationFinished) { |
| 108 | if (animationType == ANIMATION_TYPE_USER) { |
| 109 | // if controlWindowInsetsAnimation is hiding keyboard. |
| 110 | notifyHidden(); |
| 111 | } |
| 112 | } else { |
Tarandeep Singh | 94c9a83 | 2020-02-03 14:55:30 -0800 | [diff] [blame] | 113 | // remove IME surface as IME has finished hide animation. |
| 114 | removeSurface(); |
| 115 | } |
| 116 | } |
| 117 | |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 118 | /** |
| 119 | * Request {@link InputMethodManager} to show the IME. |
| 120 | * @return @see {@link android.view.InsetsSourceConsumer.ShowResult}. |
| 121 | */ |
| 122 | @Override |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 123 | public @ShowResult int requestShow(boolean fromIme) { |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 124 | // TODO: ResultReceiver for IME. |
| 125 | // 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] | 126 | |
| 127 | // If we had a request before to show from IME (tracked with mImeRequestedShow), reaching |
| 128 | // 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] | 129 | // If client window is trying to control IME and IME is already visible, it is immediate. |
| 130 | if (fromIme || mImeRequestedShow || mState.getSource(getType()).isVisible()) { |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 131 | mImeRequestedShow = false; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 132 | return ShowResult.SHOW_IMMEDIATELY; |
| 133 | } |
| 134 | |
| 135 | return getImm().requestImeShow(null /* resultReceiver */) |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 136 | ? ShowResult.IME_SHOW_DELAYED : ShowResult.IME_SHOW_FAILED; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Notify {@link InputMethodService} that IME window is hidden. |
| 141 | */ |
| 142 | @Override |
| 143 | void notifyHidden() { |
| 144 | getImm().notifyImeHidden(); |
| 145 | } |
| 146 | |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 147 | @Override |
Tarandeep Singh | 94c9a83 | 2020-02-03 14:55:30 -0800 | [diff] [blame] | 148 | public void removeSurface() { |
| 149 | getImm().removeImeSurface(); |
| 150 | } |
| 151 | |
| 152 | @Override |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 153 | public void setControl(@Nullable InsetsSourceControl control, int[] showTypes, |
| 154 | int[] hideTypes) { |
| 155 | super.setControl(control, showTypes, hideTypes); |
| 156 | if (control == null) { |
| 157 | hide(); |
| 158 | } |
| 159 | } |
| 160 | |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 161 | private boolean isDummyOrEmptyEditor(EditorInfo info) { |
| 162 | // TODO(b/123044812): Handle dummy input gracefully in IME Insets API |
| 163 | return info == null || (info.fieldId <= 0 && info.inputType <= 0); |
| 164 | } |
| 165 | |
| 166 | private boolean isServedEditorRendered() { |
| 167 | if (mFocusedEditor == null || mPreRenderedEditor == null |
| 168 | || isDummyOrEmptyEditor(mFocusedEditor) |
| 169 | || isDummyOrEmptyEditor(mPreRenderedEditor)) { |
| 170 | // No view is focused or ready. |
| 171 | return false; |
| 172 | } |
| 173 | return areEditorsSimilar(mFocusedEditor, mPreRenderedEditor); |
| 174 | } |
| 175 | |
| 176 | @VisibleForTesting |
| 177 | public static boolean areEditorsSimilar(EditorInfo info1, EditorInfo info2) { |
| 178 | // We don't need to compare EditorInfo.fieldId (View#id) since that shouldn't change |
| 179 | // IME views. |
| 180 | boolean areOptionsSimilar = |
| 181 | info1.imeOptions == info2.imeOptions |
| 182 | && info1.inputType == info2.inputType |
| 183 | && TextUtils.equals(info1.packageName, info2.packageName); |
| 184 | areOptionsSimilar &= info1.privateImeOptions != null |
| 185 | ? info1.privateImeOptions.equals(info2.privateImeOptions) : true; |
| 186 | |
| 187 | if (!areOptionsSimilar) { |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | // compare bundle extras. |
| 192 | if ((info1.extras == null && info2.extras == null) || info1.extras == info2.extras) { |
| 193 | return true; |
| 194 | } |
| 195 | if ((info1.extras == null && info2.extras != null) |
| 196 | || (info1.extras == null && info2.extras != null)) { |
| 197 | return false; |
| 198 | } |
| 199 | if (info1.extras.hashCode() == info2.extras.hashCode() |
| 200 | || info1.extras.equals(info1)) { |
| 201 | return true; |
| 202 | } |
| 203 | if (info1.extras.size() != info2.extras.size()) { |
| 204 | return false; |
| 205 | } |
| 206 | if (info1.extras.toString().equals(info2.extras.toString())) { |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | // Compare bytes |
| 211 | Parcel parcel1 = Parcel.obtain(); |
| 212 | info1.extras.writeToParcel(parcel1, 0); |
| 213 | parcel1.setDataPosition(0); |
| 214 | Parcel parcel2 = Parcel.obtain(); |
| 215 | info2.extras.writeToParcel(parcel2, 0); |
| 216 | parcel2.setDataPosition(0); |
| 217 | |
| 218 | return Arrays.equals(parcel1.createByteArray(), parcel2.createByteArray()); |
| 219 | } |
| 220 | |
| 221 | private InputMethodManager getImm() { |
Charles Chen | 5299ad0 | 2019-04-18 13:09:53 +0000 | [diff] [blame] | 222 | return mController.getViewRoot().mContext.getSystemService(InputMethodManager.class); |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 223 | } |
| 224 | } |