blob: a0e087c1d23c7e01a5bffc15e47125e9ae953559 [file] [log] [blame]
Yorke Lee2644d942013-10-28 11:05:43 -07001/*
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
Gary Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.model.dataitem;
Yorke Lee2644d942013-10-28 11:05:43 -070018
19import android.content.ContentValues;
Paul Soulosa69518c2014-07-28 14:12:23 -070020import android.content.Context;
Yorke Lee2644d942013-10-28 11:05:43 -070021import android.provider.ContactsContract;
22import android.provider.ContactsContract.CommonDataKinds.Email;
23import android.provider.ContactsContract.CommonDataKinds.Im;
Jay Shrauner467aa452014-10-22 10:31:28 -070024import android.text.TextUtils;
Yorke Lee2644d942013-10-28 11:05:43 -070025
26/**
27 * Represents an IM data item, wrapping the columns in
28 * {@link ContactsContract.CommonDataKinds.Im}.
29 */
30public 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 Soulosa69518c2014-07-28 14:12:23 -070045 final ImDataItem im = new ImDataItem(new ContentValues(item.getContentValues()), true);
Yorke Lee2644d942013-10-28 11:05:43 -070046 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 Soulosa69518c2014-07-28 14:12:23 -070085
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 Shrauner467aa452014-10-22 10:31:28 -070095 } 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 Soulosa69518c2014-07-28 14:12:23 -0700105 return false;
Jay Shrauner467aa452014-10-22 10:31:28 -0700106 } else if (getProtocol() == Im.PROTOCOL_CUSTOM &&
107 !TextUtils.equals(getCustomProtocol(), that.getCustomProtocol())) {
Paul Soulosa69518c2014-07-28 14:12:23 -0700108 // Check if custom protocols are not the same
109 return false;
Paul Soulosa69518c2014-07-28 14:12:23 -0700110 }
111 return true;
112 }
Yorke Lee2644d942013-10-28 11:05:43 -0700113}