Skip to content

Commit

Permalink
Version 1.6.8: fix a crash when upgrading from very old versions
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrob committed Feb 16, 2022
1 parent 1184241 commit 02a702c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
4 changes: 2 additions & 2 deletions MediaPhone/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {

targetSdkVersion 31
minSdkVersion 14
versionCode 49
versionName '1.6.7'
versionCode 50
versionName '1.6.8'
// versionNameSuffix = '-beta-1'
resConfigs 'en', 'es', 'fr', 'nl', 'pt', 'pl', 'ru'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
Expand All @@ -46,7 +47,7 @@ public class MediaPhoneProvider extends ContentProvider {

public static final String URI_AUTHORITY = MediaPhone.APPLICATION_NAME;
private static final String DATABASE_NAME = URI_AUTHORITY + ".db";
private static final int DATABASE_VERSION = 3;
private static final int DATABASE_VERSION = 4;

public static final String URI_PREFIX = "content://";
public static final String URI_SEPARATOR = File.separator;
Expand Down Expand Up @@ -340,6 +341,21 @@ private void createMediaLinksTable(SQLiteDatabase db) {
MEDIA_LINKS_LOCATION + "(" + MediaItem.PARENT_ID + ");");
}

private void fixVersion1To3UpgradeBug(SQLiteDatabase db) {
Cursor c = null;
try {
c = db.rawQuery("SELECT * FROM " + MEDIA_LOCATION + " LIMIT 0,1", null);
if (c.getColumnIndex(MediaItem.SPAN_FRAMES) < 0) {
db.execSQL("ALTER TABLE " + MEDIA_LOCATION + " ADD COLUMN " + MediaItem.SPAN_FRAMES + " INTEGER;");
}
} finally {
if (c != null) {
c.close();
}
}
createMediaLinksTable(db);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i(DebugUtilities.getLogTag(this), "Database upgrade requested from version " + oldVersion + " to " + newVersion);
Expand All @@ -348,38 +364,38 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

// must always check whether the items we're upgrading already exist, just in case a downgrade has occurred
Cursor c = null;
switch (newVersion) {
case 2: // version 2 added spanning media
try {
c = db.rawQuery("SELECT * FROM " + MEDIA_LOCATION + " LIMIT 0,1", null);
if (c.getColumnIndex(MediaItem.SPAN_FRAMES) < 0) {
db.execSQL("ALTER TABLE " + MEDIA_LOCATION + " ADD COLUMN " + MediaItem.SPAN_FRAMES + " INTEGER;");
}
} finally {
if (c != null) {
c.close();
}
if (oldVersion < 2) { // version 2 added spanning media
fixVersion1To3UpgradeBug(db);
}

if (oldVersion < 3) { // version 3 added an extra media metadata field to separate text duration from word count
try {
c = db.rawQuery("SELECT * FROM " + MEDIA_LOCATION + " LIMIT 0,1", null);
if (c.getColumnIndex(MediaItem.EXTRA) < 0) {
db.execSQL("ALTER TABLE " + MEDIA_LOCATION + " ADD COLUMN " + MediaItem.EXTRA + " INTEGER;");
db.execSQL(
"UPDATE " + MEDIA_LOCATION + " SET " + MediaItem.DURATION + " = -1 WHERE " + MediaItem.DURATION +
" < 0 AND " + MediaItem.TYPE + " = " + TYPE_TEXT + ";");
}
createMediaLinksTable(db);
break;

case 3: // version 3 added an extra media metadata field to separate text duration from word count
try {
c = db.rawQuery("SELECT * FROM " + MEDIA_LOCATION + " LIMIT 0,1", null);
if (c.getColumnIndex(MediaItem.EXTRA) < 0) {
db.execSQL("ALTER TABLE " + MEDIA_LOCATION + " ADD COLUMN " + MediaItem.EXTRA + " INTEGER;");
db.execSQL("UPDATE " + MEDIA_LOCATION + " SET " + MediaItem.DURATION + " = -1 WHERE " +
MediaItem.DURATION + " < 0 AND " + MediaItem.TYPE + " = " + TYPE_TEXT + ";");
}
} finally {
if (c != null) {
c.close();
}
} finally {
if (c != null) {
c.close();
}
break;
}
}

default:
break;
if (oldVersion < 4) { // version 4 is a bugfix
// versions 38 to 49 had a flaw when upgrading - databases at version 1 were not upgraded to version 2 before
// being upgraded to version 3; as a result, findAllTextMedia in UpgradeManager caused a crash on launch
// the fix is to introduce a new database version (with no new changes) and make sure the version 1->2 upgrade is
// performed when upgrading both from old versions (the oldVersion < 2 check, above); and, when upgrading from
// version 3 that may or may not have the 1->2 upgrades - note also that we need to catch the SQLiteException
// below because upgrades getColumnIndex returns < 0 before our changes have actually been committed, but the
// database itself throws an exception when trying to add what is actually a duplicate column
try {
fixVersion1To3UpgradeBug(db);
} catch (SQLiteException ignored) {
}
}
}

Expand Down
1 change: 1 addition & 0 deletions fastlane/metadata/android/en-US/changelogs/50.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed an issue that caused a crash when upgrading directly from much older versions of the app to versions 1.6.2 and later

0 comments on commit 02a702c

Please sign in to comment.