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 17 package com.example.android.autofill.service.simple; 18 19 import static android.view.autofill.AutofillManager.EXTRA_ASSIST_STRUCTURE; 20 import static android.view.autofill.AutofillManager.EXTRA_AUTHENTICATION_RESULT; 21 22 import android.app.Activity; 23 import android.app.PendingIntent; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.IntentSender; 27 import android.os.Bundle; 28 import android.os.Parcelable; 29 import android.service.autofill.Dataset; 30 import android.service.autofill.FillResponse; 31 import android.support.annotation.NonNull; 32 import android.support.annotation.Nullable; 33 import android.util.ArrayMap; 34 import android.view.autofill.AutofillId; 35 import android.view.autofill.AutofillValue; 36 import android.widget.RemoteViews; 37 38 import com.example.android.autofill.service.R; 39 40 import java.util.Map.Entry; 41 42 /** 43 * Activity used for autofill authentication, it simply sets the dataste upon tapping OK. 44 */ 45 // TODO(b/114236837): should display a small dialog, not take the full screen 46 public class SimpleAuthActivity extends Activity { 47 48 private static final String EXTRA_DATASET = "dataset"; 49 private static final String EXTRA_HINTS = "hints"; 50 private static final String EXTRA_IDS = "ids"; 51 private static final String EXTRA_AUTH_DATASETS = "auth_datasets"; 52 53 private static int sPendingIntentId = 0; 54 55 @Override onCreate(Bundle savedInstanceState)56 protected void onCreate(Bundle savedInstanceState) { 57 super.onCreate(savedInstanceState); 58 setContentView(R.layout.simple_service_auth_activity); 59 findViewById(R.id.yes).setOnClickListener((view) -> onYes()); 60 findViewById(R.id.no).setOnClickListener((view) -> onNo()); 61 } 62 onYes()63 private void onYes() { 64 Intent myIntent = getIntent(); 65 Intent replyIntent = new Intent(); 66 Dataset dataset = myIntent.getParcelableExtra(EXTRA_DATASET); 67 if (dataset != null) { 68 replyIntent.putExtra(EXTRA_AUTHENTICATION_RESULT, dataset); 69 } else { 70 String[] hints = myIntent.getStringArrayExtra(EXTRA_HINTS); 71 Parcelable[] ids = myIntent.getParcelableArrayExtra(EXTRA_IDS); 72 boolean authenticateDatasets = myIntent.getBooleanExtra(EXTRA_AUTH_DATASETS, false); 73 int size = hints.length; 74 ArrayMap<String, AutofillId> fields = new ArrayMap<>(size); 75 for (int i = 0; i < size; i++) { 76 fields.put(hints[i], (AutofillId) ids[i]); 77 } 78 FillResponse response = 79 DebugService.createResponse(this, fields, 1, authenticateDatasets); 80 replyIntent.putExtra(EXTRA_AUTHENTICATION_RESULT, response); 81 82 } 83 setResult(RESULT_OK, replyIntent); 84 finish(); 85 } 86 onNo()87 private void onNo() { 88 setResult(RESULT_CANCELED); 89 finish(); 90 } 91 newIntentSenderForDataset(@onNull Context context, @NonNull Dataset dataset)92 public static IntentSender newIntentSenderForDataset(@NonNull Context context, 93 @NonNull Dataset dataset) { 94 return newIntentSender(context, dataset, null, null, false); 95 } 96 newIntentSenderForResponse(@onNull Context context, @NonNull String[] hints, @NonNull AutofillId[] ids, boolean authenticateDatasets)97 public static IntentSender newIntentSenderForResponse(@NonNull Context context, 98 @NonNull String[] hints, @NonNull AutofillId[] ids, boolean authenticateDatasets) { 99 return newIntentSender(context, null, hints, ids, authenticateDatasets); 100 } 101 newIntentSender(@onNull Context context, @Nullable Dataset dataset, @Nullable String[] hints, @Nullable AutofillId[] ids, boolean authenticateDatasets)102 private static IntentSender newIntentSender(@NonNull Context context, 103 @Nullable Dataset dataset, @Nullable String[] hints, @Nullable AutofillId[] ids, 104 boolean authenticateDatasets) { 105 Intent intent = new Intent(context, SimpleAuthActivity.class); 106 if (dataset != null) { 107 intent.putExtra(EXTRA_DATASET, dataset); 108 } else { 109 intent.putExtra(EXTRA_HINTS, hints); 110 intent.putExtra(EXTRA_IDS, ids); 111 intent.putExtra(EXTRA_AUTH_DATASETS, authenticateDatasets); 112 } 113 114 return PendingIntent.getActivity(context, ++sPendingIntentId, intent, 115 PendingIntent.FLAG_CANCEL_CURRENT).getIntentSender(); 116 } 117 } 118