Merge "Use a single view for the accountswitchspinner." into jb-ub-mail
diff --git a/res/layout/account_switch_spinner_dropdown_item.xml b/res/layout/account_switch_spinner_dropdown_item.xml
index e38c124..f8cc18b 100644
--- a/res/layout/account_switch_spinner_dropdown_item.xml
+++ b/res/layout/account_switch_spinner_dropdown_item.xml
@@ -16,42 +16,56 @@
      limitations under the License.
 -->
 
-<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    style="?android:attr/spinnerDropDownItemStyle"
+<FrameLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
-    android:layout_height="wrap_content"
-    android:minHeight="@dimen/account_dropdown_item_height"
-    android:gravity="center_horizontal">
-    <View android:id="@+id/account_spinner_color"
-        android:layout_alignParentTop="true"
-        android:layout_alignParentRight="true"
-        android:layout_height="@dimen/color_block_height"
-        android:layout_width="@dimen/color_block_width"/>
-    <RelativeLayout
-        android:layout_height="wrap_content"
+    android:layout_height="wrap_content">
+    <!-- View shown in the navigation spinner in the actionbar.  -->
+    <include android:id="@+id/anchor" layout="@layout/account_switch_spinner_item"/>
+    <FrameLayout android:id="@+id/dropdown"
         android:layout_width="match_parent"
-        android:layout_centerVertical="true"
-        android:layout_alignParentLeft="true">
-        <!-- Something is strange here. Gmail1 allows for two-line label names while Gmail2 does
-	     not. TODO(viki): Fix. -->
-        <TextView
-            android:id="@+id/account_spinner_first"
-            android:singleLine="true"
-            android:layout_height="wrap_content"
+        android:layout_height="wrap_content">
+        <RelativeLayout
+            style="?android:attr/spinnerDropDownItemStyle"
             android:layout_width="match_parent"
-            android:ellipsize="end"
-            style="@android:style/TextAppearance.Holo.Widget.ActionBar.Title" />
-        <TextView
-            android:id="@+id/account_spinner_second"
             android:layout_height="wrap_content"
