Merge "[DO NOT MERGE] Remove method to load groups" into ub-contactsdialer-i-dev
diff --git a/src/com/android/contacts/list/DefaultContactBrowseListFragment.java b/src/com/android/contacts/list/DefaultContactBrowseListFragment.java
index 458c223..f4d244e 100644
--- a/src/com/android/contacts/list/DefaultContactBrowseListFragment.java
+++ b/src/com/android/contacts/list/DefaultContactBrowseListFragment.java
@@ -94,6 +94,7 @@
     private static final String TAG = "DefaultListFragment";
     private static final String ENABLE_DEBUG_OPTIONS_HIDDEN_CODE = "debug debug!";
     private static final String KEY_DELETION_IN_PROGRESS = "deletionInProgress";
+    private static final String KEY_SEARCH_RESULT_CLICKED = "search_result_clicked";
 
     private static final int ACTIVITY_REQUEST_CODE_SHARE = 0;
 
@@ -149,6 +150,8 @@
      */
     private boolean mDisableOptionItemSelected;
 
+    private boolean mSearchResultClicked;
+
     private ActionBarAdapter mActionBarAdapter;
     private PeopleActivity mActivity;
     private ContactsRequest mContactsRequest;
@@ -250,6 +253,22 @@
         setHasOptionsMenu(true);
     }
 
+    /**
+     * Whether a search result was clicked by the user. Tracked so that we can distinguish
+     * between exiting the search mode after a result was clicked from exiting w/o clicking
+     * any search result.
+     */
+    public boolean wasSearchResultClicked() {
+        return mSearchResultClicked;
+    }
+
+    /**
+     * Resets whether a search result was clicked by the user to false.
+     */
+    public void resetSearchResultClicked() {
+        mSearchResultClicked = false;
+    }
+
     @Override
     public CursorLoader createCursorLoader(Context context) {
         return new FavoritesAndContactsLoader(context);
@@ -371,6 +390,11 @@
         if (getAdapter().isDisplayingCheckBoxes()) {
             super.onItemClick(position, id);
             return;
+        } else {
+            if (isSearchMode()) {
+                mSearchResultClicked = true;
+                Logger.logSearchEvent(createSearchStateForSearchResultClick(position));
+            }
         }
         viewContact(position, uri, getAdapter().isEnterpriseContact(position));
     }
@@ -648,8 +672,11 @@
 
         setCheckBoxListListener(new CheckBoxListListener());
         setOnContactListActionListener(new ContactBrowserActionListener());
-        if (savedInstanceState != null && savedInstanceState.getBoolean(KEY_DELETION_IN_PROGRESS)) {
-            deleteSelectedContacts();
+        if (savedInstanceState != null) {
+            if (savedInstanceState.getBoolean(KEY_DELETION_IN_PROGRESS)) {
+                deleteSelectedContacts();
+            }
+            mSearchResultClicked = savedInstanceState.getBoolean(KEY_SEARCH_RESULT_CLICKED);
         }
 
         setDirectorySearchMode();
@@ -1183,6 +1210,7 @@
         }
         mDisableOptionItemSelected = true;
         outState.putBoolean(KEY_DELETION_IN_PROGRESS, mIsDeletionInProgress);
+        outState.putBoolean(KEY_SEARCH_RESULT_CLICKED, mSearchResultClicked);
     }
 
     @Override
diff --git a/src/com/android/contacts/list/MultiSelectContactsListFragment.java b/src/com/android/contacts/list/MultiSelectContactsListFragment.java
index 67c937c..61e92b8 100644
--- a/src/com/android/contacts/list/MultiSelectContactsListFragment.java
+++ b/src/com/android/contacts/list/MultiSelectContactsListFragment.java
@@ -68,31 +68,12 @@
 
     private static final String EXTRA_KEY_SELECTED_CONTACTS = "selected_contacts";
 
-    private static final String KEY_SEARCH_RESULT_CLICKED = "search_result_clicked";
-
     private OnCheckBoxListActionListener mCheckBoxListListener;
-    private boolean mSearchResultClicked;
 
     public void setCheckBoxListListener(OnCheckBoxListActionListener checkBoxListListener) {
         mCheckBoxListListener = checkBoxListListener;
     }
 
-    /**
-     * Whether a search result was clicked by the user. Tracked so that we can distinguish
-     * between exiting the search mode after a result was clicked from exiting w/o clicking
-     * any search result.
-     */
-    public boolean wasSearchResultClicked() {
-        return mSearchResultClicked;
-    }
-
-    /**
-     * Resets whether a search result was clicked by the user to false.
-     */
-    public void resetSearchResultClicked() {
-        mSearchResultClicked = false;
-    }
-
     public void setAnimateOnLoad(boolean shouldAnimate) {
         mAnimateOnLoad = shouldAnimate;
     }
