1/* 2 * Copyright (C) 2016 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 */ 16package com.android.providers.blockednumber; 17 18import android.content.Context; 19import android.database.sqlite.SQLiteDatabase; 20import android.database.sqlite.SQLiteOpenHelper; 21import android.provider.BlockedNumberContract.BlockedNumbers; 22 23import com.android.internal.annotations.VisibleForTesting; 24import com.android.internal.util.Preconditions; 25 26public class BlockedNumberDatabaseHelper { 27 private static final int DATABASE_VERSION = 2; 28 29 private static final String DATABASE_NAME = "blockednumbers.db"; 30 31 private static BlockedNumberDatabaseHelper sInstance; 32 33 private final Context mContext; 34 35 private final OpenHelper mOpenHelper; 36 37 public interface Tables { 38 String BLOCKED_NUMBERS = "blocked"; 39 } 40 41 private static final class OpenHelper extends SQLiteOpenHelper { 42 public OpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, 43 int version) { 44 super(context, name, factory, version); 45 } 46 47 @Override 48 public void onCreate(SQLiteDatabase db) { 49 createTables(db); 50 } 51 52 @Override 53 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 54 if (oldVersion < 2) { 55 db.execSQL("DROP TABLE IF EXISTS blocked"); 56 createTables(db); 57 } 58 } 59 60 private void createTables(SQLiteDatabase db) { 61 db.execSQL("CREATE TABLE " + Tables.BLOCKED_NUMBERS + " (" + 62 BlockedNumbers.COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 63 BlockedNumbers.COLUMN_ORIGINAL_NUMBER + " TEXT NOT NULL UNIQUE," + 64 BlockedNumbers.COLUMN_E164_NUMBER + " TEXT" + 65 ")"); 66 67 db.execSQL("CREATE INDEX blocked_number_idx_original ON " + Tables.BLOCKED_NUMBERS + 68 " (" + BlockedNumbers.COLUMN_ORIGINAL_NUMBER + ");"); 69 db.execSQL("CREATE INDEX blocked_number_idx_e164 ON " + Tables.BLOCKED_NUMBERS + " (" + 70 BlockedNumbers.COLUMN_E164_NUMBER + 71 ");"); 72 } 73 } 74 75 @VisibleForTesting 76 public static BlockedNumberDatabaseHelper newInstanceForTest(Context context) { 77 return new BlockedNumberDatabaseHelper(context, /* instanceIsForTesting =*/ true); 78 } 79 80 private BlockedNumberDatabaseHelper(Context context, boolean instanceIsForTesting) { 81 Preconditions.checkNotNull(context); 82 mContext = context; 83 mOpenHelper = new OpenHelper(mContext, 84 instanceIsForTesting ? null : DATABASE_NAME, null, DATABASE_VERSION); 85 } 86 87 public static synchronized BlockedNumberDatabaseHelper getInstance(Context context) { 88 if (sInstance == null) { 89 sInstance = new BlockedNumberDatabaseHelper( 90 context, 91 /* instanceIsForTesting = */ false); 92 } 93 return sInstance; 94 } 95 96 public SQLiteDatabase getReadableDatabase() { 97 return mOpenHelper.getReadableDatabase(); 98 } 99 100 public SQLiteDatabase getWritableDatabase() { 101 return mOpenHelper.getWritableDatabase(); 102 } 103 104 public void wipeForTest() { 105 getWritableDatabase().execSQL("DELETE FROM " + Tables.BLOCKED_NUMBERS); 106 } 107} 108