Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [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 | 4bcb01a | 2017-11-21 16:47:13 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 21 | import android.app.Activity; |
Felipe Leme | 4bcb01a | 2017-11-21 16:47:13 -0800 | [diff] [blame] | 22 | import android.content.IntentSender; |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 23 | import android.os.RemoteException; |
Felipe Leme | 436ab6a | 2016-12-15 11:56:15 -0800 | [diff] [blame] | 24 | |
Felipe Leme | 4bcb01a | 2017-11-21 16:47:13 -0800 | [diff] [blame] | 25 | import com.android.internal.util.Preconditions; |
| 26 | |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 27 | /** |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 28 | * Handles save requests from the {@link AutofillService} into the {@link Activity} being |
| 29 | * autofilled. |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 30 | */ |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 31 | public final class SaveCallback { |
| 32 | private final ISaveCallback mCallback; |
| 33 | private boolean mCalled; |
Felipe Leme | 436ab6a | 2016-12-15 11:56:15 -0800 | [diff] [blame] | 34 | |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 35 | /** @hide */ |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 36 | SaveCallback(ISaveCallback callback) { |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 37 | mCallback = callback; |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /** |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 41 | * Notifies the Android System that an |
Felipe Leme | 30040fa | 2017-10-03 11:41:52 -0700 | [diff] [blame] | 42 | * {@link AutofillService#onSaveRequest(SaveRequest, SaveCallback)} was successfully handled |
Felipe Leme | e5f9c30 | 2017-04-18 17:48:49 -0700 | [diff] [blame] | 43 | * by the service. |
Felipe Leme | deff81b | 2018-09-19 11:50:52 -0700 | [diff] [blame] | 44 | * |
| 45 | * @throws IllegalStateException if this method, {@link #onSuccess(IntentSender)}, or |
| 46 | * {@link #onFailure(CharSequence)} was already called. |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 47 | */ |
Felipe Leme | 0200d9e | 2017-01-24 15:10:26 -0800 | [diff] [blame] | 48 | public void onSuccess() { |
Felipe Leme | 4bcb01a | 2017-11-21 16:47:13 -0800 | [diff] [blame] | 49 | onSuccessInternal(null); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Notifies the Android System that an |
| 54 | * {@link AutofillService#onSaveRequest(SaveRequest, SaveCallback)} was successfully handled |
| 55 | * by the service. |
| 56 | * |
| 57 | * <p>This method is useful when the service requires extra work—for example, launching an |
| 58 | * activity asking the user to authenticate first —before it can process the request, |
| 59 | * as the intent will be launched from the context of the activity being autofilled and hence |
| 60 | * will be part of that activity's stack. |
| 61 | * |
| 62 | * @param intentSender intent that will be launched from the context of activity being |
| 63 | * autofilled. |
Felipe Leme | deff81b | 2018-09-19 11:50:52 -0700 | [diff] [blame] | 64 | * |
| 65 | * @throws IllegalStateException if this method, {@link #onSuccess()}, |
| 66 | * or {@link #onFailure(CharSequence)} was already called. |
Felipe Leme | 4bcb01a | 2017-11-21 16:47:13 -0800 | [diff] [blame] | 67 | */ |
| 68 | public void onSuccess(@NonNull IntentSender intentSender) { |
| 69 | onSuccessInternal(Preconditions.checkNotNull(intentSender)); |
| 70 | } |
| 71 | |
| 72 | private void onSuccessInternal(@Nullable IntentSender intentSender) { |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 73 | assertNotCalled(); |
| 74 | mCalled = true; |
| 75 | try { |
Felipe Leme | 4bcb01a | 2017-11-21 16:47:13 -0800 | [diff] [blame] | 76 | mCallback.onSuccess(intentSender); |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 77 | } catch (RemoteException e) { |
| 78 | e.rethrowAsRuntimeException(); |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 79 | } |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 83 | * Notifies the Android System that an |
Felipe Leme | 30040fa | 2017-10-03 11:41:52 -0700 | [diff] [blame] | 84 | * {@link AutofillService#onSaveRequest(SaveRequest, SaveCallback)} could not be handled |
Felipe Leme | e5f9c30 | 2017-04-18 17:48:49 -0700 | [diff] [blame] | 85 | * by the service. |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 86 | * |
Felipe Leme | 30040fa | 2017-10-03 11:41:52 -0700 | [diff] [blame] | 87 | * <p>This method should only be called when the service could not handle the request right away |
| 88 | * and could not recover or retry it. If the service could retry or recover, it could keep |
| 89 | * the {@link SaveRequest} and call {@link #onSuccess()} instead. |
| 90 | * |
| 91 | * <p><b>Note:</b> The Android System displays an UI with the supplied error message; if |
Felipe Leme | 4bcb01a | 2017-11-21 16:47:13 -0800 | [diff] [blame] | 92 | * you prefer to show your own message, call {@link #onSuccess()} or |
| 93 | * {@link #onSuccess(IntentSender)} instead. |
Felipe Leme | 30040fa | 2017-10-03 11:41:52 -0700 | [diff] [blame] | 94 | * |
Felipe Leme | deff81b | 2018-09-19 11:50:52 -0700 | [diff] [blame] | 95 | * @param message error message to be displayed to the user. <b>Note: </b> this message is |
| 96 | * displayed on {@code logcat} logs and should not contain PII (Personally Identifiable |
| 97 | * Information, such as username or email address). |
| 98 | * |
| 99 | * @throws IllegalStateException if this method, {@link #onSuccess()}, |
| 100 | * or {@link #onSuccess(IntentSender)} was already called. |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 101 | */ |
| 102 | public void onFailure(CharSequence message) { |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 103 | assertNotCalled(); |
| 104 | mCalled = true; |
| 105 | try { |
| 106 | mCallback.onFailure(message); |
| 107 | } catch (RemoteException e) { |
| 108 | e.rethrowAsRuntimeException(); |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 111 | |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 112 | private void assertNotCalled() { |
| 113 | if (mCalled) { |
| 114 | throw new IllegalStateException("Already called"); |
Felipe Leme | 436ab6a | 2016-12-15 11:56:15 -0800 | [diff] [blame] | 115 | } |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 116 | } |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 117 | } |