18ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng/*
28ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * Copyright (C) 2013 The Android Open Source Project
38ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng *
48ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
58ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * you may not use this file except in compliance with the License.
68ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * You may obtain a copy of the License at
78ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng *
88ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
98ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng *
108ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * Unless required by applicable law or agreed to in writing, software
118ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
128ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * See the License for the specific language governing permissions and
148ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * limitations under the License
158ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng */
168ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng
178ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Chengpackage com.android.providers.contacts.testutil;
188ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng
198ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Chengimport android.content.ContentResolver;
208ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Chengimport android.content.ContentUris;
218ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Chengimport android.content.ContentValues;
228ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Chengimport android.database.Cursor;
238ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Chengimport android.net.Uri;
248ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Chengimport android.provider.ContactsContract;
258ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng
268ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng/**
278ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * Convenience methods for operating on the Contacts table.
288ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng */
298ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Chengpublic class ContactUtil {
308ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng
318ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng    private static final Uri URI = ContactsContract.Contacts.CONTENT_URI;
328ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng
338ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng    public static void update(ContentResolver resolver, long contactId,
348ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng            ContentValues values) {
358ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        Uri uri = ContentUris.withAppendedId(URI, contactId);
368ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        resolver.update(uri, values, null, null);
378ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng    }
388ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng
398ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng    public static void delete(ContentResolver resolver, long contactId) {
408ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        Uri uri = ContentUris.withAppendedId(URI, contactId);
418ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        resolver.delete(uri, null, null);
428ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng    }
438ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng
448ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng    public static boolean recordExistsForContactId(ContentResolver resolver, long contactId) {
458ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        String[] projection = new String[]{
468ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng                ContactsContract.Contacts._ID
478ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        };
488ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        Uri uri = ContentUris.withAppendedId(URI, contactId);
498ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        Cursor cursor = resolver.query(uri, projection, null, null, null);
508ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        if (cursor.moveToNext()) {
518ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng            return true;
528ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        }
538ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        return false;
548ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng    }
558ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng
568ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng    public static long queryContactLastUpdatedTimestamp(ContentResolver resolver, long contactId) {
578ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        String[] projection = new String[]{
588ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng                ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP
598ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        };
608ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng
618ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
628ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        Cursor cursor = resolver.query(uri, projection, null, null, null);
638ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        try {
648ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng            if (cursor.moveToNext()) {
658ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng                return cursor.getLong(0);
668ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng            }
678ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        } finally {
688ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng            if (cursor != null) {
698ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng                cursor.close();
708ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng            }
718ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        }
728ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng        return CommonDatabaseUtils.NOT_FOUND;
738ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng    }
748ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng}
75