Skip to content

Commit 02a702c

Browse files
committed
Version 1.6.8: fix a crash when upgrading from very old versions
1 parent 1184241 commit 02a702c

File tree

3 files changed

+49
-32
lines changed

3 files changed

+49
-32
lines changed

MediaPhone/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ android {
1010

1111
targetSdkVersion 31
1212
minSdkVersion 14
13-
versionCode 49
14-
versionName '1.6.7'
13+
versionCode 50
14+
versionName '1.6.8'
1515
// versionNameSuffix = '-beta-1'
1616
resConfigs 'en', 'es', 'fr', 'nl', 'pt', 'pl', 'ru'
1717
}

MediaPhone/src/main/java/ac/robinson/mediaphone/provider/MediaPhoneProvider.java

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.database.Cursor;
2929
import android.database.SQLException;
3030
import android.database.sqlite.SQLiteDatabase;
31+
import android.database.sqlite.SQLiteException;
3132
import android.database.sqlite.SQLiteOpenHelper;
3233
import android.database.sqlite.SQLiteQueryBuilder;
3334
import android.net.Uri;
@@ -46,7 +47,7 @@ public class MediaPhoneProvider extends ContentProvider {
4647

4748
public static final String URI_AUTHORITY = MediaPhone.APPLICATION_NAME;
4849
private static final String DATABASE_NAME = URI_AUTHORITY + ".db";
49-
private static final int DATABASE_VERSION = 3;
50+
private static final int DATABASE_VERSION = 4;
5051

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

344+
private void fixVersion1To3UpgradeBug(SQLiteDatabase db) {
345+
Cursor c = null;
346+
try {
347+
c = db.rawQuery("SELECT * FROM " + MEDIA_LOCATION + " LIMIT 0,1", null);
348+
if (c.getColumnIndex(MediaItem.SPAN_FRAMES) < 0) {
349+
db.execSQL("ALTER TABLE " + MEDIA_LOCATION + " ADD COLUMN " + MediaItem.SPAN_FRAMES + " INTEGER;");
350+
}
351+
} finally {
352+
if (c != null) {
353+
c.close();
354+
}
355+
}
356+
createMediaLinksTable(db);
357+
}
358+
343359
@Override
344360
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
345361
Log.i(DebugUtilities.getLogTag(this), "Database upgrade requested from version " + oldVersion + " to " + newVersion);
@@ -348,38 +364,38 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
348364

349365
// must always check whether the items we're upgrading already exist, just in case a downgrade has occurred
350366
Cursor c = null;
351-
switch (newVersion) {
352-
case 2: // version 2 added spanning media
353-
try {
354-
c = db.rawQuery("SELECT * FROM " + MEDIA_LOCATION + " LIMIT 0,1", null);
355-
if (c.getColumnIndex(MediaItem.SPAN_FRAMES) < 0) {
356-
db.execSQL("ALTER TABLE " + MEDIA_LOCATION + " ADD COLUMN " + MediaItem.SPAN_FRAMES + " INTEGER;");
357-
}
358-
} finally {
359-
if (c != null) {
360-
c.close();
361-
}
367+
if (oldVersion < 2) { // version 2 added spanning media
368+
fixVersion1To3UpgradeBug(db);
369+
}
370+
371+
if (oldVersion < 3) { // version 3 added an extra media metadata field to separate text duration from word count
372+
try {
373+
c = db.rawQuery("SELECT * FROM " + MEDIA_LOCATION + " LIMIT 0,1", null);
374+
if (c.getColumnIndex(MediaItem.EXTRA) < 0) {
375+
db.execSQL("ALTER TABLE " + MEDIA_LOCATION + " ADD COLUMN " + MediaItem.EXTRA + " INTEGER;");
376+
db.execSQL(
377+
"UPDATE " + MEDIA_LOCATION + " SET " + MediaItem.DURATION + " = -1 WHERE " + MediaItem.DURATION +
378+
" < 0 AND " + MediaItem.TYPE + " = " + TYPE_TEXT + ";");
362379
}
363-
createMediaLinksTable(db);
364-
break;
365-
366-
case 3: // version 3 added an extra media metadata field to separate text duration from word count
367-
try {
368-
c = db.rawQuery("SELECT * FROM " + MEDIA_LOCATION + " LIMIT 0,1", null);
369-
if (c.getColumnIndex(MediaItem.EXTRA) < 0) {
370-
db.execSQL("ALTER TABLE " + MEDIA_LOCATION + " ADD COLUMN " + MediaItem.EXTRA + " INTEGER;");
371-
db.execSQL("UPDATE " + MEDIA_LOCATION + " SET " + MediaItem.DURATION + " = -1 WHERE " +
372-
MediaItem.DURATION + " < 0 AND " + MediaItem.TYPE + " = " + TYPE_TEXT + ";");
373-
}
374-
} finally {
375-
if (c != null) {
376-
c.close();
377-
}
380+
} finally {
381+
if (c != null) {
382+
c.close();
378383
}
379-
break;
384+
}
385+
}
380386

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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 commit comments

Comments
 (0)