• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.provider.ContactsContract.CommonDataKinds.Email;
21 import android.provider.ContactsContract.CommonDataKinds.Event;
22 import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
23 import android.provider.ContactsContract.CommonDataKinds.Identity;
24 import android.provider.ContactsContract.CommonDataKinds.Im;
25 import android.provider.ContactsContract.CommonDataKinds.Nickname;
26 import android.provider.ContactsContract.CommonDataKinds.Note;
27 import android.provider.ContactsContract.CommonDataKinds.Organization;
28 import android.provider.ContactsContract.CommonDataKinds.Phone;
29 import android.provider.ContactsContract.CommonDataKinds.Photo;
30 import android.provider.ContactsContract.CommonDataKinds.Relation;
31 import android.provider.ContactsContract.CommonDataKinds.SipAddress;
32 import android.provider.ContactsContract.CommonDataKinds.StructuredName;
33 import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
34 import android.provider.ContactsContract.CommonDataKinds.Website;
35 import android.provider.ContactsContract.Contacts.Data;
36 
37 import com.android.contacts.model.AccountTypeManager;
38 import com.android.contacts.model.RawContact;
39 import com.android.contacts.model.account.AccountType;
40 
41 /**
42  * This is the base class for data items, which represents a row from the Data table.
43  */
44 public class DataItem {
45 
46     private final ContentValues mContentValues;
47 
48     /**
49      * The raw contact that this data item is associated with.  This can be null.
50      */
51     private final RawContact mRawContact;
52     private DataKind mDataKind;
53 
DataItem(RawContact rawContact, ContentValues values)54     protected DataItem(RawContact rawContact, ContentValues values) {
55         mContentValues = values;
56         mRawContact = rawContact;
57     }
58 
59     /**
60      * Factory for creating subclasses of DataItem objects based on the mimetype in the
61      * content values.  Raw contact is the raw contact that this data item is associated with.
62      */
createFrom(RawContact rawContact, ContentValues values)63     public static DataItem createFrom(RawContact rawContact, ContentValues values) {
64         final String mimeType = values.getAsString(Data.MIMETYPE);
65         if (GroupMembership.CONTENT_ITEM_TYPE.equals(mimeType)) {
66             return new GroupMembershipDataItem(rawContact, values);
67         } else if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) {
68             return new StructuredNameDataItem(rawContact, values);
69         } else if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)) {
70             return new PhoneDataItem(rawContact, values);
71         } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) {
72             return new EmailDataItem(rawContact, values);
73         } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType)) {
74             return new StructuredPostalDataItem(rawContact, values);
75         } else if (Im.CONTENT_ITEM_TYPE.equals(mimeType)) {
76             return new ImDataItem(rawContact, values);
77         } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType)) {
78             return new OrganizationDataItem(rawContact, values);
79         } else if (Nickname.CONTENT_ITEM_TYPE.equals(mimeType)) {
80             return new NicknameDataItem(rawContact, values);
81         } else if (Note.CONTENT_ITEM_TYPE.equals(mimeType)) {
82             return new NoteDataItem(rawContact, values);
83         } else if (Website.CONTENT_ITEM_TYPE.equals(mimeType)) {
84             return new WebsiteDataItem(rawContact, values);
85         } else if (SipAddress.CONTENT_ITEM_TYPE.equals(mimeType)) {
86             return new SipAddressDataItem(rawContact, values);
87         } else if (Event.CONTENT_ITEM_TYPE.equals(mimeType)) {
88             return new EventDataItem(rawContact, values);
89         } else if (Relation.CONTENT_ITEM_TYPE.equals(mimeType)) {
90             return new RelationDataItem(rawContact, values);
91         } else if (Identity.CONTENT_ITEM_TYPE.equals(mimeType)) {
92             return new IdentityDataItem(rawContact, values);
93         } else if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) {
94             return new PhotoDataItem(rawContact, values);
95         }
96 
97         // generic
98         return new DataItem(rawContact, values);
99     }
100 
getContentValues()101     public ContentValues getContentValues() {
102         return mContentValues;
103     }
104 
getRawContact()105     protected RawContact getRawContact() {
106         return mRawContact;
107     }
108 
setRawContactId(long rawContactId)109     public void setRawContactId(long rawContactId) {
110         mContentValues.put(Data.RAW_CONTACT_ID, rawContactId);
111     }
112 
113     /**
114      * Returns the data id.
115      */
getId()116     public long getId() {
117         return mContentValues.getAsLong(Data._ID);
118     }
119 
getRawContactId()120     public long getRawContactId() {
121         return mContentValues.getAsLong(Data.RAW_CONTACT_ID);
122     }
123     /**
124      * Returns the mimetype of the data.
125      */
getMimeType()126     public String getMimeType() {
127         return mContentValues.getAsString(Data.MIMETYPE);
128     }
129 
setMimeType(String mimeType)130     public void setMimeType(String mimeType) {
131         mContentValues.put(Data.MIMETYPE, mimeType);
132     }
133 
isPrimary()134     public boolean isPrimary() {
135         Integer primary = mContentValues.getAsInteger(Data.IS_PRIMARY);
136         return primary != null && primary != 0;
137     }
138 
isSuperPrimary()139     public boolean isSuperPrimary() {
140         Integer superPrimary = mContentValues.getAsInteger(Data.IS_SUPER_PRIMARY);
141         return superPrimary != null && superPrimary != 0;
142     }
143 
getDataVersion()144     public int getDataVersion() {
145         return mContentValues.getAsInteger(Data.DATA_VERSION);
146     }
147 
getAccountTypeManager()148     public AccountTypeManager getAccountTypeManager() {
149         if (mRawContact == null) {
150             return null;
151         } else {
152             return mRawContact.getAccountTypeManager();
153         }
154     }
155 
getAccountType()156     public AccountType getAccountType() {
157         if (mRawContact == null) {
158             return null;
159         } else {
160             return mRawContact.getAccountType();
161         }
162     }
163 
164     /**
165      * This method can only be invoked if the raw contact is non-null.
166      */
getDataKind()167     public DataKind getDataKind() {
168         if (mRawContact == null) {
169             throw new IllegalStateException("mRawContact must be non-null to call getDataKind()");
170         }
171 
172         if (mDataKind == null) {
173             mDataKind = getAccountTypeManager().getKindOrFallback(
174                     mRawContact.getAccountTypeString(), mRawContact.getDataSet(), getMimeType());
175         }
176 
177         return mDataKind;
178     }
179 
hasKindTypeColumn()180     public boolean hasKindTypeColumn() {
181         final String key = getDataKind().typeColumn;
182         return key != null && mContentValues.containsKey(key);
183     }
184 
getKindTypeColumn()185     public int getKindTypeColumn() {
186         final String key = getDataKind().typeColumn;
187         return mContentValues.getAsInteger(key);
188     }
189 
190     /**
191      * This builds the data string depending on the type of data item by using the generic
192      * DataKind object underneath.  This DataItem object must be associated with a raw contact
193      * for this function to work.
194      */
buildDataString()195     public String buildDataString() {
196         if (mRawContact == null) {
197             throw new IllegalStateException("mRawContact must be non-null to call getDataKind()");
198         }
199         final DataKind kind = getDataKind();
200 
201         if (kind.actionBody == null) {
202             return null;
203         }
204         CharSequence actionBody = kind.actionBody.inflateUsing(mRawContact.getContext(),
205                 mContentValues);
206         return actionBody == null ? null : actionBody.toString();
207     }
208 
209     /**
210      * This builds the data string(intended for display) depending on the type of data item. It
211      * returns the same value as {@link #buildDataString} by default, but certain data items can
212      * override it to provide their version of formatted data strings.
213      *
214      * @return Data string representing the data item, possibly formatted for display
215      */
buildDataStringForDisplay()216     public String buildDataStringForDisplay() {
217         return buildDataString();
218     }
219 
getKindString()220     public String getKindString() {
221         final DataKind kind = getDataKind();
222         return (kind.titleRes == -1 || kind.titleRes == 0) ? ""
223                 : mRawContact.getContext().getString(kind.titleRes);
224     }
225 }
226