• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 String DATABASE_NAME = "CarrierInformation.db";
32     public static final String CARRIER_KEY_TABLE = "carrier_key";
33     private static final int DATABASE_VERSION = 3;
34 
35     /**
36      * CarrierDatabaseHelper carrier database helper class.
37      *
38      * @param context of the user.
39      */
CarrierDatabaseHelper(Context context)40     public CarrierDatabaseHelper(Context context) {
41         super(context, DATABASE_NAME, null, DATABASE_VERSION);
42         setWriteAheadLoggingEnabled(false);
43     }
44 
45     public static final String KEY_TYPE = "key_type";
46     public static final String MCC = "mcc";
47     public static final String MNC = "mnc";
48     public static final String CARRIER_ID = "carrier_id";
49     public static final String PUBLIC_KEY = "public_key";
50     public static final String KEY_IDENTIFIER = "key_identifier";
51     public static final String EXPIRATION_TIME = "expiration_time";
52     public static final String LAST_MODIFIED = "last_modified";
53 
54     private static final List<String> CARRIERS_UNIQUE_FIELDS = new ArrayList<String>();
55     private static final String TEMPORARY_CARRIER_KEY_TABLE = CARRIER_KEY_TABLE + "_temp";
56 
57     static {
58         CARRIERS_UNIQUE_FIELDS.add(MCC);
59         CARRIERS_UNIQUE_FIELDS.add(MNC);
60         CARRIERS_UNIQUE_FIELDS.add(CARRIER_ID);
61         CARRIERS_UNIQUE_FIELDS.add(KEY_TYPE);
62     }
63 
getStringForCarrierKeyTableCreation(String tableName)64     public static String getStringForCarrierKeyTableCreation(String tableName) {
65         return "CREATE TABLE " + tableName +
66                 "(_id INTEGER PRIMARY KEY," +
67                 MCC + " TEXT DEFAULT ''," +
68                 MNC + " TEXT DEFAULT ''," +
69                 CARRIER_ID + " INTEGER DEFAULT -1," +
70                 KEY_TYPE + " TEXT DEFAULT ''," +
71                 KEY_IDENTIFIER + " TEXT DEFAULT ''," +
72                 PUBLIC_KEY + " BLOB DEFAULT ''," +
73                 EXPIRATION_TIME + " INTEGER DEFAULT 0," +
74                 LAST_MODIFIED + " INTEGER DEFAULT 0," +
75                 "UNIQUE (" + TextUtils.join(", ", CARRIERS_UNIQUE_FIELDS) + "));";
76     }
77 
78     @Override
onCreate(SQLiteDatabase db)79     public void onCreate(SQLiteDatabase db) {
80         db.execSQL(getStringForCarrierKeyTableCreation(CARRIER_KEY_TABLE));
81     }
82 
createCarrierTable(SQLiteDatabase db)83     public void createCarrierTable(SQLiteDatabase db) {
84         db.execSQL(getStringForCarrierKeyTableCreation(CARRIER_KEY_TABLE));
85     }
86 
dropCarrierTable(SQLiteDatabase db)87     public void dropCarrierTable(SQLiteDatabase db) {
88         db.execSQL("DROP TABLE IF EXISTS " + CARRIER_KEY_TABLE + ";");
89     }
90 
91     @Override
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)92     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
93         Log.d(TAG, "dbh.onUpgrade:db=" + db + " oldV=" + oldVersion + " newV=" + newVersion);
94         if (oldVersion < 2) {
95             dropCarrierTable(db);
96             createCarrierTable(db);
97             return;
98         }
99         if (oldVersion < 3) {
100             // Create new table and copy the contents from the existing table
101             renameCarrierTable(db);
102             createCarrierTable(db);
103             copyContents(db);
104             dropTemporaryTable(db);
105             db.execSQL("COMMIT");
106             return;
107         }
108     }
109 
110     // Renames the existing table as temporary table
renameCarrierTable(SQLiteDatabase db)111     private void renameCarrierTable(SQLiteDatabase db) {
112         db.execSQL(
113                 "ALTER TABLE " + CARRIER_KEY_TABLE + " RENAME TO " + TEMPORARY_CARRIER_KEY_TABLE);
114     }
115 
116     // Copies the content from temporary table to new table
copyContents(SQLiteDatabase db)117     private void copyContents(SQLiteDatabase db) {
118         String copyStr = new StringBuilder().append("INSERT INTO ").append(
119                 CARRIER_KEY_TABLE).append(
120                 " (MCC, MNC, KEY_TYPE, KEY_IDENTIFIER, PUBLIC_KEY, "
121                         + "EXPIRATION_TIME, LAST_MODIFIED)").append(" SELECT ").append(
122                 "MCC, MNC, KEY_TYPE, KEY_IDENTIFIER, PUBLIC_KEY, "
123                         + "EXPIRATION_TIME, LAST_MODIFIED").append(" FROM ").append(
124                 TEMPORARY_CARRIER_KEY_TABLE).toString();
125         db.execSQL(copyStr);
126     }
127 
128     // Drops the temporary table
dropTemporaryTable(SQLiteDatabase db)129     private void dropTemporaryTable(SQLiteDatabase db) {
130         db.execSQL("DROP TABLE IF EXISTS " + TEMPORARY_CARRIER_KEY_TABLE + ";");
131     }
132 }