Bug 994734 - Handle dataset refreshes in HomePanelsManager (r=margaret)
authorLucas Rocha <lucasr@mozilla.com>
Wed, 23 Apr 2014 15:22:34 +0100
changeset 180184 3cd4615c60ba0f4c0197fcf8691edc8e287a8a45
parent 180183 cb8bf883a0cd0eddf24c91a757ac7b11336d5c1c
child 180185 ed0236a51ed3388068434c58c0f5714a935fb3d2
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersmargaret
bugs994734
milestone31.0a1
Bug 994734 - Handle dataset refreshes in HomePanelsManager (r=margaret)
mobile/android/base/home/DynamicPanel.java
mobile/android/base/home/HomePanelsManager.java
--- a/mobile/android/base/home/DynamicPanel.java
+++ b/mobile/android/base/home/DynamicPanel.java
@@ -51,18 +51,17 @@ import android.widget.FrameLayout;
  * the provided {@code DatasetHandler}. This way it doesn't need to know the
  * details of how datasets are loaded and reset. Each time a dataset is
  * requested, {@code DynamicPanel} restarts a Loader with the respective ID (see
  * {@code PanelDatasetHandler}).
  *
  * See {@code PanelLayout} for more details on how {@code DynamicPanel}
  * receives dataset requests and delivers them back to the {@code PanelLayout}.
  */
-public class DynamicPanel extends HomeFragment
-                          implements GeckoEventListener {
+public class DynamicPanel extends HomeFragment {
     private static final String LOGTAG = "GeckoDynamicPanel";
 
     // Dataset ID to be used by the loader
     private static final String DATASET_REQUEST = "dataset_request";
 
     // The main view for this fragment. This contains the PanelLayout and PanelAuthLayout.
     private FrameLayout mView;
 
@@ -149,28 +148,26 @@ public class DynamicPanel extends HomeFr
 
         // Restore whatever the UI mode the fragment had before
         // a device rotation.
         if (mUIMode != null) {
             setUIMode(mUIMode);
         }
 
         mPanelAuthCache.setOnChangeListener(new PanelAuthChangeListener());
-        GeckoAppShell.registerEventListener("HomePanels:RefreshDataset", this);
     }
 
     @Override
     public void onDestroyView() {
         super.onDestroyView();
         mView = null;
         mPanelLayout = null;
         mPanelAuthLayout = null;
 
         mPanelAuthCache.setOnChangeListener(null);
-        GeckoAppShell.unregisterEventListener("HomePanels:RefreshDataset", this);
 
         if (mAuthStateTask != null) {
             mAuthStateTask.cancel(true);
             mAuthStateTask = null;
         }
     }
 
     @Override
@@ -284,52 +281,16 @@ public class DynamicPanel extends HomeFr
 
             default:
                 throw new IllegalStateException("Unrecognized UIMode in DynamicPanel");
         }
 
         mUIMode = mode;
     }
 
