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.model; 17 18 import android.app.assist.AssistStructure; 19 import android.service.autofill.SaveInfo; 20 import android.view.View; 21 import android.view.autofill.AutofillId; 22 import android.view.autofill.AutofillValue; 23 24 /** 25 * Class that represents a field that can be autofilled. It will contain a description 26 * (what type data the field holds), an AutoFillId (an ID unique to the rest of the ViewStructure), 27 * and a value (what data is currently in the field). 28 */ 29 public class AutofillField { 30 private int mSaveType = 0; 31 private String[] mHints; 32 private AutofillId mId; 33 private int mAutofillType; 34 private String[] mAutofillOptions; 35 private boolean mFocused; 36 37 public AutofillField(AssistStructure.ViewNode view) { 38 mId = view.getAutofillId(); 39 setHints(view.getAutofillHints()); 40 mAutofillType = view.getAutofillType(); 41 mAutofillOptions = view.getAutofillOptions(); 42 mFocused = view.isFocused(); 43 } 44 45 public String[] getHints() { 46 return mHints; 47 } 48 49 public void setHints(String[] hints) { 50 mHints = hints; 51 updateSaveTypeFromHints(); 52 } 53 54 public int getSaveType() { 55 return mSaveType; 56 } 57 58 public AutofillId getId() { 59 return mId; 60 } 61 62 public void setId(AutofillId id) { 63 mId = id; 64 } 65 66 public int getAutofillType() { 67 return mAutofillType; 68 } 69 70 public int getAutofillOptionIndex(String value) { 71 for (int i = 0; i < mAutofillOptions.length; i++) { 72 if (mAutofillOptions[i].equals(value)) { 73 return i; 74 } 75 } 76 return -1; 77 } 78 79 public boolean isFocused() { 80 return mFocused; 81 } 82 83 private void updateSaveTypeFromHints() { 84 mSaveType = 0; 85 if (mHints == null) { 86 return; 87 } 88 for (String hint : mHints) { 89 switch (hint) { 90 case View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE: 91 case View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY: 92 case View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH: 93 case View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR: 94 case View.AUTOFILL_HINT_CREDIT_CARD_NUMBER: 95 case View.AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE: 96 mSaveType |= SaveInfo.SAVE_DATA_TYPE_CREDIT_CARD; 97 break; 98 case View.AUTOFILL_HINT_EMAIL_ADDRESS: 99 mSaveType |= SaveInfo.SAVE_DATA_TYPE_EMAIL_ADDRESS; 100 break; 101 case View.AUTOFILL_HINT_PHONE: 102 case View.AUTOFILL_HINT_NAME: 103 mSaveType |= SaveInfo.SAVE_DATA_TYPE_GENERIC; 104 break; 105 case View.AUTOFILL_HINT_PASSWORD: 106 mSaveType |= SaveInfo.SAVE_DATA_TYPE_PASSWORD; 107 mSaveType &= ~SaveInfo.SAVE_DATA_TYPE_EMAIL_ADDRESS; 108 mSaveType &= ~SaveInfo.SAVE_DATA_TYPE_USERNAME; 109 break; 110 case View.AUTOFILL_HINT_POSTAL_ADDRESS: 111 case View.AUTOFILL_HINT_POSTAL_CODE: 112 mSaveType |= SaveInfo.SAVE_DATA_TYPE_ADDRESS; 113 break; 114 case View.AUTOFILL_HINT_USERNAME: 115 mSaveType |= SaveInfo.SAVE_DATA_TYPE_USERNAME; 116 break; 117 } 118 } 119 } 120 } 121