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 com.android.providers.contacts.database;
18 
19 import android.content.ContentValues;
20 import android.database.sqlite.SQLiteDatabase;
21 import android.provider.ContactsContract;
22 
23 import com.android.providers.contacts.ContactsDatabaseHelper;
24 import com.android.providers.contacts.util.Clock;
25 
26 /**
27  * Methods for operating on the deleted_contacts table.
28  */
29 public class DeletedContactsTableUtil {
30 
31     /**
32      * Create deleted_contacts tables and indexes.
33      *
34      * @param db The sqlite database instance.
35      */
create(SQLiteDatabase db)36     public static void create(SQLiteDatabase db) {
37         // Deleted contacts log
38         db.execSQL("CREATE TABLE " + ContactsDatabaseHelper.Tables.DELETED_CONTACTS + " (" +
39                 ContactsContract.DeletedContacts.CONTACT_ID + " INTEGER PRIMARY KEY," +
40                 ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP +
41                 " INTEGER NOT NULL default 0"
42                 + ");");
43 
44         db.execSQL(MoreDatabaseUtils.buildCreateIndexSql(
45                 ContactsDatabaseHelper.Tables.DELETED_CONTACTS,
46                 ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP));
47     }
48 
49     /**
50      * Inserts a deleted contact log record.
51      *
52      * @param db The SQLiteDatabase instance.
53      * @param contactId The contact id to insert.
54      * @return The row id
55      */
insertDeletedContact(SQLiteDatabase db, long contactId)56     public static long insertDeletedContact(SQLiteDatabase db, long contactId) {
57         ContentValues values = new ContentValues();
58         values.put(ContactsContract.DeletedContacts.CONTACT_ID, contactId);
59         values.put(ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP,
60                 Clock.getInstance().currentTimeMillis());
61         // a.k.a upsert
62         return db.insertWithOnConflict(ContactsDatabaseHelper.Tables.DELETED_CONTACTS, null, values,
63                 SQLiteDatabase.CONFLICT_REPLACE);
64     }
65 
66     /**
67      * Deletes old log records.
68      *
69      * @param db The database instance to use.
70      */
deleteOldLogs(SQLiteDatabase db)71     public static int deleteOldLogs(SQLiteDatabase db) {
72 
73         long time = Clock.getInstance().currentTimeMillis() -
74                 ContactsContract.DeletedContacts.DAYS_KEPT_MILLISECONDS;
75 
76         String[] args = new String[]{time + ""};
77 
78         return db.delete(ContactsDatabaseHelper.Tables.DELETED_CONTACTS,
79                 ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP + " < ?", args);
80     }
81 }
82