Home | History | Annotate | Download | only in autofill
      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 
     19 import android.app.Activity;
     20 import android.os.RemoteException;
     21 
     22 /**
     23  * Handles save requests from the {@link AutofillService} into the {@link Activity} being
     24  * autofilled.
     25  */
     26 public final class SaveCallback {
     27     private final ISaveCallback mCallback;
     28     private boolean mCalled;
     29 
     30     /** @hide */
     31     SaveCallback(ISaveCallback callback) {
     32         mCallback = callback;
     33     }
     34 
     35     /**
     36      * Notifies the Android System that an
     37      * {@link AutofillService#onSaveRequest(SaveRequest, SaveCallback)} was successfully handled
     38      * by the service.
     39      *
     40      * <p>If the service could not handle the request right away&mdash;for example, because it must
     41      * launch an activity asking the user to authenticate first or because the network is
     42      * down&mdash;it should still call {@link #onSuccess()}.
     43      *
     44      * @throws RuntimeException if an error occurred while calling the Android System.
     45      */
     46     public void onSuccess() {
     47         assertNotCalled();
     48         mCalled = true;
     49         try {
     50             mCallback.onSuccess();
     51         } catch (RemoteException e) {
     52             e.rethrowAsRuntimeException();
     53         }
     54     }
     55 
     56     /**
     57      * Notifies the Android System that an
     58      * {@link AutofillService#onSaveRequest(SaveRequest, SaveCallback)} could not be handled
     59      * by the service.
     60      *
     61      * <p>This method should only be called when the service could not handle the request right away
     62      * and could not recover or retry it. If the service could retry or recover, it could keep
     63      * the {@link SaveRequest} and call {@link #onSuccess()} instead.
     64      *
     65      * <p><b>Note:</b> The Android System displays an UI with the supplied error message; if
     66      * you prefer to show your own message, call {@link #onSuccess()} instead.
     67      *
     68      * @param message error message to be displayed to the user.
     69      *
     70      * @throws RuntimeException if an error occurred while calling the Android System.
     71      */
     72     public void onFailure(CharSequence message) {
     73         assertNotCalled();
     74         mCalled = true;
     75         try {
     76             mCallback.onFailure(message);
     77         } catch (RemoteException e) {
     78             e.rethrowAsRuntimeException();
     79         }
     80     }
     81 
     82     private void assertNotCalled() {
     83         if (mCalled) {
     84             throw new IllegalStateException("Already called");
     85         }
     86     }
     87 }
     88