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