Making UI.LIST_STARRED_ACTION and UI.LIST_FREQUENT_ACTION work with loaders/fragments.

Change-Id: I14dd9466068f6a5768494b6befd9ee374fb49e6c
diff --git a/src/com/android/contacts/ContactsListActivity.java b/src/com/android/contacts/ContactsListActivity.java
index 7ee3908..08c55ef 100644
--- a/src/com/android/contacts/ContactsListActivity.java
+++ b/src/com/android/contacts/ContactsListActivity.java
@@ -467,7 +467,6 @@
             case MODE_CUSTOM:
             case MODE_INSERT_OR_EDIT_CONTACT:
             case MODE_QUERY_PICK_TO_EDIT:
-            case MODE_FREQUENT:
             case MODE_QUERY: {
                 DefaultContactBrowseListFragment fragment = new DefaultContactBrowseListFragment();
                 if (!mSearchMode) {
@@ -551,9 +550,21 @@
                 mListFragment = fragment;
                 break;
             }
+            case MODE_FREQUENT:
+            case MODE_STARRED:
             case MODE_STREQUENT: {
                 StrequentContactListFragment fragment = new StrequentContactListFragment();
-                fragment.setSectionHeaderDisplayEnabled(false);
+                if (mMode == MODE_FREQUENT) {
+                    fragment.setFrequentlyContactedContactsIncluded(true);
+                    fragment.setStarredContactsIncluded(false);
+                } else if (mMode == MODE_STARRED) {
+                    fragment.setFrequentlyContactedContactsIncluded(false);
+                    fragment.setStarredContactsIncluded(true);
+                } else {
+                    fragment.setFrequentlyContactedContactsIncluded(true);
+                    fragment.setStarredContactsIncluded(true);
+                }
+
                 fragment.setOnContactListActionListener(new OnContactBrowserActionListener() {
                     public void onSearchAllContactsAction(String string) {
                         doSearch();
diff --git a/src/com/android/contacts/list/StrequentContactListAdapter.java b/src/com/android/contacts/list/StrequentContactListAdapter.java
index cef0604..1fa4077 100644
--- a/src/com/android/contacts/list/StrequentContactListAdapter.java
+++ b/src/com/android/contacts/list/StrequentContactListAdapter.java
@@ -39,6 +39,8 @@
     private TextView mSeparatorView;
     private OnClickListener mCallButtonListener;
     private int mCallButtonId;
+    private boolean mStarredContactsIncluded;
+    private boolean mFrequentlyContactedContactsIncluded;
 
     public StrequentContactListAdapter(Context context, int callButtonId) {
         super(context);
@@ -49,15 +51,35 @@
         mCallButtonListener = callButtonListener;
     }
 
+    public void setStarredContactsIncluded(boolean flag) {
+        mStarredContactsIncluded = flag;
+    }
+
+    public void setFrequentlyContactedContactsIncluded(boolean flag) {
+        mFrequentlyContactedContactsIncluded = flag;
+    }
+
     @Override
     public void configureLoader(CursorLoader loader) {
-        loader.setUri(Contacts.CONTENT_STREQUENT_URI);
-        loader.setProjection(PROJECTION);
-        if (getSortOrder() == ContactsContract.Preferences.SORT_ORDER_PRIMARY) {
-            loader.setSortOrder(Contacts.SORT_KEY_PRIMARY);
+        String sortOrder = getSortOrder() == ContactsContract.Preferences.SORT_ORDER_PRIMARY
+                ? Contacts.SORT_KEY_PRIMARY
+                : Contacts.SORT_KEY_ALTERNATIVE;
+        if (mStarredContactsIncluded && mFrequentlyContactedContactsIncluded) {
+            loader.setUri(Contacts.CONTENT_STREQUENT_URI);
+        } else if (mStarredContactsIncluded) {
+            loader.setUri(Contacts.CONTENT_URI);
+            loader.setSelection(Contacts.STARRED + "!=0");
+        } else if (mFrequentlyContactedContactsIncluded) {
+            loader.setUri(Contacts.CONTENT_URI);
+            loader.setSelection(Contacts.TIMES_CONTACTED + " > 0");
+            sortOrder = Contacts.TIMES_CONTACTED + " DESC";
         } else {
-            loader.setSortOrder(Contacts.SORT_KEY_ALTERNATIVE);
+            throw new UnsupportedOperationException("Neither StarredContactsIncluded nor "
+                    + "FrequentlyContactedContactsIncluded is set");
         }
+
+        loader.setProjection(PROJECTION);
+        loader.setSortOrder(sortOrder);
     }
 
     @Override
@@ -66,17 +88,20 @@
 
         // Get the split between starred and frequent items, if the mode is strequent
         mFrequentSeparatorPos = ListView.INVALID_POSITION;
-        int count = 0;
-        if (cursor != null && (count = cursor.getCount()) > 0) {
-            cursor.moveToPosition(-1);
-            for (int i = 0; cursor.moveToNext(); i++) {
-                int starred = cursor.getInt(CONTACT_STARRED_COLUMN_INDEX);
-                if (starred == 0) {
-                    if (i > 0) {
-                        // Only add the separator when there are starred items present
-                        mFrequentSeparatorPos = i;
+
+        if (mStarredContactsIncluded && mFrequentlyContactedContactsIncluded) {
+            int count = 0;
+            if (cursor != null && (count = cursor.getCount()) > 0) {
+                cursor.moveToPosition(-1);
+                for (int i = 0; cursor.moveToNext(); i++) {
+                    int starred = cursor.getInt(CONTACT_STARRED_COLUMN_INDEX);
+                    if (starred == 0) {
+                        if (i > 0) {
+                            // Only add the separator when there are starred items present
+                            mFrequentSeparatorPos = i;
+                        }
+                        break;
                     }
-                    break;
                 }
             }
         }
@@ -104,7 +129,8 @@
 
     @Override
     public Object getItem(int position) {
-        if (position < mFrequentSeparatorPos) {
+        if (mFrequentSeparatorPos == ListView.INVALID_POSITION
+                || position < mFrequentSeparatorPos) {
             return super.getItem(position);
         } else {
             return super.getItem(position - 1);
@@ -113,7 +139,8 @@
 
     @Override
     public long getItemId(int position) {
-        if (position < mFrequentSeparatorPos) {
+        if (mFrequentSeparatorPos == ListView.INVALID_POSITION
+                || position < mFrequentSeparatorPos) {
             return super.getItemId(position);
         } else {
             return super.getItemId(position - 1);
@@ -131,7 +158,8 @@
 
     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
-        if (position < mFrequentSeparatorPos) {
+        if (mFrequentSeparatorPos == ListView.INVALID_POSITION
+                || position < mFrequentSeparatorPos) {
             return super.getView(position, convertView, parent);
         } else if (position == mFrequentSeparatorPos) {
             if (mSeparatorView == null) {
diff --git a/src/com/android/contacts/list/StrequentContactListFragment.java b/src/com/android/contacts/list/StrequentContactListFragment.java
index be330ec..9779d10 100644
--- a/src/com/android/contacts/list/StrequentContactListFragment.java
+++ b/src/com/android/contacts/list/StrequentContactListFragment.java
@@ -30,6 +30,24 @@
 
     private static final int CALL_BUTTON_ID = android.R.id.button1;
 
+    private boolean mStarredContactsIncluded = true;
+    private boolean mFrequentlyContactedContactsIncluded = true;
+
+    public StrequentContactListFragment() {
+        setSectionHeaderDisplayEnabled(false);
+        setPhotoLoaderEnabled(true);
+    }
+
+    public void setStarredContactsIncluded(boolean flag) {
+        mStarredContactsIncluded = flag;
+        configureAdapter();
+    }
+
+    public void setFrequentlyContactedContactsIncluded(boolean flag) {
+        mFrequentlyContactedContactsIncluded = flag;
+        configureAdapter();
+    }
+
     @Override
     protected void onItemClick(int position, long id) {
         ContactListAdapter adapter = getAdapter();
@@ -50,6 +68,17 @@
     }
 
     @Override
+    protected void configureAdapter() {
+        super.configureAdapter();
+
+        StrequentContactListAdapter adapter = (StrequentContactListAdapter)getAdapter();
+        if (adapter != null) {
+            adapter.setStarredContactsIncluded(mStarredContactsIncluded);
+            adapter.setFrequentlyContactedContactsIncluded(mFrequentlyContactedContactsIncluded);
+        }
+    }
+
+    @Override
     protected View inflateView(LayoutInflater inflater, ViewGroup container) {
         return inflater.inflate(R.layout.contacts_list_content, null);
     }