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
17package com.android.loaderapp.fragments;
18
19import com.android.loaderapp.CursorFactoryListAdapter;
20import com.android.loaderapp.R;
21import com.android.loaderapp.CursorFactoryListAdapter.ResourceViewFactory;
22import com.android.loaderapp.model.GroupsListLoader;
23
24import android.app.LoaderManagingFragment;
25import android.content.Loader;
26import android.database.Cursor;
27import android.database.MatrixCursor;
28import android.database.MergeCursor;
29import android.os.Bundle;
30import android.view.LayoutInflater;
31import android.view.View;
32import android.view.ViewGroup;
33import android.widget.AdapterView;
34import android.widget.ListView;
35import android.widget.AdapterView.OnItemClickListener;
36
37public 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 {
49        public void onAllContactsSelected();
50        public void onFavoritesSelected();
51        public void onGroupSelected(String name);
52    }
53
54    public void setController(Controller controller) {
55        mController = controller;
56    }
57
58    @Override
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
70    public void onInitializeLoaders() {
71        startLoading(LOADER_GROUPS, null);
72    }
73
74    @Override
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
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
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
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}