Add groups tab and show list of groups

- New groups activity, fragment, and adapter classes,
as well as and associated XML layouts
- For phone only, tablet UI is unaffected

Bug: 4409350
Change-Id: Ibeb592142e5ddc1efa5701472bbc72bde11d9760
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 83331da..ed03e77 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -336,6 +336,22 @@
             </intent-filter>
         </activity>
 
+
+        <!-- List of groups -->
+        <activity android:name=".activities.GroupBrowserActivity"
+            android:label="@string/contactsGroupsLabel"
+            android:theme="@style/ContactBrowserTheme"
+            android:launchMode="singleTop"
+            android:clearTaskOnLaunch="true">
+            <!-- TODO: Remove this temporary intent action name when the fragmentization
+                 work is done. -->
+            <intent-filter>
+                <action android:name="com.android.phone.action.GROUPS_LIST" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.TAB" />
+            </intent-filter>
+        </activity>
+
         <!-- Used to show QuickContact window over a translucent activity, which is a
              temporary hack until we add better framework support. -->
         <activity
diff --git a/res/layout/group_browse_list_fragment.xml b/res/layout/group_browse_list_fragment.xml
new file mode 100644
index 0000000..50c02c8
--- /dev/null
+++ b/res/layout/group_browse_list_fragment.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical">
+
+    <ListView
+      android:id="@+id/list"
+      android:layout_width="match_parent"
+      android:layout_height="0dip"
+      android:fastScrollEnabled="true"
+      android:scrollbarStyle="outsideOverlay"
+      android:layout_weight="1" />
+
+   <TextView
+     android:id="@+id/empty"
+     android:layout_width="match_parent"
+     android:layout_height="match_parent"
+     android:gravity="center"
+     android:text="@string/noGroups"
+     android:visibility="gone"/>
+
+</LinearLayout>
diff --git a/res/layout/group_browse_list_item.xml b/res/layout/group_browse_list_item.xml
new file mode 100644
index 0000000..accbedd
--- /dev/null
+++ b/res/layout/group_browse_list_item.xml
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="horizontal"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content">
+
+    <ImageView
+        android:id="@+id/icon"
+        android:scaleType="center"
+        android:layout_width="wrap_content"
+        android:layout_height="match_parent"
+        android:layout_marginLeft="10dip"
+        android:layout_marginRight="10dip"
+        android:layout_gravity="center_vertical" />
+
+    <LinearLayout
+        android:orientation="vertical"
+        android:layout_width="wrap_content"
+        android:layout_height="match_parent"
+        android:paddingTop="5dip"
+        android:paddingBottom="5dip">
+
+        <TextView
+            android:id="@+id/label"
+            android:layout_height="wrap_content"
+            android:layout_width="wrap_content"
+            android:gravity="center_vertical"
+            android:textAppearance="?android:attr/textAppearanceLarge"
+            android:ellipsize="end"
+            android:singleLine="true" />
+
+        <TextView
+            android:id="@+id/account"
+            android:layout_height="wrap_content"
+            android:layout_width="wrap_content"
+            android:gravity="center_vertical"
+            android:textAppearance="?android:attr/textAppearanceSmall"
+            android:textColor="?android:attr/textColorSecondary"
+            android:ellipsize="end"
+            android:singleLine="true"
+            android:textStyle="italic" />
+
+    </LinearLayout>
+
+</LinearLayout>
diff --git a/res/layout/group_browser_activity.xml b/res/layout/group_browser_activity.xml
new file mode 100644
index 0000000..f7187d9
--- /dev/null
+++ b/res/layout/group_browser_activity.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+    <fragment
+        android:id="@+id/list_fragment"
+        class="com.android.contacts.group.GroupBrowseListFragment"
+        android:layout_height="match_parent"
+        android:layout_width="match_parent"
+    />
+
+</FrameLayout>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index b531431..5605d25 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -264,6 +264,9 @@
     <!-- The text displayed when the contacts list is empty while displaying all contacts -->
     <string name="noContacts">No contacts.</string>
 
