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