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 | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 44 | */ |
Felipe Leme | 0200d9e | 2017-01-24 15:10:26 -0800 | [diff] [blame] | 45 | public void onSuccess() { |
Felipe Leme | 4bcb01a | 2017-11-21 16:47:13 -0800 | [diff] [blame] | 46 | onSuccessInternal(null); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Notifies the Android System that an |
| 51 | * {@link AutofillService#onSaveRequest(SaveRequest, SaveCallback)} was successfully handled |
| 52 | * by the service. |
| 53 | * |
| 54 | * <p>This method is useful when the service requires extra work—for example, launching an |
| 55 | * activity asking the user to authenticate first —before it can process the request, |
| 56 | * as the intent will be launched from the context of the activity being autofilled and hence |
| 57 | * will be part of that activity's stack. |
| 58 | * |
| 59 | * @param intentSender intent that will be launched from the context of activity being |
| 60 | * autofilled. |
| 61 | */ |
| 62 | public void onSuccess(@NonNull IntentSender intentSender) { |
| 63 | onSuccessInternal(Preconditions.checkNotNull(intentSender)); |
| 64 | } |
| 65 | |
| 66 | private void onSuccessInternal(@Nullable IntentSender intentSender) { |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 67 | assertNotCalled(); |
| 68 | mCalled = true; |
| 69 | try { |
Felipe Leme | 4bcb01a | 2017-11-21 16:47:13 -0800 | [diff] [blame] | 70 | mCallback.onSuccess(intentSender); |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 71 | } catch (RemoteException e) { |
| 72 | e.rethrowAsRuntimeException(); |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 73 | } |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 77 | * Notifies the Android System that an |
Felipe Leme | 30040fa | 2017-10-03 11:41:52 -0700 | [diff] [blame] | 78 | * {@link AutofillService#onSaveRequest(SaveRequest, SaveCallback)} could not be handled |
Felipe Leme | e5f9c30 | 2017-04-18 17:48:49 -0700 | [diff] [blame] | 79 | * by the service. |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 80 | * |
Felipe Leme | 30040fa | 2017-10-03 11:41:52 -0700 | [diff] [blame] | 81 | * <p>This method should only be called when the service could not handle the request right away |
| 82 | * and could not recover or retry it. If the service could retry or recover, it could keep |
| 83 | * the {@link SaveRequest} and call {@link #onSuccess()} instead. |
| 84 | * |
| 85 | * <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] | 86 | * you prefer to show your own message, call {@link #onSuccess()} or |
| 87 | * {@link #onSuccess(IntentSender)} instead. |
Felipe Leme | 30040fa | 2017-10-03 11:41:52 -0700 | [diff] [blame] | 88 | * |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 89 | * @param message error message to be displayed to the user. |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 90 | */ |
| 91 | public void onFailure(CharSequence message) { |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 92 | assertNotCalled(); |
| 93 | mCalled = true; |
| 94 | try { |
| 95 | mCallback.onFailure(message); |
| 96 | } catch (RemoteException e) { |
| 97 | e.rethrowAsRuntimeException(); |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 98 | } |
| 99 | } |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 100 | |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 101 | private void assertNotCalled() { |
| 102 | if (mCalled) { |
| 103 | throw new IllegalStateException("Already called"); |
Felipe Leme | 436ab6a | 2016-12-15 11:56:15 -0800 | [diff] [blame] | 104 | } |
Felipe Leme | 6d55387 | 2016-12-08 17:13:25 -0800 | [diff] [blame] | 105 | } |
Felipe Leme | 1ca634a | 2016-11-28 17:21:21 -0800 | [diff] [blame] | 106 | } |