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 android.provider.CallLog.Calls; 23 import com.android.dialer.calllog.database.contract.AnnotatedCallLogContract.AnnotatedCallLog; 24 import com.android.dialer.common.LogUtil; 25 import java.util.Locale; 26 27 /** {@link SQLiteOpenHelper} for the AnnotatedCallLog database. */ 28 class AnnotatedCallLogDatabaseHelper extends SQLiteOpenHelper { 29 private final int maxRows; 30 AnnotatedCallLogDatabaseHelper(Context appContext, int maxRows)31 AnnotatedCallLogDatabaseHelper(Context appContext, int maxRows) { 32 super(appContext, "annotated_call_log.db", null, 1); 33 this.maxRows = maxRows; 34 } 35 36 private static final String CREATE_TABLE_SQL = 37 "create table if not exists " 38 + AnnotatedCallLog.TABLE 39 + " (" 40 + (AnnotatedCallLog._ID + " integer primary key, ") 41 + (AnnotatedCallLog.TIMESTAMP + " integer, ") 42 + (AnnotatedCallLog.NUMBER + " blob, ") 43 + (AnnotatedCallLog.FORMATTED_NUMBER + " text, ") 44 + (AnnotatedCallLog.NUMBER_PRESENTATION + " integer, ") 45 + (AnnotatedCallLog.DURATION + " integer, ") 46 + (AnnotatedCallLog.DATA_USAGE + " integer, ") 47 + (AnnotatedCallLog.IS_READ + " integer, ") 48 + (AnnotatedCallLog.NEW + " integer, ") 49 + (AnnotatedCallLog.GEOCODED_LOCATION + " text, ") 50 + (AnnotatedCallLog.PHONE_ACCOUNT_COMPONENT_NAME + " text, ") 51 + (AnnotatedCallLog.PHONE_ACCOUNT_ID + " text, ") 52 + (AnnotatedCallLog.PHONE_ACCOUNT_LABEL + " text, ") 53 + (AnnotatedCallLog.PHONE_ACCOUNT_COLOR + " integer, ") 54 + (AnnotatedCallLog.FEATURES + " integer, ") 55 + (AnnotatedCallLog.TRANSCRIPTION + " integer, ") 56 + (AnnotatedCallLog.VOICEMAIL_URI + " text, ") 57 + (AnnotatedCallLog.CALL_TYPE + " integer not null, ") 58 + (AnnotatedCallLog.NUMBER_ATTRIBUTES + " blob, ") 59 + (AnnotatedCallLog.TRANSCRIPTION_STATE + " integer") 60 + ");"; 61 62 /** 63 * Deletes all but the first maxRows rows (by timestamp, excluding voicemails) to keep the table a 64 * manageable size. 65 */ 66 private static final String CREATE_TRIGGER_SQL = 67 "create trigger delete_old_rows after insert on " 68 + AnnotatedCallLog.TABLE 69 + " when (select count(*) from " 70 + AnnotatedCallLog.TABLE 71 + " where " 72 + AnnotatedCallLog.CALL_TYPE 73 + " != " 74 + Calls.VOICEMAIL_TYPE 75 + ") > %d" 76 + " begin delete from " 77 + AnnotatedCallLog.TABLE 78 + " where " 79 + AnnotatedCallLog._ID 80 + " in (select " 81 + AnnotatedCallLog._ID 82 + " from " 83 + AnnotatedCallLog.TABLE 84 + " where " 85 + AnnotatedCallLog.CALL_TYPE 86 + " != " 87 + Calls.VOICEMAIL_TYPE 88 + " order by timestamp limit (select count(*)-%d" 89 + " from " 90 + AnnotatedCallLog.TABLE 91 + " where " 92 + AnnotatedCallLog.CALL_TYPE 93 + " != " 94 + Calls.VOICEMAIL_TYPE 95 + ")); end;"; 96 97 private static final String CREATE_INDEX_ON_CALL_TYPE_SQL = 98 "create index call_type_index on " 99 + AnnotatedCallLog.TABLE 100 + " (" 101 + AnnotatedCallLog.CALL_TYPE 102 + ");"; 103 104 private static final String CREATE_INDEX_ON_NUMBER_SQL = 105 "create index number_index on " 106 + AnnotatedCallLog.TABLE 107 + " (" 108 + AnnotatedCallLog.NUMBER 109 + ");"; 110 111 @Override onCreate(SQLiteDatabase db)112 public void onCreate(SQLiteDatabase db) { 113 LogUtil.enterBlock("AnnotatedCallLogDatabaseHelper.onCreate"); 114 long startTime = System.currentTimeMillis(); 115 db.execSQL(CREATE_TABLE_SQL); 116 db.execSQL(String.format(Locale.US, CREATE_TRIGGER_SQL, maxRows, maxRows)); 117 db.execSQL(CREATE_INDEX_ON_CALL_TYPE_SQL); 118 db.execSQL(CREATE_INDEX_ON_NUMBER_SQL); 119 // TODO(zachh): Consider logging impression. 120 LogUtil.i( 121 "AnnotatedCallLogDatabaseHelper.onCreate", 122 "took: %dms", 123 System.currentTimeMillis() - startTime); 124 } 125 126 @Override onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)127 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} 128 } 129