Home | History | Annotate | Download | only in editor
      1 /*
      2  * Copyright (C) 2015 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 
     17 package com.android.contacts.editor;
     18 
     19 import com.android.contacts.common.model.RawContactDelta;
     20 import com.android.contacts.common.model.ValuesDelta;
     21 import com.android.contacts.common.model.account.AccountType;
     22 import com.android.contacts.common.model.account.AccountType.EditField;
     23 import com.android.contacts.common.model.dataitem.DataKind;
     24 
     25 import android.text.TextUtils;
     26 
     27 import java.util.ArrayList;
     28 import java.util.List;
     29 
     30 /**
     31  * Holder for the multi account raw contact data needed to back an editor input field.
     32  */
     33 public final class KindSectionData {
     34 
     35     private final AccountType mAccountType;
     36     private final DataKind mDataKind;
     37     private final RawContactDelta mRawContactDelta;
     38 
     39     public KindSectionData(AccountType accountType, DataKind dataKind,
     40             RawContactDelta rawContactDelta) {
     41         mAccountType = accountType;
     42         mDataKind = dataKind;
     43         mRawContactDelta = rawContactDelta;
     44     }
     45 
     46     public AccountType getAccountType() {
     47         return mAccountType;
     48     }
     49 
     50     /** Returns all ValuesDeltas for the data kind this section represents.*/
     51     public List<ValuesDelta> getValuesDeltas() {
     52         final List<ValuesDelta> valuesDeltas = mRawContactDelta.getMimeEntries(mDataKind.mimeType);
     53         return valuesDeltas == null ? new ArrayList<ValuesDelta>() : valuesDeltas;
     54     }
     55 
     56     /** Returns visible and non deleted ValuesDeltas for the data kind this section represents. */
     57     public List<ValuesDelta> getVisibleValuesDeltas() {
     58         final ArrayList<ValuesDelta> valuesDeltas = new ArrayList<> ();
     59         for (ValuesDelta valuesDelta : getValuesDeltas()) {
     60             // Same conditions as KindSectionView#rebuildFromState
     61             if (valuesDelta.isVisible() && !valuesDelta.isDelete()) {
     62                 valuesDeltas.add(valuesDelta);
     63             }
     64         }
     65         return valuesDeltas;
     66     }
     67 
     68     /** Returns non-empty ValuesDeltas for the data kind this section represents. */
     69     public List<ValuesDelta> getNonEmptyValuesDeltas() {
     70         final ArrayList<ValuesDelta> valuesDeltas = new ArrayList<> ();
     71         for (ValuesDelta valuesDelta : getValuesDeltas()) {
     72             if (!isEmpty(valuesDelta)) {
     73                 valuesDeltas.add(valuesDelta);
     74             }
     75         }
     76         return valuesDeltas;
     77     }
     78 
     79     /** Returns the super primary ValuesDelta for the data kind this section represents. */
     80     public ValuesDelta getSuperPrimaryValuesDelta() {
     81         for (ValuesDelta valuesDelta : getValuesDeltas()) {
     82             if (valuesDelta.isSuperPrimary()) return valuesDelta;
     83         }
     84         return null;
     85     }
     86 
     87     /** Returns the ValuesDelta with the given ID. */
     88     public ValuesDelta getValuesDeltaById(Long id) {
     89         for (ValuesDelta valuesDelta : getValuesDeltas()) {
     90             if (valuesDelta.getId().equals(id)) return valuesDelta;
     91         }
     92         return null;
     93     }
     94 
     95     /** Returns the first non empty ValuesDelta for the data kind this section represents. */
     96     public ValuesDelta getFirstNonEmptyValuesDelta() {
     97         for (ValuesDelta valuesDelta : getValuesDeltas()) {
     98             if (!isEmpty(valuesDelta)) return valuesDelta;
     99         }
    100         return null;
    101     }
    102 
    103     private boolean isEmpty(ValuesDelta valuesDelta) {
    104         if (mDataKind.fieldList != null) {
    105             for (EditField editField : mDataKind.fieldList) {
    106                 final String column = editField.column;
    107                 final String value = valuesDelta.getAsString(column);
    108                 if (!TextUtils.isEmpty(value)) return false;
    109             }
    110         }
    111         return true;
    112     }
    113 
    114     public DataKind getDataKind() {
    115         return mDataKind;
    116     }
    117 
    118     public RawContactDelta getRawContactDelta() {
    119         return mRawContactDelta;
    120     }
    121 }
    122