1 /* 2 ** 3 ** Copyright (C) 2014, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 18 package com.android.providers.telephony; 19 20 import android.content.Context; 21 import android.database.sqlite.SQLiteDatabase; 22 import android.database.sqlite.SQLiteOpenHelper; 23 import android.text.TextUtils; 24 import android.util.Log; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 29 public class CarrierDatabaseHelper extends SQLiteOpenHelper { 30 private static final String TAG = "CarrierDatabaseHelper"; 31 private static final boolean DBG = true; 32 33 private static final String DATABASE_NAME = "CarrierInformation.db"; 34 public static final String CARRIER_KEY_TABLE = "carrier_key"; 35 private static final int DATABASE_VERSION = 2; 36 37 /** 38 * CarrierDatabaseHelper carrier database helper class. 39 * @param context of the user. 40 */ CarrierDatabaseHelper(Context context)41 public CarrierDatabaseHelper(Context context) { 42 super(context, DATABASE_NAME, null, DATABASE_VERSION); 43 setWriteAheadLoggingEnabled(false); 44 } 45 46 public static final String KEY_TYPE = "key_type"; 47 public static final String MCC = "mcc"; 48 public static final String MNC = "mnc"; 49 public static final String MVNO_TYPE = "mvno_type"; 50 public static final String MVNO_MATCH_DATA = "mvno_match_data"; 51 public static final String PUBLIC_KEY = "public_key"; 52 public static final String KEY_IDENTIFIER = "key_identifier"; 53 public static final String EXPIRATION_TIME = "expiration_time"; 54 public static final String LAST_MODIFIED = "last_modified"; 55 56 private static final List<String> CARRIERS_UNIQUE_FIELDS = new ArrayList<String>(); 57 58 static { 59 CARRIERS_UNIQUE_FIELDS.add(MCC); 60 CARRIERS_UNIQUE_FIELDS.add(MNC); 61 CARRIERS_UNIQUE_FIELDS.add(KEY_TYPE); 62 CARRIERS_UNIQUE_FIELDS.add(MVNO_TYPE); 63 CARRIERS_UNIQUE_FIELDS.add(MVNO_MATCH_DATA); 64 } 65 getStringForCarrierKeyTableCreation(String tableName)66 public static String getStringForCarrierKeyTableCreation(String tableName) { 67 return "CREATE TABLE " + tableName + 68 "(_id INTEGER PRIMARY KEY," + 69 MCC + " TEXT DEFAULT ''," + 70 MNC + " TEXT DEFAULT ''," + 71 MVNO_TYPE + " TEXT DEFAULT ''," + 72 MVNO_MATCH_DATA + " TEXT DEFAULT ''," + 73 KEY_TYPE + " TEXT DEFAULT ''," + 74 KEY_IDENTIFIER + " TEXT DEFAULT ''," + 75 PUBLIC_KEY + " BLOB DEFAULT ''," + 76 EXPIRATION_TIME + " INTEGER DEFAULT 0," + 77 LAST_MODIFIED + " INTEGER DEFAULT 0," + 78 "UNIQUE (" + TextUtils.join(", ", CARRIERS_UNIQUE_FIELDS) + "));"; 79 } 80 81 @Override onCreate(SQLiteDatabase db)82 public void onCreate(SQLiteDatabase db) { 83 db.execSQL(getStringForCarrierKeyTableCreation(CARRIER_KEY_TABLE)); 84 } 85 createCarrierTable(SQLiteDatabase db)86 public void createCarrierTable(SQLiteDatabase db) { 87 db.execSQL(getStringForCarrierKeyTableCreation(CARRIER_KEY_TABLE)); 88 } 89 dropCarrierTable(SQLiteDatabase db)90 public void dropCarrierTable(SQLiteDatabase db) { 91 db.execSQL("DROP TABLE IF EXISTS " + CARRIER_KEY_TABLE + ";"); 92 } 93 94 @Override onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)95 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 96 Log.d(TAG, "dbh.onUpgrade:+ db=" + db + " oldV=" + oldVersion + " newV=" + newVersion); 97 if (oldVersion < 2) { 98 dropCarrierTable(db); 99 createCarrierTable(db); 100 } 101 } 102 } 103