| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.inputmethodservice; |
| 18 |
|
| 19 |
import static android.inputmethodservice.InputMethodService.DEBUG; |
| 20 |
|
| 21 |
import android.annotation.MainThread; |
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.Nullable; |
| 24 |
import android.os.Bundle; |
| 25 |
import android.os.Handler; |
| 26 |
import android.os.IBinder; |
| 27 |
import android.os.Looper; |
| 28 |
import android.os.RemoteException; |
| 29 |
import android.util.Log; |
| 30 |
import android.view.autofill.AutofillId; |
| 31 |
import android.view.inputmethod.EditorInfo; |
| 32 |
import android.view.inputmethod.InlineSuggestionsRequest; |
| 33 |
import android.view.inputmethod.InlineSuggestionsResponse; |
| 34 |
|
| 35 |
import com.android.internal.view.IInlineSuggestionsRequestCallback; |
| 36 |
import com.android.internal.view.InlineSuggestionsRequestInfo; |
| 37 |
|
| 38 |
import java.util.function.Consumer; |
| 39 |
import java.util.function.Function; |
| 40 |
import java.util.function.Supplier; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
class InlineSuggestionSessionController { |
| 60 |
private static final String TAG = "InlineSuggestionSessionController"; |
| 61 |
|
| 62 |
private final Handler mMainThreadHandler = new Handler(Looper.getMainLooper(), null, true); |
| 63 |
|
| 64 |
@NonNull |
| 65 |
private final Function<Bundle, InlineSuggestionsRequest> mRequestSupplier; |
| 66 |
@NonNull |
| 67 |
private final Supplier<IBinder> mHostInputTokenSupplier; |
| 68 |
@NonNull |
| 69 |
private final Consumer<InlineSuggestionsResponse> mResponseConsumer; |
| 70 |
|
| 71 |
|
| 72 |
@Nullable |
| 73 |
private String mImeClientPackageName; |
| 74 |
@Nullable |
| 75 |
private AutofillId mImeClientFieldId; |
| 76 |
private boolean mImeInputStarted; |
| 77 |
private boolean mImeInputViewStarted; |
| 78 |
|
| 79 |
@Nullable |
| 80 |
private InlineSuggestionSession mSession; |
| 81 |
|
| 82 |
InlineSuggestionSessionController( |
| 83 |
@NonNull Function<Bundle, InlineSuggestionsRequest> requestSupplier, |
| 84 |
@NonNull Supplier<IBinder> hostInputTokenSupplier, |
| 85 |
@NonNull Consumer<InlineSuggestionsResponse> responseConsumer) { |
| 86 |
mRequestSupplier = requestSupplier; |
| 87 |
mHostInputTokenSupplier = hostInputTokenSupplier; |
| 88 |
mResponseConsumer = responseConsumer; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
@MainThread |
| 96 |
void onMakeInlineSuggestionsRequest(@NonNull InlineSuggestionsRequestInfo requestInfo, |
| 97 |
@NonNull IInlineSuggestionsRequestCallback callback) { |
| 98 |
if (DEBUG) Log.d(TAG, "onMakeInlineSuggestionsRequest: " + requestInfo); |
| 99 |
|
| 100 |
if (mSession != null) { |
| 101 |
mSession.invalidate(); |
| 102 |
} |
| 103 |
mSession = new InlineSuggestionSession(requestInfo, callback, mRequestSupplier, |
| 104 |
mHostInputTokenSupplier, mResponseConsumer, this, mMainThreadHandler); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
if (mImeInputStarted && match(mSession.getRequestInfo())) { |
| 109 |
mSession.makeInlineSuggestionRequestUncheck(); |
| 110 |
|
| 111 |
if (mImeInputViewStarted) { |
| 112 |
try { |
| 113 |
mSession.getRequestCallback().onInputMethodStartInputView(); |
| 114 |
} catch (RemoteException e) { |
| 115 |
Log.w(TAG, "onInputMethodStartInputView() remote exception:" + e); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
@MainThread |
| 126 |
void notifyOnStartInput(@Nullable String imeClientPackageName, |
| 127 |
@Nullable AutofillId imeFieldId) { |
| 128 |
if (DEBUG) Log.d(TAG, "notifyOnStartInput: " + imeClientPackageName + ", " + imeFieldId); |
| 129 |
if (imeClientPackageName == null || imeFieldId == null) { |
| 130 |
return; |
| 131 |
} |
| 132 |
mImeInputStarted = true; |
| 133 |
mImeClientPackageName = imeClientPackageName; |
| 134 |
mImeClientFieldId = imeFieldId; |
| 135 |
|
| 136 |
if (mSession != null) { |
| 137 |
mSession.consumeInlineSuggestionsResponse(InlineSuggestionSession.EMPTY_RESPONSE); |
| 138 |
|
| 139 |
|
| 140 |
if (!mSession.isCallbackInvoked() && match(mSession.getRequestInfo())) { |
| 141 |
mSession.makeInlineSuggestionRequestUncheck(); |
| 142 |
} else if (mSession.shouldSendImeStatus()) { |
| 143 |
try { |
| 144 |
mSession.getRequestCallback().onInputMethodStartInput(mImeClientFieldId); |
| 145 |
} catch (RemoteException e) { |
| 146 |
Log.w(TAG, "onInputMethodStartInput() remote exception:" + e); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
@MainThread |
| 158 |
void notifyOnShowInputRequested(boolean requestResult) { |
| 159 |
if (DEBUG) Log.d(TAG, "notifyShowInputRequested"); |
| 160 |
if (mSession != null && mSession.shouldSendImeStatus()) { |
| 161 |
try { |
| 162 |
mSession.getRequestCallback().onInputMethodShowInputRequested(requestResult); |
| 163 |
} catch (RemoteException e) { |
| 164 |
Log.w(TAG, "onInputMethodShowInputRequested() remote exception:" + e); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
@MainThread |
| 175 |
void notifyOnStartInputView() { |
| 176 |
if (DEBUG) Log.d(TAG, "notifyOnStartInputView"); |
| 177 |
mImeInputViewStarted = true; |
| 178 |
if (mSession != null && mSession.shouldSendImeStatus()) { |
| 179 |
try { |
| 180 |
mSession.getRequestCallback().onInputMethodStartInputView(); |
| 181 |
} catch (RemoteException e) { |
| 182 |
Log.w(TAG, "onInputMethodStartInputView() remote exception:" + e); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
@MainThread |
| 193 |
void notifyOnFinishInputView() { |
| 194 |
if (DEBUG) Log.d(TAG, "notifyOnFinishInputView"); |
| 195 |
mImeInputViewStarted = false; |
| 196 |
if (mSession != null && mSession.shouldSendImeStatus()) { |
| 197 |
try { |
| 198 |
mSession.getRequestCallback().onInputMethodFinishInputView(); |
| 199 |
} catch (RemoteException e) { |
| 200 |
Log.w(TAG, "onInputMethodFinishInputView() remote exception:" + e); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
@MainThread |
| 210 |
void notifyOnFinishInput() { |
| 211 |
if (DEBUG) Log.d(TAG, "notifyOnFinishInput"); |
| 212 |
mImeClientPackageName = null; |
| 213 |
mImeClientFieldId = null; |
| 214 |
mImeInputViewStarted = false; |
| 215 |
mImeInputStarted = false; |
| 216 |
if (mSession != null && mSession.shouldSendImeStatus()) { |
| 217 |
try { |
| 218 |
mSession.getRequestCallback().onInputMethodFinishInput(); |
| 219 |
} catch (RemoteException e) { |
| 220 |
Log.w(TAG, "onInputMethodFinishInput() remote exception:" + e); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
@MainThread |
| 229 |
boolean match(@Nullable InlineSuggestionsRequestInfo requestInfo) { |
| 230 |
return match(requestInfo, mImeClientPackageName, mImeClientFieldId); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
@MainThread |
| 237 |
boolean match(@Nullable AutofillId autofillId) { |
| 238 |
return match(autofillId, mImeClientFieldId); |
| 239 |
} |
| 240 |
|
| 241 |
private static boolean match( |
| 242 |
@Nullable InlineSuggestionsRequestInfo inlineSuggestionsRequestInfo, |
| 243 |
@Nullable String imeClientPackageName, @Nullable AutofillId imeClientFieldId) { |
| 244 |
if (inlineSuggestionsRequestInfo == null || imeClientPackageName == null |
| 245 |
|| imeClientFieldId == null) { |
| 246 |
return false; |
| 247 |
} |
| 248 |
return inlineSuggestionsRequestInfo.getComponentName().getPackageName().equals( |
| 249 |
imeClientPackageName) && match(inlineSuggestionsRequestInfo.getAutofillId(), |
| 250 |
imeClientFieldId); |
| 251 |
} |
| 252 |
|
| 253 |
private static boolean match(@Nullable AutofillId autofillId, |
| 254 |
@Nullable AutofillId imeClientFieldId) { |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
return autofillId != null && imeClientFieldId != null |
| 260 |
&& autofillId.getViewId() == imeClientFieldId.getViewId(); |
| 261 |
} |
| 262 |
} |