1 /*
2  * Copyright (C) 2018 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 package android.service.autofill.augmented;
17 
18 import static android.service.autofill.augmented.AugmentedAutofillService.sDebug;
19 
20 import android.annotation.NonNull;
21 import android.annotation.SystemApi;
22 import android.os.RemoteException;
23 import android.service.autofill.augmented.AugmentedAutofillService.AutofillProxy;
24 import android.util.Log;
25 import android.util.Pair;
26 import android.view.autofill.AutofillId;
27 import android.view.autofill.AutofillValue;
28 
29 import com.android.internal.util.Preconditions;
30 
31 import java.util.List;
32 
33 /**
34  * Object used to interact with the autofill system.
35  *
36  * @hide
37  */
38 @SystemApi
39 public final class FillController {
40     private static final String TAG = FillController.class.getSimpleName();
41 
42     private final AutofillProxy mProxy;
43 
FillController(@onNull AutofillProxy proxy)44     FillController(@NonNull AutofillProxy proxy) {
45         mProxy = proxy;
46     }
47 
48     /**
49      * Fills the activity with the provided values.
50      *
51      * <p>As a side effect, the {@link FillWindow} associated with the {@link FillResponse} will be
52      * automatically {@link FillWindow#destroy() destroyed}.
53      */
autofill(@onNull List<Pair<AutofillId, AutofillValue>> values)54     public void autofill(@NonNull List<Pair<AutofillId, AutofillValue>> values) {
55         Preconditions.checkNotNull(values);
56 
57         if (sDebug) {
58             Log.d(TAG, "autofill() with " + values.size() + " values");
59         }
60 
61         try {
62             mProxy.autofill(values);
63         } catch (RemoteException e) {
64             e.rethrowAsRuntimeException();
65         }
66 
67         final FillWindow fillWindow = mProxy.getFillWindow();
68         if (fillWindow != null) {
69             fillWindow.destroy();
70         }
71     }
72 }
73