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 38 import com.android.contacts.common.model.dataitem.DataKind; 39 40 /** 41 * This is the base class for data items, which represents a row from the Data table. 42 */ 43 public class DataItem { 44 45 private final ContentValues mContentValues; 46 47 protected DataItem(ContentValues values) { 48 mContentValues = values; 49 } 50 51 /** 52 * Factory for creating subclasses of DataItem objects based on the mimetype in the 53 * content values. Raw contact is the raw contact that this data item is associated with. 54 */ 55 public static DataItem createFrom(ContentValues values) { 56 final String mimeType = values.getAsString(Data.MIMETYPE); 57 if (GroupMembership.CONTENT_ITEM_TYPE.equals(mimeType)) { 58 return new GroupMembershipDataItem(values); 59 } else if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) { 60 return new StructuredNameDataItem(values); 61 } else if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)) { 62 return new PhoneDataItem(values); 63 } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) { 64 return new EmailDataItem(values); 65 } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType)) { 66 return new StructuredPostalDataItem(values); 67 } else if (Im.CONTENT_ITEM_TYPE.equals(mimeType)) { 68 return new ImDataItem(values); 69 } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType)) { 70 return new OrganizationDataItem(values); 71 } else if (Nickname.CONTENT_ITEM_TYPE.equals(mimeType)) { 72 return new NicknameDataItem(values); 73 } else if (Note.CONTENT_ITEM_TYPE.equals(mimeType)) { 74 return new NoteDataItem(values); 75 } else if (Website.CONTENT_ITEM_TYPE.equals(mimeType)) { 76 return new WebsiteDataItem(values); 77 } else if (SipAddress.CONTENT_ITEM_TYPE.equals(mimeType)) { 78 return new SipAddressDataItem(values); 79 } else if (Event.CONTENT_ITEM_TYPE.equals(mimeType)) { 80 return new EventDataItem(values); 81 } else if (Relation.CONTENT_ITEM_TYPE.equals(mimeType)) { 82 return new RelationDataItem(values); 83 } else if (Identity.CONTENT_ITEM_TYPE.equals(mimeType)) { 84 return new IdentityDataItem(values); 85 } else if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) { 86 return new PhotoDataItem(values); 87 } 88 89 // generic 90 return new DataItem(values); 91 } 92 93 public ContentValues getContentValues() { 94 return mContentValues; 95 } 96 97 public void setRawContactId(long rawContactId) { 98 mContentValues.put(Data.RAW_CONTACT_ID, rawContactId); 99 } 100 101 /** 102 * Returns the data id. 103 */ 104 public long getId() { 105 return mContentValues.getAsLong(Data._ID); 106 } 107 108 public long getRawContactId() { 109 return mContentValues.getAsLong(Data.RAW_CONTACT_ID); 110 } 111 /** 112 * Returns the mimetype of the data. 113 */ 114 public String getMimeType() { 115 return mContentValues.getAsString(Data.MIMETYPE); 116 } 117 118 public void setMimeType(String mimeType) { 119 mContentValues.put(Data.MIMETYPE, mimeType); 120 } 121 122 public boolean isPrimary() { 123 Integer primary = mContentValues.getAsInteger(Data.IS_PRIMARY); 124 return primary != null && primary != 0; 125 } 126 127 public boolean isSuperPrimary() { 128 Integer superPrimary = mContentValues.getAsInteger(Data.IS_SUPER_PRIMARY); 129 return superPrimary != null && superPrimary != 0; 130 } 131 132 public int getDataVersion() { 133 return mContentValues.getAsInteger(Data.DATA_VERSION); 134 } 135 136 public boolean hasKindTypeColumn(DataKind kind) { 137 final String key = kind.typeColumn; 138 return key != null && mContentValues.containsKey(key); 139 } 140 141 public int getKindTypeColumn(DataKind kind) { 142 final String key = kind.typeColumn; 143 return mContentValues.getAsInteger(key); 144 } 145 146 /** 147 * This builds the data string depending on the type of data item by using the generic 148 * DataKind object underneath. 149 */ 150 public String buildDataString(Context context, DataKind kind) { 151 if (kind.actionBody == null) { 152 return null; 153 } 154 CharSequence actionBody = kind.actionBody.inflateUsing(context, mContentValues); 155 return actionBody == null ? null : actionBody.toString(); 156 } 157 158 /** 159 * This builds the data string(intended for display) depending on the type of data item. It 160 * returns the same value as {@link #buildDataString} by default, but certain data items can 161 * override it to provide their version of formatted data strings. 162 * 163 * @return Data string representing the data item, possibly formatted for display 164 */ 165 public String buildDataStringForDisplay(Context context, DataKind kind) { 166 return buildDataString(context, kind); 167 } 168 } 169