创建联系人及通话记录

58    private static final int TIMEOUT = 5000;
59    private static final int INNER_LOOP = 5;
60    private static final int EXPECTED_FRAMES = 100;
61    private static final String PACKAGE_NAME = "com.google.android.dialer";
62    private static final String RES_PACKAGE_NAME = "com.android.dialer";
63    private static final String RES_PACKAGE_NAME2 = "com.android.contacts";
64    private static final String RES_PACKAGE_NAME3 = "android";
65    private static final String APP_NAME = "Phone";
66    private static final String CONTACT_NAME = "A AAA Test Account";
67    private static final String CONTACT_NUMBER = "2468";
69    static final int PICK_CONTACT_REQUEST = 1;

162    // Method to insert a new contact
163    public void insertNewContacts() throws OperationApplicationException, RemoteException {
    
    
164        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
165        int rawContactID = ops.size();
166        // to insert a new raw contact in the table ContactsContract.RawContacts
167        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
168                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "Test")
169                .withValue(RawContacts.ACCOUNT_NAME, CONTACT_NAME)
170                .build());
171
172        // to insert display name in the table ContactsContract.Data
173        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
174                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
175                .withValue(ContactsContract.Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
176                .withValue(StructuredName.DISPLAY_NAME, CONTACT_NAME)
177                .build());
178
179        // to insert Mobile Number in the table ContactsContract.Data
180        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
181                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
182                .withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
183                .withValue(Phone.NUMBER, CONTACT_NUMBER)
184                .withValue(Phone.TYPE, CommonDataKinds.Phone.TYPE_MOBILE)
185                .build());
186
187            // Executing all the insert operations as a single database transaction
188        getInstrumentation().getContext().getContentResolver()
189                .applyBatch(ContactsContract.AUTHORITY, ops);
190    }
191
192    // Checks whether certain contact exists or not
193    public boolean doesContactExist() {
    
    
194        Uri uri = Uri.withAppendedPath(
195                ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(CONTACT_NUMBER));
196        Cursor contactLookup = getInstrumentation().getContext().getContentResolver().query(
197                uri, new String[] {
    
    
198                        BaseColumns._ID,
199                        ContactsContract.PhoneLookup.DISPLAY_NAME },
200                        null,
201                        null,
202                        null);
203        boolean found = false;
204        try {
    
    
205            if (contactLookup != null && contactLookup.getCount() > 0) {
    
    
206                contactLookup.moveToNext();
207                if (contactLookup.getString(contactLookup.getColumnIndex(
208                            ContactsContract.Data.DISPLAY_NAME)).equals(CONTACT_NAME))
209                    found = true;
210            }
211        } finally {
    
    
212            if (contactLookup != null) {
    
    
213                contactLookup.close();
214            }
215        }
216
217        return found;
218    }
219
220    // Inserts a new entry in the call log
221    public void addNumToCalLog(String number){
    
    
222        ContentValues values = new ContentValues();
223        values.put(CallLog.Calls.NUMBER, number);
224        values.put(CallLog.Calls.DATE, System.currentTimeMillis());
225        values.put(CallLog.Calls.DURATION, 0);
226        values.put(CallLog.Calls.TYPE, CallLog.Calls.OUTGOING_TYPE);
227        values.put(CallLog.Calls.NEW, 1);
228        values.put(CallLog.Calls.CACHED_NAME, "");
229        values.put(CallLog.Calls.CACHED_NUMBER_TYPE, 0);
230        values.put(CallLog.Calls.CACHED_NUMBER_LABEL, "");
231        getInstrumentation().getContext().getContentResolver()
232                .insert(CallLog.Calls.CONTENT_URI, values);
233    }
234
235    // Gets call log count
236    public int getCallLogCount() {
    
    
237       Cursor cursor = getInstrumentation().getContext().getContentResolver()
238               .query(CallLog.Calls.CONTENT_URI, null, null, null, null);
239       return cursor.getCount();
240    }
241
242    // Generates a random phone number
243    public String getRandomPhoneNumber() {
    
    
244        Random rand = new Random();
245        int num1 = (rand.nextInt(7) + 1) * 100 + (rand.nextInt(8) * 10) + rand.nextInt(8);
246        int num2 = rand.nextInt(743);
247        int num3 = rand.nextInt(10000);
248
249        return String.format("%03d-%03d-%04d", num1, num2, num3);
250    }

31        // Clear call log and ensure there are no outgoing calls
32        Context context = getInstrumentation().getContext();
33        ContentResolver resolver = context.getContentResolver();
34        resolver.delete(CallLog.Calls.CONTENT_URI, null, null);

       // Add a single call and verify it returns as last outgoing call
53        ContentValues values = new ContentValues();
54        values.put(CallLog.Calls.NUMBER, TEST_NUMBER);
55        values.put(CallLog.Calls.TYPE, Integer.valueOf(CallLog.Calls.OUTGOING_TYPE));
56        values.put(CallLog.Calls.DATE, Long.valueOf(0 /*start time*/));
57        values.put(CallLog.Calls.DURATION, Long.valueOf(5 /*call duration*/));
58
59        resolver.insert(CallLog.Calls.CONTENT_URI, values);

猜你喜欢

转载自blog.csdn.net/weixin_41693437/article/details/125372830