Home | History | Annotate | Download | only in model
      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.model;
     17 
     18 import android.util.Log;
     19 import android.view.View;
     20 
     21 import com.example.android.autofill.service.AutofillHints;
     22 import com.google.common.base.Preconditions;
     23 import com.google.gson.annotations.Expose;
     24 
     25 import java.util.Arrays;
     26 
     27 import static com.example.android.autofill.service.CommonUtil.TAG;
     28 import static com.example.android.autofill.service.AutofillHints.convertToStoredHintNames;
     29 import static com.example.android.autofill.service.AutofillHints.filterForSupportedHints;
     30 
     31 /**
     32  * JSON serializable data class containing the same data as an {@link AutofillValue}.
     33  */
     34 public class FilledAutofillField {
     35     @Expose
     36     private String mTextValue = null;
     37     @Expose
     38     private Long mDateValue = null;
     39     @Expose
     40     private Boolean mToggleValue = null;
     41 
     42     //TODO add explicit mListValue
     43 
     44     /**
     45      * Does not need to be serialized into persistent storage, so it's not exposed.
     46      */
     47     private String[] mAutofillHints = null;
     48 
     49     public FilledAutofillField(String... hints) {
     50         mAutofillHints = filterForSupportedHints(hints);
     51         convertToStoredHintNames(mAutofillHints);
     52     }
     53 
     54     public void setListValue(CharSequence[] autofillOptions, int listValue) {
     55         /* Only set list value when a hint is allowed to store list values. */
     56         Preconditions.checkArgument(
     57                 AutofillHints.isValidTypeForHints(mAutofillHints, View.AUTOFILL_TYPE_LIST),
     58                 "List is invalid autofill type for hint(s) - %s",
     59                 Arrays.toString(mAutofillHints));
     60         if (autofillOptions != null && autofillOptions.length > 0) {
     61             mTextValue = autofillOptions[listValue].toString();
     62         } else {
     63             Log.w(TAG, "autofillOptions should have at least one entry.");
     64         }
     65     }
     66 
     67     public String[] getAutofillHints() {
     68         return mAutofillHints;
     69     }
     70 
     71     public String getTextValue() {
     72         return mTextValue;
     73     }
     74 
     75     public void setTextValue(CharSequence textValue) {
     76         /* Only set text value when a hint is allowed to store text values. */
     77         Preconditions.checkArgument(
     78                 AutofillHints.isValidTypeForHints(mAutofillHints, View.AUTOFILL_TYPE_TEXT),
     79                 "Text is invalid autofill type for hint(s) - %s",
     80                 Arrays.toString(mAutofillHints));
     81         mTextValue = textValue.toString();
     82     }
     83 
     84     public Long getDateValue() {
     85         return mDateValue;
     86     }
     87 
     88     public void setDateValue(Long dateValue) {
     89         /* Only set date value when a hint is allowed to store date values. */
     90         Preconditions.checkArgument(
     91                 AutofillHints.isValidTypeForHints(mAutofillHints, View.AUTOFILL_TYPE_DATE),
     92                 "Date is invalid autofill type for hint(s) - %s"
     93                 , Arrays.toString(mAutofillHints));
     94         mDateValue = dateValue;
     95     }
     96 
     97     public Boolean getToggleValue() {
     98         return mToggleValue;
     99     }
    100 
    101     public void setToggleValue(Boolean toggleValue) {
    102         /* Only set toggle value when a hint is allowed to store toggle values. */
    103         Preconditions.checkArgument(
    104                 AutofillHints.isValidTypeForHints(mAutofillHints, View.AUTOFILL_TYPE_TOGGLE),
    105                 "Toggle is invalid autofill type for hint(s) - %s",
    106                 Arrays.toString(mAutofillHints));
    107         mToggleValue = toggleValue;
    108     }
    109 
    110     public boolean isNull() {
    111         return mTextValue == null && mDateValue == null && mToggleValue == null;
    112     }
    113 
    114     @Override
    115     public boolean equals(Object o) {
    116         if (this == o) return true;
    117         if (o == null || getClass() != o.getClass()) return false;
    118 
    119         FilledAutofillField that = (FilledAutofillField) o;
    120 
    121         if (mTextValue != null ? !mTextValue.equals(that.mTextValue) : that.mTextValue != null)
    122             return false;
    123         if (mDateValue != null ? !mDateValue.equals(that.mDateValue) : that.mDateValue != null)
    124             return false;
    125         return mToggleValue != null ? mToggleValue.equals(that.mToggleValue) :
    126                 that.mToggleValue == null;
    127     }
    128 
    129     @Override
    130     public int hashCode() {
    131         int result = mTextValue != null ? mTextValue.hashCode() : 0;
    132         result = 31 * result + (mDateValue != null ? mDateValue.hashCode() : 0);
    133         result = 31 * result + (mToggleValue != null ? mToggleValue.hashCode() : 0);
    134         return result;
    135     }
    136 }
    137