+    <!-- The text displayed when the groups list is empty while displaying all groups [CHAR LIMIT=NONE] -->
+    <string name="noGroups">No groups.</string>
+
     <!-- The text displayed when the contacts list is empty while displaying results after searching contacts -->
     <string name="noMatchingContacts">No matching contacts found.</string>
 
@@ -349,6 +352,9 @@
     <!-- The description text for the contacts tab. Space is limited for this string, so the shorter the better -->
     <string name="contactsIconLabel">Contacts</string>
 
+    <!-- The description text for the groups tab. Space is limited for this string, so the shorter the better -->
+    <string name="contactsGroupsLabel">Groups</string>
+
     <!-- The description text for the favorites tab. Space is limited for this string, so the shorter the better -->
     <string name="contactsFavoritesLabel">Favorites</string>
 
diff --git a/src/com/android/contacts/activities/DialtactsActivity.java b/src/com/android/contacts/activities/DialtactsActivity.java
index 5054309..55106ef 100644
--- a/src/com/android/contacts/activities/DialtactsActivity.java
+++ b/src/com/android/contacts/activities/DialtactsActivity.java
@@ -91,6 +91,7 @@
         setupCallLogTab();
         setupContactsTab();
         setupFavoritesTab();
+        setupGroupsTab();
 
         // Load the last manually loaded tab
         final SharedPreferences prefs = getSharedPreferences(PREFS_DIALTACTS, MODE_PRIVATE);
@@ -172,6 +173,18 @@
                 .setContent(intent));
     }
 
