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