• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 android.provider.cts.contacts;
18 
19 import static android.provider.CallLog.Calls.CONTENT_URI;
20 import static android.provider.CallLog.Calls.MISSED_REASON_NOT_MISSED;
21 import static android.provider.CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME;
22 import static android.provider.CallLog.Calls.PHONE_ACCOUNT_ID;
23 import static android.provider.CallLog.Calls.PRESENTATION_ALLOWED;
24 
25 import android.content.Context;
26 import android.database.Cursor;
27 import android.net.Uri;
28 import android.provider.CallLog;
29 import android.telecom.CallerInfo;
30 import android.telecom.PhoneAccountHandle;
31 
32 import java.util.Random;
33 
34 /** A generator that generates random call log entries for testing purposes. */
35 public final class CallLogGenerator {
36     private static final String NAME_CHARACTERS =
37             "abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789.";
38     private static final String NUMBER_CHARACTERS = "0123456789";
39 
40     private static final int MAX_NAME_SIZE = 24;
41     private static final int MAX_PHONE_NUMBER_SIZE = 12;
42     private static final int MAX_DURATION_SECONDS = 300;
43     private static final int MAX_PHONE_TYPE = 3;
44 
45     private final Random mRandom = new Random();
46     private final Context mContext;
47 
48     /** A constructor with a provided context. */
CallLogGenerator(Context context)49     public CallLogGenerator(Context context) {
50         mContext = context;
51     }
52 
53     /**
54      * Generates a specified number of call logs for a given phone account.
55      *
56      * @param phoneAccountHandle The handle to the phone account for which to generate call logs.
57      * @param size The number of call logs to generate.
58      */
generateCallLogs(PhoneAccountHandle phoneAccountHandle, int size)59     public void generateCallLogs(PhoneAccountHandle phoneAccountHandle, int size) {
60         for (int i = 0; i < size; i++) {
61             addCallLog(phoneAccountHandle);
62         }
63     }
64 
65     /**
66      * Adds a random call log entry.
67      * @param phoneAccountHandle The phone account associated with the call.
68      * @return The URI of the newly created call log entry.
69      */
addCallLog(PhoneAccountHandle phoneAccountHandle)70     public Uri addCallLog(PhoneAccountHandle phoneAccountHandle) {
71         final CallerInfo callerInfo = new CallerInfo();
72         callerInfo.setName(getRandomName());
73         return CallLog.Calls.addCall(
74                 callerInfo,
75                 mContext,
76                 getRandomPhoneNumber(),
77                 PRESENTATION_ALLOWED,
78                 getRandomPhoneType(),
79                 0 /* features */,
80                 phoneAccountHandle,
81                 System.currentTimeMillis(),
82                 getRandomDuration(),
83                 null /* dataUsage */,
84                 MISSED_REASON_NOT_MISSED,
85                 0 /* isPhoneAccountMigrationPending */);
86     }
87 
88     /**
89      * Deletes call logs associated with a specific phone account.
90      *
91      * @param phoneAccountHandle The phone account associated with the call.
92      * @return The number of call log entries deleted.
93      */
deleteCallLogs(PhoneAccountHandle phoneAccountHandle)94     public int deleteCallLogs(PhoneAccountHandle phoneAccountHandle) {
95         final String accountComponentString =
96                 phoneAccountHandle.getComponentName().flattenToString();
97         final String accountId = phoneAccountHandle.getId();
98         return mContext.getContentResolver().delete(
99                 CONTENT_URI,
100                 PHONE_ACCOUNT_COMPONENT_NAME + " = ? AND " + PHONE_ACCOUNT_ID + " = ?",
101                 new String[] {accountComponentString, accountId});
102     }
103 
104     /**
105      * Get the number of call log entries with a specific phone account.
106      *
107      * @param phoneAccountHandle The phone account associated with the call.
108      * @return The number of call log entries.
109      */
getCallLogSize(PhoneAccountHandle phoneAccountHandle)110     public int getCallLogSize(PhoneAccountHandle phoneAccountHandle) {
111         final String accountComponentString =
112                 phoneAccountHandle.getComponentName().flattenToString();
113         final String accountId = phoneAccountHandle.getId();
114         try (Cursor cursor = mContext.getContentResolver().query(
115                 CONTENT_URI,
116                 new String[] {"_id"},
117                 PHONE_ACCOUNT_COMPONENT_NAME + " = ? AND " + PHONE_ACCOUNT_ID + " = ?",
118                 new String[] {accountComponentString, accountId},
119                 null /* sortOrder */)) {
120             return cursor != null ? cursor.getCount() : 0;
121         }
122     }
123 
getRandomPhoneNumber()124     private String getRandomPhoneNumber() {
125         final int length = mRandom.nextInt(MAX_PHONE_NUMBER_SIZE) + 1;
126         return randomString(NUMBER_CHARACTERS, length);
127     }
128 
getRandomName()129     private String getRandomName() {
130         final int length = mRandom.nextInt(MAX_NAME_SIZE) + 1;
131         return randomString(NAME_CHARACTERS, length);
132     }
133 
getRandomPhoneType()134     private int getRandomPhoneType() {
135         return mRandom.nextInt(MAX_PHONE_TYPE) + 1;
136     }
137 
getRandomDuration()138     private int getRandomDuration() {
139         return mRandom.nextInt(MAX_DURATION_SECONDS) + 1;
140     }
141 
randomString(String characters, int length)142     private String randomString(String characters, int length) {
143         final char[] bytes = new char[length];
144         for (int i = 0; i < length; ++i) {
145             final int charIx = mRandom.nextInt(characters.length());
146             bytes[i] = characters.charAt(charIx);
147         }
148         return new String(bytes);
149     }
150 }
151