1 /*
2  * Copyright (C) 2020 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.autofillservice.cts.activities;
18 
19 import android.app.PendingIntent;
20 import android.autofillservice.cts.R;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentSender;
24 import android.os.Bundle;
25 import android.service.autofill.Dataset;
26 import android.util.Log;
27 import android.view.autofill.AutofillManager;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 /**
33  * Activity for testing Augmented Autofill authentication flow. This activity shows a simple UI;
34  * when the UI is tapped, it returns whatever data was configured via the auth intent.
35  */
36 public class AugmentedAuthActivity extends AbstractAutoFillActivity {
37     private static final String TAG = "AugmentedAuthActivity";
38 
39     public static final String ID_AUTH_ACTIVITY_BUTTON = "button";
40 
41     private static final String EXTRA_DATASET_TO_RETURN = "dataset_to_return";
42     private static final String EXTRA_CLIENT_STATE_TO_RETURN = "client_state_to_return";
43     private static final String EXTRA_RESULT_CODE_TO_RETURN = "result_code_to_return";
44 
45     private static final List<PendingIntent> sPendingIntents = new ArrayList<>(1);
46 
resetStaticState()47     public static void resetStaticState() {
48         for (PendingIntent pendingIntent : sPendingIntents) {
49             pendingIntent.cancel();
50         }
51         sPendingIntents.clear();
52     }
53 
createSender(Context context, int requestCode, Dataset datasetToReturn, Bundle clientStateToReturn, int resultCodeToReturn)54     public static IntentSender createSender(Context context, int requestCode,
55             Dataset datasetToReturn, Bundle clientStateToReturn, int resultCodeToReturn) {
56         Intent intent = new Intent(context, AugmentedAuthActivity.class);
57         intent.putExtra(EXTRA_DATASET_TO_RETURN, datasetToReturn);
58         intent.putExtra(EXTRA_CLIENT_STATE_TO_RETURN, clientStateToReturn);
59         intent.putExtra(EXTRA_RESULT_CODE_TO_RETURN, resultCodeToReturn);
60         PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, intent,
61                 PendingIntent.FLAG_IMMUTABLE);
62         sPendingIntents.add(pendingIntent);
63         return pendingIntent.getIntentSender();
64     }
65 
66     @Override
onCreate(Bundle savedInstanceState)67     protected void onCreate(Bundle savedInstanceState) {
68         super.onCreate(savedInstanceState);
69 
70         Log.d(TAG, "Auth activity invoked, showing auth UI");
71         setContentView(R.layout.single_button_activity);
72         findViewById(R.id.button).setOnClickListener((v) -> {
73             Log.d(TAG, "Auth UI tapped, returning result");
74 
75             Intent intent = getIntent();
76             Dataset dataset = intent.getParcelableExtra(EXTRA_DATASET_TO_RETURN);
77             Bundle clientState = intent.getParcelableExtra(EXTRA_CLIENT_STATE_TO_RETURN);
78             int resultCode = intent.getIntExtra(EXTRA_RESULT_CODE_TO_RETURN, RESULT_OK);
79             Log.d(TAG, "Output: dataset=" + dataset + ", clientState=" + clientState
80                     + ", resultCode=" + resultCode);
81 
82             Intent result = new Intent();
83             result.putExtra(AutofillManager.EXTRA_AUTHENTICATION_RESULT, dataset);
84             result.putExtra(AutofillManager.EXTRA_CLIENT_STATE, clientState);
85             setResult(resultCode, result);
86 
87             finish();
88         });
89     }
90 }
91