• 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.compat.AppCompatConstants;
29 import com.android.dialer.phonenumbercache.CallLogQuery;
30 import com.android.dialer.phonenumberutil.PhoneNumberHelper;
31 import java.util.Objects;
32 
33 /**
34  * Groups together calls in the call log. The primary grouping attempts to group together calls to
35  * and from the same number into a single row on the call log. A secondary grouping assigns calls,
36  * grouped via the primary grouping, to "day groups". The day groups provide a means of identifying
37  * the calls which occurred "Today", "Yesterday", "Last week", or "Other".
38  *
39  * <p>This class is meant to be used in conjunction with {@link GroupingListAdapter}.
40  */
41 public class CallLogGroupBuilder {
42 
43   /**
44    * Day grouping for call log entries used to represent no associated day group. Used primarily
45    * when retrieving the previous day group, but there is no previous day group (i.e. we are at the
46    * start of the list).
47    */
48   public static final int DAY_GROUP_NONE = -1;
49   /** Day grouping for calls which occurred today. */
50   public static final int DAY_GROUP_TODAY = 0;
51   /** Day grouping for calls which occurred yesterday. */
52   public static final int DAY_GROUP_YESTERDAY = 1;
53   /** Day grouping for calls which occurred before last week. */
54   public static final int DAY_GROUP_OTHER = 2;
55   /** Instance of the time object used for time calculations. */
56   private static final Time TIME = new Time();
57   /** The object on which the groups are created. */
58   private final GroupCreator mGroupCreator;
59 
CallLogGroupBuilder(GroupCreator groupCreator)60   public CallLogGroupBuilder(GroupCreator groupCreator) {
61     mGroupCreator = groupCreator;
62   }
63 
64   /**
65    * Finds all groups of adjacent entries in the call log which should be grouped together and calls
66    * {@link GroupCreator#addGroup(int, int)} on {@link #mGroupCreator} for each of them.
67    *
68    * <p>For entries that are not grouped with others, we do not need to create a group of size one.
69    *
70    * <p>It assumes that the cursor will not change during its execution.
71    *
72    * @see GroupingListAdapter#addGroups(Cursor)
73    */
addGroups(Cursor cursor)74   public void addGroups(Cursor cursor) {
75     final int count = cursor.getCount();
76     if (count == 0) {
77       return;
78     }
79 
80     // Clear any previous day grouping information.
81     mGroupCreator.clearDayGroups();
82 
83     // Get current system time, used for calculating which day group calls belong to.
84     long currentTime = System.currentTimeMillis();
85     cursor.moveToFirst();
86 
87     // Determine the day group for the first call in the cursor.
88     final long firstDate = cursor.getLong(CallLogQuery.DATE);
89     final long firstRowId = cursor.getLong(CallLogQuery.ID);
90     int groupDayGroup = getDayGroup(firstDate, currentTime);
91     mGroupCreator.setDayGroup(firstRowId, groupDayGroup);
92 
93     // Instantiate the group values to those of the first call in the cursor.
94     String groupNumber = cursor.getString(CallLogQuery.NUMBER);
95     String groupPostDialDigits =
96         (VERSION.SDK_INT >= VERSION_CODES.N) ? cursor.getString(CallLogQuery.POST_DIAL_DIGITS) : "";
97     String groupViaNumbers =
98         (VERSION.SDK_INT >= VERSION_CODES.N) ? cursor.getString(CallLogQuery.VIA_NUMBER) : "";
99     int groupCallType = cursor.getInt(CallLogQuery.CALL_TYPE);
100     String groupAccountComponentName = cursor.getString(CallLogQuery.ACCOUNT_COMPONENT_NAME);
101     String groupAccountId = cursor.getString(CallLogQuery.ACCOUNT_ID);
102     int groupSize = 1;
103 
104     String number;
105     String numberPostDialDigits;
106     String numberViaNumbers;
107     int callType;
108     String accountComponentName;
109     String accountId;
110 
111     while (cursor.moveToNext()) {
112       // Obtain the values for the current call to group.
113       number = cursor.getString(CallLogQuery.NUMBER);
114       numberPostDialDigits =
115           (VERSION.SDK_INT >= VERSION_CODES.N)
116               ? cursor.getString(CallLogQuery.POST_DIAL_DIGITS)
117               : "";
118       numberViaNumbers =
119           (VERSION.SDK_INT >= VERSION_CODES.N) ? cursor.getString(CallLogQuery.VIA_NUMBER) : "";
120       callType = cursor.getInt(CallLogQuery.CALL_TYPE);
121       accountComponentName = cursor.getString(CallLogQuery.ACCOUNT_COMPONENT_NAME);
122       accountId = cursor.getString(CallLogQuery.ACCOUNT_ID);
123 
124       final boolean isSameNumber = equalNumbers(groupNumber, number);
125       final boolean isSamePostDialDigits = groupPostDialDigits.equals(numberPostDialDigits);
126       final boolean isSameViaNumbers = groupViaNumbers.equals(numberViaNumbers);
127       final boolean isSameAccount =
128           isSameAccount(groupAccountComponentName, accountComponentName, groupAccountId, accountId);
129 
130       // Group with the same number and account. Never group voicemails. Only group blocked
131       // calls with other blocked calls.
132       if (isSameNumber
133           && isSameAccount
134           && isSamePostDialDigits
135           && isSameViaNumbers
136           && areBothNotVoicemail(callType, groupCallType)
137           && (areBothNotBlocked(callType, groupCallType)
138               || areBothBlocked(callType, groupCallType))) {
139         // Increment the size of the group to include the current call, but do not create
140         // the group until finding a call that does not match.
141         groupSize++;
142       } else {
143         // The call group has changed. Determine the day group for the new call group.
144         final long date = cursor.getLong(CallLogQuery.DATE);
145         groupDayGroup = getDayGroup(date, currentTime);
146 
147         // Create a group for the previous group of calls, which does not include the
148         // current call.
149         mGroupCreator.addGroup(cursor.getPosition() - groupSize, groupSize);
150 
151         // Start a new group; it will include at least the current call.
152         groupSize = 1;
153 
154         // Update the group values to those of the current call.
155         groupNumber = number;
156         groupPostDialDigits = numberPostDialDigits;
157         groupViaNumbers = numberViaNumbers;
158         groupCallType = callType;
159         groupAccountComponentName = accountComponentName;
160         groupAccountId = accountId;
161       }
162 
163       // Save the day group associated with the current call.
164       final long currentCallId = cursor.getLong(CallLogQuery.ID);
165       mGroupCreator.setDayGroup(currentCallId, groupDayGroup);
166     }
167 
168     // Create a group for the last set of calls.
169     mGroupCreator.addGroup(count - groupSize, groupSize);
170   }
171 
172   @VisibleForTesting
equalNumbers(@ullable String number1, @Nullable String number2)173   boolean equalNumbers(@Nullable String number1, @Nullable String number2) {
174     if (PhoneNumberHelper.isUriNumber(number1) || PhoneNumberHelper.isUriNumber(number2)) {
175       return compareSipAddresses(number1, number2);
176     } else {
177       return PhoneNumberUtils.compare(number1, number2);
178     }
179   }
180 
isSameAccount(String name1, String name2, String id1, String id2)181   private boolean isSameAccount(String name1, String name2, String id1, String id2) {
182     return TextUtils.equals(name1, name2) && TextUtils.equals(id1, id2);
183   }
184 
185   @VisibleForTesting
compareSipAddresses(@ullable String number1, @Nullable String number2)186   boolean compareSipAddresses(@Nullable String number1, @Nullable String number2) {
187     if (number1 == null || number2 == null) {
188       return Objects.equals(number1, number2);
189     }
190 
191     int index1 = number1.indexOf('@');
192     final String userinfo1;
193     final String rest1;
194     if (index1 != -1) {
195       userinfo1 = number1.substring(0, index1);
196       rest1 = number1.substring(index1);
197     } else {
198       userinfo1 = number1;
199       rest1 = "";
200     }
201 
202     int index2 = number2.indexOf('@');
203     final String userinfo2;
204     final String rest2;
205     if (index2 != -1) {
206       userinfo2 = number2.substring(0, index2);
207       rest2 = number2.substring(index2);
208     } else {
209       userinfo2 = number2;
210       rest2 = "";
211     }
212 
213     return userinfo1.equals(userinfo2) && rest1.equalsIgnoreCase(rest2);
214   }
215 
216   /**
217    * Given a call date and the current date, determine which date group the call belongs in.
218    *
219    * @param date The call date.
220    * @param now The current date.
221    * @return The date group the call belongs in.
222    */
getDayGroup(long date, long now)223   private int getDayGroup(long date, long now) {
224     int days = DateUtils.getDayDifference(TIME, date, now);
225 
226     if (days == 0) {
227       return DAY_GROUP_TODAY;
228     } else if (days == 1) {
229       return DAY_GROUP_YESTERDAY;
230     } else {
231       return DAY_GROUP_OTHER;
232     }
233   }
234 
areBothNotVoicemail(int callType, int groupCallType)235   private boolean areBothNotVoicemail(int callType, int groupCallType) {
236     return callType != AppCompatConstants.CALLS_VOICEMAIL_TYPE
237         && groupCallType != AppCompatConstants.CALLS_VOICEMAIL_TYPE;
238   }
239 
areBothNotBlocked(int callType, int groupCallType)240   private boolean areBothNotBlocked(int callType, int groupCallType) {
241     return callType != AppCompatConstants.CALLS_BLOCKED_TYPE
242         && groupCallType != AppCompatConstants.CALLS_BLOCKED_TYPE;
243   }
244 
areBothBlocked(int callType, int groupCallType)245   private boolean areBothBlocked(int callType, int groupCallType) {
246     return callType == AppCompatConstants.CALLS_BLOCKED_TYPE
247         && groupCallType == AppCompatConstants.CALLS_BLOCKED_TYPE;
248   }
249 
250   public interface GroupCreator {
251 
252     /**
253      * Defines the interface for adding a group to the call log. The primary group for a call log
254      * groups the calls together based on the number which was dialed.
255      *
256      * @param cursorPosition The starting position of the group in the cursor.
257      * @param size The size of the group.
258      */
addGroup(int cursorPosition, int size)259     void addGroup(int cursorPosition, int size);
260 
261     /**
262      * Defines the interface for tracking the day group each call belongs to. Calls in a call group
263      * are assigned the same day group as the first call in the group. The day group assigns calls
264      * to the buckets: Today, Yesterday, Last week, and Other
265      *
266      * @param rowId The row Id of the current call.
267      * @param dayGroup The day group the call belongs in.
268      */
setDayGroup(long rowId, int dayGroup)269     void setDayGroup(long rowId, int dayGroup);
270 
271     /** Defines the interface for clearing the day groupings information on rebind/regroup. */
clearDayGroups()272     void clearDayGroups();
273   }
274 }
275