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