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 23 import com.android.common.widget.GroupingListAdapter; 24 import com.google.common.annotations.VisibleForTesting; 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 */ 31 public 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 (!sameNumber) { 74 // Should only group with calls from the same number. 75 shouldGroup = false; 76 } else if (firstCallType == Calls.VOICEMAIL_TYPE) { 77 // never group voicemail. 78 shouldGroup = false; 79 } else { 80 // Incoming, outgoing, and missed calls group together. 81 shouldGroup = (callType == Calls.INCOMING_TYPE || callType == Calls.OUTGOING_TYPE || 82 callType == Calls.MISSED_TYPE); 83 } 84 85 if (shouldGroup) { 86 // Increment the size of the group to include the current call, but do not create 87 // the group until we find a call that does not match. 88 currentGroupSize++; 89 } else { 90 // Create a group for the previous set of calls, excluding the current one, but do 91 // not create a group for a single call. 92 if (currentGroupSize > 1) { 93 addGroup(cursor.getPosition() - currentGroupSize, currentGroupSize); 94 } 95 // Start a new group; it will include at least the current call. 96 currentGroupSize = 1; 97 // The current entry is now the first in the group. 98 firstNumber = currentNumber; 99 firstCallType = callType; 100 } 101 } 102 // If the last set of calls at the end of the call log was itself a group, create it now. 103 if (currentGroupSize > 1) { 104 addGroup(count - currentGroupSize, currentGroupSize); 105 } 106 } 107 108 /** 109 * Creates a group of items in the cursor. 110 * <p> 111 * The group is always unexpanded. 112 * 113 * @see CallLogAdapter#addGroup(int, int, boolean) 114 */ 115 private void addGroup(int cursorPosition, int size) { 116 mGroupCreator.addGroup(cursorPosition, size, false); 117 } 118 119 @VisibleForTesting 120 boolean equalNumbers(String number1, String number2) { 121 if (PhoneNumberUtils.isUriNumber(number1) || PhoneNumberUtils.isUriNumber(number2)) { 122 return compareSipAddresses(number1, number2); 123 } else { 124 return PhoneNumberUtils.compare(number1, number2); 125 } 126 } 127 128 @VisibleForTesting 129 boolean compareSipAddresses(String number1, String number2) { 130 if (number1 == null || number2 == null) return number1 == number2; 131 132 int index1 = number1.indexOf('@'); 133 final String userinfo1; 134 final String rest1; 135 if (index1 != -1) { 136 userinfo1 = number1.substring(0, index1); 137 rest1 = number1.substring(index1); 138 } else { 139 userinfo1 = number1; 140 rest1 = ""; 141 } 142 143 int index2 = number2.indexOf('@'); 144 final String userinfo2; 145 final String rest2; 146 if (index2 != -1) { 147 userinfo2 = number2.substring(0, index2); 148 rest2 = number2.substring(index2); 149 } else { 150 userinfo2 = number2; 151 rest2 = ""; 152 } 153 154 return userinfo1.equals(userinfo2) && rest1.equalsIgnoreCase(rest2); 155 } 156 } 157