Merge "Tests for BP1->BP2 upgrade"
diff --git a/src/com/android/browser/provider/BrowserProvider2.java b/src/com/android/browser/provider/BrowserProvider2.java
index 59bef25..36064a5 100644
--- a/src/com/android/browser/provider/BrowserProvider2.java
+++ b/src/com/android/browser/provider/BrowserProvider2.java
@@ -1231,6 +1231,7 @@
                                 new String[] { Long.toString(id) });
                     }
                 }
+                c.close();
                 break;
             }
             case THUMBNAILS_ID: {
@@ -1263,8 +1264,12 @@
                     " AND account_type = ? AND account_name = ?",
                     new String[] { ChromeSyncColumns.FOLDER_NAME_BOOKMARKS_BAR,
                     accountType, accountName }, null, null, null);
-            if (c.moveToFirst()) {
-                return c.getLong(0);
+            try {
+                if (c.moveToFirst()) {
+                    return c.getLong(0);
+                }
+            } finally {
+                c.close();
             }
         }
         return FIXED_ID_ROOT;
@@ -1741,8 +1746,8 @@
             if (c.moveToFirst()) {
                 parentAccountName = c.getString(0);
                 parentAccountType = c.getString(1);
-                c.close();
             }
+            c.close();
         } else if (values.containsKey(Bookmarks.ACCOUNT_NAME)
                 || values.containsKey(Bookmarks.ACCOUNT_TYPE)) {
             // TODO: Implement if needed (no one needs this yet)
diff --git a/src/com/android/browser/provider/SnapshotProvider.java b/src/com/android/browser/provider/SnapshotProvider.java
index 49557f7..c0aad23 100644
--- a/src/com/android/browser/provider/SnapshotProvider.java
+++ b/src/com/android/browser/provider/SnapshotProvider.java
@@ -106,7 +106,6 @@
 
     @Override
     public boolean onCreate() {
-        mOpenHelper = new SnapshotDatabaseHelper(getContext());
         IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_EJECT);
         filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
         getContext().registerReceiver(mExternalStorageReceiver, filter);
@@ -117,13 +116,15 @@
 
         @Override
         public void onReceive(Context context, Intent intent) {
-            try {
-                mOpenHelper.close();
-            } catch (Throwable t) {
-                // We failed to close the open helper, which most likely means
-                // another thread is busy attempting to open the database
-                // or use the database. Let that thread try to gracefully
-                // deal with the error
+            if (mOpenHelper != null) {
+                try {
+                    mOpenHelper.close();
+                } catch (Throwable t) {
+                    // We failed to close the open helper, which most likely means
+                    // another thread is busy attempting to open the database
+                    // or use the database. Let that thread try to gracefully
+                    // deal with the error
+                }
             }
         }
     };
@@ -132,6 +133,9 @@
         String state = Environment.getExternalStorageState();
         if (Environment.MEDIA_MOUNTED.equals(state)) {
             try {
+                if (mOpenHelper == null) {
+                    mOpenHelper = new SnapshotDatabaseHelper(getContext());
+                }
                 return mOpenHelper.getWritableDatabase();
             } catch (Throwable t) {
                 return null;
@@ -145,6 +149,9 @@
         if (Environment.MEDIA_MOUNTED.equals(state)
                 || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
             try {
+                if (mOpenHelper == null) {
+                    mOpenHelper = new SnapshotDatabaseHelper(getContext());
+                }
                 return mOpenHelper.getReadableDatabase();
             } catch (Throwable t) {
                 return null;