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