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 java.util.List;
30 import java.util.Objects;
31 
32 /**
33  * Object used to interact with the autofill system.
34  *
35  * @hide
36  */
37 @SystemApi
38 public final class FillController {
39     private static final String TAG = FillController.class.getSimpleName();
40 
41     private final AutofillProxy mProxy;
42 
FillController(@onNull AutofillProxy proxy)43     FillController(@NonNull AutofillProxy proxy) {
44         mProxy = proxy;
45     }
46 
47     /**
48      * Fills the activity with the provided values.
49      *
50      * <p>As a side effect, the {@link FillWindow} associated with the {@link FillResponse} will be
51      * automatically {@link FillWindow#destroy() destroyed}.
52      */
autofill(@onNull List<Pair<AutofillId, AutofillValue>> values)53     public void autofill(@NonNull List<Pair<AutofillId, AutofillValue>> values) {
54         Objects.requireNonNull(values);
55 
56         if (sDebug) {
57             Log.d(TAG, "autofill() with " + values.size() + " values");
58         }
59 
60         try {
61             mProxy.autofill(values);
62         } catch (RemoteException e) {
63             e.rethrowAsRuntimeException();
64         }
65 
66         final FillWindow fillWindow = mProxy.getFillWindow();
67         if (fillWindow != null) {
68             fillWindow.destroy();
69         }
70     }
71 }
72