-    @Override
-    public void handleMessage(String event, final JSONObject message) {
-        if (event.equals("HomePanels:RefreshDataset")) {
-            ThreadUtils.postToUiThread(new Runnable() {
-                @Override
-                public void run() {
-                    handleDatasetRefreshRequest(message);
-                }
-            });
-        }
-    }
-
-    /**
-     * Handles a dataset refresh request from Gecko. This is usually
-     * triggered by a HomeStorage.save() call in an add-on.
-     */
-    private void handleDatasetRefreshRequest(JSONObject message) {
-        final String datasetId;
-        try {
-            datasetId = message.getString("datasetId");
-        } catch (JSONException e) {
-            Log.e(LOGTAG, "Failed to handle dataset refresh", e);
-            return;
-        }
-
-        final Activity activity = getActivity();
-        if (activity == null) {
-            return;
-        }
-
-        Log.d(LOGTAG, "Refresh request for dataset: " + datasetId);
-
-        final ContentResolver cr = activity.getContentResolver();
-        cr.notifyChange(HomeProvider.getDatasetNotificationUri(datasetId), null);
-    }
-
     /**
      * Used by the PanelLayout to make load and reset requests to
      * the holding fragment.
      */
     private class PanelDatasetHandler implements DatasetHandler {
         @Override
         public void requestDataset(DatasetRequest request) {
             Log.d(LOGTAG, "Requesting request: " + request);
--- a/mobile/android/base/home/HomePanelsManager.java
+++ b/mobile/android/base/home/HomePanelsManager.java
@@ -11,38 +11,41 @@ import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Queue;
 import java.util.Set;
 import java.util.concurrent.ConcurrentLinkedQueue;
 
 import org.json.JSONException;
 import org.json.JSONObject;
+import org.mozilla.gecko.db.HomeProvider;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.home.HomeConfig.PanelConfig;
 import org.mozilla.gecko.home.PanelInfoManager.PanelInfo;
 import org.mozilla.gecko.home.PanelInfoManager.RequestCallback;
 import org.mozilla.gecko.util.GeckoEventListener;
 import org.mozilla.gecko.util.ThreadUtils;
 
+import android.content.ContentResolver;
 import android.content.Context;
 import android.os.Handler;
 import android.util.Log;
 
 public class HomePanelsManager implements GeckoEventListener {
     public static final String LOGTAG = "HomePanelsManager";
 
     private static final HomePanelsManager sInstance = new HomePanelsManager();
 
     private static final int INVALIDATION_DELAY_MSEC = 500;
     private static final int PANEL_INFO_TIMEOUT_MSEC = 1000;
 
     private static final String EVENT_HOMEPANELS_INSTALL = "HomePanels:Install";
     private static final String EVENT_HOMEPANELS_UNINSTALL = "HomePanels:Uninstall";
     private static final String EVENT_HOMEPANELS_UPDATE = "HomePanels:Update";
+    private static final String EVENT_HOMEPANELS_REFRESH = "HomePanels:RefreshDataset";
 
     private static final String JSON_KEY_PANEL = "panel";
     private static final String JSON_KEY_PANEL_ID = "id";
 
     private enum ChangeType {
         UNINSTALL,
         INSTALL,
         UPDATE,
@@ -80,16 +83,17 @@ public class HomePanelsManager implement
 
     public void init(Context context) {
         mContext = context;
         mHomeConfig = HomeConfig.getDefault(context);
 
         GeckoAppShell.getEventDispatcher().registerEventListener(EVENT_HOMEPANELS_INSTALL, this);
         GeckoAppShell.getEventDispatcher().registerEventListener(EVENT_HOMEPANELS_UNINSTALL, this);
         GeckoAppShell.getEventDispatcher().registerEventListener(EVENT_HOMEPANELS_UPDATE, this);
+        GeckoAppShell.getEventDispatcher().registerEventListener(EVENT_HOMEPANELS_REFRESH, this);
     }
 
     public void onLocaleReady(final String locale) {
         ThreadUtils.getBackgroundHandler().post(new Runnable() {
             @Override
             public void run() {
                 final String configLocale = mHomeConfig.getLocale();
                 if (configLocale == null || !configLocale.equals(locale)) {
@@ -107,16 +111,19 @@ public class HomePanelsManager implement
                 handlePanelInstall(createPanelConfigFromMessage(message), InvalidationMode.DELAYED);
             } else if (event.equals(EVENT_HOMEPANELS_UNINSTALL)) {
                 Log.d(LOGTAG, EVENT_HOMEPANELS_UNINSTALL);
                 final String panelId = message.getString(JSON_KEY_PANEL_ID);
                 handlePanelUninstall(panelId);
             } else if (event.equals(EVENT_HOMEPANELS_UPDATE)) {
                 Log.d(LOGTAG, EVENT_HOMEPANELS_UPDATE);
                 handlePanelUpdate(createPanelConfigFromMessage(message));
+            } else if (event.equals(EVENT_HOMEPANELS_REFRESH)) {
+                Log.d(LOGTAG, EVENT_HOMEPANELS_REFRESH);
+                handleDatasetRefresh(message);
             }
         } catch (Exception e) {
             Log.e(LOGTAG, "Failed to handle event " + event, e);
         }
     }
 
     private PanelConfig createPanelConfigFromMessage(JSONObject message) throws JSONException {
         final JSONObject json = message.getJSONObject(JSON_KEY_PANEL);
@@ -170,16 +177,38 @@ public class HomePanelsManager implement
      */
     private void handleLocaleChange() {
         mPendingChanges.offer(new ConfigChange(ChangeType.REFRESH));
         Log.d(LOGTAG, "handleLocaleChange: " + mPendingChanges.size());
 
         scheduleInvalidation(InvalidationMode.IMMEDIATE);
     }
 
+
+    /**
+     * Handles a dataset refresh request from Gecko. This is usually
+     * triggered by a HomeStorage.save() call in an add-on.
+     *
+     * Runs in the gecko thread.
+     */
+    private void handleDatasetRefresh(JSONObject message) {
+        final String datasetId;
+        try {
+            datasetId = message.getString("datasetId");
+        } catch (JSONException e) {
+            Log.e(LOGTAG, "Failed to handle dataset refresh", e);
+            return;
+        }
+
+        Log.d(LOGTAG, "Refresh request for dataset: " + datasetId);
+
+        final ContentResolver cr = mContext.getContentResolver();
+        cr.notifyChange(HomeProvider.getDatasetNotificationUri(datasetId), null);
+    }
+
     /**
      * Runs in the gecko or main thread.
      */
     private void scheduleInvalidation(InvalidationMode mode) {
         final Handler handler = ThreadUtils.getBackgroundHandler();
 
         handler.removeCallbacks(mInvalidationRunnable);