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
17package com.android.contacts.calllog;
18
19import com.android.common.widget.GroupingListAdapter;
20import com.google.common.annotations.VisibleForTesting;
21
22import android.database.Cursor;
23import android.provider.CallLog.Calls;
24import android.telephony.PhoneNumberUtils;
25
26/**
27 * Groups together calls in the call log.
28 * <p>
29 * This class is meant to be used in conjunction with {@link GroupingListAdapter}.
30 */
31public class CallLogGroupBuilder {
32    public interface GroupCreator {
33        public void addGroup(int cursorPosition, int size, boolean expanded);
34    }
35
36    /** The object on which the groups are created. */
37    private final GroupCreator mGroupCreator;
38
39    public CallLogGroupBuilder(GroupCreator groupCreator) {
40        mGroupCreator = groupCreator;
41    }
42
43    /**
44     * Finds all groups of adjacent entries in the call log which should be grouped together and
45     * calls {@link GroupCreator#addGroup(int, int, boolean)} on {@link #mGroupCreator} for each of
46     * them.
47     * <p>
48     * For entries that are not grouped with others, we do not need to create a group of size one.
49     * <p>
50     * It assumes that the cursor will not change during its execution.
51     *
52     * @see GroupingListAdapter#addGroups(Cursor)
53     */
54    public void addGroups(Cursor cursor) {
55        final int count = cursor.getCount();
56        if (count == 0) {
57            return;
58        }
59
60        int currentGroupSize = 1;
61        cursor.moveToFirst();
62        // The number of the first entry in the group.
63        String firstNumber = cursor.getString(CallLogQuery.NUMBER);
64        // This is the type of the first call in the group.
65        int firstCallType = cursor.getInt(CallLogQuery.CALL_TYPE);
66        while (cursor.moveToNext()) {
67            // The number of the current row in the cursor.
68            final String currentNumber = cursor.getString(CallLogQuery.NUMBER);
69            final int callType = cursor.getInt(CallLogQuery.CALL_TYPE);
70            final boolean sameNumber = equalNumbers(firstNumber, currentNumber);
71            final boolean shouldGroup;
72
73            if (CallLogQuery.isSectionHeader(cursor)) {
74                // Cannot group headers.
75                shouldGroup = false;
76            } else if (!sameNumber) {
77                // Should only group with calls from the same number.
78                shouldGroup = false;
79            } else if (firstCallType == Calls.VOICEMAIL_TYPE
80                    || firstCallType == Calls.MISSED_TYPE) {
81                // Voicemail and missed calls should only be grouped with subsequent missed calls.
82                shouldGroup = callType == Calls.MISSED_TYPE;
83            } else {
84                // Incoming and outgoing calls group together.
85                shouldGroup = callType == Calls.INCOMING_TYPE || callType == Calls.OUTGOING_TYPE;
86            }
87
88            if (shouldGroup) {
89                // Increment the size of the group to include the current call, but do not create
90                // the group until we find a call that does not match.
91                currentGroupSize++;
92            } else {
93                // Create a group for the previous set of calls, excluding the current one, but do
94                // not create a group for a single call.
95                if (currentGroupSize > 1) {
96                    addGroup(cursor.getPosition() - currentGroupSize, currentGroupSize);
97                }
98                // Start a new group; it will include at least the current call.
99                currentGroupSize = 1;
100                // The current entry is now the first in the group.
101                firstNumber = currentNumber;
102                firstCallType = callType;
103            }
104        }
105        // If the last set of calls at the end of the call log was itself a group, create it now.
106        if (currentGroupSize > 1) {
107            addGroup(count - currentGroupSize, currentGroupSize);
108        }
109    }
110
111    /**
112     * Creates a group of items in the cursor.
113     * <p>
114     * The group is always unexpanded.
115     *
116     * @see CallLogAdapter#addGroup(int, int, boolean)
117     */
118    private void addGroup(int cursorPosition, int size) {
119        mGroupCreator.addGroup(cursorPosition, size, false);
120    }
121
122    @VisibleForTesting
123    boolean equalNumbers(String number1, String number2) {
124        if (PhoneNumberUtils.isUriNumber(number1) || PhoneNumberUtils.isUriNumber(number2)) {
125            return compareSipAddresses(number1, number2);
126        } else {
127            return PhoneNumberUtils.compare(number1, number2);
128        }
129    }
130
131    @VisibleForTesting
132    boolean compareSipAddresses(String number1, String number2) {
133        if (number1 == null || number2 == null) return number1 == number2;
134
135        int index1 = number1.indexOf('@');
136        final String userinfo1;
137        final String rest1;
138        if (index1 != -1) {
139            userinfo1 = number1.substring(0, index1);
140            rest1 = number1.substring(index1);
141        } else {
142            userinfo1 = number1;
143            rest1 = "";
144        }
145
146        int index2 = number2.indexOf('@');
147        final String userinfo2;
148        final String rest2;
149        if (index2 != -1) {
150            userinfo2 = number2.substring(0, index2);
151            rest2 = number2.substring(index2);
152        } else {
153            userinfo2 = number2;
154            rest2 = "";
155        }
156
157        return userinfo1.equals(userinfo2) && rest1.equalsIgnoreCase(rest2);
158    }
159}
160