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