Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.service.autofill; |
| 18 | |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 20 | import android.app.Activity; |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 21 | import android.os.RemoteException; |
Felipe Leme | 3ebb359 | 2018-09-18 14:59:30 -0700 | [diff] [blame] | 22 | import android.util.Log; |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 23 | |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 24 | /** |
Laura Davis | 43e75d9 | 2018-08-10 15:46:06 -0700 | [diff] [blame] | 25 | * <p><code>FillCallback</code> handles autofill requests from the {@link AutofillService} into |
| 26 | * the {@link Activity} being autofilled. |
| 27 | * |
| 28 | * <p>To learn about using Autofill services in your app, read |
| 29 | * <a href="/guide/topics/text/autofill-services">Build autofill services</a>. |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 30 | */ |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 31 | public final class FillCallback { |
Felipe Leme | 3ebb359 | 2018-09-18 14:59:30 -0700 | [diff] [blame] | 32 | |
| 33 | private static final String TAG = "FillCallback"; |
| 34 | |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 35 | private final IFillCallback mCallback; |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 36 | private final int mRequestId; |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 37 | private boolean mCalled; |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 38 | |
| 39 | /** @hide */ |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 40 | public FillCallback(IFillCallback callback, int requestId) { |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 41 | mCallback = callback; |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 42 | mRequestId = requestId; |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** |
Felipe Leme | deff81b1 | 2018-09-19 11:50:52 -0700 | [diff] [blame] | 46 | * Notifies the Android System that a fill request |
| 47 | * ({@link AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal, |
| 48 | * FillCallback)}) was successfully fulfilled by the service. |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 49 | * |
Felipe Leme | deff81b1 | 2018-09-19 11:50:52 -0700 | [diff] [blame] | 50 | * <p>This method should always be called, even if the service doesn't have the heuristics to |
| 51 | * fulfill the request (in which case it should be called with {@code null}). |
| 52 | * |
| 53 | * <p>See the main {@link AutofillService} documentation for more details and examples. |
| 54 | * |
| 55 | * @param response autofill information for that activity, or {@code null} when the service |
| 56 | * cannot autofill the activity. |
| 57 | * |
| 58 | * @throws IllegalStateException if this method or {@link #onFailure(CharSequence)} was already |
| 59 | * called. |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 60 | */ |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 61 | public void onSuccess(@Nullable FillResponse response) { |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 62 | assertNotCalled(); |
| 63 | mCalled = true; |
Philip P. Moltmann | c761963 | 2017-04-25 11:57:37 -0700 | [diff] [blame] | 64 | |
| 65 | if (response != null) { |
| 66 | response.setRequestId(mRequestId); |
| 67 | } |
| 68 | |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 69 | try { |
Philip P. Moltmann | c761963 | 2017-04-25 11:57:37 -0700 | [diff] [blame] | 70 | mCallback.onSuccess(response); |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 71 | } catch (RemoteException e) { |
| 72 | e.rethrowAsRuntimeException(); |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 76 | /** |
Felipe Leme | deff81b1 | 2018-09-19 11:50:52 -0700 | [diff] [blame] | 77 | * Notifies the Android System that a fill request ( |
Felipe Leme | e5f9c30 | 2017-04-18 17:48:49 -0700 | [diff] [blame] | 78 | * {@link AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal, |
Felipe Leme | deff81b1 | 2018-09-19 11:50:52 -0700 | [diff] [blame] | 79 | * FillCallback)}) could not be fulfilled by the service (for example, because the user data was |
| 80 | * not available yet), so the request could be retried later. |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 81 | * |
Felipe Leme | deff81b1 | 2018-09-19 11:50:52 -0700 | [diff] [blame] | 82 | * <p><b>Note: </b>this method should not be used when the service didn't have the heursitics to |
| 83 | * fulfill the request; in this case, the service should call {@link #onSuccess(FillResponse) |
| 84 | * onSuccess(null)} instead. |
| 85 | * |
Felipe Leme | d9dc954 | 2018-09-19 11:54:28 -0700 | [diff] [blame] | 86 | * <p><b>Note: </b>prior to {@link android.os.Build.VERSION_CODES#Q}, this |
| 87 | * method was not working as intended and the service should always call |
Felipe Leme | deff81b1 | 2018-09-19 11:50:52 -0700 | [diff] [blame] | 88 | * {@link #onSuccess(FillResponse) onSuccess(null)} instead. |
| 89 | * |
Felipe Leme | d9dc954 | 2018-09-19 11:54:28 -0700 | [diff] [blame] | 90 | * <p><b>Note: </b>for apps targeting {@link android.os.Build.VERSION_CODES#Q} or higher, this |
| 91 | * method just logs the message on {@code logcat}; for apps targetting older SDKs, it also |
| 92 | * displays the message to user using a {@link android.widget.Toast}. Generally speaking, you |
| 93 | * should not display an error to the user if the request failed, unless the request had the |
| 94 | * {@link FillRequest#FLAG_MANUAL_REQUEST} flag. |
| 95 | * |
| 96 | * @param message error message. <b>Note: </b> this message should <b>not</b> contain PII |
| 97 | * (Personally Identifiable Information, such as username or email address). |
Felipe Leme | deff81b1 | 2018-09-19 11:50:52 -0700 | [diff] [blame] | 98 | * |
| 99 | * @throws IllegalStateException if this method or {@link #onSuccess(FillResponse)} was already |
| 100 | * called. |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 101 | */ |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 102 | public void onFailure(@Nullable CharSequence message) { |
Felipe Leme | d9dc954 | 2018-09-19 11:54:28 -0700 | [diff] [blame] | 103 | Log.w(TAG, "onFailure(): " + message); |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 104 | assertNotCalled(); |
| 105 | mCalled = true; |
| 106 | try { |
Felipe Leme | f61ba5c | 2018-05-21 11:18:46 -0700 | [diff] [blame] | 107 | mCallback.onFailure(mRequestId, message); |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 108 | } catch (RemoteException e) { |
| 109 | e.rethrowAsRuntimeException(); |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 113 | private void assertNotCalled() { |
| 114 | if (mCalled) { |
| 115 | throw new IllegalStateException("Already called"); |
Felipe Leme | 436ab6a | 2016-12-15 11:56:15 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 118 | } |