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 | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 42 | * Notifies the Android System that an |
Felipe Leme | e5f9c30 | 2017-04-18 17:48:49 -0700 | [diff] [blame] | 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 | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 46 | * @param response autofill information for that activity, or {@code null} when the activity |
| 47 | * cannot be autofilled (for example, if it only contains read-only fields). See |
Felipe Leme | 436ab6a | 2016-12-15 11:56:15 -0800 | [diff] [blame] | 48 | * {@link FillResponse} for examples. |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 49 | */ |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 50 | public void onSuccess(@Nullable FillResponse response) { |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 51 | assertNotCalled(); |
| 52 | mCalled = true; |
Philip P. Moltmann | c761963 | 2017-04-25 11:57:37 -0700 | [diff] [blame] | 53 | |
| 54 | if (response != null) { |
| 55 | response.setRequestId(mRequestId); |
| 56 | } |
| 57 | |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 58 | try { |
Philip P. Moltmann | c761963 | 2017-04-25 11:57:37 -0700 | [diff] [blame] | 59 | mCallback.onSuccess(response); |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 60 | } catch (RemoteException e) { |
| 61 | e.rethrowAsRuntimeException(); |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 65 | /** |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 66 | * Notifies the Android System that an |
Felipe Leme | e5f9c30 | 2017-04-18 17:48:49 -0700 | [diff] [blame] | 67 | * {@link AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal, |
| 68 | * FillCallback)} could not be fulfilled by the service. |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 69 | * |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 70 | * @param message error message to be displayed to the user. |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 71 | */ |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 72 | public void onFailure(@Nullable CharSequence message) { |
| 73 | assertNotCalled(); |
| 74 | mCalled = true; |
| 75 | try { |
Felipe Leme | f61ba5c | 2018-05-21 11:18:46 -0700 | [diff] [blame] | 76 | mCallback.onFailure(mRequestId, message); |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 77 | } catch (RemoteException e) { |
| 78 | e.rethrowAsRuntimeException(); |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 82 | private void assertNotCalled() { |
| 83 | if (mCalled) { |
| 84 | throw new IllegalStateException("Already called"); |
Felipe Leme | 436ab6a | 2016-12-15 11:56:15 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
Felipe Leme | 29a5b0d | 2016-10-25 14:57:11 -0700 | [diff] [blame] | 87 | } |