Fix weird case of multi cell icons

 - Don't auto-inflate cell icons
 - Move callback to better time (shouldn't get callbacks before
   setSubs)
 - Fix accidental callback in MobileSignalController constructor
 - Fix subscription listener to run on the right thread to avoid
   the thread issue allowing this bug

Bug: 21504138
Change-Id: Ie1adf8eb05e1042245eead5354b29b314a2e0aae
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/SignalClusterView.java b/packages/SystemUI/src/com/android/systemui/statusbar/SignalClusterView.java
index 5a4acb4..da1f03e 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/SignalClusterView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/SignalClusterView.java
@@ -224,7 +224,10 @@
     public void setMobileDataIndicators(IconState statusIcon, IconState qsIcon, int statusType,
             int qsType, boolean activityIn, boolean activityOut, String typeContentDescription,
             String description, boolean isWide, int subId) {
-        PhoneState state = getOrInflateState(subId);
+        PhoneState state = getState(subId);
+        if (state == null) {
+            return;
+        }
         state.mMobileVisible = statusIcon.visible && !mBlockMobile;
         state.mMobileStrengthId = statusIcon.icon;
         state.mMobileTypeId = statusType;
@@ -281,13 +284,14 @@
         return true;
     }
 
-    private PhoneState getOrInflateState(int subId) {
+    private PhoneState getState(int subId) {
         for (PhoneState state : mPhoneStates) {
             if (state.mSubId == subId) {
                 return state;
             }
         }
-        return inflatePhoneState(subId);
+        Log.e(TAG, "Unexpected subscription " + subId);
+        return null;
     }
 
     private PhoneState inflatePhoneState(int subId) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/MobileSignalController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/MobileSignalController.java
index b21767b..b1c650e 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/MobileSignalController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/MobileSignalController.java
@@ -291,6 +291,7 @@
             notifyListenersIfNecessary();
         } else if (action.equals(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED)) {
             updateDataSim();
+            notifyListenersIfNecessary();
         }
     }
 
@@ -308,7 +309,6 @@
             // for long.
             mCurrentState.dataSim = true;
         }
-        notifyListenersIfNecessary();
     }
 
     /**
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java
index 1ba87da..ff0e8a3 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java
@@ -110,6 +110,8 @@
     // The current user ID.
     private int mCurrentUserId;
 
+    private OnSubscriptionsChangedListener mSubscriptionListener;
+
     // Handler that all broadcasts are received on.
     private final Handler mReceiverHandler;
     // Handler that all callbacks are made on.
@@ -179,6 +181,9 @@
         for (MobileSignalController mobileSignalController : mMobileSignalControllers.values()) {
             mobileSignalController.registerListener();
         }
+        if (mSubscriptionListener == null) {
+            mSubscriptionListener = new SubListener();
+        }
         mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);
 
         // broadcasts
@@ -422,7 +427,6 @@
                         : lhs.getSimSlotIndex() - rhs.getSimSlotIndex();
             }
         });
-        mCallbackHandler.setSubs(subscriptions);
         mCurrentSubscriptions = subscriptions;
 
         HashMap<Integer, MobileSignalController> cachedControllers =
@@ -455,6 +459,9 @@
                 cachedControllers.get(key).unregisterListener();
             }
         }
+        mCallbackHandler.setSubs(subscriptions);
+        notifyAllListeners();
+
         // There may be new MobileSignalControllers around, make sure they get the current
         // inet condition and airplane mode.
         pushConnectivityToSignals();
@@ -724,13 +731,12 @@
         return info;
     }
 
-    private final OnSubscriptionsChangedListener mSubscriptionListener =
-            new OnSubscriptionsChangedListener() {
+    private class SubListener extends OnSubscriptionsChangedListener {
         @Override
         public void onSubscriptionsChanged() {
             updateMobileControllers();
-        };
-    };
+        }
+    }
 
     /**
      * Used to register listeners from the BG Looper, this way the PhoneStateListeners that