@@ -119,7 +100,6 @@
             final TreeSet<Long> selectedContactIds = (TreeSet<Long>)
                     savedInstanceState.getSerializable(EXTRA_KEY_SELECTED_CONTACTS);
             getAdapter().setSelectedContactIds(selectedContactIds);
-            mSearchResultClicked = savedInstanceState.getBoolean(KEY_SEARCH_RESULT_CLICKED);
         }
     }
 
@@ -149,7 +129,6 @@
     public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
         outState.putSerializable(EXTRA_KEY_SELECTED_CONTACTS, getSelectedContactIds());
-        outState.putBoolean(KEY_SEARCH_RESULT_CLICKED, mSearchResultClicked);
     }
 
     public void displayCheckBoxes(boolean displayCheckBoxes) {
@@ -204,11 +183,6 @@
         }
         if (getAdapter().isDisplayingCheckBoxes()) {
             getAdapter().toggleSelectionOfContactId(contactId);
-        } else {
-            if (isSearchMode()) {
-                mSearchResultClicked = true;
-                Logger.logSearchEvent(createSearchStateForSearchResultClick(position));
-            }
         }
         if (mCheckBoxListListener != null && getAdapter().getSelectedContactIds().size() == 0) {
             mCheckBoxListListener.onStopDisplayingCheckBoxes();
diff --git a/src/com/android/contacts/preference/DefaultAccountPreference.java b/src/com/android/contacts/preference/DefaultAccountPreference.java
index efff5a0..d43b8d5 100644
--- a/src/com/android/contacts/preference/DefaultAccountPreference.java
+++ b/src/com/android/contacts/preference/DefaultAccountPreference.java
@@ -23,7 +23,6 @@
 import android.util.AttributeSet;
 import android.view.View;
 
-import com.android.contacts.model.AccountTypeManager;
 import com.android.contacts.model.account.AccountInfo;
 import com.android.contacts.model.account.AccountWithDataSet;
 import com.android.contacts.util.AccountsListAdapter;
@@ -33,7 +32,6 @@
 public class DefaultAccountPreference extends DialogPreference {
     private ContactsPreferences mPreferences;
     private AccountsListAdapter mListAdapter;
-    private AccountTypeManager mAccountTypeManager;
     private List<AccountInfo> mAccounts;
     private int mChosenIndex = -1;
 
@@ -51,6 +49,7 @@
         mAccounts = accounts;
         if (mListAdapter != null) {
             mListAdapter.setAccounts(accounts, null);
+            notifyChanged();
         }
     }
 
@@ -66,7 +65,6 @@
         if (mAccounts != null) {
             mListAdapter.setAccounts(mAccounts, null);
         }
-        mAccountTypeManager = AccountTypeManager.getInstance(getContext());
     }
 
     @Override
@@ -77,11 +75,11 @@
     @Override
     public CharSequence getSummary() {
         final AccountWithDataSet defaultAccount = mPreferences.getDefaultAccount();
-        if (defaultAccount == null ||
-                !mAccountTypeManager.exists(defaultAccount)) {
+        if (defaultAccount == null || mAccounts == null ||
+                !AccountInfo.contains(mAccounts, defaultAccount)) {
             return null;
         } else {
-            return mAccountTypeManager.getAccountInfoForAccount(defaultAccount).getNameLabel();
+            return AccountInfo.getAccount(mAccounts, defaultAccount).getNameLabel();
         }
     }
 
diff --git a/src/com/android/contacts/preference/DisplayOptionsPreferenceFragment.java b/src/com/android/contacts/preference/DisplayOptionsPreferenceFragment.java
index e9c3a01..3f68d05 100644
--- a/src/com/android/contacts/preference/DisplayOptionsPreferenceFragment.java
+++ b/src/com/android/contacts/preference/DisplayOptionsPreferenceFragment.java
@@ -294,14 +294,9 @@
     @Override
     public void onAccountsLoaded(List<AccountInfo> accounts) {
         // Hide accounts preferences if no writable accounts exist
-        if (accounts.isEmpty()) {
-            getPreferenceScreen().removePreference(findPreference(KEY_DEFAULT_ACCOUNT));
-            getPreferenceScreen().removePreference(findPreference(KEY_CUSTOM_CONTACTS_FILTER));
-        } else {
-            final DefaultAccountPreference preference =
-                    (DefaultAccountPreference) findPreference(KEY_DEFAULT_ACCOUNT);
-            preference.setAccounts(accounts);
-        }
+        final DefaultAccountPreference preference =
+                (DefaultAccountPreference) findPreference(KEY_DEFAULT_ACCOUNT);
+        preference.setAccounts(accounts);
     }
 
     @Override