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.calllog;
18 
19 import android.database.Cursor;
20 import android.provider.CallLog.Calls;
21 import android.telephony.PhoneNumberUtils;
22 import android.text.format.Time;
23 
24 import com.android.common.widget.GroupingListAdapter;
25 import com.android.contacts.common.util.DateUtils;
26 import com.android.contacts.common.util.PhoneNumberHelper;
27 
28 import com.google.common.annotations.VisibleForTesting;
29 
30 import java.util.Objects;
31 
32 /**
33  * Groups together calls in the call log.  The primary grouping attempts to group together calls
34  * to and from the same number into a single row on the call log.
35  * A secondary grouping assigns calls, grouped via the primary grouping, to "day groups".  The day
36  * groups provide a means of identifying the calls which occurred "Today", "Yesterday", "Last week",
37  * or "Other".
38  * <p>
39  * This class is meant to be used in conjunction with {@link GroupingListAdapter}.
40  */
41 public class CallLogGroupBuilder {
42     public interface GroupCreator {
43 
44         /**
45          * Defines the interface for adding a group to the call log.
46          * The primary group for a call log groups the calls together based on the number which was
47          * dialed.
48          * @param cursorPosition The starting position of the group in the cursor.
49          * @param size The size of the group.
50          * @param expanded Whether the group is expanded; always false for the call log.
51          */
addGroup(int cursorPosition, int size, boolean expanded)52         public void addGroup(int cursorPosition, int size, boolean expanded);
53 
54         /**
55          * Defines the interface for tracking the day group each call belongs to.  Calls in a call
56          * group are assigned the same day group as the first call in the group.  The day group
57          * assigns calls to the buckets: Today, Yesterday, Last week, and Other
58          *
59          * @param rowId The row Id of the current call.
60          * @param dayGroup The day group the call belongs in.
61          */
setDayGroup(long rowId, int dayGroup)62         public void setDayGroup(long rowId, int dayGroup);
63 
64         /**
65          * Defines the interface for clearing the day groupings information on rebind/regroup.
66          */
clearDayGroups()67         public void clearDayGroups();
68     }
69 
70     /**
71      * Day grouping for call log entries used to represent no associated day group.  Used primarily
72      * when retrieving the previous day group, but there is no previous day group (i.e. we are at
73      * the start of the list).
74      */
75     public static final int DAY_GROUP_NONE = -1;
76 
77     /** Day grouping for calls which occurred today. */
78     public static final int DAY_GROUP_TODAY = 0;
79 
80     /** Day grouping for calls which occurred yesterday. */
81     public static final int DAY_GROUP_YESTERDAY = 1;
82 
83     /** Day grouping for calls which occurred before last week. */
84     public static final int DAY_GROUP_OTHER = 2;
85 
86     /** Instance of the time object used for time calculations. */
87     private static final Time TIME = new Time();
88 
89     /** The object on which the groups are created. */
90     private final GroupCreator mGroupCreator;
91 
CallLogGroupBuilder(GroupCreator groupCreator)92     public CallLogGroupBuilder(GroupCreator groupCreator) {
93         mGroupCreator = groupCreator;
94     }
95 
96     /**
97      * Finds all groups of adjacent entries in the call log which should be grouped together and
98      * calls {@link GroupCreator#addGroup(int, int, boolean)} on {@link #mGroupCreator} for each of
99      * them.
100      * <p>
101      * For entries that are not grouped with others, we do not need to create a group of size one.
102      * <p>
103      * It assumes that the cursor will not change during its execution.
104      *
105      * @see GroupingListAdapter#addGroups(Cursor)
106      */
addGroups(Cursor cursor)107     public void addGroups(Cursor cursor) {
108         final int count = cursor.getCount();
109         if (count == 0) {
110             return;
111         }
112 
113         // Clear any previous day grouping information.
114         mGroupCreator.clearDayGroups();
115 
116         // Get current system time, used for calculating which day group calls belong to.
117         long currentTime = System.currentTimeMillis();
118 
119         int currentGroupSize = 1;
120         cursor.moveToFirst();
121         // The number of the first entry in the group.
122         String firstNumber = cursor.getString(CallLogQuery.NUMBER);
123         // This is the type of the first call in the group.
124         int firstCallType = cursor.getInt(CallLogQuery.CALL_TYPE);
125 
126         // The account information of the first entry in the group.
127         String firstAccountComponentName = cursor.getString(CallLogQuery.ACCOUNT_COMPONENT_NAME);
128         String firstAccountId = cursor.getString(CallLogQuery.ACCOUNT_ID);
129 
130         // Determine the day group for the first call in the cursor.
131         final long firstDate = cursor.getLong(CallLogQuery.DATE);
132         final long firstRowId = cursor.getLong(CallLogQuery.ID);
133         int currentGroupDayGroup = getDayGroup(firstDate, currentTime);
134         mGroupCreator.setDayGroup(firstRowId, currentGroupDayGroup);
135 
136         while (cursor.moveToNext()) {
137             // The number of the current row in the cursor.
138             final String currentNumber = cursor.getString(CallLogQuery.NUMBER);
139             final int callType = cursor.getInt(CallLogQuery.CALL_TYPE);
140             final String currentAccountComponentName = cursor.getString(
141                     CallLogQuery.ACCOUNT_COMPONENT_NAME);
142             final String currentAccountId = cursor.getString(CallLogQuery.ACCOUNT_ID);
143 
144             final boolean sameNumber = equalNumbers(firstNumber, currentNumber);
145             final boolean sameAccountComponentName = Objects.equals(
146                     firstAccountComponentName,
147                     currentAccountComponentName);
148             final boolean sameAccountId = Objects.equals(
149                     firstAccountId,
150                     currentAccountId);
151             final boolean sameAccount = sameAccountComponentName && sameAccountId;
152 
153             final boolean shouldGroup;
154             final long currentCallId = cursor.getLong(CallLogQuery.ID);
155             final long date = cursor.getLong(CallLogQuery.DATE);
156 
157             if (!sameNumber || !sameAccount) {
158                 // Should only group with calls from the same number.
159                 shouldGroup = false;
160             } else if (firstCallType == Calls.VOICEMAIL_TYPE) {
161                 // never group voicemail.
162                 shouldGroup = false;
163             } else {
164                 // Incoming, outgoing, and missed calls group together.
165                 shouldGroup = callType != Calls.VOICEMAIL_TYPE;
166             }
167 
168             if (shouldGroup) {
169                 // Increment the size of the group to include the current call, but do not create
170                 // the group until we find a call that does not match.
171                 currentGroupSize++;
172             } else {
173                 // The call group has changed, so determine the day group for the new call group.
174                 // This ensures all calls grouped together in the call log are assigned the same
175                 // day group.
176                 currentGroupDayGroup = getDayGroup(date, currentTime);
177 
178                 // Create a group for the previous set of calls, excluding the current one, but do
179                 // not create a group for a single call.
180                 if (currentGroupSize > 1) {
181                     addGroup(cursor.getPosition() - currentGroupSize, currentGroupSize);
182                 }
183                 // Start a new group; it will include at least the current call.
184                 currentGroupSize = 1;
185                 // The current entry is now the first in the group.
186                 firstNumber = currentNumber;
187                 firstCallType = callType;
188                 firstAccountComponentName = currentAccountComponentName;
189                 firstAccountId = currentAccountId;
190             }
191 
192             // Save the day group associated with the current call.
193             mGroupCreator.setDayGroup(currentCallId, currentGroupDayGroup);
194         }
195         // If the last set of calls at the end of the call log was itself a group, create it now.
196         if (currentGroupSize > 1) {
197             addGroup(count - currentGroupSize, currentGroupSize);
198         }
199     }
200 
201     /**
202      * Creates a group of items in the cursor.
203      * <p>
204      * The group is always unexpanded.
205      *
206      * @see CallLogAdapter#addGroup(int, int, boolean)
207      */
addGroup(int cursorPosition, int size)208     private void addGroup(int cursorPosition, int size) {
209         mGroupCreator.addGroup(cursorPosition, size, false);
210     }
211 
212     @VisibleForTesting
equalNumbers(String number1, String number2)213     boolean equalNumbers(String number1, String number2) {
214         if (PhoneNumberHelper.isUriNumber(number1) || PhoneNumberHelper.isUriNumber(number2)) {
215             return compareSipAddresses(number1, number2);
216         } else {
217             return PhoneNumberUtils.compare(number1, number2);
218         }
219     }
220 
221     @VisibleForTesting
compareSipAddresses(String number1, String number2)222     boolean compareSipAddresses(String number1, String number2) {
223         if (number1 == null || number2 == null) return number1 == number2;
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 }
269