1 /*
2  * Copyright (C) 2017 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.assist.AssistStructure;
20 import android.autofillservice.cts.R;
21 import android.autofillservice.cts.testcore.CannedFillResponse;
22 import android.autofillservice.cts.testcore.Helper;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.os.Parcelable;
26 import android.view.autofill.AutofillManager;
27 
28 /**
29  * An activity that authenticates on button press
30  */
31 public class ManualAuthenticationActivity extends AbstractAutoFillActivity {
32     private static CannedFillResponse sResponse;
33     private static CannedFillResponse.CannedDataset sDataset;
34 
setResponse(CannedFillResponse response)35     public static void setResponse(CannedFillResponse response) {
36         sResponse = response;
37         sDataset = null;
38     }
39 
setDataset(CannedFillResponse.CannedDataset dataset)40     public static void setDataset(CannedFillResponse.CannedDataset dataset) {
41         sDataset = dataset;
42         sResponse = null;
43     }
44 
45     @Override
onCreate(Bundle savedInstanceState)46     protected void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48 
49         setContentView(R.layout.single_button_activity);
50 
51         findViewById(R.id.button).setOnClickListener((v) -> {
52             AssistStructure structure = getIntent().getParcelableExtra(
53                     AutofillManager.EXTRA_ASSIST_STRUCTURE);
54             if (structure != null) {
55                 Parcelable result;
56                 if (sResponse != null) {
57                     result = sResponse.asFillResponse(/* contexts= */ null,
58                             (id) -> Helper.findNodeByResourceId(structure, id));
59                 } else if (sDataset != null) {
60                     result = sDataset.asDatasetWithNodeResolver(
61                             (id) -> Helper.findNodeByResourceId(structure, id));
62                 } else {
63                     throw new IllegalStateException("no dataset or response");
64                 }
65 
66                 // Pass on the auth result
67                 Intent intent = new Intent();
68                 intent.putExtra(AutofillManager.EXTRA_AUTHENTICATION_RESULT, result);
69                 setResult(RESULT_OK, intent);
70             }
71 
72             // Done
73             finish();
74         });
75     }
76 }
77