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