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 package com.example.android.autofill.service; 17 18 import android.content.Context; 19 import android.content.IntentSender; 20 import android.service.autofill.Dataset; 21 import android.service.autofill.FillResponse; 22 import android.service.autofill.SaveInfo; 23 import android.support.annotation.DrawableRes; 24 import android.util.Log; 25 import android.view.autofill.AutofillId; 26 import android.widget.RemoteViews; 27 28 import com.example.android.autofill.service.model.FilledAutofillFieldCollection; 29 30 import java.util.HashMap; 31 import java.util.Set; 32 33 import static com.example.android.autofill.service.CommonUtil.TAG; 34 35 /** 36 * This is a class containing helper methods for building Autofill Datasets and Responses. 37 */ 38 public final class AutofillHelper { 39 40 private AutofillHelper() { 41 throw new UnsupportedOperationException("provide static methods only"); 42 } 43 44 /** 45 * Wraps autofill data in a LoginCredential Dataset object which can then be sent back to the 46 * client View. 47 */ 48 public static Dataset newDataset(Context context, 49 AutofillFieldMetadataCollection autofillFields, 50 FilledAutofillFieldCollection filledAutofillFieldCollection, boolean datasetAuth) { 51 String datasetName = filledAutofillFieldCollection.getDatasetName(); 52 if (datasetName != null) { 53 Dataset.Builder datasetBuilder; 54 if (datasetAuth) { 55 datasetBuilder = new Dataset.Builder 56 (newRemoteViews(context.getPackageName(), datasetName, 57 R.drawable.ic_lock_black_24dp)); 58 IntentSender sender = 59 AuthActivity.getAuthIntentSenderForDataset(context, datasetName); 60 datasetBuilder.setAuthentication(sender); 61 } else { 62 datasetBuilder = new Dataset.Builder 63 (newRemoteViews(context.getPackageName(), datasetName, 64 R.drawable.ic_person_black_24dp)); 65 } 66 boolean setValueAtLeastOnce = 67 filledAutofillFieldCollection.applyToFields(autofillFields, datasetBuilder); 68 if (setValueAtLeastOnce) { 69 return datasetBuilder.build(); 70 } 71 } 72 return null; 73 } 74 75 public static RemoteViews newRemoteViews(String packageName, String remoteViewsText, 76 @DrawableRes int drawableId) { 77 RemoteViews presentation = 78 new RemoteViews(packageName, R.layout.multidataset_service_list_item); 79 presentation.setTextViewText(R.id.text, remoteViewsText); 80 presentation.setImageViewResource(R.id.icon, drawableId); 81 return presentation; 82 } 83 84 /** 85 * Wraps autofill data in a Response object (essentially a series of Datasets) which can then 86 * be sent back to the client View. 87 */ 88 public static FillResponse newResponse(Context context, 89 boolean datasetAuth, AutofillFieldMetadataCollection autofillFields, 90 HashMap<String, FilledAutofillFieldCollection> clientFormDataMap) { 91 FillResponse.Builder responseBuilder = new FillResponse.Builder(); 92 if (clientFormDataMap != null) { 93 Set<String> datasetNames = clientFormDataMap.keySet(); 94 for (String datasetName : datasetNames) { 95 FilledAutofillFieldCollection filledAutofillFieldCollection = 96 clientFormDataMap.get(datasetName); 97 if (filledAutofillFieldCollection != null) { 98 Dataset dataset = newDataset(context, autofillFields, 99 filledAutofillFieldCollection, datasetAuth); 100 if (dataset != null) { 101 responseBuilder.addDataset(dataset); 102 } 103 } 104 } 105 } 106 if (autofillFields.getSaveType() != 0) { 107 AutofillId[] autofillIds = autofillFields.getAutofillIds(); 108 responseBuilder.setSaveInfo 109 (new SaveInfo.Builder(autofillFields.getSaveType(), autofillIds).build()); 110 return responseBuilder.build(); 111 } else { 112 Log.d(TAG, "These fields are not meant to be saved by autofill."); 113 return null; 114 } 115 } 116 } 117