Bug 1000849 - Make a dummy MatrixCursor to listen for dataset changed notifications. r=lucasr a=lsblakk
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Fri, 25 Apr 2014 08:29:27 -0700
changeset 192136 c763c8695fe47f682d62aa72d9d1ac639ff7ff48
parent 192135 e67471d96cb659b73c5f4e2ca6ff721ffa45f36d
child 192137 d58b97f124bff62557a57f5b8f9218df57d4abb7
push id3503
push userraliiev@mozilla.com
push dateMon, 28 Apr 2014 18:51:11 +0000
treeherdermozilla-beta@c95ac01e332e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerslucasr, lsblakk
bugs1000849
milestone30.0a2
Bug 1000849 - Make a dummy MatrixCursor to listen for dataset changed notifications. r=lucasr a=lsblakk
mobile/android/base/db/BrowserContract.java
mobile/android/base/db/HomeProvider.java
--- a/mobile/android/base/db/BrowserContract.java
+++ b/mobile/android/base/db/BrowserContract.java
@@ -333,16 +333,19 @@ public class BrowserContract {
 
         public static final String DATASET_ID = "dataset_id";
         public static final String URL = "url";
         public static final String TITLE = "title";
         public static final String DESCRIPTION = "description";
         public static final String IMAGE_URL = "image_url";
         public static final String CREATED = "created";
         public static final String FILTER = "filter";
+
+        public static final String[] DEFAULT_PROJECTION =
+            new String[] { _ID, DATASET_ID, URL, TITLE, DESCRIPTION, IMAGE_URL, FILTER };
     }
 
     /*
      * Contains names and schema definitions for tables and views
      * no longer being used by current ContentProviders. These values are used
      * to make incremental updates to the schema during a database upgrade. Will be
      * removed with bug 947018.
      */
--- a/mobile/android/base/db/HomeProvider.java
+++ b/mobile/android/base/db/HomeProvider.java
@@ -80,22 +80,27 @@ public class HomeProvider extends SQLite
             throw new IllegalArgumentException("All queries should contain a dataset ID parameter");
         }
 
         selection = DBUtils.concatenateWhere(selection, HomeItems.DATASET_ID + " = ?");
         selectionArgs = DBUtils.appendSelectionArgs(selectionArgs,
                                                     new String[] { datasetId });
 
         // Otherwise, let the SQLiteContentProvider implementation take care of this query for us!
-        final Cursor c = super.query(uri, projection, selection, selectionArgs, sortOrder);
-        if (c != null) {
-            final ContentResolver cr = getContext().getContentResolver();
-            c.setNotificationUri(cr, getDatasetNotificationUri(datasetId));
+        Cursor c = super.query(uri, projection, selection, selectionArgs, sortOrder);
+
+        // SQLiteBridgeContentProvider may return a null Cursor if the database hasn't been created yet.
+        // However, we need a non-null cursor in order to listen for notifications.
+        if (c == null) {
+            c = new MatrixCursor(projection != null ? projection : HomeItems.DEFAULT_PROJECTION);
         }
 
+        final ContentResolver cr = getContext().getContentResolver();
+        c.setNotificationUri(cr, getDatasetNotificationUri(datasetId));
+
         return c;
     }
 
     /**
      * Returns a cursor populated with static fake data.
      */
     private Cursor queryFakeItems(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
         JSONArray items = null;
@@ -104,27 +109,17 @@ public class HomeProvider extends SQLite
         } catch (IOException e) {
             Log.e(LOGTAG, "Error getting fake home items", e);
             return null;
         } catch (JSONException e) {
             Log.e(LOGTAG, "Error parsing fake_home_items.json", e);
             return null;
         }
 
-        final String[] itemsColumns = new String[] {
-            HomeItems._ID,
-            HomeItems.DATASET_ID,
-            HomeItems.URL,
-            HomeItems.TITLE,
-            HomeItems.DESCRIPTION,
-            HomeItems.IMAGE_URL,
-            HomeItems.FILTER
-        };
-
-        final MatrixCursor c = new MatrixCursor(itemsColumns);
+        final MatrixCursor c = new MatrixCursor(HomeItems.DEFAULT_PROJECTION);
         for (int i = 0; i < items.length(); i++) {
             try {
                 final JSONObject item = items.getJSONObject(i);
                 c.addRow(new Object[] {
                     item.getInt("id"),
                     item.getString("dataset_id"),
                     item.getString("url"),
                     item.getString("title"),