Home | History | Annotate | Download | only in dataitem
      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.common.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     /**
    109      * Returns the mimetype of the data.
    110      */
    111     public String getMimeType() {
    112         return mContentValues.getAsString(Data.MIMETYPE);
    113     }
    114 
    115     public void setMimeType(String mimeType) {
    116         mContentValues.put(Data.MIMETYPE, mimeType);
    117     }
    118 
    119     public boolean isPrimary() {
    120         Integer primary = mContentValues.getAsInteger(Data.IS_PRIMARY);
    121         return primary != null && primary != 0;
    122     }
    123 
    124     public boolean isSuperPrimary() {
    125         Integer superPrimary = mContentValues.getAsInteger(Data.IS_SUPER_PRIMARY);
    126         return superPrimary != null && superPrimary != 0;
    127     }
    128 
    129     public boolean hasKindTypeColumn(DataKind kind) {
    130         final String key = kind.typeColumn;
    131         return key != null && mContentValues.containsKey(key) &&
    132             mContentValues.getAsInteger(key) != null;
    133     }
    134 
    135     public int getKindTypeColumn(DataKind kind) {
    136         final String key = kind.typeColumn;
    137         return mContentValues.getAsInteger(key);
    138     }
    139 
    140     /**
    141      * This builds the data string depending on the type of data item by using the generic
    142      * DataKind object underneath.
    143      */
    144     public String buildDataString(Context context, DataKind kind) {
    145         if (kind.actionBody == null) {
    146             return null;
    147         }
    148         CharSequence actionBody = kind.actionBody.inflateUsing(context, mContentValues);
    149         return actionBody == null ? null : actionBody.toString();
    150     }
    151 
    152     /**
    153      * This builds the data string(intended for display) depending on the type of data item. It
    154      * returns the same value as {@link #buildDataString} by default, but certain data items can
    155      * override it to provide their version of formatted data strings.
    156      *
    157      * @return Data string representing the data item, possibly formatted for display
    158      */
    159     public String buildDataStringForDisplay(Context context, DataKind kind) {
    160         return buildDataString(context, kind);
    161     }
    162 }
    163