1 /*
2  * Copyright (C) 2021 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 static android.service.autofill.AutofillService.EXTRA_ERROR;
20 import static android.service.autofill.AutofillService.EXTRA_RESULT;
21 
22 import static com.android.internal.util.Preconditions.checkArgumentInRange;
23 
24 import static java.util.Objects.requireNonNull;
25 
26 import android.annotation.NonNull;
27 import android.os.Bundle;
28 import android.os.DeadObjectException;
29 import android.os.RemoteException;
30 import android.util.Log;
31 
32 import com.android.internal.os.IResultReceiver;
33 
34 import java.util.Set;
35 
36 final class SavedDatasetsInfoCallbackImpl implements SavedDatasetsInfoCallback {
37     private static final String TAG = "AutofillService";
38 
39     @NonNull
40     private final IResultReceiver mReceiver;
41     @NonNull
42     private final String mType;
43 
44     /**
45      * Creates a {@link SavedDatasetsInfoCallback} that returns the {@link
46      * SavedDatasetsInfo#getCount() number} of saved datasets of {@code type} to the {@code
47      * receiver}.
48      */
SavedDatasetsInfoCallbackImpl(@onNull IResultReceiver receiver, @NonNull String type)49     SavedDatasetsInfoCallbackImpl(@NonNull IResultReceiver receiver, @NonNull String type) {
50         mReceiver = requireNonNull(receiver);
51         mType = requireNonNull(type);
52     }
53 
54     @Override
onSuccess(@onNull Set<SavedDatasetsInfo> results)55     public void onSuccess(@NonNull Set<SavedDatasetsInfo> results) {
56         requireNonNull(results);
57         if (results.isEmpty()) {
58             send(1, null);
59             return;
60         }
61         int count = -1;
62         for (SavedDatasetsInfo info : results) {
63             if (mType.equals(info.getType())) {
64                 count = info.getCount();
65             }
66         }
67         if (count < 0) {
68             send(1, null);
69             return;
70         }
71         Bundle bundle = new Bundle(/* capacity= */ 1);
72         bundle.putInt(EXTRA_RESULT, count);
73         send(0, bundle);
74     }
75 
76     @Override
onError(@rror int error)77     public void onError(@Error int error) {
78         checkArgumentInRange(error, ERROR_OTHER, ERROR_NEEDS_USER_ACTION, "error");
79         Bundle bundle = new Bundle(/* capacity= */ 1);
80         bundle.putInt(EXTRA_ERROR, error);
81         send(1, bundle);
82     }
83 
send(int resultCode, Bundle bundle)84     private void send(int resultCode, Bundle bundle) {
85         try {
86             mReceiver.send(resultCode, bundle);
87         } catch (DeadObjectException e) {
88             Log.w(TAG, "Failed to send onSavedPasswordCountRequest result: " + e);
89         } catch (RemoteException e) {
90             throw e.rethrowAsRuntimeException();
91         }
92     }
93 }
94