DataItem.java revision 851222a96b5d68602fb361ea3527101e893f67e3
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
17package com.android.contacts.model.dataitem;
18
19import android.content.ContentValues;
20import android.provider.ContactsContract.CommonDataKinds.Email;
21import android.provider.ContactsContract.CommonDataKinds.Event;
22import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
23import android.provider.ContactsContract.CommonDataKinds.Identity;
24import android.provider.ContactsContract.CommonDataKinds.Im;
25import android.provider.ContactsContract.CommonDataKinds.Nickname;
26import android.provider.ContactsContract.CommonDataKinds.Note;
27import android.provider.ContactsContract.CommonDataKinds.Organization;
28import android.provider.ContactsContract.CommonDataKinds.Phone;
29import android.provider.ContactsContract.CommonDataKinds.Photo;
30import android.provider.ContactsContract.CommonDataKinds.Relation;
31import android.provider.ContactsContract.CommonDataKinds.SipAddress;
32import android.provider.ContactsContract.CommonDataKinds.StructuredName;
33import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
34import android.provider.ContactsContract.CommonDataKinds.Website;
35import android.provider.ContactsContract.Contacts.Data;
36
37import com.android.contacts.model.AccountTypeManager;
38import com.android.contacts.model.RawContact;
39import 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 */
44public 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
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     */
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
101    public ContentValues getContentValues() {
102        return mContentValues;
103    }
104
105    protected RawContact getRawContact() {
106        return mRawContact;
107    }
108
109    public void setRawContactId(long rawContactId) {
110        mContentValues.put(Data.RAW_CONTACT_ID, rawContactId);
111    }
112
113    /**
114     * Returns the data id.
115     */
116    public long getId() {
117        return mContentValues.getAsLong(Data._ID);
118    }
119
120    public long getRawContactId() {
121        return mContentValues.getAsLong(Data.RAW_CONTACT_ID);
122    }
123    /**
124     * Returns the mimetype of the data.
125     */
126    public String getMimeType() {
127        return mContentValues.getAsString(Data.MIMETYPE);
128    }
129
130    public void setMimeType(String mimeType) {
131        mContentValues.put(Data.MIMETYPE, mimeType);
132    }
133
134    public boolean isPrimary() {
135        Integer primary = mContentValues.getAsInteger(Data.IS_PRIMARY);
136        return primary != null && primary != 0;
137    }
138
139    public boolean isSuperPrimary() {
140        Integer superPrimary = mContentValues.getAsInteger(Data.IS_SUPER_PRIMARY);
141        return superPrimary != null && superPrimary != 0;
142    }
143
144    public int getDataVersion() {
145        return mContentValues.getAsInteger(Data.DATA_VERSION);
146    }
147
148    public AccountTypeManager getAccountTypeManager() {
149        if (mRawContact == null) {
150            return null;
151        } else {
152            return mRawContact.getAccountTypeManager();
153        }
154    }
155
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     */
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
180    public boolean hasKindTypeColumn() {
181        final String key = getDataKind().typeColumn;
182        return key != null && mContentValues.containsKey(key);
183    }
184
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     */
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    public String getKindString() {
210        final DataKind kind = getDataKind();
211        return (kind.titleRes == -1 || kind.titleRes == 0) ? ""
212                : mRawContact.getContext().getString(kind.titleRes);
213    }
214}
215