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