-            android:layout_width="match_parent"
-            android:ellipsize="end"
-            android:layout_below="@id/account_spinner_first"
-            style="@android:style/TextAppearance.Holo.Widget.ActionBar.Subtitle" />
-    </RelativeLayout>
-    <TextView
-        android:id="@+id/account_spinner_unread_count"
-        style="@style/UnreadCount"
-        android:layout_centerVertical="true"
-        android:layout_alignParentRight="true"/>
-</RelativeLayout>
+            android:minHeight="@dimen/account_dropdown_item_height"
+            android:gravity="center_horizontal"
+            android:id="@+id/default_view">
+            <View android:id="@+id/account_spinner_color"
+                android:layout_alignParentTop="true"
+                android:layout_alignParentRight="true"
+                android:layout_height="@dimen/color_block_height"
+                android:layout_width="@dimen/color_block_width"/>
+            <RelativeLayout
+                android:layout_height="wrap_content"
+                android:layout_width="match_parent"
+                android:layout_centerVertical="true"
+                android:layout_alignParentLeft="true">
+                <!-- Something is strange here. Gmail1 allows for two-line label names while Gmail2 does
+	               not. TODO(viki): Fix. -->
+                <TextView
+                    android:id="@+id/account_spinner_first"
+                    android:singleLine="true"
+                    android:layout_height="wrap_content"
+                    android:layout_width="match_parent"
+                    android:ellipsize="end"
+                    style="@android:style/TextAppearance.Holo.Widget.ActionBar.Title" />
+                <TextView
+                    android:id="@+id/account_spinner_second"
+                    android:layout_height="wrap_content"
+                    android:layout_width="match_parent"
+                    android:ellipsize="end"
+                    android:layout_below="@id/account_spinner_first"
+                    style="@android:style/TextAppearance.Holo.Widget.ActionBar.Subtitle" />
+            </RelativeLayout>
+            <TextView
+                android:id="@+id/account_spinner_unread_count"
+                style="@style/UnreadCount"
+                android:layout_centerVertical="true"
+                android:layout_alignParentRight="true"/>
+        </RelativeLayout>
+        <include android:id="@+id/header" layout="@layout/account_switch_spinner_dropdown_header"/>
+        <include android:id="@+id/empty" layout="@layout/empty"/>
+    </FrameLayout>
+</FrameLayout>
diff --git a/src/com/android/mail/AccountSpinnerAdapter.java b/src/com/android/mail/AccountSpinnerAdapter.java
index f81247a..a510efb 100644
--- a/src/com/android/mail/AccountSpinnerAdapter.java
+++ b/src/com/android/mail/AccountSpinnerAdapter.java
@@ -94,8 +94,7 @@
      * @return the type of account: one of {@link #TYPE_ACCOUNT}, {@link #TYPE_HEADER}, or
      * {@link #TYPE_FOLDER}.
      */
-    @Override
-    public int getItemViewType(int position) {
+    public int getType(int position) {
         if (position == 0) {
             return TYPE_DEAD_HEADER;
         }
@@ -212,7 +211,7 @@
 
     @Override
     public Object getItem(int position) {
-        switch (getItemViewType(position)){
+        switch (getType(position)){
             case TYPE_DEAD_HEADER:
                 return "dead header";
             case TYPE_ACCOUNT:
@@ -268,26 +267,23 @@
     // always return what we believe that view is.
     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
-        convertView = mInflater.inflate(R.layout.account_switch_spinner_item, null);
-        ((TextView) convertView.findViewById(R.id.account_spinner_first))
+        if (convertView == null) {
+            convertView = mInflater.inflate(R.layout.account_switch_spinner_dropdown_item, null);
+        }
+        // Hide everything else.
+        View anchor = showAnchor(convertView);
+        // Show the anchor
+        anchor.findViewById(R.id.anchor).setVisibility(View.VISIBLE);
+        ((TextView) anchor.findViewById(R.id.account_spinner_first))
             .setText(getFolderLabel());
-        ((TextView) convertView.findViewById(R.id.account_spinner_second))
+        ((TextView) anchor.findViewById(R.id.account_spinner_second))
             .setText(getCurrentAccountName());
-        populateUnreadCountView((TextView) convertView.findViewById(R.id.unread),
+        populateUnreadCountView((TextView) anchor.findViewById(R.id.unread),
                 getFolderUnreadCount());
         return convertView;
     }
 
     @Override
-    public int getViewTypeCount() {
-        // If recent folders are shown, then two views: Recent folders and a header, and potentially
-        // one "show all folders" item.
-        final int folderTypes = mRecentFoldersVisible ?  (2 + (mShowAllFoldersItem ? 1 : 0)) : 0;
-        // Accounts are the type of view always shown.
-        return 2 + folderTypes;
-    }
-
-    @Override
     public boolean hasStableIds() {
         // The account manager could add new accounts, so don't claim that the IDs are stable.
         return false;
@@ -299,6 +295,23 @@
         return false;
     }
 
+    private View showAnchor(View convertView) {
+        convertView.findViewById(R.id.dropdown).setVisibility(View.GONE);
+        View anchor = convertView.findViewById(R.id.anchor);
+        anchor.setVisibility(View.VISIBLE);
+        return anchor;
+    }
+
+    private View showDropdown(View convertView) {
+        convertView.findViewById(R.id.anchor).setVisibility(View.GONE);
+        convertView.findViewById(R.id.empty).setVisibility(View.GONE);
+        convertView.findViewById(R.id.header).setVisibility(View.GONE);
+        convertView.findViewById(R.id.default_view).setVisibility(View.GONE);
+        View dropdown = convertView.findViewById(R.id.dropdown);
+        dropdown.setVisibility(View.VISIBLE);
+        return dropdown;
+    }
+
     @Override
     public View getDropDownView(int position, View convertView, ViewGroup parent) {
         // Shown in the first text view with big font.
@@ -307,9 +320,14 @@
         String smallText = "";
         int color = 0;
         int unreadCount = 0;
-        switch (getItemViewType(position)) {
+        if (convertView == null) {
+            convertView = mInflater.inflate(R.layout.account_switch_spinner_dropdown_item, null);
+        }
+        // Show dropdown portion.
+        View dropdown = showDropdown(convertView);
+        switch (getType(position)) {
             case TYPE_DEAD_HEADER:
-                convertView = mInflater.inflate(R.layout.empty, null);
+                dropdown.findViewById(R.id.empty).setVisibility(View.VISIBLE);
                 return convertView;
             case TYPE_ACCOUNT:
                 // TODO(viki): Get real Inbox or Priority Inbox using the URI. Remove ugly hack.
@@ -318,10 +336,9 @@
                 color = getAccountColor(position);
                 break;
             case TYPE_HEADER:
-                convertView = mInflater.inflate(R.layout.account_switch_spinner_dropdown_header,
-                        null);
+                dropdown.findViewById(R.id.header).setVisibility(View.VISIBLE);
                 final String label = getCurrentAccountName();
-                TextView accountLabel = ((TextView) convertView.findViewById(
+                TextView accountLabel = ((TextView) dropdown.findViewById(
                         R.id.account_spinner_header_account));
                 if (accountLabel != null) {
                     accountLabel.setText(label);
@@ -336,11 +353,11 @@
                 bigText = mContext.getResources().getString(R.string.show_all_folders);
                 break;
         }
-        convertView = mInflater.inflate(R.layout.account_switch_spinner_dropdown_item, null);
-        displayOrHide(convertView, R.id.account_spinner_first, bigText);
-        displayOrHide(convertView, R.id.account_spinner_second, smallText);
+        dropdown.findViewById(R.id.default_view).setVisibility(View.VISIBLE);
+        displayOrHide(dropdown, R.id.account_spinner_first, bigText);
+        displayOrHide(dropdown, R.id.account_spinner_second, smallText);
 
-        final View colorView = convertView.findViewById(R.id.account_spinner_color);
+        final View colorView = dropdown.findViewById(R.id.account_spinner_color);
         if (color != 0) {
             colorView.setVisibility(View.VISIBLE);
             colorView.setBackgroundColor(color);
@@ -348,7 +365,7 @@
             colorView.setVisibility(View.INVISIBLE);
         }
         populateUnreadCountView(
-                (TextView) convertView.findViewById(R.id.account_spinner_unread_count),
+                (TextView) dropdown.findViewById(R.id.account_spinner_unread_count),
                 unreadCount);
         return convertView;
 
@@ -412,7 +429,7 @@
     @Override
     public boolean isEnabled(int position) {
         // Don't want the user selecting the header.
-        final int type = getItemViewType(position);
+        final int type = getType(position);
         return type != TYPE_DEAD_HEADER && type != TYPE_HEADER;
     }
 
diff --git a/src/com/android/mail/ui/MailActionBarView.java b/src/com/android/mail/ui/MailActionBarView.java
index 518475d..ef1c2c4 100644
--- a/src/com/android/mail/ui/MailActionBarView.java
+++ b/src/com/android/mail/ui/MailActionBarView.java
@@ -332,7 +332,7 @@
             return false;
         }
         LogUtils.d(LOG_TAG, "onNavigationItemSelected(%d, %d) called", position, id);
-        final int type = mSpinner.getItemViewType(position);
+        final int type = mSpinner.getType(position);
         switch (type) {
             case AccountSpinnerAdapter.TYPE_ACCOUNT:
                 // Get the capabilities associated with this account.