Home | History | Annotate | Download | only in database
      1 /*
      2  * Copyright (C) 2017 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.dialer.calllog.database;
     18 
     19 import android.content.Context;
     20 import android.database.sqlite.SQLiteDatabase;
     21 import android.database.sqlite.SQLiteOpenHelper;
     22 import com.android.dialer.calllog.database.contract.AnnotatedCallLogContract.AnnotatedCallLog;
     23 import com.android.dialer.common.LogUtil;
     24 import java.util.Locale;
     25 
     26 /** {@link SQLiteOpenHelper} for the AnnotatedCallLog database. */
     27 class AnnotatedCallLogDatabaseHelper extends SQLiteOpenHelper {
     28   private final int maxRows;
     29 
     30   AnnotatedCallLogDatabaseHelper(Context appContext, int maxRows) {
     31     super(appContext, "annotated_call_log.db", null, 1);
     32     this.maxRows = maxRows;
     33   }
     34 
     35   private static final String CREATE_TABLE_SQL =
     36       new StringBuilder()
     37           .append("create table if not exists " + AnnotatedCallLog.TABLE + " (")
     38           .append(AnnotatedCallLog._ID + " integer primary key, ")
     39           .append(AnnotatedCallLog.TIMESTAMP + " integer, ")
     40           .append(AnnotatedCallLog.CONTACT_NAME + " string, ")
     41           .append(AnnotatedCallLog.NUMBER + " blob")
     42           .append(");")
     43           .toString();
     44 
     45   /** Deletes all but the first maxRows rows (by timestamp) to keep the table a manageable size. */
     46   private static final String CREATE_TRIGGER_SQL =
     47       "create trigger delete_old_rows after insert on "
     48           + AnnotatedCallLog.TABLE
     49           + " when (select count(*) from "
     50           + AnnotatedCallLog.TABLE
     51           + ") > %d"
     52           + " begin delete from "
     53           + AnnotatedCallLog.TABLE
     54           + " where "
     55           + AnnotatedCallLog._ID
     56           + " in (select "
     57           + AnnotatedCallLog._ID
     58           + " from "
     59           + AnnotatedCallLog.TABLE
     60           + " order by timestamp limit (select count(*)-%d"
     61           + " from "
     62           + AnnotatedCallLog.TABLE
     63           + " )); end;";
     64 
     65   @Override
     66   public void onCreate(SQLiteDatabase db) {
     67     LogUtil.enterBlock("AnnotatedCallLogDatabaseHelper.onCreate");
     68     long startTime = System.currentTimeMillis();
     69     db.execSQL(CREATE_TABLE_SQL);
     70     db.execSQL(String.format(Locale.US, CREATE_TRIGGER_SQL, maxRows, maxRows));
     71     // TODO: Consider logging impression.
     72     LogUtil.i(
     73         "AnnotatedCallLogDatabaseHelper.onCreate",
     74         "took: %dms",
     75         System.currentTimeMillis() - startTime);
     76   }
     77 
     78   @Override
     79   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
     80 }
     81