[Android] MediaDatabase: work around devices with broken /data

Edward Wang git at videolan.org
Mon Aug 26 21:37:06 CEST 2013


vlc-ports/android | branch: master | Edward Wang <edward.c.wang at compdigitec.com> | Mon Aug 26 17:15:30 2013 +0000| [b53d25f4f0d42803bc3ba1a324951efb879f2713] | committer: Edward Wang

MediaDatabase: work around devices with broken /data

Allow these devices to at least start up and play files

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=b53d25f4f0d42803bc3ba1a324951efb879f2713
---

 .../src/org/videolan/vlc/MediaDatabase.java        |   32 ++++++++++++++++++++
 .../src/org/videolan/vlc/VLCApplication.java       |    4 +--
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaDatabase.java b/vlc-android/src/org/videolan/vlc/MediaDatabase.java
index ebf1a46..0bc505e 100644
--- a/vlc-android/src/org/videolan/vlc/MediaDatabase.java
+++ b/vlc-android/src/org/videolan/vlc/MediaDatabase.java
@@ -35,6 +35,7 @@ import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteException;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
@@ -111,6 +112,37 @@ public class MediaDatabase {
             super(context, DB_NAME, null, DB_VERSION);
         }
 
+        @Override
+        public SQLiteDatabase getWritableDatabase() {
+            SQLiteDatabase db;
+            try {
+                return super.getWritableDatabase();
+            } catch(SQLiteException e) {
+                try {
+                    db = SQLiteDatabase.openOrCreateDatabase(VLCApplication.getAppContext().getDatabasePath(DB_NAME), null);
+                } catch(SQLiteException e2) {
+                    Log.w(TAG, "SQLite database could not be created! Media library cannot be saved.");
+                    db = SQLiteDatabase.create(null);
+                }
+            }
+            int version = db.getVersion();
+            if (version != DB_VERSION) {
+                db.beginTransaction();
+                try {
+                    if (version == 0) {
+                        onCreate(db);
+                    } else {
+                        onUpgrade(db, version, DB_VERSION);
+                    }
+                    db.setVersion(DB_VERSION);
+                    db.setTransactionSuccessful();
+                } finally {
+                    db.endTransaction();
+                }
+            }
+            return db;
+        }
+
         public void dropMediaTableQuery(SQLiteDatabase db) {
             String query = "DROP TABLE " + MEDIA_TABLE_NAME + ";";
             db.execSQL(query);
diff --git a/vlc-android/src/org/videolan/vlc/VLCApplication.java b/vlc-android/src/org/videolan/vlc/VLCApplication.java
index 604d592..f713f84 100644
--- a/vlc-android/src/org/videolan/vlc/VLCApplication.java
+++ b/vlc-android/src/org/videolan/vlc/VLCApplication.java
@@ -70,10 +70,10 @@ public class VLCApplication extends Application {
                     getBaseContext().getResources().getDisplayMetrics());
         }
 
+        instance = this;
+
         // Initialize the database soon enough to avoid any race condition and crash
         MediaDatabase.getInstance(this);
-
-        instance = this;
     }
 
     /**



More information about the Android mailing list