blob: e9160decd702832dedf199883249e082f2eff28a [file] [log] [blame]
Walter Jang363d3fd2015-09-16 10:29:07 -07001/*
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
17package com.android.contacts.editor;
18
19import com.android.contacts.common.model.RawContactDelta;
20import com.android.contacts.common.model.ValuesDelta;
21import com.android.contacts.common.model.account.AccountType;
22import com.android.contacts.common.model.account.AccountType.EditField;
23import com.android.contacts.common.model.dataitem.DataKind;
24
25import android.provider.ContactsContract.CommonDataKinds.StructuredName;
26import android.text.TextUtils;
27
28import java.util.ArrayList;
29import java.util.Collections;
30import java.util.List;
31
32/**
33 * Holder for the multi account raw contact data needed to back an editor input field.
34 */
35public final class KindSectionData {
36
37 private final AccountType mAccountType;
38 private final List<ValuesDelta> mValuesDeltas;
39 private final DataKind mDataKind;
40 private final RawContactDelta mRawContactDelta;
41
42 public KindSectionData(AccountType accountType, DataKind dataKind,
43 RawContactDelta rawContactDelta) {
44 mAccountType = accountType;
45 mDataKind = dataKind;
46 mRawContactDelta = rawContactDelta;
47
48 // Note that for phonetic names we use the structured name mime type to look up values
49 final String mimeType = DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME.equals(dataKind.mimeType)
50 ? StructuredName.CONTENT_ITEM_TYPE : dataKind.mimeType;
51 mValuesDeltas = mRawContactDelta.hasMimeEntries(mimeType)
52 ? mRawContactDelta.getMimeEntries(mimeType)
53 : Collections.EMPTY_LIST;
54 }
55
56 public AccountType getAccountType() {
57 return mAccountType;
58 }
59
60 public boolean hasValuesDeltas() {
61 return !mValuesDeltas.isEmpty();
62 }
63
64 public List<ValuesDelta> getValuesDeltas() {
65 return mValuesDeltas;
66 }
67
68 public ValuesDelta getSuperPrimaryValuesDelta() {
69 for (ValuesDelta valuesDelta : mValuesDeltas) {
70 if (valuesDelta.isSuperPrimary()) return valuesDelta;
71 }
72 return null;
73 }
74
75 public ValuesDelta getValuesDeltaById(Long id) {
76 for (ValuesDelta valuesDelta : mValuesDeltas) {
77 if (valuesDelta.getId().equals(id)) return valuesDelta;
78 }
79 return null;
80 }
81
82 public ValuesDelta getFirstNonEmptyValuesDelta() {
83 if (mDataKind.fieldList != null) {
84 for (ValuesDelta valuesDelta : mValuesDeltas) {
85 for (EditField editField : mDataKind.fieldList) {
86 final String column = editField.column;
87 final String value = valuesDelta.getAsString(column);
88 if (!TextUtils.isEmpty(value)) return valuesDelta;
89 }
90 }
91 }
92 return null;
93 }
94
95 public boolean hasNonEmptyValuesDelta() {
96 return !getNonEmptyValuesDeltas().isEmpty();
97 }
98
99 public List<ValuesDelta> getNonEmptyValuesDeltas() {
100 final List<ValuesDelta> valuesDeltas = new ArrayList<>();
101 if (mDataKind.fieldList != null) {
102 for (ValuesDelta valuesDelta : mValuesDeltas) {
103 for (EditField editField : mDataKind.fieldList) {
104 final String column = editField.column;
105 final String value = valuesDelta.getAsString(column);
106 if (!TextUtils.isEmpty(value)) valuesDeltas.add(valuesDelta);
107 }
108 }
109 }
110 return valuesDeltas;
111 }
112
113 public DataKind getDataKind() {
114 return mDataKind;
115 }
116
117 public RawContactDelta getRawContactDelta() {
118 return mRawContactDelta;
119 }
120
121 public String toString() {
122 return String.format("%s<accountType=%s dataSet=%s values=%s>",
123 KindSectionData.class.getSimpleName(),
124 mAccountType.accountType,
125 mAccountType.dataSet,
126 hasValuesDeltas() ? getValuesDeltas().size() : "null");
127 }
128}