1 /* 2 * Copyright (C) 2012 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.model.dataitem; 18 19 import android.content.ContentValues; 20 import android.content.Context; 21 import android.provider.ContactsContract.CommonDataKinds.Email; 22 import android.provider.ContactsContract.CommonDataKinds.Event; 23 import android.provider.ContactsContract.CommonDataKinds.GroupMembership; 24 import android.provider.ContactsContract.CommonDataKinds.Identity; 25 import android.provider.ContactsContract.CommonDataKinds.Im; 26 import android.provider.ContactsContract.CommonDataKinds.Nickname; 27 import android.provider.ContactsContract.CommonDataKinds.Note; 28 import android.provider.ContactsContract.CommonDataKinds.Organization; 29 import android.provider.ContactsContract.CommonDataKinds.Phone; 30 import android.provider.ContactsContract.CommonDataKinds.Photo; 31 import android.provider.ContactsContract.CommonDataKinds.Relation; 32 import android.provider.ContactsContract.CommonDataKinds.SipAddress; 33 import android.provider.ContactsContract.CommonDataKinds.StructuredName; 34 import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; 35 import android.provider.ContactsContract.CommonDataKinds.Website; 36 import android.provider.ContactsContract.Contacts.Data; 37 import com.android.contacts.Collapser; 38 import com.android.contacts.MoreContactUtils; 39 import com.android.contacts.model.RawContactModifier; 40 41 /** 42 * This is the base class for data items, which represents a row from the Data table. 43 */ 44 public class DataItem implements Collapser.Collapsible<DataItem> { 45 46 private final ContentValues mContentValues; 47 protected DataKind mKind; 48 DataItem(ContentValues values)49 protected DataItem(ContentValues values) { 50 mContentValues = values; 51 } 52 53 /** 54 * Factory for creating subclasses of DataItem objects based on the mimetype in the 55 * content values. Raw contact is the raw contact that this data item is associated with. 56 */ createFrom(ContentValues values)57 public static DataItem createFrom(ContentValues values) { 58 final String mimeType = values.getAsString(Data.MIMETYPE); 59 if (GroupMembership.CONTENT_ITEM_TYPE.equals(mimeType)) { 60 return new GroupMembershipDataItem(values); 61 } else if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) { 62 return new StructuredNameDataItem(values); 63 } else if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)) { 64 return new PhoneDataItem(values); 65 } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) { 66 return new EmailDataItem(values); 67 } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType)) { 68 return new StructuredPostalDataItem(values); 69 } else if (Im.CONTENT_ITEM_TYPE.equals(mimeType)) { 70 return new ImDataItem(values); 71 } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType)) { 72 return new OrganizationDataItem(values); 73 } else if (Nickname.CONTENT_ITEM_TYPE.equals(mimeType)) { 74 return new NicknameDataItem(values); 75 } else if (Note.CONTENT_ITEM_TYPE.equals(mimeType)) { 76 return new NoteDataItem(values); 77 } else if (Website.CONTENT_ITEM_TYPE.equals(mimeType)) { 78 return new WebsiteDataItem(values); 79 } else if (SipAddress.CONTENT_ITEM_TYPE.equals(mimeType)) { 80 return new SipAddressDataItem(values); 81 } else if (Event.CONTENT_ITEM_TYPE.equals(mimeType)) { 82 return new EventDataItem(values); 83 } else if (Relation.CONTENT_ITEM_TYPE.equals(mimeType)) { 84 return new RelationDataItem(values); 85 } else if (Identity.CONTENT_ITEM_TYPE.equals(mimeType)) { 86 return new IdentityDataItem(values); 87 } else if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) { 88 return new PhotoDataItem(values); 89 } else if (CustomDataItem.MIMETYPE_CUSTOM_FIELD.equals(mimeType)) { 90 return new CustomDataItem(values); 91 } 92 93 // generic 94 return new DataItem(values); 95 } 96 getContentValues()97 public ContentValues getContentValues() { 98 return mContentValues; 99 } 100 setRawContactId(long rawContactId)101 public void setRawContactId(long rawContactId) { 102 mContentValues.put(Data.RAW_CONTACT_ID, rawContactId); 103 } 104 getRawContactId()105 public Long getRawContactId() { 106 return mContentValues.getAsLong(Data.RAW_CONTACT_ID); 107 } 108 109 /** 110 * Returns the data id. 111 */ getId()112 public long getId() { 113 return mContentValues.getAsLong(Data._ID); 114 } 115 116 /** 117 * Returns the mimetype of the data. 118 */ getMimeType()119 public String getMimeType() { 120 return mContentValues.getAsString(Data.MIMETYPE); 121 } 122 setMimeType(String mimeType)123 public void setMimeType(String mimeType) { 124 mContentValues.put(Data.MIMETYPE, mimeType); 125 } 126 isPrimary()127 public boolean isPrimary() { 128 Integer primary = mContentValues.getAsInteger(Data.IS_PRIMARY); 129 return primary != null && primary != 0; 130 } 131 isSuperPrimary()132 public boolean isSuperPrimary() { 133 Integer superPrimary = mContentValues.getAsInteger(Data.IS_SUPER_PRIMARY); 134 return superPrimary != null && superPrimary != 0; 135 } 136 hasKindTypeColumn(DataKind kind)137 public boolean hasKindTypeColumn(DataKind kind) { 138 final String key = kind.typeColumn; 139 return key != null && mContentValues.containsKey(key) && 140 mContentValues.getAsInteger(key) != null; 141 } 142 getKindTypeColumn(DataKind kind)143 public int getKindTypeColumn(DataKind kind) { 144 final String key = kind.typeColumn; 145 return mContentValues.getAsInteger(key); 146 } 147 148 /** 149 * Indicates the carrier presence value for the current {@link DataItem}. 150 * 151 * @return {@link Data#CARRIER_PRESENCE_VT_CAPABLE} if the {@link DataItem} supports carrier 152 * video calling, {@code 0} otherwise. 153 */ getCarrierPresence()154 public int getCarrierPresence() { 155 final Integer value = mContentValues.getAsInteger(Data.CARRIER_PRESENCE); 156 return value != null ? value.intValue() : 0; 157 } 158 159 /** 160 * This builds the data string depending on the type of data item by using the generic 161 * DataKind object underneath. 162 */ buildDataString(Context context, DataKind kind)163 public String buildDataString(Context context, DataKind kind) { 164 if (kind.actionBody == null) { 165 return null; 166 } 167 CharSequence actionBody = kind.actionBody.inflateUsing(context, mContentValues); 168 return actionBody == null ? null : actionBody.toString(); 169 } 170 171 /** 172 * This builds the data string(intended for display) depending on the type of data item. It 173 * returns the same value as {@link #buildDataString} by default, but certain data items can 174 * override it to provide their version of formatted data strings. 175 * 176 * @return Data string representing the data item, possibly formatted for display 177 */ buildDataStringForDisplay(Context context, DataKind kind)178 public String buildDataStringForDisplay(Context context, DataKind kind) { 179 return buildDataString(context, kind); 180 } 181 setDataKind(DataKind kind)182 public void setDataKind(DataKind kind) { 183 mKind = kind; 184 } 185 getDataKind()186 public DataKind getDataKind() { 187 return mKind; 188 } 189 190 @Override collapseWith(DataItem that)191 public void collapseWith(DataItem that) { 192 DataKind thisKind = getDataKind(); 193 DataKind thatKind = that.getDataKind(); 194 // If this does not have a type and that does, or if that's type is higher precedence, 195 // use that's type 196 if ((!hasKindTypeColumn(thisKind) && that.hasKindTypeColumn(thatKind)) || 197 that.hasKindTypeColumn(thatKind) && 198 RawContactModifier.getTypePrecedence(thisKind, getKindTypeColumn(thisKind)) 199 > 200 RawContactModifier.getTypePrecedence(thatKind, that.getKindTypeColumn(thatKind))) { 201 mContentValues.put(thatKind.typeColumn, that.getKindTypeColumn(thatKind)); 202 mKind = thatKind; 203 } 204 205 // Choose the max of the maxLines and maxLabelLines values. 206 mKind.maxLinesForDisplay = Math.max(thisKind.maxLinesForDisplay, 207 thatKind.maxLinesForDisplay); 208 209 // If any of the collapsed entries are super primary make the whole thing super primary. 210 if (isSuperPrimary() || that.isSuperPrimary()) { 211 mContentValues.put(Data.IS_SUPER_PRIMARY, 1); 212 mContentValues.put(Data.IS_PRIMARY, 1); 213 } 214 215 // If any of the collapsed entries are primary make the whole thing primary. 216 if (isPrimary() || that.isPrimary()) { 217 mContentValues.put(Data.IS_PRIMARY, 1); 218 } 219 } 220 221 @Override shouldCollapseWith(DataItem t, Context context)222 public boolean shouldCollapseWith(DataItem t, Context context) { 223 if (mKind == null || t.getDataKind() == null) { 224 return false; 225 } 226 return MoreContactUtils.shouldCollapse(getMimeType(), buildDataString(context, mKind), 227 t.getMimeType(), t.buildDataString(context, t.getDataKind())); 228 } 229 } 230