TelephonyProvider: add upgrade support from cm-14.1

* only support upgrades from the latest 14.1 db schema

Change-Id: I2bd6b121b098f1074b77b7f5c5e811775738502a
diff --git a/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java b/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java
index 4b84fe5..8b2c839 100644
--- a/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java
+++ b/src/com/android/providers/telephony/MmsSmsDatabaseHelper.java
@@ -240,7 +240,7 @@
     private static boolean sFakeLowStorageTest = false;     // for testing only
 
     static final String DATABASE_NAME = "mmssms.db";
-    static final int DATABASE_VERSION = 66;
+    static final int DATABASE_VERSION = 68;
     private static final int IDLE_CONNECTION_TIMEOUT_MS = 30000;
 
     private final Context mContext;
@@ -890,10 +890,16 @@
             "offset INTEGER);";
 
     private void createSmsTables(SQLiteDatabase db) {
+        createSmsTables(db, false);
+    }
+
+    private void createSmsTables(SQLiteDatabase db, boolean onlySMS) {
         // N.B.: Whenever the columns here are changed, the columns in
         // {@ref MmsSmsProvider} must be changed to match.
         db.execSQL(CREATE_SMS_TABLE_STRING);
 
+        if (onlySMS) return;
+
         /**
          * This table is used by the SMS dispatcher to hold
          * incomplete partial messages until all the parts arrive.
@@ -934,6 +940,10 @@
     }
 
     private void createCommonTables(SQLiteDatabase db) {
+        createCommonTables(db, false);
+    }
+
+    private void createCommonTables(SQLiteDatabase db, boolean threadsOnly) {
         // TODO Ensure that each entry is removed when the last use of
         // any address equivalent to its address is removed.
 
@@ -946,9 +956,10 @@
          * will be used. The _id is created with AUTOINCREMENT so it
          * will never be reused again if a recipient is deleted.
          */
-        db.execSQL("CREATE TABLE canonical_addresses (" +
-                   "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
-                   "address TEXT);");
+        if (!threadsOnly)
+            db.execSQL("CREATE TABLE canonical_addresses (" +
+                       "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
+                       "address TEXT);");
 
         /**
          * This table maps the subject and an ordered set of recipient
@@ -971,6 +982,8 @@
                    Threads.ERROR + " INTEGER DEFAULT 0," +
                    Threads.HAS_ATTACHMENT + " INTEGER DEFAULT 0);");
 
+        if (threadsOnly)
+            return;
         /**
          * This table stores the queue of messages to be sent/downloaded.
          */
@@ -1486,7 +1499,25 @@
             } finally {
                 db.endTransaction();
             }
-
+            // fall through
+        case 66:
+            // fall through - version not used by AOSP
+        case 67:
+            // fall through - version not used by AOSP
+        case 68:
+            // upgrade from 14.1
+            if (currentVersion <= 68) {
+                return;
+            }
+            try {
+                upgradeDatabaseToVersion68(db);
+                db.setTransactionSuccessful();
+            } catch (Throwable ex) {
+                Log.e(TAG, ex.getMessage(), ex);
+                break;
+            } finally {
+                db.endTransaction();
+            }
             return;
         }
 
@@ -1789,6 +1820,39 @@
         }
     }
 
