1 /*
2  * Copyright (C) 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.loaderapp.fragments;
18 
19 import com.android.loaderapp.CursorFactoryListAdapter;
20 import com.android.loaderapp.R;
21 import com.android.loaderapp.CursorFactoryListAdapter.ResourceViewFactory;
22 import com.android.loaderapp.model.GroupsListLoader;
23 
24 import android.app.LoaderManagingFragment;
25 import android.content.Loader;
26 import android.database.Cursor;
27 import android.database.MatrixCursor;
28 import android.database.MergeCursor;
29 import android.os.Bundle;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.AdapterView;
34 import android.widget.ListView;
35 import android.widget.AdapterView.OnItemClickListener;
36 
37 public class GroupsListFragment extends LoaderManagingFragment<Cursor>
38         implements OnItemClickListener {
39     private static final long GROUP_ID_ALL_CONTACTS = -1;
40     private static final long GROUP_ID_FAVORITES = -2;
41 
42     private static final int LOADER_GROUPS = 0;
43 
44     Controller mController;
45     ListView mList;
46     CursorFactoryListAdapter mAdapter;
47 
48     public interface Controller {
onAllContactsSelected()49         public void onAllContactsSelected();
onFavoritesSelected()50         public void onFavoritesSelected();
onGroupSelected(String name)51         public void onGroupSelected(String name);
52     }
53 
setController(Controller controller)54     public void setController(Controller controller) {
55         mController = controller;
56     }
57 
58     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState)59     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
60         ListView list = (ListView) inflater.inflate(R.layout.contacts_list, container, false);
61         list.setOnItemClickListener(this);
62         mAdapter = new CursorFactoryListAdapter(getActivity(),
63                 new ResourceViewFactory(R.layout.xlarge_list_item));
64         list.setAdapter(mAdapter);
65         mList = list;
66         return list;
67     }
68 
69     @Override
onInitializeLoaders()70     public void onInitializeLoaders() {
71         startLoading(LOADER_GROUPS, null);
72     }
73 
74     @Override
onCreateLoader(int id, Bundle args)75     protected Loader onCreateLoader(int id, Bundle args) {
76         switch (id) {
77             case LOADER_GROUPS: {
78                 return new GroupsListLoader(getActivity());
79             }
80         }
81 
82         return null;
83     }
84 
85     @Override
onLoadFinished(Loader loader, Cursor data)86     public void onLoadFinished(Loader loader, Cursor data) {
87         switch (loader.getId()) {
88             case LOADER_GROUPS: {
89                 setData(data);
90                 break;
91             }
92         }
93     }
94 
setData(Cursor groups)95     private void setData(Cursor groups) {
96         MatrixCursor psuedoGroups = new MatrixCursor(new String[] { "_id", "name" });
97         psuedoGroups.newRow().add(-1).add("All Contacts");
98         psuedoGroups.newRow().add(-2).add("Favorites");
99         mAdapter.changeCursor(new MergeCursor(new Cursor[] { psuedoGroups, groups }));
100     }
101 
onItemClick(AdapterView<?> parent, View view, int position, long id)102     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
103         if (id == GROUP_ID_ALL_CONTACTS) {
104             mController.onAllContactsSelected();
105         } else if (id == GROUP_ID_FAVORITES) {
106             mController.onFavoritesSelected();
107         } else {
108             Cursor cursor = (Cursor) mAdapter.getItem(position);
109             mController.onGroupSelected(cursor.getString(GroupsListLoader.COLUMN_TITLE));
110         }
111     }
112 }