1/*
2 * Copyright (C) 2010 The Android Open Source Project
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 */
16package com.android.contacts.list;
17
18import com.android.contacts.R;
19
20import android.app.Activity;
21import android.app.LoaderManager.LoaderCallbacks;
22import android.content.ContentUris;
23import android.content.CursorLoader;
24import android.content.Intent;
25import android.content.Loader;
26import android.database.Cursor;
27import android.os.Bundle;
28import android.provider.ContactsContract.Contacts;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.TextView;
33
34/**
35 * Fragment for the Join Contact list.
36 */
37public class JoinContactListFragment extends ContactEntryListFragment<JoinContactListAdapter> {
38
39    private static final int DISPLAY_NAME_LOADER = -2;
40
41    private static final String KEY_TARGET_CONTACT_ID = "targetContactId";
42
43    private OnContactPickerActionListener mListener;
44    private long mTargetContactId;
45
46    private final LoaderCallbacks<Cursor> mLoaderCallbacks = new LoaderCallbacks<Cursor>() {
47
48        @Override
49        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
50            switch (id) {
51                case DISPLAY_NAME_LOADER: {
52                    // Loader for the display name of the target contact
53                    return new CursorLoader(getActivity(),
54                            ContentUris.withAppendedId(Contacts.CONTENT_URI, mTargetContactId),
55                            new String[] { Contacts.DISPLAY_NAME }, null, null, null);
56                }
57                case JoinContactListAdapter.PARTITION_ALL_CONTACTS: {
58                    JoinContactLoader loader = new JoinContactLoader(getActivity());
59                    JoinContactListAdapter adapter = getAdapter();
60                    if (adapter != null) {
61                        adapter.configureLoader(loader, 0);
62                    }
63                    return loader;
64                }
65            }
66            throw new IllegalArgumentException("No loader for ID=" + id);
67        }
68
69        @Override
70        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
71            switch (loader.getId()) {
72                case DISPLAY_NAME_LOADER: {
73                    if (data != null && data.moveToFirst()) {
74                        showTargetContactName(data.getString(0));
75                    }
76                    break;
77                }
78                case JoinContactListAdapter.PARTITION_ALL_CONTACTS: {
79                    Cursor suggestionsCursor = ((JoinContactLoader) loader).getSuggestionsCursor();
80                    onContactListLoaded(suggestionsCursor, data);
81                    break;
82                }
83            }
84        }
85
86        public void onLoaderReset(Loader<Cursor> loader) {
87        }
88    };
89
90    public JoinContactListFragment() {
91        setPhotoLoaderEnabled(true);
92        setSectionHeaderDisplayEnabled(true);
93        setVisibleScrollbarEnabled(false);
94        setQuickContactEnabled(false);
95    }
96
97    public void setOnContactPickerActionListener(OnContactPickerActionListener listener) {
98        mListener = listener;
99    }
100
101    @Override
102    protected void startLoading() {
103        configureAdapter();
104
105        getLoaderManager().initLoader(DISPLAY_NAME_LOADER, null, mLoaderCallbacks);
106        getLoaderManager().initLoader(JoinContactListAdapter.PARTITION_ALL_CONTACTS,
107                null, mLoaderCallbacks);
108    }
109
110    private void onContactListLoaded(Cursor suggestionsCursor, Cursor allContactsCursor) {
111        JoinContactListAdapter adapter = getAdapter();
112        adapter.setSuggestionsCursor(suggestionsCursor);
113        setVisibleScrollbarEnabled(true);
114        onPartitionLoaded(JoinContactListAdapter.PARTITION_ALL_CONTACTS, allContactsCursor);
115    }
116
117    private void showTargetContactName(String displayName) {
118        Activity activity = getActivity();
119        TextView blurbView = (TextView) activity.findViewById(R.id.join_contact_blurb);
120        String blurb = activity.getString(R.string.blurbJoinContactDataWith, displayName);
121        blurbView.setText(blurb);
122    }
123
124    public void setTargetContactId(long targetContactId) {
125        mTargetContactId = targetContactId;
126    }
127
128    @Override
129    public JoinContactListAdapter createListAdapter() {
130        return new JoinContactListAdapter(getActivity());
131    }
132
133    @Override
134    protected void configureAdapter() {
135        super.configureAdapter();
136        JoinContactListAdapter adapter = getAdapter();
137        adapter.setTargetContactId(mTargetContactId);
138    }
139
140    @Override
141    protected View inflateView(LayoutInflater inflater, ViewGroup container) {
142        return inflater.inflate(R.layout.join_contact_picker_list_content, null);
143    }
144
145    @Override
146    protected void onItemClick(int position, long id) {
147        JoinContactListAdapter adapter = getAdapter();
148        int partition = adapter.getPartitionForPosition(position);
149        mListener.onPickContactAction(adapter.getContactUri(position));
150    }
151
152    @Override
153    public void onPickerResult(Intent data) {
154        mListener.onPickContactAction(data.getData());
155    }
156
157    @Override
158    public void onSaveInstanceState(Bundle outState) {
159        super.onSaveInstanceState(outState);
160        outState.putLong(KEY_TARGET_CONTACT_ID, mTargetContactId);
161    }
162
163    @Override
164    public void restoreSavedState(Bundle savedState) {
165        super.restoreSavedState(savedState);
166        if (savedState != null) {
167            mTargetContactId = savedState.getLong(KEY_TARGET_CONTACT_ID);
168        }
169    }
170}
171