1/*
2 * Copyright (C) 2013 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
17package com.android.providers.contacts.testutil;
18
19import android.content.ContentResolver;
20import android.content.ContentUris;
21import android.content.ContentValues;
22import android.database.Cursor;
23import android.net.Uri;
24import android.provider.ContactsContract;
25
26/**
27 * Convenience methods for operating on the Contacts table.
28 */
29public class ContactUtil {
30
31    private static final Uri URI = ContactsContract.Contacts.CONTENT_URI;
32
33    public static void update(ContentResolver resolver, long contactId,
34            ContentValues values) {
35        Uri uri = ContentUris.withAppendedId(URI, contactId);
36        resolver.update(uri, values, null, null);
37    }
38
39    public static void delete(ContentResolver resolver, long contactId) {
40        Uri uri = ContentUris.withAppendedId(URI, contactId);
41        resolver.delete(uri, null, null);
42    }
43
44    public static boolean recordExistsForContactId(ContentResolver resolver, long contactId) {
45        String[] projection = new String[]{
46                ContactsContract.Contacts._ID
47        };
48        Uri uri = ContentUris.withAppendedId(URI, contactId);
49        Cursor cursor = resolver.query(uri, projection, null, null, null);
50        if (cursor.moveToNext()) {
51            return true;
52        }
53        return false;
54    }
55
56    public static long queryContactLastUpdatedTimestamp(ContentResolver resolver, long contactId) {
57        String[] projection = new String[]{
58                ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP
59        };
60
61        Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
62        Cursor cursor = resolver.query(uri, projection, null, null, null);
63        try {
64            if (cursor.moveToNext()) {
65                return cursor.getLong(0);
66            }
67        } finally {
68            if (cursor != null) {
69                cursor.close();
70            }
71        }
72        return CommonDatabaseUtils.NOT_FOUND;
73    }
74}
75