Get the IconMerger working again.

Change-Id: I73719f4fd475a39d4c1245de45c6a13c31e6323b
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/IconMerger.java b/packages/SystemUI/src/com/android/systemui/statusbar/IconMerger.java
index 26f5f51..027bed4a 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/IconMerger.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/IconMerger.java
@@ -19,22 +19,37 @@
 import android.content.Context;
 import android.os.Handler;
 import android.util.AttributeSet;
+import android.util.Slog;
 import android.view.View;
 import android.widget.LinearLayout;
 
+import com.android.systemui.R;
+
+
 public class IconMerger extends LinearLayout {
-    PhoneStatusBarService service;
+    private static final String TAG = "IconMerger";
+
+    private StatusBarIconView mMoreView;
 
     public IconMerger(Context context, AttributeSet attrs) {
         super(context, attrs);
     }
 
+    public void addMoreView(StatusBarIconView v, LinearLayout.LayoutParams lp) {
+        super.addView(v, lp);
+        mMoreView = v;
+    }
+
+    public void addView(StatusBarIconView v, int index, LinearLayout.LayoutParams lp) {
+        if (index == 0) {
+            throw new RuntimeException("Attempt to put view before the more view: " + v);
+        }
+        super.addView(v, index, lp);
+    }
+
     @Override
     protected void onLayout(boolean changed, int l, int t, int r, int b) {
         super.onLayout(changed, l, t, r, b);
-        if (true) {
-            return;
-        }
 
         final int maxWidth = r - l;
         final int N = getChildCount();
@@ -51,13 +66,12 @@
         }
 
         // find the first visible one that isn't the more icon
-        View moreView = null;
+        final StatusBarIconView moreView = mMoreView;
         int fitLeft = -1;
         int startIndex = -1;
         for (i=0; i<N; i++) {
             final View child = getChildAt(i);
-            if (com.android.internal.R.drawable.stat_notify_more == child.getId()) {
-                moreView = child;
+            if (child == moreView) {
                 startIndex = i+1;
             }
             else if (child.getVisibility() != GONE) {
@@ -67,7 +81,11 @@
         }
 
         if (moreView == null || startIndex < 0) {
-            throw new RuntimeException("Status Bar / IconMerger moreView == null");
+            return;
+            /*
+            throw new RuntimeException("Status Bar / IconMerger moreView == " + moreView
+                    + " startIndex=" + startIndex);
+            */
         }
         
         // if it fits without the more icon, then hide the more icon and update fitLeft
@@ -85,14 +103,14 @@
         int breakingPoint = fitLeft + extra + adjust;
         int number = 0;
         for (i=startIndex; i<N; i++) {
-            final View child = getChildAt(i);
+            final StatusBarIconView child = (StatusBarIconView)getChildAt(i);
             if (child.getVisibility() != GONE) {
                 int childLeft = child.getLeft();
                 int childRight = child.getRight();
                 if (childLeft < breakingPoint) {
                     // hide this one
                     child.layout(0, child.getTop(), 0, child.getBottom());
-                    int n = 0; // XXX this.service.getIconNumberForView(child);
+                    int n = child.getStatusBarIcon().number;
                     if (n == 0) {
                         number += 1;
                     } else if (n > 0) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/PhoneStatusBarService.java b/packages/SystemUI/src/com/android/systemui/statusbar/PhoneStatusBarService.java
index e7d8282..155b7d7 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/PhoneStatusBarService.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/PhoneStatusBarService.java
@@ -233,7 +233,6 @@
         mStatusBarView = sb;
         mStatusIcons = (LinearLayout)sb.findViewById(R.id.statusIcons);
         mNotificationIcons = (IconMerger)sb.findViewById(R.id.notificationIcons);
-        mNotificationIcons.service = this;
         mIcons = (LinearLayout)sb.findViewById(R.id.icons);
         mTickerView = sb.findViewById(R.id.ticker);
         mDateView = (DateView)sb.findViewById(R.id.date);
@@ -268,6 +267,12 @@
 
         mEdgeBorder = res.getDimensionPixelSize(R.dimen.status_bar_edge_ignore);
 
+        // the more notifications icon
+        StatusBarIconView moreView = new StatusBarIconView(this, "more");
+        moreView.set(new StatusBarIcon(null, R.drawable.stat_notify_more, 0));
+        mNotificationIcons.addMoreView(moreView,
+                new LinearLayout.LayoutParams(mIconWidth, mHeight));
+
         // set the inital view visibility
         setAreThereNotifications();
         mDateView.setVisibility(View.INVISIBLE);
@@ -414,13 +419,13 @@
         updateExpandedViewPos(EXPANDED_LEAVE_ALONE);
     }
 
-    private int chooseIconIndex(boolean isOngoing, int index) {
-        final int ongoingSize = mOngoing.size();
+    private int chooseIconIndex(boolean isOngoing, int viewIndex) {
         final int latestSize = mLatest.size();
-        if (!isOngoing) {
-            index = mLatest.size() + index;
+        if (isOngoing) {
+            return latestSize + (mOngoing.size() - viewIndex);
+        } else {
+            return latestSize - viewIndex;
         }
-        return (ongoingSize + latestSize) - index - 1;
     }
 
     View[] makeNotificationView(StatusBarNotification notification, ViewGroup parent) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/StatusBarIconView.java b/packages/SystemUI/src/com/android/systemui/statusbar/StatusBarIconView.java
index 959ac7c..ca5db88 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/StatusBarIconView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/StatusBarIconView.java
@@ -124,4 +124,8 @@
 
         return null;
     }
+
+    public StatusBarIcon getStatusBarIcon() {
+        return mIcon;
+    }
 }