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