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 static android.provider.ContactsContract.DeletedContacts;
20
21import android.content.ContentResolver;
22import android.content.ContentUris;
23import android.database.Cursor;
24import android.net.Uri;
25
26import java.util.List;
27
28/**
29 * Convenience methods for operating on the DeletedContacts table.
30 */
31public class DeletedContactUtil {
32
33    private static final Uri URI = DeletedContacts.CONTENT_URI;
34
35    public static long queryDeletedTimestampForContactId(ContentResolver resolver, long contactId) {
36        String[] projection = new String[]{
37                DeletedContacts.CONTACT_DELETED_TIMESTAMP
38        };
39        Uri uri = ContentUris.withAppendedId(URI, contactId);
40        Cursor cursor = resolver.query(uri, projection, null, null, null);
41        if (cursor.moveToNext()) {
42            return cursor.getLong(0);
43        }
44        return CommonDatabaseUtils.NOT_FOUND;
45    }
46
47    public static long getCount(ContentResolver resolver) {
48        String[] projection = new String[] {
49                DeletedContacts.CONTACT_ID
50        };
51        Cursor cursor = resolver.query(URI, projection, null, null, null);
52        try {
53            return cursor.getCount();
54        } finally {
55            CommonDatabaseUtils.closeQuietly(cursor);
56        }
57    }
58
59    /**
60     * Queries all records.
61     *
62     * @return A list of records.  Where each record is represented as an array of strings.
63     */
64    public static List<String[]> query(ContentResolver resolver, String[] projection) {
65        Cursor cursor = resolver.query(URI, projection, null, null, null);
66        return CommonDatabaseUtils.multiRecordToArray(cursor);
67    }
68
69    /**
70     * Queries all records after a given timestamp.
71     *
72     * @return A list of records.  Where each record is represented as an array of strings.
73     */
74    public static List<String[]> querySinceTimestamp(ContentResolver resolver, String[] projection,
75            long timestamp) {
76        String selection = DeletedContacts.CONTACT_DELETED_TIMESTAMP + ">?";
77        String[] args = new String[] {timestamp + ""};
78        Cursor cursor = resolver.query(URI, projection, selection, args, null);
79        return CommonDatabaseUtils.multiRecordToArray(cursor);
80    }
81}
82