+    private void setupGroupsTab() {
+        // This is a temporary intent action until the refactoring for the phone/contacts
+        // split is complete.
+        Intent intent = new Intent("com.android.phone.action.GROUPS_LIST");
+                        intent.setClass(this, GroupBrowserActivity.class);
+
+        mTabHost.addTab(mTabHost.newTabSpec("groups")
+                .setIndicator(getString(R.string.contactsGroupsLabel),
+                        getResources().getDrawable(R.drawable.ic_menu_display_all_holo_light))
+                .setContent(intent));
+    }
+
     /**
      * Returns true if the intent is due to hitting the green send key while in a call.
      *
diff --git a/src/com/android/contacts/activities/GroupBrowserActivity.java b/src/com/android/contacts/activities/GroupBrowserActivity.java
new file mode 100644
index 0000000..e8b3ad8
--- /dev/null
+++ b/src/com/android/contacts/activities/GroupBrowserActivity.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts.activities;
+
+import com.android.contacts.ContactsActivity;
+import com.android.contacts.R;
+
+import android.content.Intent;
+import android.os.Bundle;
+
+/**
+ * Displays a list to browse groups.
+ */
+public class GroupBrowserActivity extends ContactsActivity {
+
+    private static final String TAG = "GroupBrowserActivity";
+
+    public GroupBrowserActivity() {
+    }
+
+    @Override
+    protected void onCreate(Bundle savedState) {
+        super.onCreate(savedState);
+        configureContentView(true, savedState);
+    }
+
+    @Override
+    protected void onNewIntent(Intent intent) {
+        setIntent(intent);
+        configureContentView(false, null);
+    }
+
+    private void configureContentView(boolean createContentView, Bundle savedState) {
+        // TODO: Create Intent Resolver to handle the different ways users can get to this list.
+        // TODO: Use savedState if necessary
+        // TODO: Setup action bar
+        if (createContentView) {
+            setContentView(R.layout.group_browser_activity);
+        }
+    }
+}
diff --git a/src/com/android/contacts/group/GroupBrowseListAdapter.java b/src/com/android/contacts/group/GroupBrowseListAdapter.java
new file mode 100644
index 0000000..ed69776
--- /dev/null
+++ b/src/com/android/contacts/group/GroupBrowseListAdapter.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts.group;
+
+import com.android.contacts.R;
+import com.android.contacts.GroupMetaData;
+
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.ImageView;
+import android.widget.TextView;
+
+import java.util.List;
+
+/**
+ * Adapter to populate the list of groups.
+ */
+public class GroupBrowseListAdapter extends BaseAdapter {
+
+    private LayoutInflater mLayoutInflater;
+    private List<GroupMetaData> mGroupList;
+
+    public GroupBrowseListAdapter(Context context, List<GroupMetaData> groupList) {
+        mLayoutInflater = LayoutInflater.from(context);
+        mGroupList = groupList;
+    }
+
+    @Override
+    public int getCount() {
+        return mGroupList.size();
+    }
+
+    @Override
+    public long getItemId(int position) {
+        return getItem(position).getGroupId();
+    }
+
+    @Override
+    public GroupMetaData getItem(int position) {
+        return mGroupList.get(position);
+    }
+
+    @Override
+    public View getView(int position, View convertView, ViewGroup parent) {
+        if (convertView == null) {
+            convertView = mLayoutInflater.inflate(R.layout.group_browse_list_item, parent, false);
+        }
+        GroupMetaData group = getItem(position);
+        ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
+        TextView label = (TextView) convertView.findViewById(R.id.label);
+        TextView account = (TextView) convertView.findViewById(R.id.account);
+        icon.setImageResource(R.drawable.ic_menu_display_all_holo_light);
+        label.setText(group.getTitle());
+        account.setText(group.getAccountName());
+        return convertView;
+    }
+
+}
\ No newline at end of file
diff --git a/src/com/android/contacts/group/GroupBrowseListFragment.java b/src/com/android/contacts/group/GroupBrowseListFragment.java
new file mode 100644
index 0000000..978ce13
--- /dev/null
+++ b/src/com/android/contacts/group/GroupBrowseListFragment.java
@@ -0,0 +1,172 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts.group;
+
+import com.android.contacts.GroupMetaData;
+import com.android.contacts.GroupMetaDataLoader;
+import com.android.contacts.R;
+
+import android.app.Activity;
+import android.app.Fragment;
+import android.app.LoaderManager;
+import android.app.LoaderManager.LoaderCallbacks;
+import android.content.Context;
+import android.content.CursorLoader;
+import android.content.Loader;
+import android.database.Cursor;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.MotionEvent;
+import android.view.View;
+import android.view.View.OnFocusChangeListener;
+import android.view.View.OnTouchListener;
+import android.view.ViewGroup;
+import android.view.inputmethod.InputMethodManager;
+import android.widget.ListView;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Fragment to display the list of groups.
+ */
+public class GroupBrowseListFragment extends Fragment
+        implements OnFocusChangeListener, OnTouchListener {
+
+    private static final String TAG = "GroupBrowseListFragment";
+
+    private static final int LOADER_GROUPS = 1;
+
+    private Context mContext;
+    private Cursor mGroupListCursor;
+    private List<GroupMetaData> mGroupList = new ArrayList<GroupMetaData>();
+
+    private View mRootView;
+    private ListView mListView;
+    private View mEmptyView;
+
+    public GroupBrowseListFragment() {
+    }
+
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+            Bundle savedInstanceState) {
+        mRootView = inflater.inflate(R.layout.group_browse_list_fragment, null);
+        mListView = (ListView) mRootView.findViewById(R.id.list);
+        mListView.setOnFocusChangeListener(this);
+        mListView.setOnTouchListener(this);
+        mEmptyView = mRootView.findViewById(R.id.empty);
+        return mRootView;
+    }
+
+    @Override
+    public void onAttach(Activity activity) {
+        super.onAttach(activity);
+        mContext = activity;
+    }
+
+    @Override
+    public void onDetach() {
+        super.onDetach();
+        mContext = null;
+    }
+
+    @Override
+    public void onStart() {
+        getLoaderManager().initLoader(LOADER_GROUPS, null, mGroupLoaderListener);
+        super.onStart();
+    }
+
+    /**
+     * The listener for the group meta data loader.
+     */
+    private final LoaderManager.LoaderCallbacks<Cursor> mGroupLoaderListener =
+            new LoaderCallbacks<Cursor>() {
+
+        @Override
+        public CursorLoader onCreateLoader(int id, Bundle args) {
+            return new GroupMetaDataLoader(mContext);
+        }
+
+        @Override
+        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
+            mGroupListCursor = data;
+            bindGroupList();
+        }
+
+        public void onLoaderReset(Loader<Cursor> loader) {
+        }
+    };
+
+    private void bindGroupList() {
+        if (mGroupListCursor == null) {
+            return;
+        }
+        mGroupList.clear();
+        mGroupListCursor.moveToPosition(-1);
+        while (mGroupListCursor.moveToNext()) {
+            String accountName = mGroupListCursor.getString(GroupMetaDataLoader.ACCOUNT_NAME);
+            String accountType = mGroupListCursor.getString(GroupMetaDataLoader.ACCOUNT_TYPE);
+            long groupId = mGroupListCursor.getLong(GroupMetaDataLoader.GROUP_ID);
+            String title = mGroupListCursor.getString(GroupMetaDataLoader.TITLE);
+            boolean defaultGroup = mGroupListCursor.isNull(GroupMetaDataLoader.AUTO_ADD)
+                    ? false
+                    : mGroupListCursor.getInt(GroupMetaDataLoader.AUTO_ADD) != 0;
+            boolean favorites = mGroupListCursor.isNull(GroupMetaDataLoader.FAVORITES)
+                    ? false
+                    : mGroupListCursor.getInt(GroupMetaDataLoader.FAVORITES) != 0;
+
+            // TODO: Separate groups according to account name and type.
+            mGroupList.add(new GroupMetaData(
+                    accountName, accountType, groupId, title, defaultGroup, favorites));
+        }
+
+        mListView.setAdapter(new GroupBrowseListAdapter(mContext, mGroupList));
+        mListView.setEmptyView(mEmptyView);
+    }
+
+    private void hideSoftKeyboard() {
+        if (mContext == null) {
+            return;
+        }
+        // Hide soft keyboard, if visible
+        InputMethodManager inputMethodManager = (InputMethodManager)
+                mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
+        inputMethodManager.hideSoftInputFromWindow(mListView.getWindowToken(), 0);
+    }
+
+    /**
+     * Dismisses the soft keyboard when the list takes focus.
+     */
+    @Override
+    public void onFocusChange(View view, boolean hasFocus) {
+        if (view == mListView && hasFocus) {
+            hideSoftKeyboard();
+        }
+    }
+
+    /**
+     * Dismisses the soft keyboard when the list is touched.
+     */
+    @Override
+    public boolean onTouch(View view, MotionEvent event) {
+        if (view == mListView) {
+            hideSoftKeyboard();
+        }
+        return false;
+    }
+}
diff --git a/src/com/android/contacts/list/ContactEntryListFragment.java b/src/com/android/contacts/list/ContactEntryListFragment.java
index c85f582..fcced62 100644
--- a/src/com/android/contacts/list/ContactEntryListFragment.java
+++ b/src/com/android/contacts/list/ContactEntryListFragment.java
@@ -821,6 +821,7 @@
         }
     }
 
+    @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         hideSoftKeyboard();
 
@@ -840,6 +841,7 @@
     /**
      * Dismisses the soft keyboard when the list takes focus.
      */
+    @Override
     public void onFocusChange(View view, boolean hasFocus) {
         if (view == mListView && hasFocus) {
             hideSoftKeyboard();
@@ -849,6 +851,7 @@
     /**
      * Dismisses the soft keyboard when the list is touched.
      */
+    @Override
     public boolean onTouch(View view, MotionEvent event) {
         if (view == mListView) {
             hideSoftKeyboard();