1 /* 2 * Copyright (C) 2011 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.dialer.app.calllog; 18 19 import android.content.Context; 20 import android.database.Cursor; 21 import android.provider.CallLog.Calls; 22 import android.support.annotation.Nullable; 23 import android.support.annotation.VisibleForTesting; 24 import android.telephony.PhoneNumberUtils; 25 import android.text.TextUtils; 26 import android.text.format.Time; 27 import com.android.contacts.common.util.DateUtils; 28 import com.android.dialer.calllogutils.CallbackActionHelper; 29 import com.android.dialer.calllogutils.CallbackActionHelper.CallbackAction; 30 import com.android.dialer.compat.telephony.TelephonyManagerCompat; 31 import com.android.dialer.inject.ApplicationContext; 32 import com.android.dialer.phonenumbercache.CallLogQuery; 33 import com.android.dialer.phonenumberutil.PhoneNumberHelper; 34 import java.util.Objects; 35 36 /** 37 * Groups together calls in the call log. The primary grouping attempts to group together calls to 38 * and from the same number into a single row on the call log. A secondary grouping assigns calls, 39 * grouped via the primary grouping, to "day groups". The day groups provide a means of identifying 40 * the calls which occurred "Today", "Yesterday", "Last week", or "Other". 41 * 42 * <p>This class is meant to be used in conjunction with {@link GroupingListAdapter}. 43 */ 44 public class CallLogGroupBuilder { 45 46 /** 47 * Day grouping for call log entries used to represent no associated day group. Used primarily 48 * when retrieving the previous day group, but there is no previous day group (i.e. we are at the 49 * start of the list). 50 */ 51 public static final int DAY_GROUP_NONE = -1; 52 /** Day grouping for calls which occurred today. */ 53 public static final int DAY_GROUP_TODAY = 0; 54 /** Day grouping for calls which occurred yesterday. */ 55 public static final int DAY_GROUP_YESTERDAY = 1; 56 /** Day grouping for calls which occurred before last week. */ 57 public static final int DAY_GROUP_OTHER = 2; 58 /** Instance of the time object used for time calculations. */ 59 private static final Time TIME = new Time(); 60 61 private final Context appContext; 62 /** The object on which the groups are created. */ 63 private final GroupCreator groupCreator; 64 CallLogGroupBuilder(@pplicationContext Context appContext, GroupCreator groupCreator)65 public CallLogGroupBuilder(@ApplicationContext Context appContext, GroupCreator groupCreator) { 66 this.appContext = appContext; 67 this.groupCreator = groupCreator; 68 } 69 70 /** 71 * Finds all groups of adjacent entries in the call log which should be grouped together and calls 72 * {@link GroupCreator#addGroup(int, int)} on {@link #groupCreator} for each of them. 73 * 74 * <p>For entries that are not grouped with others, we do not need to create a group of size one. 75 * 76 * <p>It assumes that the cursor will not change during its execution. 77 * 78 * @see GroupingListAdapter#addGroups(Cursor) 79 */ addGroups(Cursor cursor)80 public void addGroups(Cursor cursor) { 81 final int count = cursor.getCount(); 82 if (count == 0) { 83 return; 84 } 85 86 // Clear any previous day grouping information. 87 groupCreator.clearDayGroups(); 88 89 // Get current system time, used for calculating which day group calls belong to. 90 long currentTime = System.currentTimeMillis(); 91 cursor.moveToFirst(); 92 93 // Determine the day group for the first call in the cursor. 94 final long firstDate = cursor.getLong(CallLogQuery.DATE); 95 final long firstRowId = cursor.getLong(CallLogQuery.ID); 96 int groupDayGroup = getDayGroup(firstDate, currentTime); 97 groupCreator.setDayGroup(firstRowId, groupDayGroup); 98 99 // Determine the callback action for the first call in the cursor. 100 String groupNumber = cursor.getString(CallLogQuery.NUMBER); 101 String groupAccountComponentName = cursor.getString(CallLogQuery.ACCOUNT_COMPONENT_NAME); 102 int groupFeatures = cursor.getInt(CallLogQuery.FEATURES); 103 int groupCallbackAction = 104 CallbackActionHelper.getCallbackAction( 105 appContext, groupNumber, groupFeatures, groupAccountComponentName); 106 groupCreator.setCallbackAction(firstRowId, groupCallbackAction); 107 108 // Instantiate other group values to those of the first call in the cursor. 109 String groupAccountId = cursor.getString(CallLogQuery.ACCOUNT_ID); 110 String groupPostDialDigits = cursor.getString(CallLogQuery.POST_DIAL_DIGITS); 111 String groupViaNumbers = cursor.getString(CallLogQuery.VIA_NUMBER); 112 int groupCallType = cursor.getInt(CallLogQuery.CALL_TYPE); 113 int groupSize = 1; 114 115 String number; 116 String numberPostDialDigits; 117 String numberViaNumbers; 118 int callType; 119 int callFeatures; 120 String accountComponentName; 121 String accountId; 122 int callbackAction; 123 124 while (cursor.moveToNext()) { 125 // Obtain the values for the current call to group. 126 number = cursor.getString(CallLogQuery.NUMBER); 127 numberPostDialDigits = cursor.getString(CallLogQuery.POST_DIAL_DIGITS); 128 numberViaNumbers = cursor.getString(CallLogQuery.VIA_NUMBER); 129 callType = cursor.getInt(CallLogQuery.CALL_TYPE); 130 callFeatures = cursor.getInt(CallLogQuery.FEATURES); 131 accountComponentName = cursor.getString(CallLogQuery.ACCOUNT_COMPONENT_NAME); 132 accountId = cursor.getString(CallLogQuery.ACCOUNT_ID); 133 callbackAction = 134 CallbackActionHelper.getCallbackAction( 135 appContext, number, callFeatures, accountComponentName); 136 137 final boolean isSameNumber = equalNumbers(groupNumber, number); 138 final boolean isSamePostDialDigits = groupPostDialDigits.equals(numberPostDialDigits); 139 final boolean isSameViaNumbers = groupViaNumbers.equals(numberViaNumbers); 140 final boolean isSameAccount = 141 isSameAccount(groupAccountComponentName, accountComponentName, groupAccountId, accountId); 142 final boolean isSameCallbackAction = (groupCallbackAction == callbackAction); 143 144 // Group calls with the following criteria: 145 // (1) Calls with the same number, account, and callback action should be in the same group; 146 // (2) Never group voice mails; and 147 // (3) Only group blocked calls with other blocked calls. 148 // (4) Only group calls that were assisted dialed with other calls that were assisted dialed. 149 if (isSameNumber 150 && isSameAccount 151 && isSamePostDialDigits 152 && isSameViaNumbers 153 && isSameCallbackAction 154 && areBothNotVoicemail(callType, groupCallType) 155 && (areBothNotBlocked(callType, groupCallType) || areBothBlocked(callType, groupCallType)) 156 && meetsAssistedDialingGroupingCriteria(groupFeatures, callFeatures)) { 157 // Increment the size of the group to include the current call, but do not create 158 // the group until finding a call that does not match. 159 groupSize++; 160 } else { 161 // The call group has changed. Determine the day group for the new call group. 162 final long date = cursor.getLong(CallLogQuery.DATE); 163 groupDayGroup = getDayGroup(date, currentTime); 164 165 // Create a group for the previous group of calls, which does not include the 166 // current call. 167 groupCreator.addGroup(cursor.getPosition() - groupSize, groupSize); 168 169 // Start a new group; it will include at least the current call. 170 groupSize = 1; 171 172 // Update the group values to those of the current call. 173 groupNumber = number; 174 groupPostDialDigits = numberPostDialDigits; 175 groupViaNumbers = numberViaNumbers; 176 groupCallType = callType; 177 groupAccountComponentName = accountComponentName; 178 groupAccountId = accountId; 179 groupCallbackAction = callbackAction; 180 groupFeatures = callFeatures; 181 } 182 183 // Save the callback action and the day group associated with the current call. 184 final long currentCallId = cursor.getLong(CallLogQuery.ID); 185 groupCreator.setCallbackAction(currentCallId, groupCallbackAction); 186 groupCreator.setDayGroup(currentCallId, groupDayGroup); 187 } 188 189 // Create a group for the last set of calls. 190 groupCreator.addGroup(count - groupSize, groupSize); 191 } 192 193 /** 194 * Returns true when the two input numbers can be considered identical enough for caller ID 195 * purposes and put in a call log group. 196 */ 197 @VisibleForTesting equalNumbers(@ullable String number1, @Nullable String number2)198 boolean equalNumbers(@Nullable String number1, @Nullable String number2) { 199 if (PhoneNumberHelper.isUriNumber(number1) || PhoneNumberHelper.isUriNumber(number2)) { 200 return compareSipAddresses(number1, number2); 201 } 202 203 // PhoneNumberUtils.compare(String, String) ignores special characters such as '#'. For example, 204 // it thinks "123" and "#123" are identical enough for caller ID purposes. 205 // When either input number contains special characters, we put the two in the same group iff 206 // their raw numbers are exactly the same. 207 if (PhoneNumberHelper.numberHasSpecialChars(number1) 208 || PhoneNumberHelper.numberHasSpecialChars(number2)) { 209 return PhoneNumberHelper.sameRawNumbers(number1, number2); 210 } 211 212 return PhoneNumberUtils.compare(number1, number2); 213 } 214 isSameAccount(String name1, String name2, String id1, String id2)215 private boolean isSameAccount(String name1, String name2, String id1, String id2) { 216 return TextUtils.equals(name1, name2) && TextUtils.equals(id1, id2); 217 } 218 219 @VisibleForTesting compareSipAddresses(@ullable String number1, @Nullable String number2)220 boolean compareSipAddresses(@Nullable String number1, @Nullable String number2) { 221 if (number1 == null || number2 == null) { 222 return Objects.equals(number1, number2); 223 } 224 225 int index1 = number1.indexOf('@'); 226 final String userinfo1; 227 final String rest1; 228 if (index1 != -1) { 229 userinfo1 = number1.substring(0, index1); 230 rest1 = number1.substring(index1); 231 } else { 232 userinfo1 = number1; 233 rest1 = ""; 234 } 235 236 int index2 = number2.indexOf('@'); 237 final String userinfo2; 238 final String rest2; 239 if (index2 != -1) { 240 userinfo2 = number2.substring(0, index2); 241 rest2 = number2.substring(index2); 242 } else { 243 userinfo2 = number2; 244 rest2 = ""; 245 } 246 247 return userinfo1.equals(userinfo2) && rest1.equalsIgnoreCase(rest2); 248 } 249 250 /** 251 * Given a call date and the current date, determine which date group the call belongs in. 252 * 253 * @param date The call date. 254 * @param now The current date. 255 * @return The date group the call belongs in. 256 */ getDayGroup(long date, long now)257 private int getDayGroup(long date, long now) { 258 int days = DateUtils.getDayDifference(TIME, date, now); 259 260 if (days == 0) { 261 return DAY_GROUP_TODAY; 262 } else if (days == 1) { 263 return DAY_GROUP_YESTERDAY; 264 } else { 265 return DAY_GROUP_OTHER; 266 } 267 } 268 areBothNotVoicemail(int callType, int groupCallType)269 private boolean areBothNotVoicemail(int callType, int groupCallType) { 270 return callType != Calls.VOICEMAIL_TYPE && groupCallType != Calls.VOICEMAIL_TYPE; 271 } 272 areBothNotBlocked(int callType, int groupCallType)273 private boolean areBothNotBlocked(int callType, int groupCallType) { 274 return callType != Calls.BLOCKED_TYPE && groupCallType != Calls.BLOCKED_TYPE; 275 } 276 areBothBlocked(int callType, int groupCallType)277 private boolean areBothBlocked(int callType, int groupCallType) { 278 return callType == Calls.BLOCKED_TYPE && groupCallType == Calls.BLOCKED_TYPE; 279 } 280 meetsAssistedDialingGroupingCriteria(int groupFeatures, int callFeatures)281 private boolean meetsAssistedDialingGroupingCriteria(int groupFeatures, int callFeatures) { 282 int groupAssisted = (groupFeatures & TelephonyManagerCompat.FEATURES_ASSISTED_DIALING); 283 int callAssisted = (callFeatures & TelephonyManagerCompat.FEATURES_ASSISTED_DIALING); 284 285 return groupAssisted == callAssisted; 286 } 287 288 public interface GroupCreator { 289 290 /** 291 * Defines the interface for adding a group to the call log. The primary group for a call log 292 * groups the calls together based on the number which was dialed. 293 * 294 * @param cursorPosition The starting position of the group in the cursor. 295 * @param size The size of the group. 296 */ addGroup(int cursorPosition, int size)297 void addGroup(int cursorPosition, int size); 298 299 /** 300 * Defines the interface for tracking each call's callback action. Calls in a call group are 301 * associated with the same callback action as the first call in the group. The value of a 302 * callback action should be one of the categories in {@link CallbackAction}. 303 * 304 * @param rowId The row ID of the current call. 305 * @param callbackAction The current call's callback action. 306 */ setCallbackAction(long rowId, @CallbackAction int callbackAction)307 void setCallbackAction(long rowId, @CallbackAction int callbackAction); 308 309 /** 310 * Defines the interface for tracking the day group each call belongs to. Calls in a call group 311 * are assigned the same day group as the first call in the group. The day group assigns calls 312 * to the buckets: Today, Yesterday, Last week, and Other 313 * 314 * @param rowId The row ID of the current call. 315 * @param dayGroup The day group the call belongs to. 316 */ setDayGroup(long rowId, int dayGroup)317 void setDayGroup(long rowId, int dayGroup); 318 319 /** Defines the interface for clearing the day groupings information on rebind/regroup. */ clearDayGroups()320 void clearDayGroups(); 321 } 322 } 323