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
17package com.android.contacts.model.dataitem;
18
19import android.content.ContentValues;
20import android.provider.ContactsContract;
21import android.provider.ContactsContract.CommonDataKinds.Email;
22import android.provider.ContactsContract.CommonDataKinds.Im;
23
24import com.android.contacts.model.RawContact;
25
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(RawContact rawContact, ContentValues values) {
35        super(rawContact, values);
36        mCreatedFromEmail = false;
37    }
38
39    private ImDataItem(RawContact rawContact, ContentValues values,
40            boolean createdFromEmail) {
41        super(rawContact, values);
42        mCreatedFromEmail = createdFromEmail;
43    }
44
45    public static ImDataItem createFromEmail(EmailDataItem item) {
46        ImDataItem im = new ImDataItem(item.getRawContact(),
47                new ContentValues(item.getContentValues()), true);
48        im.setMimeType(Im.CONTENT_ITEM_TYPE);
49        return im;
50    }
51
52    public String getData() {
53        if (mCreatedFromEmail) {
54            return getContentValues().getAsString(Email.DATA);
55        } else {
56            return getContentValues().getAsString(Im.DATA);
57        }
58    }
59
60    /**
61     * Values are one of Im.TYPE_*
62     */
63    public int getType() {
64        return getContentValues().getAsInteger(Im.TYPE);
65    }
66
67    public String getLabel() {
68        return getContentValues().getAsString(Im.LABEL);
69    }
70
71    /**
72     * Values are one of Im.PROTOCOL_
73     */
74    public Integer getProtocol() {
75        return getContentValues().getAsInteger(Im.PROTOCOL);
76    }
77
78    public boolean isProtocolValid() {
79        return getProtocol() != null;
80    }
81
82    public String getCustomProtocol() {
83        return getContentValues().getAsString(Im.CUSTOM_PROTOCOL);
84    }
85
86    public int getChatCapability() {
87        Integer result = getContentValues().getAsInteger(Im.CHAT_CAPABILITY);
88        return result == null ? 0 : result;
89    }
90
91    public boolean isCreatedFromEmail() {
92        return mCreatedFromEmail;
93    }
94}
95