1 /* 2 * Copyright (C) 2018 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.car.dialer.livedata; 18 19 import static com.android.car.dialer.livedata.CallHistoryLiveData.CallType.CALL_TYPE_ALL; 20 21 import android.Manifest; 22 import android.content.Context; 23 import android.database.Cursor; 24 import android.net.Uri; 25 import android.provider.CallLog; 26 27 import androidx.annotation.IntDef; 28 29 import com.android.car.telephony.common.AsyncQueryLiveData; 30 import com.android.car.telephony.common.PhoneCallLog; 31 import com.android.car.telephony.common.QueryParam; 32 33 import java.util.ArrayList; 34 import java.util.List; 35 36 /** 37 * Live data which loads call history. 38 */ 39 //TODO: Rename to PhoneCallLogLiveData 40 public class CallHistoryLiveData extends AsyncQueryLiveData<List<PhoneCallLog>> { 41 /** The default limit of loading call logs */ 42 private final static int DEFAULT_CALL_LOG_LIMIT = 100; 43 private static final String[] EMPTY_STRING_ARRAY = new String[0]; 44 45 @IntDef({ 46 CALL_TYPE_ALL, 47 CallType.INCOMING_TYPE, 48 CallType.OUTGOING_TYPE, 49 CallType.MISSED_TYPE, 50 }) 51 public @interface CallType { 52 int CALL_TYPE_ALL = -1; 53 int INCOMING_TYPE = CallLog.Calls.INCOMING_TYPE; 54 int OUTGOING_TYPE = CallLog.Calls.OUTGOING_TYPE; 55 int MISSED_TYPE = CallLog.Calls.MISSED_TYPE; 56 int VOICEMAIL_TYPE = CallLog.Calls.VOICEMAIL_TYPE; 57 } 58 59 /** 60 * Creates a new instance of call history live data which loads all types of call history 61 * with a limit of 100 logs. 62 */ newInstance(Context context, String accountName)63 public static CallHistoryLiveData newInstance(Context context, String accountName) { 64 return newInstance(context, CALL_TYPE_ALL, DEFAULT_CALL_LOG_LIMIT, accountName); 65 } 66 newInstance(Context context, int callType, int limit, String accountName)67 private static CallHistoryLiveData newInstance(Context context, int callType, int limit, 68 String accountName) { 69 StringBuilder where = new StringBuilder(); 70 List<String> selectionArgs = new ArrayList<>(); 71 limit = Math.max(limit, 0); 72 73 if (callType != CALL_TYPE_ALL) { 74 // add a filter for call type 75 where.append("(" + CallLog.Calls.TYPE + " = ?)"); 76 selectionArgs.add(Integer.toString(callType)); 77 where.append(" AND "); 78 } 79 where.append("(" + CallLog.Calls.PHONE_ACCOUNT_ID + " = ?)"); 80 selectionArgs.add(accountName); 81 82 String selection = where.length() > 0 ? where.toString() : null; 83 84 Uri uri = CallLog.Calls.CONTENT_URI.buildUpon() 85 .appendQueryParameter(CallLog.Calls.LIMIT_PARAM_KEY, 86 Integer.toString(limit)) 87 .build(); 88 QueryParam queryParam = new QueryParam( 89 uri, 90 null, 91 selection, 92 selectionArgs.toArray(EMPTY_STRING_ARRAY), 93 CallLog.Calls.DEFAULT_SORT_ORDER, 94 Manifest.permission.READ_CALL_LOG); 95 return new CallHistoryLiveData(context, queryParam); 96 } 97 98 private final Context mContext; CallHistoryLiveData(Context context, QueryParam queryParam)99 private CallHistoryLiveData(Context context, QueryParam queryParam) { 100 super(context, QueryParam.of(queryParam)); 101 mContext = context; 102 } 103 104 @Override convertToEntity(Cursor cursor)105 protected List<PhoneCallLog> convertToEntity(Cursor cursor) { 106 List<PhoneCallLog> resultList = new ArrayList<>(); 107 108 while (cursor.moveToNext()) { 109 PhoneCallLog phoneCallLog = PhoneCallLog.fromCursor(mContext, cursor); 110 PhoneCallLog previousCallLog = resultList.isEmpty() ? null : resultList.get( 111 resultList.size() - 1); 112 113 if (previousCallLog == null || !previousCallLog.merge(phoneCallLog)) { 114 resultList.add(phoneCallLog); 115 } 116 } 117 return resultList; 118 } 119 } 120