Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 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; |
Paul Soulos | a69518c | 2014-07-28 14:12:23 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 21 | import android.provider.ContactsContract; |
| 22 | import android.provider.ContactsContract.CommonDataKinds.Email; |
| 23 | import android.provider.ContactsContract.CommonDataKinds.Im; |
Jay Shrauner | 467aa45 | 2014-10-22 10:31:28 -0700 | [diff] [blame] | 24 | import android.text.TextUtils; |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 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) { |
Paul Soulos | a69518c | 2014-07-28 14:12:23 -0700 | [diff] [blame] | 45 | final ImDataItem im = new ImDataItem(new ContentValues(item.getContentValues()), true); |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 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 | } |
Paul Soulos | a69518c | 2014-07-28 14:12:23 -0700 | [diff] [blame] | 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; |
Jay Shrauner | 467aa45 | 2014-10-22 10:31:28 -0700 | [diff] [blame] | 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()) { |
Paul Soulos | a69518c | 2014-07-28 14:12:23 -0700 | [diff] [blame] | 105 | return false; |
Jay Shrauner | 467aa45 | 2014-10-22 10:31:28 -0700 | [diff] [blame] | 106 | } else if (getProtocol() == Im.PROTOCOL_CUSTOM && |
| 107 | !TextUtils.equals(getCustomProtocol(), that.getCustomProtocol())) { |
Paul Soulos | a69518c | 2014-07-28 14:12:23 -0700 | [diff] [blame] | 108 | // Check if custom protocols are not the same |
| 109 | return false; |
Paul Soulos | a69518c | 2014-07-28 14:12:23 -0700 | [diff] [blame] | 110 | } |
| 111 | return true; |
| 112 | } |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 113 | } |