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.phonelookup.database;
18 
19 import android.content.Context;
20 import android.database.sqlite.SQLiteDatabase;
21 import android.database.sqlite.SQLiteOpenHelper;
22 import android.os.SystemClock;
23 import com.android.dialer.common.LogUtil;
24 import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
25 import com.android.dialer.inject.ApplicationContext;
26 import com.android.dialer.phonelookup.database.contract.PhoneLookupHistoryContract.PhoneLookupHistory;
27 import com.google.common.util.concurrent.ListenableFuture;
28 import com.google.common.util.concurrent.ListeningExecutorService;
29 import javax.inject.Inject;
30 import javax.inject.Singleton;
31 
32 /** {@link SQLiteOpenHelper} for the PhoneLookupHistory database. */
33 @Singleton
34 public class PhoneLookupHistoryDatabaseHelper extends SQLiteOpenHelper {
35 
36   private static final String FILENAME = "phone_lookup_history.db";
37 
38   private final Context appContext;
39   private final ListeningExecutorService backgroundExecutor;
40 
41   @Inject
PhoneLookupHistoryDatabaseHelper( @pplicationContext Context appContext, @BackgroundExecutor ListeningExecutorService backgroundExecutor)42   PhoneLookupHistoryDatabaseHelper(
43       @ApplicationContext Context appContext,
44       @BackgroundExecutor ListeningExecutorService backgroundExecutor) {
45     super(appContext, FILENAME, null, 1);
46 
47     this.appContext = appContext;
48     this.backgroundExecutor = backgroundExecutor;
49   }
50 
51   // TODO(zachh): LAST_MODIFIED is no longer read and can be deleted.
52   private static final String CREATE_TABLE_SQL =
53       "create table if not exists "
54           + PhoneLookupHistory.TABLE
55           + " ("
56           + (PhoneLookupHistory.NORMALIZED_NUMBER + " text primary key not null, ")
57           + (PhoneLookupHistory.PHONE_LOOKUP_INFO + " blob not null, ")
58           + (PhoneLookupHistory.LAST_MODIFIED + " long not null")
59           + ");";
60 
61   private static final String CREATE_INDEX_ON_LAST_MODIFIED_SQL =
62       "create index last_modified_index on "
63           + PhoneLookupHistory.TABLE
64           + " ("
65           + PhoneLookupHistory.LAST_MODIFIED
66           + ");";
67 
68   @Override
onCreate(SQLiteDatabase db)69   public void onCreate(SQLiteDatabase db) {
70     LogUtil.enterBlock("PhoneLookupHistoryDatabaseHelper.onCreate");
71     long startTime = SystemClock.uptimeMillis();
72     db.execSQL(CREATE_TABLE_SQL);
73     db.execSQL(CREATE_INDEX_ON_LAST_MODIFIED_SQL);
74     // TODO(zachh): Consider logging impression.
75     LogUtil.i(
76         "PhoneLookupHistoryDatabaseHelper.onCreate",
77         "took: %dms",
78         SystemClock.uptimeMillis() - startTime);
79   }
80 
81   @Override
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)82   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
83 
84   /** Closes the database and deletes it. */
delete()85   public ListenableFuture<Void> delete() {
86     return backgroundExecutor.submit(
87         () -> {
88           close();
89           appContext.deleteDatabase(FILENAME);
90           return null;
91         });
92   }
93 }
94