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
18package com.android.providers.telephony;
19
20import android.content.Context;
21import android.database.sqlite.SQLiteDatabase;
22import android.database.sqlite.SQLiteOpenHelper;
23import android.text.TextUtils;
24import android.util.Log;
25
26import java.util.ArrayList;
27import java.util.List;
28
29public 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     */
41    public CarrierDatabaseHelper(Context context) {
42        super(context, DATABASE_NAME, null, DATABASE_VERSION);
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 MVNO_TYPE = "mvno_type";
49    public static final String MVNO_MATCH_DATA = "mvno_match_data";
50    public static final String PUBLIC_KEY = "public_key";
51    public static final String KEY_IDENTIFIER = "key_identifier";
52    public static final String EXPIRATION_TIME = "expiration_time";
53    public static final String LAST_MODIFIED = "last_modified";
54
55    private static final List<String> CARRIERS_UNIQUE_FIELDS = new ArrayList<String>();
56
57    static {
58        CARRIERS_UNIQUE_FIELDS.add(MCC);
59        CARRIERS_UNIQUE_FIELDS.add(MNC);
60        CARRIERS_UNIQUE_FIELDS.add(KEY_TYPE);
61        CARRIERS_UNIQUE_FIELDS.add(MVNO_TYPE);
62        CARRIERS_UNIQUE_FIELDS.add(MVNO_MATCH_DATA);
63    }
64
65    public static String getStringForCarrierKeyTableCreation(String tableName) {
66        return "CREATE TABLE " + tableName +
67                "(_id INTEGER PRIMARY KEY," +
68                MCC + " TEXT DEFAULT ''," +
69                MNC + " TEXT DEFAULT ''," +
70                MVNO_TYPE + " TEXT DEFAULT ''," +
71                MVNO_MATCH_DATA + " TEXT DEFAULT ''," +
72                KEY_TYPE + " TEXT DEFAULT ''," +
73                KEY_IDENTIFIER + " TEXT DEFAULT ''," +
74                PUBLIC_KEY + " BLOB DEFAULT ''," +
75                EXPIRATION_TIME + " INTEGER DEFAULT 0," +
76                LAST_MODIFIED + " INTEGER DEFAULT 0," +
77                "UNIQUE (" + TextUtils.join(", ", CARRIERS_UNIQUE_FIELDS) + "));";
78    }
79
80    @Override
81    public void onCreate(SQLiteDatabase db) {
82        db.execSQL(getStringForCarrierKeyTableCreation(CARRIER_KEY_TABLE));
83    }
84
85    public void createCarrierTable(SQLiteDatabase db) {
86        db.execSQL(getStringForCarrierKeyTableCreation(CARRIER_KEY_TABLE));
87    }
88
89    public void dropCarrierTable(SQLiteDatabase db) {
90        db.execSQL("DROP TABLE IF EXISTS " + CARRIER_KEY_TABLE + ";");
91    }
92
93    @Override
94    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
95        Log.d(TAG, "dbh.onUpgrade:+ db=" + db + " oldV=" + oldVersion + " newV=" + newVersion);
96        if (oldVersion < 2) {
97            dropCarrierTable(db);
98            createCarrierTable(db);
99        }
100    }
101}
102