1/*
2 * Copyright (C) 2014 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 */
16package com.android.contacts.interactions;
17
18import com.android.contacts.R;
19import com.android.contacts.common.util.ContactDisplayUtils;
20
21import android.content.ContentValues;
22import android.content.Context;
23import android.content.Intent;
24import android.graphics.drawable.Drawable;
25import android.net.Uri;
26import android.provider.Telephony.Sms;
27import android.text.BidiFormatter;
28import android.text.Spannable;
29import android.text.TextDirectionHeuristics;
30
31/**
32 * Represents an sms interaction, wrapping the columns in
33 * {@link android.provider.Telephony.Sms}.
34 */
35public class SmsInteraction implements ContactInteraction {
36
37    private static final String URI_TARGET_PREFIX = "smsto:";
38    private static final int SMS_ICON_RES = R.drawable.ic_message_24dp;
39    private static BidiFormatter sBidiFormatter = BidiFormatter.getInstance();
40
41    private ContentValues mValues;
42
43    public SmsInteraction(ContentValues values) {
44        mValues = values;
45    }
46
47    @Override
48    public Intent getIntent() {
49        String address = getAddress();
50        return address == null ? null : new Intent(Intent.ACTION_VIEW).setData(
51                Uri.parse(URI_TARGET_PREFIX + address));
52    }
53
54    @Override
55    public long getInteractionDate() {
56        Long date = getDate();
57        return date == null ? -1 : date;
58    }
59
60    @Override
61    public String getViewHeader(Context context) {
62        String body = getBody();
63        if (getType() == Sms.MESSAGE_TYPE_SENT) {
64            body = context.getResources().getString(R.string.message_from_you_prefix, body);
65        }
66        return body;
67    }
68
69    @Override
70    public String getViewBody(Context context) {
71        return getAddress();
72    }
73
74    @Override
75    public String getViewFooter(Context context) {
76        Long date = getDate();
77        return date == null ? null : ContactInteractionUtil.formatDateStringFromTimestamp(
78                date, context);
79    }
80
81    @Override
82    public Drawable getIcon(Context context) {
83        return context.getResources().getDrawable(SMS_ICON_RES);
84    }
85
86    @Override
87    public Drawable getBodyIcon(Context context) {
88        return null;
89    }
90
91    @Override
92    public Drawable getFooterIcon(Context context) {
93        return null;
94    }
95
96    public String getAddress() {
97        final String address = mValues.getAsString(Sms.ADDRESS);
98        return address == null ? null :
99            sBidiFormatter.unicodeWrap(address, TextDirectionHeuristics.LTR);
100    }
101
102    public String getBody() {
103        return mValues.getAsString(Sms.BODY);
104    }
105
106    public Long getDate() {
107        return mValues.getAsLong(Sms.DATE);
108    }
109
110
111    public Long getDateSent() {
112        return mValues.getAsLong(Sms.DATE_SENT);
113    }
114
115    public Integer getErrorCode() {
116        return mValues.getAsInteger(Sms.ERROR_CODE);
117    }
118
119    public Boolean getLocked() {
120        return mValues.getAsBoolean(Sms.LOCKED);
121    }
122
123    public Integer getPerson() {
124        return mValues.getAsInteger(Sms.PERSON);
125    }
126
127    public Integer getProtocol() {
128        return mValues.getAsInteger(Sms.PROTOCOL);
129    }
130
131    public Boolean getRead() {
132        return mValues.getAsBoolean(Sms.READ);
133    }
134
135    public Boolean getReplyPathPresent() {
136        return mValues.getAsBoolean(Sms.REPLY_PATH_PRESENT);
137    }
138
139    public Boolean getSeen() {
140        return mValues.getAsBoolean(Sms.SEEN);
141    }
142
143    public String getServiceCenter() {
144        return mValues.getAsString(Sms.SERVICE_CENTER);
145    }
146
147    public Integer getStatus() {
148        return mValues.getAsInteger(Sms.STATUS);
149    }
150
151    public String getSubject() {
152        return mValues.getAsString(Sms.SUBJECT);
153    }
154
155    public Integer getThreadId() {
156        return mValues.getAsInteger(Sms.THREAD_ID);
157    }
158
159    public Integer getType() {
160        return mValues.getAsInteger(Sms.TYPE);
161    }
162
163    @Override
164    public Spannable getContentDescription(Context context) {
165        final String phoneNumber = getViewBody(context);
166        final String contentDescription = context.getResources().getString(
167                R.string.content_description_recent_sms,
168                getViewHeader(context), phoneNumber, getViewFooter(context));
169        return ContactDisplayUtils.getTelephoneTtsSpannable(contentDescription, phoneNumber);
170    }
171
172    @Override
173    public int getIconResourceId() {
174        return SMS_ICON_RES;
175    }
176}
177