Bug 1081948 - Don't use null as an EnumMap key. r=margaret
authorRichard Newman <rnewman@mozilla.com>
Mon, 13 Oct 2014 09:27:15 -0700
changeset 210152 8a6478ddd0e0b51cb1f76631454f0ea7c732a4df
parent 210114 4f831092e95831c355fef06627e2aa332a6df9bb
child 210153 94a4e0128424f23be029b94bf05310e52836f3a1
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersmargaret
bugs1081948
milestone35.0a1
Bug 1081948 - Don't use null as an EnumMap key. r=margaret
mobile/android/base/home/RemoteTabsPanel.java
--- a/mobile/android/base/home/RemoteTabsPanel.java
+++ b/mobile/android/base/home/RemoteTabsPanel.java
@@ -1,17 +1,16 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.home;
 
 import java.util.EnumMap;
-import java.util.HashMap;
 import java.util.Map;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.fxa.AccountLoader;
 import org.mozilla.gecko.fxa.FirefoxAccounts;
 import org.mozilla.gecko.fxa.FxAccountConstants;
 import org.mozilla.gecko.fxa.login.State;
 import org.mozilla.gecko.fxa.login.State.Action;
@@ -47,20 +46,24 @@ public class RemoteTabsPanel extends Hom
 
     // The current fragment being shown to reflect the system account state. We
     // don't want to detach and re-attach panels unnecessarily, because that
     // causes flickering.
     private Fragment mCurrentFragment;
 
     // A lazily-populated cache of fragments corresponding to the possible
     // system account states. We don't want to re-create panels unnecessarily,
-    // because that can cause flickering. Be aware that null is a valid key; it
-    // corresponds to "no Account, neither Firefox nor Legacy Sync."
+    // because that can cause flickering. `null` is not a valid key.
     private final Map<Action, Fragment> mFragmentCache = new EnumMap<>(Action.class);
 
+    // The fragment that corresponds to the null action -- "no Account,
+    // neither Firefox nor Legacy Sync."
+    // Lazily populated.
+    private Fragment mFallbackFragment;
+
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         return inflater.inflate(R.layout.home_remote_tabs_panel, container, false);
     }
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
@@ -170,22 +173,30 @@ public class RemoteTabsPanel extends Hom
      * A null Account means there is no Account (Sync or Firefox) on the device.
      *
      * @param account
      *            Android Account (Sync or Firefox); may be null.
      */
     private Fragment getFragmentNeeded(Account account) {
         final Action actionNeeded = getActionNeeded(account);
 
-        // We use containsKey rather than get because null is a valid key.
-        if (!mFragmentCache.containsKey(actionNeeded)) {
-            final Fragment fragment = makeFragmentForAction(actionNeeded);
+        if (actionNeeded == null) {
+            if (mFallbackFragment == null) {
+                mFallbackFragment = makeFragmentForAction(null);
+            }
+            return mFallbackFragment;
+        }
+
+        Fragment fragment = mFragmentCache.get(actionNeeded);
+        if (fragment == null) {
+            fragment = makeFragmentForAction(actionNeeded);
             mFragmentCache.put(actionNeeded, fragment);
         }
-        return mFragmentCache.get(actionNeeded);
+
+        return fragment;
     }
 
     /**
      * Update the UI to reflect the given <code>Account</code> and its state.
      * <p>
      * A null Account means there is no Account (Sync or Firefox) on the device.
      *
      * @param account