1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Copyright (C) 2013 The Android Open Source Project
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Licensed under the Apache License, Version 2.0 (the "License");
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * you may not use this file except in compliance with the License.
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * You may obtain a copy of the License at
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *      http://www.apache.org/licenses/LICENSE-2.0
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Unless required by applicable law or agreed to in writing, software
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * distributed under the License is distributed on an "AS IS" BASIS,
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * See the License for the specific language governing permissions and
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * limitations under the License
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compackage com.android.providers.contacts.database;
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comimport android.content.ContentValues;
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comimport android.database.sqlite.SQLiteDatabase;
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comimport android.provider.ContactsContract;
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comimport com.android.providers.contacts.ContactsDatabaseHelper;
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comimport com.android.providers.contacts.util.Clock;
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/**
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Methods for operating on the deleted_contacts table.
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic class DeletedContactsTableUtil {
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
31d0306a15938a971e10dd8648d3e17b001c4b0014commit-bot@chromium.org    /**
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * Create deleted_contacts tables and indexes.
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     *
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param db The sqlite database instance.
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public static void create(SQLiteDatabase db) {
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // Deleted contacts log
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        db.execSQL("CREATE TABLE " + ContactsDatabaseHelper.Tables.DELETED_CONTACTS + " (" +
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                ContactsContract.DeletedContacts.CONTACT_ID + " INTEGER PRIMARY KEY," +
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP +
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                " INTEGER NOT NULL default 0"
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                + ");");
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        db.execSQL(MoreDatabaseUtils.buildCreateIndexSql(
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                ContactsDatabaseHelper.Tables.DELETED_CONTACTS,
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP));
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /**
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * Inserts a deleted contact log record.
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     *
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param db The SQLiteDatabase instance.
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param contactId The contact id to insert.
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @return The row id
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public static long insertDeletedContact(SQLiteDatabase db, long contactId) {
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        ContentValues values = new ContentValues();
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        values.put(ContactsContract.DeletedContacts.CONTACT_ID, contactId);
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        values.put(ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP,
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                Clock.getInstance().currentTimeMillis());
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // a.k.a upsert
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return db.insertWithOnConflict(ContactsDatabaseHelper.Tables.DELETED_CONTACTS, null, values,
63e61a86cfa00ea393ecc4a71fca94e1d476a37ecccommit-bot@chromium.org                SQLiteDatabase.CONFLICT_REPLACE);
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
66    /**
67     * Deletes old log records.
68     *
69     * @param db The database instance to use.
70     */
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