Bug 974598 - Handle locale changes immediately in HomeConfigInvalidator (r=margaret)
authorLucas Rocha <lucasr@mozilla.com>
Fri, 21 Feb 2014 10:50:15 +0000
changeset 170379 cc947666b4edcf07608b53eb78e762059ce42c45
parent 170378 6d779ac440d6db3ccd9d7297cd2b2a93af059686
child 170380 7c7963536edbde5b19b5dc7e486e778122410c02
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersmargaret
bugs974598
milestone30.0a1
Bug 974598 - Handle locale changes immediately in HomeConfigInvalidator (r=margaret)
mobile/android/base/home/HomeConfigInvalidator.java
--- a/mobile/android/base/home/HomeConfigInvalidator.java
+++ b/mobile/android/base/home/HomeConfigInvalidator.java
@@ -43,23 +43,33 @@ public class HomeConfigInvalidator imple
     private static final String EVENT_HOMEPANELS_UPDATE = "HomePanels:Update";
 
     private static final String JSON_KEY_PANEL = "panel";
     private static final String JSON_KEY_PANEL_ID = "id";
 
     private enum ChangeType {
         UNINSTALL,
         INSTALL,
-        UPDATE
+        UPDATE,
+        REFRESH
+    }
+
+    private enum InvalidationMode {
+        DELAYED,
+        IMMEDIATE
     }
 
     private static class ConfigChange {
         private final ChangeType type;
         private final Object target;
 
+        public ConfigChange(ChangeType type) {
+            this(type, null);
+        }
+
         public ConfigChange(ChangeType type, Object target) {
             this.type = type;
             this.target = target;
         }
     }
 
     private Context mContext;
     private HomeConfig mHomeConfig;
@@ -81,17 +91,17 @@ public class HomeConfigInvalidator imple
     }
 
     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)) {
-                    handlePanelUpdate(null);
+                    handleLocaleChange();
                 }
             }
         });
     }
 
     @Override
     public void handleMessage(String event, JSONObject message) {
         try {
@@ -118,53 +128,64 @@ public class HomeConfigInvalidator imple
 
     /**
      * Runs in the gecko thread.
      */
     private void handlePanelInstall(PanelConfig panelConfig) {
         mPendingChanges.offer(new ConfigChange(ChangeType.INSTALL, panelConfig));
         Log.d(LOGTAG, "handlePanelInstall: " + mPendingChanges.size());
 
-        scheduleInvalidation();
+        scheduleInvalidation(InvalidationMode.DELAYED);
     }
 
     /**
      * Runs in the gecko thread.
      */
     private void handlePanelUninstall(String panelId) {
         mPendingChanges.offer(new ConfigChange(ChangeType.UNINSTALL, panelId));
         Log.d(LOGTAG, "handlePanelUninstall: " + mPendingChanges.size());
 
-        scheduleInvalidation();
+        scheduleInvalidation(InvalidationMode.DELAYED);
     }
 
     /**
-     * Schedules a panel update in HomeConfig. Runs in the gecko or
-     * background thread.
-     *
-     * @param panelConfig the target PanelConfig instance or NULL to refresh
-     *                    all HomeConfig entries.
+     * Runs in the gecko thread.
      */
     private void handlePanelUpdate(PanelConfig panelConfig) {
         mPendingChanges.offer(new ConfigChange(ChangeType.UPDATE, panelConfig));
         Log.d(LOGTAG, "handlePanelUpdate: " + mPendingChanges.size());
 
-        scheduleInvalidation();
+        scheduleInvalidation(InvalidationMode.DELAYED);
+    }
+
+    /**
+     * Runs in the background thread.
+     */
+    private void handleLocaleChange() {
+        mPendingChanges.offer(new ConfigChange(ChangeType.REFRESH));
+        Log.d(LOGTAG, "handleLocaleChange: " + mPendingChanges.size());
+
+        scheduleInvalidation(InvalidationMode.IMMEDIATE);
     }
 
     /**
      * Runs in the gecko or main thread.
      */
-    private void scheduleInvalidation() {
+    private void scheduleInvalidation(InvalidationMode mode) {
         final Handler handler = ThreadUtils.getBackgroundHandler();
 
         handler.removeCallbacks(mInvalidationRunnable);
-        handler.postDelayed(mInvalidationRunnable, INVALIDATION_DELAY_MSEC);
 
-        Log.d(LOGTAG, "scheduleInvalidation: scheduled new invalidation");
+        if (mode == InvalidationMode.IMMEDIATE) {
+            handler.post(mInvalidationRunnable);
+        } else {
+            handler.postDelayed(mInvalidationRunnable, INVALIDATION_DELAY_MSEC);
+        }
+
+        Log.d(LOGTAG, "scheduleInvalidation: scheduled new invalidation: " + mode);
     }
 
     /**
      * Replace an element if a matching PanelConfig is
      * present in the given list.
      */
     private boolean replacePanelConfig(List<PanelConfig> panelConfigs, PanelConfig panelConfig) {
         final int index = panelConfigs.indexOf(panelConfig);
@@ -187,17 +208,17 @@ public class HomeConfigInvalidator imple
 
         return null;
     }
 
     /**
      * Runs in the background thread.
      */
     private List<PanelConfig> executePendingChanges(List<PanelConfig> panelConfigs) {
-        boolean shouldRefreshAll = false;
+        boolean shouldRefresh = false;
 
         while (!mPendingChanges.isEmpty()) {
             final ConfigChange pendingChange = mPendingChanges.poll();
 
             switch (pendingChange.type) {
                 case UNINSTALL: {
                     final String panelId = (String) pendingChange.target;
                     final PanelConfig panelConfig = findPanelConfigWithId(panelConfigs, panelId);
@@ -213,29 +234,29 @@ public class HomeConfigInvalidator imple
                         panelConfigs.add(panelConfig);
                         Log.d(LOGTAG, "executePendingChanges: added panel " + panelConfig.getId());
                     }
                     break;
                 }
 
                 case UPDATE: {
                     final PanelConfig panelConfig = (PanelConfig) pendingChange.target;
-                    if (panelConfig != null) {
-                        if (!replacePanelConfig(panelConfigs, panelConfig)) {
-                            Log.w(LOGTAG, "Tried to update non-existing panel " + panelConfig.getId());
-                        }
-                    } else {
-                        shouldRefreshAll = true;
+                    if (!replacePanelConfig(panelConfigs, panelConfig)) {
+                        Log.w(LOGTAG, "Tried to update non-existing panel " + panelConfig.getId());
                     }
                     break;
                 }
+
+                case REFRESH: {
+                    shouldRefresh = true;
+                }
             }
         }
 
-        if (shouldRefreshAll) {
+        if (shouldRefresh) {
             return executeRefresh(panelConfigs);
         } else {
             return panelConfigs;
         }
     }
 
     /**
      * Runs in the background thread.