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;
     22 import android.provider.ContactsContract.CommonDataKinds.Email;
     23 import android.provider.ContactsContract.CommonDataKinds.Im;
     24 import android.text.TextUtils;
     25 
     26 /**
     27  * Represents an IM data item, wrapping the columns in
     28  * {@link ContactsContract.CommonDataKinds.Im}.
     29  */
     30 public class ImDataItem extends DataItem {
     31 
     32     private final boolean mCreatedFromEmail;
     33 
     34     /* package */ ImDataItem(ContentValues values) {
     35         super(values);
     36         mCreatedFromEmail = false;
     37     }
     38 
     39     private ImDataItem(ContentValues values, boolean createdFromEmail) {
     40         super(values);
     41         mCreatedFromEmail = createdFromEmail;
     42     }
     43 
     44     public static ImDataItem createFromEmail(EmailDataItem item) {
     45         final ImDataItem im = new ImDataItem(new ContentValues(item.getContentValues()), true);
     46         im.setMimeType(Im.CONTENT_ITEM_TYPE);
     47         return im;
     48     }
     49 
     50     public String getData() {
     51         if (mCreatedFromEmail) {
     52             return getContentValues().getAsString(Email.DATA);
     53         } else {
     54             return getContentValues().getAsString(Im.DATA);
     55         }
     56     }
     57 
     58     public String getLabel() {
     59         return getContentValues().getAsString(Im.LABEL);
     60     }
     61 
     62     /**
     63      * Values are one of Im.PROTOCOL_
     64      */
     65     public Integer getProtocol() {
     66         return getContentValues().getAsInteger(Im.PROTOCOL);
     67     }
     68 
     69     public boolean isProtocolValid() {
     70         return getProtocol() != null;
     71     }
     72 
     73     public String getCustomProtocol() {
     74         return getContentValues().getAsString(Im.CUSTOM_PROTOCOL);
     75     }
     76 
     77     public int getChatCapability() {
     78         Integer result = getContentValues().getAsInteger(Im.CHAT_CAPABILITY);
     79         return result == null ? 0 : result;
     80     }
     81 
     82     public boolean isCreatedFromEmail() {
     83         return mCreatedFromEmail;
     84     }
     85 
     86     @Override
     87     public boolean shouldCollapseWith(DataItem t, Context context) {
     88         if (!(t instanceof ImDataItem) || mKind == null || t.getDataKind() == null) {
     89             return false;
     90         }
     91         final ImDataItem that = (ImDataItem) t;
     92         // IM can have the same data put different protocol. These should not collapse.
     93         if (!getData().equals(that.getData())) {
     94             return false;
     95         } else if (!isProtocolValid() || !that.isProtocolValid()) {
     96             // Deal with invalid protocol as if it was custom. If either has a non valid
     97             // protocol, check to see if the other has a valid that is not custom
     98             if (isProtocolValid()) {
     99                 return getProtocol() == Im.PROTOCOL_CUSTOM;
    100             } else if (that.isProtocolValid()) {
    101                 return that.getProtocol() == Im.PROTOCOL_CUSTOM;
    102             }
    103             return true;
    104         } else if (getProtocol() != that.getProtocol()) {
    105             return false;
    106         } else if (getProtocol() == Im.PROTOCOL_CUSTOM &&
    107                 !TextUtils.equals(getCustomProtocol(), that.getCustomProtocol())) {
    108             // Check if custom protocols are not the same
    109             return false;
    110         }
    111         return true;
    112     }
    113 }
    114