Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 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 | */ |
| 16 | package com.android.contacts.interactions; |
| 17 | |
| 18 | import com.android.contacts.R; |
yaolu | 3c5e312 | 2016-09-14 14:53:46 -0700 | [diff] [blame] | 19 | import com.android.contacts.common.GeoUtil; |
| 20 | import com.android.contacts.common.compat.PhoneNumberUtilsCompat; |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 21 | import com.android.contacts.common.util.BitmapUtil; |
Walter Jang | 7ce5352 | 2014-10-29 13:26:43 -0700 | [diff] [blame] | 22 | import com.android.contacts.common.util.ContactDisplayUtils; |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 23 | |
| 24 | import android.content.ContentValues; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.res.Resources; |
| 28 | import android.graphics.PorterDuff; |
| 29 | import android.graphics.drawable.Drawable; |
| 30 | import android.net.Uri; |
| 31 | import android.provider.CallLog.Calls; |
| 32 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
Brian Attwell | c62cc79 | 2014-10-02 12:35:07 -0700 | [diff] [blame] | 33 | import android.text.BidiFormatter; |
Walter Jang | 7ce5352 | 2014-10-29 13:26:43 -0700 | [diff] [blame] | 34 | import android.text.Spannable; |
Brian Attwell | c62cc79 | 2014-10-02 12:35:07 -0700 | [diff] [blame] | 35 | import android.text.TextDirectionHeuristics; |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 36 | |
| 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 | */ |
| 49 | public 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 Attwell | c62cc79 | 2014-10-02 12:35:07 -0700 | [diff] [blame] | 54 | private static BidiFormatter sBidiFormatter = BidiFormatter.getInstance(); |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 55 | |
| 56 | private ContentValues mValues; |
| 57 | |
| 58 | public CallLogInteraction(ContentValues values) { |
| 59 | mValues = values; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public Intent getIntent() { |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 64 | String number = getNumber(); |
| 65 | return number == null ? null : new Intent(Intent.ACTION_CALL).setData( |
| 66 | Uri.parse(URI_TARGET_PREFIX + number)); |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String getViewHeader(Context context) { |
yaolu | 3c5e312 | 2016-09-14 14:53:46 -0700 | [diff] [blame] | 71 | 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 Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public long getInteractionDate() { |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 83 | Long date = getDate(); |
| 84 | return date == null ? -1 : date; |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public String getViewBody(Context context) { |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 89 | Integer numberType = getCachedNumberType(); |
| 90 | if (numberType == null) { |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 91 | return null; |
| 92 | } |
| 93 | return Phone.getTypeLabel(context.getResources(), getCachedNumberType(), |
| 94 | getCachedNumberLabel()).toString(); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public String getViewFooter(Context context) { |
yaolu | 0dd79bb | 2016-08-09 14:19:33 -0700 | [diff] [blame] | 99 | 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 Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 111 | } |
| 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 Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 127 | Integer type = getType(); |
| 128 | if (type == null) { |
| 129 | return null; |
| 130 | } |
| 131 | switch (type) { |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 132 | case Calls.INCOMING_TYPE: |
| 133 | callArrow = res.getDrawable(CALL_ARROW_ICON_RES); |
kenji oda | 168a7a7 | 2016-09-27 16:21:46 +0900 | [diff] [blame] | 134 | callArrow.mutate().setColorFilter(res.getColor(R.color.call_arrow_green), |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 135 | PorterDuff.Mode.MULTIPLY); |
| 136 | break; |
| 137 | case Calls.MISSED_TYPE: |
| 138 | callArrow = res.getDrawable(CALL_ARROW_ICON_RES); |
kenji oda | 168a7a7 | 2016-09-27 16:21:46 +0900 | [diff] [blame] | 139 | callArrow.mutate().setColorFilter(res.getColor(R.color.call_arrow_red), |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 140 | 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 Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 159 | public Integer getCachedNumberType() { |
| 160 | return mValues.getAsInteger(Calls.CACHED_NUMBER_TYPE); |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 163 | public Long getDate() { |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 164 | return mValues.getAsLong(Calls.DATE); |
| 165 | } |
| 166 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 167 | public Long getDuration() { |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 168 | return mValues.getAsLong(Calls.DURATION); |
| 169 | } |
| 170 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 171 | public Boolean getIsRead() { |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 172 | return mValues.getAsBoolean(Calls.IS_READ); |
| 173 | } |
| 174 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 175 | public Integer getLimitParamKey() { |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 176 | return mValues.getAsInteger(Calls.LIMIT_PARAM_KEY); |
| 177 | } |
| 178 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 179 | public Boolean getNew() { |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 180 | return mValues.getAsBoolean(Calls.NEW); |
| 181 | } |
| 182 | |
| 183 | public String getNumber() { |
Jay Shrauner | b221576 | 2014-10-22 09:02:53 -0700 | [diff] [blame] | 184 | final String number = mValues.getAsString(Calls.NUMBER); |
| 185 | return number == null ? null : |
| 186 | sBidiFormatter.unicodeWrap(number, TextDirectionHeuristics.LTR); |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 189 | public Integer getNumberPresentation() { |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 190 | return mValues.getAsInteger(Calls.NUMBER_PRESENTATION); |
| 191 | } |
| 192 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 193 | public Integer getOffsetParamKey() { |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 194 | return mValues.getAsInteger(Calls.OFFSET_PARAM_KEY); |
| 195 | } |
| 196 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 197 | public Integer getType() { |
Paul Soulos | ab84044 | 2014-06-17 14:08:40 -0700 | [diff] [blame] | 198 | return mValues.getAsInteger(Calls.TYPE); |
| 199 | } |
Paul Soulos | 23e2836 | 2014-08-29 14:57:08 -0700 | [diff] [blame] | 200 | |
| 201 | @Override |
Walter Jang | 7ce5352 | 2014-10-29 13:26:43 -0700 | [diff] [blame] | 202 | 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 Soulos | 23e2836 | 2014-08-29 14:57:08 -0700 | [diff] [blame] | 208 | } |
| 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 Soulos | 48290be | 2014-09-08 13:44:51 -0700 | [diff] [blame] | 230 | |
| 231 | @Override |
| 232 | public int getIconResourceId() { |
| 233 | return CALL_LOG_ICON_RES; |
| 234 | } |
Jay Shrauner | b221576 | 2014-10-22 09:02:53 -0700 | [diff] [blame] | 235 | } |