diff options
author | Danny Baumann <dannybaumann@web.de> | 2016-11-01 09:59:25 +0100 |
---|---|---|
committer | Michael Bestas <mikeioannina@gmail.com> | 2016-12-30 22:25:43 +0200 |
commit | 8718d9bc91d15d6191830d770710fca2c5f751aa (patch) | |
tree | 1b57db0b301f2e5324b748aff360d3b02ca1a7bf | |
parent | 4c61dd60fe79cbfbf6fc7a1e62e0197aed481a3d (diff) | |
download | android_packages_providers_TelephonyProvider-8718d9bc91d15d6191830d770710fca2c5f751aa.tar.gz android_packages_providers_TelephonyProvider-8718d9bc91d15d6191830d770710fca2c5f751aa.tar.bz2 android_packages_providers_TelephonyProvider-8718d9bc91d15d6191830d770710fca2c5f751aa.zip |
Add DB upgrade path for SMS/MMS and telephony provider.
Change-Id: I47ed09b0df9ade4bc50cebd8d0ea8cd229a1221d
-rw-r--r-- | src/com/android/providers/telephony/MmsSmsDatabaseHelper.java | 132 | ||||
-rw-r--r-- | src/com/android/providers/telephony/TelephonyProvider.java | 259 |
2 files changed, 318 insertions, 73 deletions
diff --git a/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java b/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java index 7cb8b74..a6c5c36 100644 --- a/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java +++ b/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java @@ -189,7 +189,7 @@ public class MmsSmsDatabaseHelper extends SQLiteOpenHelper { private static final String NO_SUCH_TABLE_EXCEPTION_MESSAGE = "no such table"; static final String DATABASE_NAME = "mmssms.db"; - static final int DATABASE_VERSION = 64; + static final int DATABASE_VERSION = 67; private final Context mContext; private LowStorageMonitor mLowStorageMonitor; @@ -1413,13 +1413,16 @@ public class MmsSmsDatabaseHelper extends SQLiteOpenHelper { } // fall through case 60: - if (currentVersion <= 60) { + // Priority column is now part of onOpen() + // fall through + case 61: + if (currentVersion <= 61) { return; } db.beginTransaction(); try { - upgradeDatabaseToVersion61(db); + upgradeDatabaseToVersion62(db); db.setTransactionSuccessful(); } catch (Throwable ex) { Log.e(TAG, ex.getMessage(), ex); @@ -1428,14 +1431,17 @@ public class MmsSmsDatabaseHelper extends SQLiteOpenHelper { db.endTransaction(); } // fall through - case 61: - if (currentVersion <= 61) { + case 62: + // Priority column is now part of onOpen() + // fall through + case 63: + if (currentVersion <= 63) { return; } db.beginTransaction(); try { - upgradeDatabaseToVersion62(db); + upgradeDatabaseToVersion64(db); db.setTransactionSuccessful(); } catch (Throwable ex) { Log.e(TAG, ex.getMessage(), ex); @@ -1444,14 +1450,20 @@ public class MmsSmsDatabaseHelper extends SQLiteOpenHelper { db.endTransaction(); } // fall through - case 62: - if (currentVersion <= 62) { + case 64: + if (currentVersion <= 64) { + return; + } + // Unread count as a feature is dropped + // fall through + case 65: + if (currentVersion <= 65) { return; } db.beginTransaction(); try { - upgradeDatabaseToVersion63(db); + upgradeDatabaseToVersion66(db); db.setTransactionSuccessful(); } catch (Throwable ex) { Log.e(TAG, ex.getMessage(), ex); @@ -1459,15 +1471,17 @@ public class MmsSmsDatabaseHelper extends SQLiteOpenHelper { } finally { db.endTransaction(); } - // fall through - case 63: - if (currentVersion <= 63) { + //fall through + case 66: + if (currentVersion <= 66) { return; } db.beginTransaction(); try { - upgradeDatabaseToVersion64(db); + upgradeDatabaseToAOSPVersion62(db); + upgradeDatabaseToAOSPVersion63(db); + upgradeDatabaseToAOSPVersion64(db); db.setTransactionSuccessful(); } catch (Throwable ex) { Log.e(TAG, ex.getMessage(), ex); @@ -1703,7 +1717,7 @@ public class MmsSmsDatabaseHelper extends SQLiteOpenHelper { + Threads.ARCHIVED + " INTEGER DEFAULT 0"); } - private void upgradeDatabaseToVersion61(SQLiteDatabase db) { + private void upgradeDatabaseToVersion66(SQLiteDatabase db) { db.execSQL("CREATE VIEW " + SmsProvider.VIEW_SMS_RESTRICTED + " AS " + "SELECT * FROM " + SmsProvider.TABLE_SMS + " WHERE " + Sms.TYPE + "=" + Sms.MESSAGE_TYPE_INBOX + @@ -1719,7 +1733,93 @@ public class MmsSmsDatabaseHelper extends SQLiteOpenHelper { } + // Determine whether this database has CM11 columns... + private boolean isCM11DB(SQLiteDatabase db) { + Cursor c = null; + try { + final String query = "SELECT sub_id, pri FROM sms"; + c = db.rawQuery(query, null); + } catch (Exception e) { + } finally { + if (c != null) { + c.close(); + } + } + return c != null; + } + private void upgradeDatabaseToVersion62(SQLiteDatabase db) { + if (isCM11DB(db)) { + // CM11 was 60, which means we skipped a few updates... + try { + upgradeDatabaseToVersion58(db); + } catch (Exception e) { } + try { + upgradeDatabaseToVersion59(db); + } catch (Exception e) { } + try { + upgradeDatabaseToVersion60(db); + } catch (Exception e) { } + } + } + + private void upgradeDatabaseToVersion64(SQLiteDatabase db) { + try { + db.execSQL("ALTER TABLE " + MmsProvider.TABLE_PDU +" ADD COLUMN " + + Mms.SUBSCRIPTION_ID + " INTEGER DEFAULT -1"); + } catch (SQLiteException e) { + // ignore (DB was older than version 58), we'll remove the data later + } + db.execSQL("ALTER TABLE " + MmsSmsProvider.TABLE_PENDING_MSG +" ADD COLUMN " + + "pending_sub_id" + " INTEGER DEFAULT 0"); + try { + db.execSQL("ALTER TABLE " + SmsProvider.TABLE_SMS +" ADD COLUMN " + + Sms.SUBSCRIPTION_ID + " INTEGER DEFAULT -1"); + } catch (SQLiteException e) { + // see above + } + try { + db.execSQL("ALTER TABLE " + SmsProvider.TABLE_RAW +" ADD COLUMN " + + Sms.SUBSCRIPTION_ID + " INTEGER DEFAULT -1"); + } catch (SQLiteException e) { + // see above + } + // remove old data from the sub_id column (if present), it was already copied + // over to the phone_id column in upgradeDatabaseToVersion59 + db.execSQL("UPDATE " + MmsProvider.TABLE_PDU + " SET " + Mms.SUBSCRIPTION_ID + " = -1"); + db.execSQL("UPDATE " + SmsProvider.TABLE_SMS + " SET " + Sms.SUBSCRIPTION_ID + " = -1"); + db.execSQL("UPDATE " + SmsProvider.TABLE_RAW + " SET " + Sms.SUBSCRIPTION_ID + " = -1"); + } + + // Try to copy data from existing src column to new column which supposed + // to be added before calling this functin. + // If src or dest column not exsit, it will just bail out. + private void tryCopyColumn(SQLiteDatabase db, String table, + String srcColumn, String destColumn) { + if (isColumnExist(db, table, srcColumn) && isColumnExist(db, table, destColumn) ) { + db.execSQL("update " + table + " set " + destColumn + " = " + + srcColumn); + } + } + + private boolean isColumnExist(SQLiteDatabase db, String table, String column) { + Cursor cursor = db.query(table, null, null, null, null, null, null); + if (cursor != null) { + try { + cursor.getColumnIndexOrThrow(column); + return true; + } catch (IllegalArgumentException e) { + Log.e(TAG, "isColumnExsit: " + "table: " + table + " column: " + + column + " IllegalArgumentException", e); + return false; + } finally { + cursor.close(); + } + } + return false; + } + + private void upgradeDatabaseToAOSPVersion62(SQLiteDatabase db) { // When a non-FBE device is upgraded to N, all MMS attachment files are moved from // /data/data to /data/user_de. We need to update the paths stored in the parts table to // reflect this change. @@ -1749,11 +1849,11 @@ public class MmsSmsDatabaseHelper extends SQLiteOpenHelper { " WHERE INSTR(" + Part._DATA + ", '" + partsDirName + "') > 0"); } - private void upgradeDatabaseToVersion63(SQLiteDatabase db) { + private void upgradeDatabaseToAOSPVersion63(SQLiteDatabase db) { db.execSQL("ALTER TABLE " + SmsProvider.TABLE_RAW +" ADD COLUMN deleted INTEGER DEFAULT 0"); } - private void upgradeDatabaseToVersion64(SQLiteDatabase db) { + private void upgradeDatabaseToAOSPVersion64(SQLiteDatabase db) { db.execSQL("ALTER TABLE " + SmsProvider.TABLE_RAW +" ADD COLUMN message_body TEXT"); } private void checkAndUpdateSmsTable(SQLiteDatabase db) { diff --git a/src/com/android/providers/telephony/TelephonyProvider.java b/src/com/android/providers/telephony/TelephonyProvider.java index e1201af..d32b34b 100644 --- a/src/com/android/providers/telephony/TelephonyProvider.java +++ b/src/com/android/providers/telephony/TelephonyProvider.java @@ -46,6 +46,7 @@ import android.text.TextUtils; import android.util.Log; import android.util.Xml; +import com.android.internal.telephony.RILConstants; import com.android.internal.util.XmlUtils; import org.xmlpull.v1.XmlPullParser; @@ -68,7 +69,7 @@ public class TelephonyProvider extends ContentProvider private static final boolean DBG = true; private static final boolean VDBG = false; // STOPSHIP if true - private static final int DATABASE_VERSION = 18 << 16; + private static final int DATABASE_VERSION = 23 << 16; private static final int URL_UNKNOWN = 0; private static final int URL_TELEPHONY = 1; private static final int URL_CURRENT = 2; @@ -556,63 +557,141 @@ public class TelephonyProvider extends ContentProvider oldVersion = 8 << 16 | 6; } if (oldVersion < (9 << 16 | 6)) { - db.execSQL("ALTER TABLE " + CARRIERS_TABLE + - " ADD COLUMN sub_id INTEGER DEFAULT " + - SubscriptionManager.INVALID_SUBSCRIPTION_ID + ";"); + upgradeForSubIdIfNecessary(db); oldVersion = 9 << 16 | 6; } if (oldVersion < (10 << 16 | 6)) { - db.execSQL("ALTER TABLE " + CARRIERS_TABLE + - " ADD COLUMN profile_id INTEGER DEFAULT 0;"); - db.execSQL("ALTER TABLE " + CARRIERS_TABLE + - " ADD COLUMN modem_cognitive BOOLEAN DEFAULT 0;"); - db.execSQL("ALTER TABLE " + CARRIERS_TABLE + - " ADD COLUMN max_conns INTEGER DEFAULT 0;"); - db.execSQL("ALTER TABLE " + CARRIERS_TABLE + - " ADD COLUMN wait_time INTEGER DEFAULT 0;"); - db.execSQL("ALTER TABLE " + CARRIERS_TABLE + - " ADD COLUMN max_conns_time INTEGER DEFAULT 0;"); + upgradeForProfileIdIfNecessary(db); oldVersion = 10 << 16 | 6; } if (oldVersion < (11 << 16 | 6)) { - db.execSQL("ALTER TABLE " + CARRIERS_TABLE + - " ADD COLUMN mtu INTEGER DEFAULT 0;"); + upgradeForMtuIfNecessary(db); oldVersion = 11 << 16 | 6; } if (oldVersion < (12 << 16 | 6)) { + upgradeForSimInfoIfNecessary(db); + oldVersion = 12 << 16 | 6; + } + if (oldVersion < (13 << 16 | 6)) { + upgradeForSubscriptionInfoIfNecessary(db); + oldVersion = 13 << 16 | 6; + } + if (oldVersion < (16 << 16 | 6)) { + // Handle migration from current AOSP (by artificially bumping the version + // and handle migration from cm-11.0, by checking for an existing column + // and then forcing the introduction of new columns + Cursor c = null; + + try { + c = db.rawQuery("SELECT preferred from " + CARRIERS_TABLE, null); + // if this column exists, then we need to add all the columns previously + // added by AOSP since version (9 << 16 | 6) + if (c != null) { + if (c.moveToNext()) { + upgradeForSubIdIfNecessary(db); + upgradeForProfileIdIfNecessary(db); + upgradeForMtuIfNecessary(db); + upgradeForSimInfoIfNecessary(db); + upgradeForSubscriptionInfoIfNecessary(db); + } + } + } catch (SQLException e) { + if (DBG) { + log("onUpgrade skipping " + CARRIERS_TABLE + " upgrade. " + + " The table will get created in onOpen."); + } + } + + oldVersion = (16 << 16 | 6); + } + //CM Switched from Version13 to Version17/18 + if (oldVersion < (17 << 16 | 6)) { + try { + upgradeForProfileIdIfNecessary(db); + } catch (SQLiteException e) { + if (DBG) { + log("onUpgrade " + CARRIERS_TABLE + ": profile_id already present."); + } + } + + try { + db.execSQL("ALTER TABLE " + CARRIERS_TABLE + + " ADD COLUMN mtu INTEGER DEFAULT 0;"); + } catch (SQLiteException e) { + if (DBG) { + log("onUpgrade " + CARRIERS_TABLE + ": mtu already present."); + } + } + + try { + // Add ppp_number field if it's missing + db.execSQL("ALTER TABLE " + CARRIERS_TABLE + + " ADD COLUMN ppp_number TEXT DEFAULT '';"); + } catch (SQLiteException e) { + if (DBG) { + log("onUpgrade " + CARRIERS_TABLE + ": ppp_number already present."); + } + } + + try { + // Add localized_name field if it's missing + db.execSQL("ALTER TABLE " + CARRIERS_TABLE + + " ADD COLUMN localized_name TEXT DEFAULT '';"); + } catch (SQLiteException e) { + if (DBG) { + log("onUpgrade " + CARRIERS_TABLE + ": localized_name already present."); + } + } + + try { + // Add visit_area field if it's missing + db.execSQL("ALTER TABLE " + CARRIERS_TABLE + + " ADD COLUMN visit_area TEXT DEFAULT '';"); + } catch (SQLiteException e) { + if (DBG) { + log("onUpgrade " + CARRIERS_TABLE + ": visit_area already present."); + } + } + try { // Try to update the siminfo table. It might not be there. db.execSQL("ALTER TABLE " + SIMINFO_TABLE + - " ADD COLUMN " + SubscriptionManager.MCC + " INTEGER DEFAULT 0;"); - db.execSQL("ALTER TABLE " + SIMINFO_TABLE + - " ADD COLUMN " + SubscriptionManager.MNC + " INTEGER DEFAULT 0;"); + " ADD COLUMN " + SubscriptionManager.CARRIER_NAME + " TEXT DEFAULT '';"); } catch (SQLiteException e) { if (DBG) { log("onUpgrade skipping " + SIMINFO_TABLE + " upgrade. " + " The table will get created in onOpen."); } } - oldVersion = 12 << 16 | 6; + + try { + // read_only was present in CM11, but not in CM12. Add it if it's missing. + db.execSQL("ALTER TABLE " + CARRIERS_TABLE + + " ADD COLUMN read_only BOOLEAN DEFAULT 0;"); + } catch (SQLiteException e) { + if (DBG) { + log("onUpgrade " + CARRIERS_TABLE + ": read_only already present."); + } + } + oldVersion = 17 << 16 | 6; } - if (oldVersion < (13 << 16 | 6)) { + if (oldVersion < (18 << 16 | 6)) { try { // Try to update the siminfo table. It might not be there. - db.execSQL("ALTER TABLE " + SIMINFO_TABLE + " ADD COLUMN " + - SubscriptionManager.CARRIER_NAME + " TEXT DEFAULT '';"); + db.execSQL("ALTER TABLE " + SIMINFO_TABLE + + " ADD COLUMN " + SubscriptionManager.USER_NETWORK_MODE + + " INTEGER DEFAULT " + RILConstants.PREFERRED_NETWORK_MODE + ";"); } catch (SQLiteException e) { if (DBG) { log("onUpgrade skipping " + SIMINFO_TABLE + " upgrade. " + " The table will get created in onOpen."); } } - oldVersion = 13 << 16 | 6; - } - if (oldVersion < (14 << 16 | 6)) { - // Do nothing. This is to avoid recreating table twice. Table is anyway recreated - // for next version and that takes care of updates for this version as well. - // This version added a new column user_edited to carriers db. + oldVersion = 18 << 16 | 6; } - if (oldVersion < (15 << 16 | 6)) { + //CM Switched from Version13 to Version17/18, comments below reflect AOSP. In case + //of CM we will be upgrading from version 18, but the logic applies. + if (oldVersion < (21 << 16 | 6)) { // Most devices should be upgrading from version 13. On upgrade new db will be // populated from the xml included in OTA but user and carrier edited/added entries // need to be preserved. This new version also adds new columns EDITED and @@ -673,9 +752,31 @@ public class TelephonyProvider extends ContentProvider c.close(); } - oldVersion = 15 << 16 | 6; + oldVersion = 21 << 16 | 6; } - if (oldVersion < (16 << 16 | 6)) { + if (oldVersion < (22 << 16 | 6)) { + Cursor c = null; + try { + c = db.query(CARRIERS_TABLE, null, null, null, null, null, null, + String.valueOf(1)); + if (c == null || c.getColumnIndex(USER_VISIBLE) == -1) { + db.execSQL("ALTER TABLE " + CARRIERS_TABLE + " ADD COLUMN " + + USER_VISIBLE + " BOOLEAN DEFAULT 1;"); + } else { + if (DBG) { + log("onUpgrade skipping " + CARRIERS_TABLE + " upgrade. Column " + + USER_VISIBLE + " already exists."); + } + } + } finally { + if (c != null) { + c.close(); + } + } + oldVersion = 22 << 16 | 6; + } + if (oldVersion < (23 << 16 | 6)) { + // Upgrade for AOSP version 16 try { // Try to update the siminfo table. It might not be there. // These columns may already be present in which case execSQL will throw an @@ -710,30 +811,8 @@ public class TelephonyProvider extends ContentProvider " The table will get created in onOpen."); } } - oldVersion = 16 << 16 | 6; - } - if (oldVersion < (17 << 16 | 6)) { - Cursor c = null; - try { - c = db.query(CARRIERS_TABLE, null, null, null, null, null, null, - String.valueOf(1)); - if (c == null || c.getColumnIndex(USER_VISIBLE) == -1) { - db.execSQL("ALTER TABLE " + CARRIERS_TABLE + " ADD COLUMN " + - USER_VISIBLE + " BOOLEAN DEFAULT 1;"); - } else { - if (DBG) { - log("onUpgrade skipping " + CARRIERS_TABLE + " upgrade. Column " + - USER_VISIBLE + " already exists."); - } - } - } finally { - if (c != null) { - c.close(); - } - } - oldVersion = 17 << 16 | 6; - } - if (oldVersion < (18 << 16 | 6)) { + + // AOSP version 18 try { // Try to update the siminfo table. It might not be there. db.execSQL("ALTER TABLE " + SIMINFO_TABLE + " ADD COLUMN " + @@ -745,7 +824,20 @@ public class TelephonyProvider extends ContentProvider " The table will get created in onOpen."); } } - oldVersion = 18 << 16 | 6; + + // Convert old (CAF) sub_state to new (AOSP) sim_provisioning_status flag + try { + db.execSQL("UPDATE " + SIMINFO_TABLE + " SET " + + SubscriptionManager.SIM_PROVISIONING_STATUS + + " = sub_state - 1;"); + db.execSQL("ALTER TABLE " + SIMINFO_TABLE + " DROP COLUMN sub_state;"); + } catch (SQLiteException e) { + if (DBG) { + log("onUpgrade skipping subscription state upgrade. "); + } + } + + oldVersion = 23 << 16 | 6; } if (DBG) { log("dbh.onUpgrade:- db=" + db + " oldV=" + oldVersion + " newV=" + newVersion); @@ -1050,6 +1142,59 @@ public class TelephonyProvider extends ContentProvider } } + private void upgradeForSubIdIfNecessary(SQLiteDatabase db) { + db.execSQL("ALTER TABLE " + CARRIERS_TABLE + + " ADD COLUMN sub_id INTEGER DEFAULT " + SubscriptionManager.INVALID_SUBSCRIPTION_ID + ";"); + } + + private void upgradeForProfileIdIfNecessary(SQLiteDatabase db) { + db.execSQL("ALTER TABLE " + CARRIERS_TABLE + + " ADD COLUMN profile_id INTEGER DEFAULT 0;"); + db.execSQL("ALTER TABLE " + CARRIERS_TABLE + + " ADD COLUMN modem_cognitive BOOLEAN DEFAULT 0;"); + db.execSQL("ALTER TABLE " + CARRIERS_TABLE + + " ADD COLUMN max_conns INTEGER DEFAULT 0;"); + db.execSQL("ALTER TABLE " + CARRIERS_TABLE + + " ADD COLUMN wait_time INTEGER DEFAULT 0;"); + db.execSQL("ALTER TABLE " + CARRIERS_TABLE + + " ADD COLUMN max_conns_time INTEGER DEFAULT 0;"); + } + + private void upgradeForMtuIfNecessary(SQLiteDatabase db) { + db.execSQL("ALTER TABLE " + CARRIERS_TABLE + + " ADD COLUMN mtu INTEGER DEFAULT 0;"); + } + + private void upgradeForSimInfoIfNecessary(SQLiteDatabase db) { + try { + // Try to update the siminfo table. It might not be there. + db.execSQL("ALTER TABLE " + SIMINFO_TABLE + + " ADD COLUMN " + SubscriptionManager.MCC + " INTEGER DEFAULT 0;"); + db.execSQL("ALTER TABLE " + SIMINFO_TABLE + + " ADD COLUMN " + SubscriptionManager.MNC + " INTEGER DEFAULT 0;"); + } catch (SQLiteException e) { + if (DBG) { + log("onUpgrade skipping " + SIMINFO_TABLE + " upgrade. " + + " The table will get created in onOpen."); + } + } + } + + private void upgradeForSubscriptionInfoIfNecessary(SQLiteDatabase db) { + try { + // Try to update the siminfo table. It might not be there. + db.execSQL("ALTER TABLE " + SIMINFO_TABLE + + " ADD COLUMN sub_state INTEGER DEFAULT 1;"); + db.execSQL("ALTER TABLE " + SIMINFO_TABLE + + " ADD COLUMN network_mode INTEGER DEFAULT -1;"); + } catch (SQLiteException e) { + if (DBG) { + log("onUpgrade skipping " + SIMINFO_TABLE + " upgrade. " + + " The table will get created in onOpen."); + } + } + } + private void getStringValueFromCursor(ContentValues cv, Cursor c, String key) { String fromCursor = c.getString(c.getColumnIndex(key)); if (!TextUtils.isEmpty(fromCursor)) { |