Bug 1081948 - Don't use null as an EnumMap key. r=margaret, a=sledru
authorRichard Newman <rnewman@mozilla.com>
Mon, 13 Oct 2014 09:27:15 -0700
changeset 223453 735ab4890a4a3f28e163e59b0051722140671644
parent 223452 4a20ef9528b871c02107c0512c25467dbad08e34
child 223454 a06058806b8bf998c3f3a132a8105cfcda0866b9
push id5
push usergszorc@mozilla.com
push dateWed, 29 Oct 2014 02:51:31 +0000
reviewersmargaret, sledru
bugs1081948
milestone35.0a2
Bug 1081948 - Don't use null as an EnumMap key. r=margaret, a=sledru
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