Home | History | Annotate | Download | only in multidatasetservice
      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.multidatasetservice
     17 
     18 import android.app.assist.AssistStructure.ViewNode;
     19 import android.service.autofill.SaveInfo
     20 import android.view.View
     21 import android.view.autofill.AutofillId
     22 
     23 /**
     24  * A stripped down version of a [ViewNode] that contains only autofill-relevant metadata. It also
     25  * contains a `saveType` flag that is calculated based on the [ViewNode]'s autofill hints.
     26  */
     27 class AutofillFieldMetadata(view: ViewNode) {
     28     var saveType = 0
     29         private set
     30 
     31     val autofillHints = view.autofillHints.filter(AutofillHelper::isValidHint).toTypedArray()
     32     val autofillId: AutofillId = view.autofillId
     33     val autofillType: Int = view.autofillType
     34     val autofillOptions: Array<CharSequence>? = view.autofillOptions
     35     val isFocused: Boolean = view.isFocused
     36 
     37     init {
     38         updateSaveTypeFromHints()
     39     }
     40 
     41     /**
     42      * When the [ViewNode] is a list that the user needs to choose a string from (i.e. a spinner),
     43      * this is called to return the index of a specific item in the list.
     44      */
     45     fun getAutofillOptionIndex(value: CharSequence): Int {
     46         if (autofillOptions != null) {
     47             return autofillOptions.indexOf(value)
     48         } else {
     49             return -1
     50         }
     51     }
     52 
     53     private fun updateSaveTypeFromHints() {
     54         saveType = 0
     55         for (hint in autofillHints) {
     56             when (hint) {
     57                 View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE,
     58                 View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY,
     59                 View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH,
     60                 View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR,
     61                 View.AUTOFILL_HINT_CREDIT_CARD_NUMBER,
     62                 View.AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE -> {
     63                     saveType = saveType or SaveInfo.SAVE_DATA_TYPE_CREDIT_CARD
     64                 }
     65                 View.AUTOFILL_HINT_EMAIL_ADDRESS -> {
     66                     saveType = saveType or SaveInfo.SAVE_DATA_TYPE_EMAIL_ADDRESS
     67                 }
     68                 View.AUTOFILL_HINT_PHONE, View.AUTOFILL_HINT_NAME -> {
     69                     saveType = saveType or SaveInfo.SAVE_DATA_TYPE_GENERIC
     70                 }
     71                 View.AUTOFILL_HINT_PASSWORD -> {
     72                     saveType = saveType or SaveInfo.SAVE_DATA_TYPE_PASSWORD
     73                     saveType = saveType and SaveInfo.SAVE_DATA_TYPE_EMAIL_ADDRESS.inv()
     74                     saveType = saveType and SaveInfo.SAVE_DATA_TYPE_USERNAME.inv()
     75                 }
     76                 View.AUTOFILL_HINT_POSTAL_ADDRESS,
     77                 View.AUTOFILL_HINT_POSTAL_CODE -> {
     78                     saveType = saveType or SaveInfo.SAVE_DATA_TYPE_ADDRESS
     79                 }
     80                 View.AUTOFILL_HINT_USERNAME -> {
     81                     saveType = saveType or SaveInfo.SAVE_DATA_TYPE_USERNAME
     82                 }
     83             }
     84         }
     85     }
     86 }
     87