Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 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 | |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 19 | import android.text.TextUtils; |
| 20 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 21 | import com.android.contacts.common.model.RawContactDelta; |
| 22 | import com.android.contacts.common.model.ValuesDelta; |
| 23 | import com.android.contacts.common.model.account.AccountType; |
| 24 | import com.android.contacts.common.model.account.AccountType.EditField; |
| 25 | import com.android.contacts.common.model.dataitem.DataKind; |
| 26 | |
Walter Jang | 228e02f | 2015-11-13 09:11:01 -0800 | [diff] [blame] | 27 | import java.util.ArrayList; |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 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; |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 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; |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | public AccountType getAccountType() { |
| 47 | return mAccountType; |
| 48 | } |
| 49 | |
Walter Jang | 228e02f | 2015-11-13 09:11:01 -0800 | [diff] [blame] | 50 | /** Returns all ValuesDeltas for the data kind this section represents.*/ |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 51 | public List<ValuesDelta> getValuesDeltas() { |
Walter Jang | 228e02f | 2015-11-13 09:11:01 -0800 | [diff] [blame] | 52 | final List<ValuesDelta> valuesDeltas = mRawContactDelta.getMimeEntries(mDataKind.mimeType); |
| 53 | return valuesDeltas == null ? new ArrayList<ValuesDelta>() : valuesDeltas; |
| 54 | } |
| 55 | |
Tingting Wang | d482e0c | 2015-11-16 10:13:29 -0800 | [diff] [blame] | 56 | /** Returns visible and non deleted ValuesDeltas for the data kind this section represents. */ |
Wenyi Wang | 1b4ccf1 | 2015-11-13 14:00:09 -0800 | [diff] [blame] | 57 | public List<ValuesDelta> getVisibleValuesDeltas() { |
Walter Jang | 228e02f | 2015-11-13 09:11:01 -0800 | [diff] [blame] | 58 | final ArrayList<ValuesDelta> valuesDeltas = new ArrayList<> (); |
| 59 | for (ValuesDelta valuesDelta : getValuesDeltas()) { |
| 60 | // Same conditions as KindSectionView#rebuildFromState |
Tingting Wang | d482e0c | 2015-11-16 10:13:29 -0800 | [diff] [blame] | 61 | if (valuesDelta.isVisible() && !valuesDelta.isDelete()) { |
Walter Jang | 228e02f | 2015-11-13 09:11:01 -0800 | [diff] [blame] | 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; |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 79 | /** Returns the super primary ValuesDelta for the data kind this section represents. */ |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 80 | public ValuesDelta getSuperPrimaryValuesDelta() { |
Walter Jang | 228e02f | 2015-11-13 09:11:01 -0800 | [diff] [blame] | 81 | for (ValuesDelta valuesDelta : getValuesDeltas()) { |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 82 | if (valuesDelta.isSuperPrimary()) return valuesDelta; |
| 83 | } |
| 84 | return null; |
| 85 | } |
| 86 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 87 | /** Returns the ValuesDelta with the given ID. */ |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 88 | public ValuesDelta getValuesDeltaById(Long id) { |
Walter Jang | 228e02f | 2015-11-13 09:11:01 -0800 | [diff] [blame] | 89 | for (ValuesDelta valuesDelta : getValuesDeltas()) { |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 90 | if (valuesDelta.getId().equals(id)) return valuesDelta; |
| 91 | } |
| 92 | return null; |
| 93 | } |
| 94 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 95 | /** Returns the first non empty ValuesDelta for the data kind this section represents. */ |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 96 | public ValuesDelta getFirstNonEmptyValuesDelta() { |
Walter Jang | 228e02f | 2015-11-13 09:11:01 -0800 | [diff] [blame] | 97 | for (ValuesDelta valuesDelta : getValuesDeltas()) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 98 | if (!isEmpty(valuesDelta)) return valuesDelta; |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 99 | } |
| 100 | return null; |
| 101 | } |
| 102 | |
Walter Jang | 228e02f | 2015-11-13 09:11:01 -0800 | [diff] [blame] | 103 | private boolean isEmpty(ValuesDelta valuesDelta) { |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 104 | if (mDataKind.fieldList != null) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 105 | for (EditField editField : mDataKind.fieldList) { |
| 106 | final String column = editField.column; |
| 107 | final String value = valuesDelta.getAsString(column); |
Walter Jang | 228e02f | 2015-11-13 09:11:01 -0800 | [diff] [blame] | 108 | if (!TextUtils.isEmpty(value)) return false; |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
Walter Jang | 228e02f | 2015-11-13 09:11:01 -0800 | [diff] [blame] | 111 | return true; |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | public DataKind getDataKind() { |
| 115 | return mDataKind; |
| 116 | } |
| 117 | |
| 118 | public RawContactDelta getRawContactDelta() { |
| 119 | return mRawContactDelta; |
| 120 | } |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 121 | |
| 122 | public String getMimeType() { |
| 123 | return mDataKind.mimeType; |
| 124 | } |
Walter Jang | 228e02f | 2015-11-13 09:11:01 -0800 | [diff] [blame] | 125 | } |