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