blob: 8ebae0a338db282b362a4e6a8ddfe3f8c6fdc72f [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
Gary Mai98868d32016-09-14 11:55:04 -070019import android.text.TextUtils;
20
Gary Mai69c182a2016-12-05 13:07:03 -080021import com.android.contacts.model.RawContactDelta;
22import com.android.contacts.model.ValuesDelta;
23import com.android.contacts.model.account.AccountType;
24import com.android.contacts.model.account.AccountType.EditField;
25import com.android.contacts.model.dataitem.DataKind;
Walter Jang363d3fd2015-09-16 10:29:07 -070026
Walter Jang228e02f2015-11-13 09:11:01 -080027import java.util.ArrayList;
Walter Jang363d3fd2015-09-16 10:29:07 -070028import java.util.List;
29
30/**
31 * Holder for the multi account raw contact data needed to back an editor input field.
32 */
33public final class KindSectionData {
34
35 private final AccountType mAccountType;
Walter Jang363d3fd2015-09-16 10:29:07 -070036 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 Jang363d3fd2015-09-16 10:29:07 -070044 }
45
46 public AccountType getAccountType() {
47 return mAccountType;
48 }
49
Walter Jang228e02f2015-11-13 09:11:01 -080050 /** Returns all ValuesDeltas for the data kind this section represents.*/
Walter Jang363d3fd2015-09-16 10:29:07 -070051 public List<ValuesDelta> getValuesDeltas() {
Walter Jang228e02f2015-11-13 09:11:01 -080052 final List<ValuesDelta> valuesDeltas = mRawContactDelta.getMimeEntries(mDataKind.mimeType);
53 return valuesDeltas == null ? new ArrayList<ValuesDelta>() : valuesDeltas;
54 }
55
Tingting Wangd482e0c2015-11-16 10:13:29 -080056 /** Returns visible and non deleted ValuesDeltas for the data kind this section represents. */
Wenyi Wang1b4ccf12015-11-13 14:00:09 -080057 public List<ValuesDelta> getVisibleValuesDeltas() {
Walter Jang228e02f2015-11-13 09:11:01 -080058 final ArrayList<ValuesDelta> valuesDeltas = new ArrayList<> ();
59 for (ValuesDelta valuesDelta : getValuesDeltas()) {
60 // Same conditions as KindSectionView#rebuildFromState
Tingting Wangd482e0c2015-11-16 10:13:29 -080061 if (valuesDelta.isVisible() && !valuesDelta.isDelete()) {
Walter Jang228e02f2015-11-13 09:11:01 -080062 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 Jang363d3fd2015-09-16 10:29:07 -070077 }
78
Walter Jang3e5ae0d2015-09-20 12:43:37 -070079 /** Returns the super primary ValuesDelta for the data kind this section represents. */
Walter Jang363d3fd2015-09-16 10:29:07 -070080 public ValuesDelta getSuperPrimaryValuesDelta() {
Walter Jang228e02f2015-11-13 09:11:01 -080081 for (ValuesDelta valuesDelta : getValuesDeltas()) {
Walter Jang363d3fd2015-09-16 10:29:07 -070082 if (valuesDelta.isSuperPrimary()) return valuesDelta;
83 }
84 return null;
85 }
86
Walter Jang3e5ae0d2015-09-20 12:43:37 -070087 /** Returns the ValuesDelta with the given ID. */
Walter Jang363d3fd2015-09-16 10:29:07 -070088 public ValuesDelta getValuesDeltaById(Long id) {
Walter Jang228e02f2015-11-13 09:11:01 -080089 for (ValuesDelta valuesDelta : getValuesDeltas()) {
Walter Jang363d3fd2015-09-16 10:29:07 -070090 if (valuesDelta.getId().equals(id)) return valuesDelta;
91 }
92 return null;
93 }
94
Walter Jang3e5ae0d2015-09-20 12:43:37 -070095 /** Returns the first non empty ValuesDelta for the data kind this section represents. */
Walter Jang363d3fd2015-09-16 10:29:07 -070096 public ValuesDelta getFirstNonEmptyValuesDelta() {
Walter Jang228e02f2015-11-13 09:11:01 -080097 for (ValuesDelta valuesDelta : getValuesDeltas()) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -070098 if (!isEmpty(valuesDelta)) return valuesDelta;
Walter Jang363d3fd2015-09-16 10:29:07 -070099 }
100 return null;
101 }
102
Walter Jang228e02f2015-11-13 09:11:01 -0800103 private boolean isEmpty(ValuesDelta valuesDelta) {
Walter Jang363d3fd2015-09-16 10:29:07 -0700104 if (mDataKind.fieldList != null) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700105 for (EditField editField : mDataKind.fieldList) {
106 final String column = editField.column;
107 final String value = valuesDelta.getAsString(column);
Walter Jang228e02f2015-11-13 09:11:01 -0800108 if (!TextUtils.isEmpty(value)) return false;
Walter Jang363d3fd2015-09-16 10:29:07 -0700109 }
110 }
Walter Jang228e02f2015-11-13 09:11:01 -0800111 return true;
Walter Jang363d3fd2015-09-16 10:29:07 -0700112 }
113
114 public DataKind getDataKind() {
115 return mDataKind;
116 }
117
118 public RawContactDelta getRawContactDelta() {
119 return mRawContactDelta;
120 }
Gary Mai98868d32016-09-14 11:55:04 -0700121
122 public String getMimeType() {
123 return mDataKind.mimeType;
124 }
Walter Jang228e02f2015-11-13 09:11:01 -0800125}