28
28
import android .database .Cursor ;
29
29
import android .database .SQLException ;
30
30
import android .database .sqlite .SQLiteDatabase ;
31
+ import android .database .sqlite .SQLiteException ;
31
32
import android .database .sqlite .SQLiteOpenHelper ;
32
33
import android .database .sqlite .SQLiteQueryBuilder ;
33
34
import android .net .Uri ;
@@ -46,7 +47,7 @@ public class MediaPhoneProvider extends ContentProvider {
46
47
47
48
public static final String URI_AUTHORITY = MediaPhone .APPLICATION_NAME ;
48
49
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 ;
50
51
51
52
public static final String URI_PREFIX = "content://" ;
52
53
public static final String URI_SEPARATOR = File .separator ;
@@ -340,6 +341,21 @@ private void createMediaLinksTable(SQLiteDatabase db) {
340
341
MEDIA_LINKS_LOCATION + "(" + MediaItem .PARENT_ID + ");" );
341
342
}
342
343
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
+
343
359
@ Override
344
360
public void onUpgrade (SQLiteDatabase db , int oldVersion , int newVersion ) {
345
361
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) {
348
364
349
365
// must always check whether the items we're upgrading already exist, just in case a downgrade has occurred
350
366
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 + ";" );
362
379
}
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 ();
378
383
}
379
- break ;
384
+ }
385
+ }
380
386
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
+ }
383
399
}
384
400
}
385
401
0 commit comments