blob: e207b292ae0adf8e0fa7306f3a31c35b7bf2d1c5 [file] [log] [blame]
Paul Soulosab840442014-06-17 14:08:40 -07001/*
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;
yaolu3c5e3122016-09-14 14:53:46 -070019import com.android.contacts.common.GeoUtil;
20import com.android.contacts.common.compat.PhoneNumberUtilsCompat;
Paul Soulosab840442014-06-17 14:08:40 -070021import com.android.contacts.common.util.BitmapUtil;
Walter Jang7ce53522014-10-29 13:26:43 -070022import com.android.contacts.common.util.ContactDisplayUtils;
Paul Soulosab840442014-06-17 14:08:40 -070023
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
27import android.content.res.Resources;
28import android.graphics.PorterDuff;
29import android.graphics.drawable.Drawable;
30import android.net.Uri;
31import android.provider.CallLog.Calls;
32import android.provider.ContactsContract.CommonDataKinds.Phone;
Brian Attwellc62cc792014-10-02 12:35:07 -070033import android.text.BidiFormatter;
Walter Jang7ce53522014-10-29 13:26:43 -070034import android.text.Spannable;
Brian Attwellc62cc792014-10-02 12:35:07 -070035import android.text.TextDirectionHeuristics;
Paul Soulosab840442014-06-17 14:08:40 -070036
37/**
38 * Represents a call log event interaction, wrapping the columns in
39 * {@link android.provider.CallLog.Calls}.
40 *
41 * This class does not return log entries related to voicemail or SIP calls. Additionally,
42 * this class ignores number presentation. Number presentation affects how to identify phone
43 * numbers. Since, we already know the identity of the phone number owner we can ignore number
44 * presentation.
45 *
46 * As a result of ignoring voicemail and number presentation, we don't need to worry about API
47 * version.
48 */
49public class CallLogInteraction implements ContactInteraction {
50
51 private static final String URI_TARGET_PREFIX = "tel:";
52 private static final int CALL_LOG_ICON_RES = R.drawable.ic_phone_24dp;
53 private static final int CALL_ARROW_ICON_RES = R.drawable.ic_call_arrow;
Brian Attwellc62cc792014-10-02 12:35:07 -070054 private static BidiFormatter sBidiFormatter = BidiFormatter.getInstance();
Paul Soulosab840442014-06-17 14:08:40 -070055
56 private ContentValues mValues;
57
58 public CallLogInteraction(ContentValues values) {
59 mValues = values;
60 }
61
62 @Override
63 public Intent getIntent() {
Paul Soulos75d5a912014-06-24 11:22:37 -070064 String number = getNumber();
65 return number == null ? null : new Intent(Intent.ACTION_CALL).setData(
66 Uri.parse(URI_TARGET_PREFIX + number));
Paul Soulosab840442014-06-17 14:08:40 -070067 }
68
69 @Override
70 public String getViewHeader(Context context) {
yaolu3c5e3122016-09-14 14:53:46 -070071 String number = mValues.getAsString(Calls.NUMBER);
72 if (number != null) {
73 number = PhoneNumberUtilsCompat.formatNumber(number,
74 PhoneNumberUtilsCompat.normalizeNumber(number),
75 GeoUtil.getCurrentCountryIso(context));
76 return sBidiFormatter.unicodeWrap(number, TextDirectionHeuristics.LTR);
77 }
78 return null;
Paul Soulosab840442014-06-17 14:08:40 -070079 }
80
81 @Override
82 public long getInteractionDate() {
Paul Soulos75d5a912014-06-24 11:22:37 -070083 Long date = getDate();
84 return date == null ? -1 : date;
Paul Soulosab840442014-06-17 14:08:40 -070085 }
86
87 @Override
88 public String getViewBody(Context context) {
Paul Soulos75d5a912014-06-24 11:22:37 -070089 Integer numberType = getCachedNumberType();
90 if (numberType == null) {
Paul Soulosab840442014-06-17 14:08:40 -070091 return null;
92 }
93 return Phone.getTypeLabel(context.getResources(), getCachedNumberType(),
94 getCachedNumberLabel()).toString();
95 }
96
97 @Override
98 public String getViewFooter(Context context) {
yaolu0dd79bb2016-08-09 14:19:33 -070099 final Long date = getDate();
100 if (date != null) {
101 final StringBuilder callDetail = new StringBuilder();
102 callDetail.append(ContactInteractionUtil.formatDateStringFromTimestamp(date, context));
103 final Long duration = getDuration();
104 if (duration != null) {
105 callDetail.append("\n");
106 callDetail.append(ContactInteractionUtil.formatDuration(duration, context));
107 }
108 return callDetail.toString();
109 }
110 return null;
Paul Soulosab840442014-06-17 14:08:40 -0700111 }
112
113 @Override
114 public Drawable getIcon(Context context) {
115 return context.getResources().getDrawable(CALL_LOG_ICON_RES);
116 }
117
118 @Override
119 public Drawable getBodyIcon(Context context) {
120 return null;
121 }
122
123 @Override
124 public Drawable getFooterIcon(Context context) {
125 Drawable callArrow = null;
126 Resources res = context.getResources();
Paul Soulos75d5a912014-06-24 11:22:37 -0700127 Integer type = getType();
128 if (type == null) {
129 return null;
130 }
131 switch (type) {
Paul Soulosab840442014-06-17 14:08:40 -0700132 case Calls.INCOMING_TYPE:
133 callArrow = res.getDrawable(CALL_ARROW_ICON_RES);
kenji oda168a7a72016-09-27 16:21:46 +0900134 callArrow.mutate().setColorFilter(res.getColor(R.color.call_arrow_green),
Paul Soulosab840442014-06-17 14:08:40 -0700135 PorterDuff.Mode.MULTIPLY);
136 break;
137 case Calls.MISSED_TYPE:
138 callArrow = res.getDrawable(CALL_ARROW_ICON_RES);
kenji oda168a7a72016-09-27 16:21:46 +0900139 callArrow.mutate().setColorFilter(res.getColor(R.color.call_arrow_red),
Paul Soulosab840442014-06-17 14:08:40 -0700140 PorterDuff.Mode.MULTIPLY);
141 break;
142 case Calls.OUTGOING_TYPE:
143 callArrow = BitmapUtil.getRotatedDrawable(res, CALL_ARROW_ICON_RES, 180f);
144 callArrow.setColorFilter(res.getColor(R.color.call_arrow_green),
145 PorterDuff.Mode.MULTIPLY);
146 break;
147 }
148 return callArrow;
149 }
150
151 public String getCachedName() {
152 return mValues.getAsString(Calls.CACHED_NAME);
153 }
154
155 public String getCachedNumberLabel() {
156 return mValues.getAsString(Calls.CACHED_NUMBER_LABEL);
157 }
158
Paul Soulos75d5a912014-06-24 11:22:37 -0700159 public Integer getCachedNumberType() {
160 return mValues.getAsInteger(Calls.CACHED_NUMBER_TYPE);
Paul Soulosab840442014-06-17 14:08:40 -0700161 }
162
Paul Soulos75d5a912014-06-24 11:22:37 -0700163 public Long getDate() {
Paul Soulosab840442014-06-17 14:08:40 -0700164 return mValues.getAsLong(Calls.DATE);
165 }
166
Paul Soulos75d5a912014-06-24 11:22:37 -0700167 public Long getDuration() {
Paul Soulosab840442014-06-17 14:08:40 -0700168 return mValues.getAsLong(Calls.DURATION);
169 }
170
Paul Soulos75d5a912014-06-24 11:22:37 -0700171 public Boolean getIsRead() {
Paul Soulosab840442014-06-17 14:08:40 -0700172 return mValues.getAsBoolean(Calls.IS_READ);
173 }
174
Paul Soulos75d5a912014-06-24 11:22:37 -0700175 public Integer getLimitParamKey() {
Paul Soulosab840442014-06-17 14:08:40 -0700176 return mValues.getAsInteger(Calls.LIMIT_PARAM_KEY);
177 }
178
Paul Soulos75d5a912014-06-24 11:22:37 -0700179 public Boolean getNew() {
Paul Soulosab840442014-06-17 14:08:40 -0700180 return mValues.getAsBoolean(Calls.NEW);
181 }
182
183 public String getNumber() {
Jay Shraunerb2215762014-10-22 09:02:53 -0700184 final String number = mValues.getAsString(Calls.NUMBER);
185 return number == null ? null :
186 sBidiFormatter.unicodeWrap(number, TextDirectionHeuristics.LTR);
Paul Soulosab840442014-06-17 14:08:40 -0700187 }
188
Paul Soulos75d5a912014-06-24 11:22:37 -0700189 public Integer getNumberPresentation() {
Paul Soulosab840442014-06-17 14:08:40 -0700190 return mValues.getAsInteger(Calls.NUMBER_PRESENTATION);
191 }
192
Paul Soulos75d5a912014-06-24 11:22:37 -0700193 public Integer getOffsetParamKey() {
Paul Soulosab840442014-06-17 14:08:40 -0700194 return mValues.getAsInteger(Calls.OFFSET_PARAM_KEY);
195 }
196
Paul Soulos75d5a912014-06-24 11:22:37 -0700197 public Integer getType() {
Paul Soulosab840442014-06-17 14:08:40 -0700198 return mValues.getAsInteger(Calls.TYPE);
199 }
Paul Soulos23e28362014-08-29 14:57:08 -0700200
201 @Override
Walter Jang7ce53522014-10-29 13:26:43 -0700202 public Spannable getContentDescription(Context context) {
203 final String phoneNumber = getViewHeader(context);
204 final String contentDescription = context.getResources().getString(
205 R.string.content_description_recent_call,
206 getCallTypeString(context), phoneNumber, getViewFooter(context));
207 return ContactDisplayUtils.getTelephoneTtsSpannable(contentDescription, phoneNumber);
Paul Soulos23e28362014-08-29 14:57:08 -0700208 }
209
210 private String getCallTypeString(Context context) {
211 String callType = "";
212 Resources res = context.getResources();
213 Integer type = getType();
214 if (type == null) {
215 return callType;
216 }
217 switch (type) {
218 case Calls.INCOMING_TYPE:
219 callType = res.getString(R.string.content_description_recent_call_type_incoming);
220 break;
221 case Calls.MISSED_TYPE:
222 callType = res.getString(R.string.content_description_recent_call_type_missed);
223 break;
224 case Calls.OUTGOING_TYPE:
225 callType = res.getString(R.string.content_description_recent_call_type_outgoing);
226 break;
227 }
228 return callType;
229 }
Paul Soulos48290be2014-09-08 13:44:51 -0700230
231 @Override
232 public int getIconResourceId() {
233 return CALL_LOG_ICON_RES;
234 }
Jay Shraunerb2215762014-10-22 09:02:53 -0700235}