Show the selected account first in account switcher

Bug: 4584712
Change-Id: Ib041d7aa3cca3fa8f5b4780eb7a76c5e41819b39
diff --git a/src/com/android/contacts/editor/ContactEditorFragment.java b/src/com/android/contacts/editor/ContactEditorFragment.java
index 8cab92a..ba2794d 100644
--- a/src/com/android/contacts/editor/ContactEditorFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorFragment.java
@@ -670,7 +670,8 @@
             @Override
             public void onClick(View v) {
                 final ListPopupWindow popup = new ListPopupWindow(mContext, null);
-                final AccountsListAdapter adapter = new AccountsListAdapter(mContext, true);
+                final AccountsListAdapter adapter =
+                        new AccountsListAdapter(mContext, true, currentAccount);
                 popup.setWidth(anchorView.getWidth());
                 popup.setAnchorView(anchorView);
                 popup.setAdapter(adapter);
diff --git a/src/com/android/contacts/util/AccountsListAdapter.java b/src/com/android/contacts/util/AccountsListAdapter.java
index 97a9f84..1a8b3ea 100644
--- a/src/com/android/contacts/util/AccountsListAdapter.java
+++ b/src/com/android/contacts/util/AccountsListAdapter.java
@@ -17,8 +17,8 @@
 package com.android.contacts.util;
 
 import com.android.contacts.R;
-import com.android.contacts.model.AccountTypeManager;
 import com.android.contacts.model.AccountType;
+import com.android.contacts.model.AccountTypeManager;
 
 import android.accounts.Account;
 import android.content.Context;
@@ -29,6 +29,7 @@
 import android.widget.ImageView;
 import android.widget.TextView;
 
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -41,9 +42,25 @@
     private final Context mContext;
 
     public AccountsListAdapter(Context context, boolean writableOnly) {
+        this(context, writableOnly, null);
+    }
+
+    /**
+     * @param currentAccount the Account currently selected by the user, which should come
+     * first in the list. Can be null.
+     */
+    public AccountsListAdapter(Context context, boolean writableOnly,
+            Account currentAccount) {
         mContext = context;
         mAccountTypes = AccountTypeManager.getInstance(context);
-        mAccounts = mAccountTypes.getAccounts(writableOnly);
+        // We don't want possible side-effect toward AccountTypeManager
+        mAccounts = new ArrayList<Account>(mAccountTypes.getAccounts(writableOnly));
+        if (currentAccount != null
+                && !mAccounts.isEmpty()
+                && !mAccounts.get(0).equals(currentAccount)
+                && mAccounts.remove(currentAccount)) {
+            mAccounts.add(0, currentAccount);
+        }
         mInflater = LayoutInflater.from(context);
     }