+    private void upgradeDatabaseToVersion68(SQLiteDatabase db) {
+        try {
+            // back up and recreate sms table to drop priority column
+            db.execSQL("DROP TABLE IF EXISTS sms_tmp;");
+            db.execSQL("ALTER TABLE sms RENAME TO sms_tmp;");
+            createSmsTables(db, true);
+            final String columns = "_id, thread_id, address, person, date, date_sent, " +
+                    "protocol, read, status, type, reply_path_present, subject, body, service_center" +
+                    "locked, sub_id, error_code, creator, seen";
+            db.execSQL("INSERT INTO sms (" + columns + ") SELECT " + columns + " FROM sms_tmp");
+            db.execSQL("DROP TABLE sms_tmp;");
+        } catch (SQLiteException e) {
+            Log.e(TAG, "[upgradeDatabaseToVersion68] Exception removing column "
+                    + "priority; " + e);
+        }
+
+        // now do attachment_info and notification sections
+        try {
+            db.execSQL("DROP TABLE IF EXISTS threads_tmp;");
+            db.execSQL("ALTER TABLE threads RENAME TO threads_tmp;");
+            createCommonTables(db, true);
+            final String columns = Threads._ID + ", " + Threads.DATE + ", " + Threads.MESSAGE_COUNT +
+                ", " + Threads.RECIPIENT_IDS + ", " + Threads.SNIPPET + ", " + Threads.SNIPPET_CHARSET + ", " +
+                Threads.READ + ", " + Threads.ARCHIVED + ", " + Threads.TYPE + ", " + Threads.ERROR + ", " +
+                Threads.HAS_ATTACHMENT;
+            db.execSQL("INSERT INTO threads (" + columns + ") SELECT " + columns + " FROM threads_tmp");
+            db.execSQL("DROP TABLE threads_tmp");
+        } catch (SQLiteException e) {
+            Log.e(TAG, "[upgradeDatabaseToVersion68] Exception removing columns "
+                    + "from threads table; " + e);
+        }
+    }
+
     @Override
     public synchronized SQLiteDatabase getWritableDatabase() {
         SQLiteDatabase db = super.getWritableDatabase();
diff --git a/src/com/android/providers/telephony/TelephonyProvider.java b/src/com/android/providers/telephony/TelephonyProvider.java
index 49c3d10..daa8c84 100644
--- a/src/com/android/providers/telephony/TelephonyProvider.java
+++ b/src/com/android/providers/telephony/TelephonyProvider.java
@@ -119,7 +119,7 @@
     private static final boolean DBG = true;
     private static final boolean VDBG = false; // STOPSHIP if true
 
-    private static final int DATABASE_VERSION = 21 << 16;
+    private static final int DATABASE_VERSION = 24 << 16;
     private static final int URL_UNKNOWN = 0;
     private static final int URL_TELEPHONY = 1;
     private static final int URL_CURRENT = 2;
@@ -816,8 +816,11 @@
                 }
                 oldVersion = 18 << 16 | 6;
             }
-            if (oldVersion < (19 << 16 | 6)) {
-                // Upgrade steps from version 18 are:
+            // version 19 is latest AOSP version (i.e. Android 8.0).
+            // versions 20-23 are unused by AOSP.
+            // version 23 is latest cm-14.1 version
+            if (oldVersion < (24 << 16 | 6)) {
+                // Upgrade steps from Lineage 14.1 (version 23) are:
                 // 1. Create a temp table- done in createCarriersTable()
                 // 2. copy over APNs from old table to new table - done in copyDataToTmpTable()
                 // 3. Drop the existing table.
@@ -862,9 +865,7 @@
                             ": " + c.getCount());
                     c.close();
                 }
-                oldVersion = 19 << 16 | 6;
-            }
-            if (oldVersion < (20 << 16 | 6)) {
+
                 try {
                     // Try to update the siminfo table. It might not be there.
                     db.execSQL("ALTER TABLE " + SIMINFO_TABLE + " ADD COLUMN " +
@@ -879,9 +880,7 @@
                                 "The table will get created in onOpen.");
                     }
                 }
-                oldVersion = 20 << 16 | 6;
-            }
-            if (oldVersion < (21 << 16 | 6)) {
+
                 try {
                     // Try to update the siminfo table. It might not be there.
                     db.execSQL("ALTER TABLE " + CARRIERS_TABLE + " ADD COLUMN " +
@@ -894,8 +893,9 @@
                                 "The table will get created in onOpen.");
                     }
                 }
-                oldVersion = 21 << 16 | 6;
+                oldVersion = 24 << 16 | 6;
             }
+
             if (DBG) {
                 log("dbh.onUpgrade:- db=" + db + " oldV=" + oldVersion + " newV=" + newVersion);
             }