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