Bug 994734 - Pass dataset id as query argument in HomeProvider (r=margaret)
authorLucas Rocha <lucasr@mozilla.com>
Wed, 23 Apr 2014 15:22:34 +0100
changeset 180182 7d9d72c9cfac53a5b9b4406446f94e8b0ae0ce59
parent 180181 101b549e1b58e40ba91274e553cc570f60e0dccf
child 180183 cb8bf883a0cd0eddf24c91a757ac7b11336d5c1c
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersmargaret
bugs994734
milestone31.0a1
Bug 994734 - Pass dataset id as query argument in HomeProvider (r=margaret)
mobile/android/base/db/BrowserContract.java
mobile/android/base/db/HomeProvider.java
mobile/android/base/home/DynamicPanel.java
--- a/mobile/android/base/db/BrowserContract.java
+++ b/mobile/android/base/db/BrowserContract.java
@@ -37,16 +37,17 @@ public class BrowserContract {
     public static final String PARAM_PROFILE_PATH = "profilePath";
     public static final String PARAM_LIMIT = "limit";
     public static final String PARAM_IS_SYNC = "sync";
     public static final String PARAM_SHOW_DELETED = "show_deleted";
     public static final String PARAM_IS_TEST = "test";
     public static final String PARAM_INSERT_IF_NEEDED = "insert_if_needed";
     public static final String PARAM_INCREMENT_VISITS = "increment_visits";
     public static final String PARAM_EXPIRE_PRIORITY = "priority";
+    public static final String PARAM_DATASET_ID = "dataset_id";
 
     static public enum ExpirePriority {
         NORMAL,
         AGGRESSIVE
     }
 
     static public String getFrecencySortOrder(boolean includesBookmarks, boolean asc) {
         final String age = "(" + Combined.DATE_LAST_VISITED + " - " + System.currentTimeMillis() + ") / 86400000";
--- a/mobile/android/base/db/HomeProvider.java
+++ b/mobile/android/base/db/HomeProvider.java
@@ -6,16 +6,17 @@ package org.mozilla.gecko.db;
 
 import java.io.IOException;
 
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.db.BrowserContract.HomeItems;
+import org.mozilla.gecko.db.DBUtils;
 import org.mozilla.gecko.sqlite.SQLiteBridge;
 import org.mozilla.gecko.util.RawResource;
 
 import android.content.ContentValues;
 import android.content.UriMatcher;
 import android.database.Cursor;
 import android.database.MatrixCursor;
 import android.net.Uri;
@@ -68,16 +69,25 @@ public class HomeProvider extends SQLite
     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
         final int match = URI_MATCHER.match(uri);
 
         // If we're querying the fake items, don't try to get the database.
         if (match == ITEMS_FAKE) {
             return queryFakeItems(uri, projection, selection, selectionArgs, sortOrder);
         }
 
+        final String datasetId = uri.getQueryParameter(BrowserContract.PARAM_DATASET_ID);
+        if (datasetId == null) {
+            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!
         return super.query(uri, projection, selection, selectionArgs, sortOrder);
     }
 
     /**
      * Returns a cursor populated with static fake data.
      */
     private Cursor queryFakeItems(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
--- a/mobile/android/base/home/DynamicPanel.java
+++ b/mobile/android/base/home/DynamicPanel.java
@@ -4,16 +4,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.home;
 
 import org.json.JSONException;
 import org.json.JSONObject;
 
 import org.mozilla.gecko.GeckoAppShell;
+import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.db.BrowserContract.HomeItems;
 import org.mozilla.gecko.db.DBUtils;
 import org.mozilla.gecko.home.HomeConfig.PanelConfig;
 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
 import org.mozilla.gecko.home.PanelLayout.DatasetHandler;
 import org.mozilla.gecko.home.PanelLayout.DatasetRequest;
 import org.mozilla.gecko.util.GeckoEventListener;
 import org.mozilla.gecko.util.ThreadUtils;
@@ -398,25 +399,31 @@ public class DynamicPanel extends HomeFr
         public Cursor loadCursor() {
             final ContentResolver cr = getContext().getContentResolver();
 
             final String selection;
             final String[] selectionArgs;
 
             // Null represents the root filter
             if (mRequest.getFilter() == null) {
-                selection = DBUtils.concatenateWhere(HomeItems.DATASET_ID + " = ?", HomeItems.FILTER + " IS NULL");
-                selectionArgs = new String[] { mRequest.getDatasetId() };
+                selection = HomeItems.FILTER + " IS NULL";
+                selectionArgs = null;
             } else {
-                selection = DBUtils.concatenateWhere(HomeItems.DATASET_ID + " = ?", HomeItems.FILTER + " = ?");
-                selectionArgs = new String[] { mRequest.getDatasetId(), mRequest.getFilter() };
+                selection = HomeItems.FILTER + " = ?";
+                selectionArgs = new String[] { mRequest.getFilter() };
             }
 
-            // XXX: You can use CONTENT_FAKE_URI for development to pull items from fake_home_items.json.
-            final Cursor c = cr.query(HomeItems.CONTENT_URI, null, selection, selectionArgs, null);
+            final Uri queryUri = HomeItems.CONTENT_URI.buildUpon()
+                                                      .appendQueryParameter(BrowserContract.PARAM_DATASET_ID,
+                                                                            mRequest.getDatasetId())
+                                                      .build();
+
+            // XXX: You can use HomeItems.CONTENT_FAKE_URI for development
+            // to pull items from fake_home_items.json.
+            final Cursor c = cr.query(queryUri, null, selection, selectionArgs, null);
 
             // SQLiteBridgeContentProvider may return a null Cursor if the database hasn't been created yet.
             if (c != null) {
                 final Uri notificationUri = getDatasetNotificationUri(mRequest.getDatasetId());
                 c.setNotificationUri(cr, notificationUri);
             }
 
             return c;