Home | History | Annotate | Download | only in service
      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.app.assist.AssistStructure.ViewNode;
     19 import android.view.autofill.AutofillId;
     20 
     21 /**
     22  * A stripped down version of a {@link ViewNode} that contains only autofill-relevant metadata. It
     23  * also contains a {@code mSaveType} flag that is calculated based on the {@link ViewNode}]'s
     24  * autofill hints.
     25  */
     26 public class AutofillFieldMetadata {
     27     private int mSaveType = 0;
     28     private String[] mAutofillHints;
     29     private AutofillId mAutofillId;
     30     private int mAutofillType;
     31     private CharSequence[] mAutofillOptions;
     32     private boolean mFocused;
     33 
     34     public AutofillFieldMetadata(ViewNode view) {
     35         mAutofillId = view.getAutofillId();
     36         mAutofillType = view.getAutofillType();
     37         mAutofillOptions = view.getAutofillOptions();
     38         mFocused = view.isFocused();
     39         String[] hints = AutofillHints.filterForSupportedHints(view.getAutofillHints());
     40         if (hints != null) {
     41             AutofillHints.convertToStoredHintNames(hints);
     42             setHints(hints);
     43         }
     44     }
     45 
     46     public String[] getHints() {
     47         return mAutofillHints;
     48     }
     49 
     50     public void setHints(String[] hints) {
     51         mAutofillHints = hints;
     52         mSaveType = AutofillHints.getSaveTypeForHints(hints);
     53     }
     54 
     55     public int getSaveType() {
     56         return mSaveType;
     57     }
     58 
     59     public AutofillId getId() {
     60         return mAutofillId;
     61     }
     62 
     63     public int getAutofillType() {
     64         return mAutofillType;
     65     }
     66 
     67     /**
     68      * When the {@link ViewNode} is a list that the user needs to choose a string from (i.e. a
     69      * spinner), this is called to return the index of a specific item in the list.
     70      */
     71     public int getAutofillOptionIndex(String value) {
     72         for (int i = 0; i < mAutofillOptions.length; i++) {
     73             if (mAutofillOptions[i].toString().equals(value)) {
     74                 return i;
     75             }
     76         }
     77         return -1;
     78     }
     79 
     80     public boolean isFocused() {
     81         return mFocused;
     82     }
     83 }
     84