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 android.app.Activity;
19import android.app.LoaderManager.LoaderCallbacks;
20import android.content.ContentUris;
21import android.content.CursorLoader;
22import android.content.Intent;
23import android.content.Loader;
24import android.database.Cursor;
25import android.net.Uri;
26import android.os.Bundle;
27import android.provider.ContactsContract.Contacts;
28import android.text.TextUtils;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.TextView;
33
34import com.android.contacts.R;
35import com.android.contacts.list.JoinContactLoader.JoinContactLoaderResult;
36import com.android.contacts.logging.ListEvent;
37
38/**
39 * Fragment for the Join Contact list.
40 */
41public class JoinContactListFragment extends ContactEntryListFragment<JoinContactListAdapter> {
42
43    private static final int DISPLAY_NAME_LOADER = -2;
44
45    private static final String KEY_TARGET_CONTACT_ID = "targetContactId";
46
47    private OnContactPickerActionListener mListener;
48    private long mTargetContactId;
49
50    private final LoaderCallbacks<Cursor> mLoaderCallbacks = new LoaderCallbacks<Cursor>() {
51
52        @Override
53        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
54            switch (id) {
55                case DISPLAY_NAME_LOADER: {
56                    // Loader for the display name of the target contact
57                    return new CursorLoader(getActivity(),
58                            ContentUris.withAppendedId(Contacts.CONTENT_URI, mTargetContactId),
59                            new String[] { Contacts.DISPLAY_NAME }, null, null, null);
60                }
61                case JoinContactListAdapter.PARTITION_ALL_CONTACTS: {
62                    JoinContactLoader loader = new JoinContactLoader(getActivity());
63                    JoinContactListAdapter adapter = getAdapter();
64                    if (adapter != null) {
65                        adapter.configureLoader(loader, 0);
66                    }
67                    return loader;
68                }
69            }
70            throw new IllegalArgumentException("No loader for ID=" + id);
71        }
72
73        @Override
74        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
75            switch (loader.getId()) {
76                case DISPLAY_NAME_LOADER: {
77                    if (data != null && data.moveToFirst()) {
78                        showTargetContactName(data.getString(0));
79                    }
80                    break;
81                }
82                case JoinContactListAdapter.PARTITION_ALL_CONTACTS: {
83                    if (data != null) {
84                        final Cursor suggestionsCursor =
85                                ((JoinContactLoaderResult) data).suggestionCursor;
86                        onContactListLoaded(suggestionsCursor, data);
87                        maybeLogListEvent();
88                    }
89                    break;
90                }
91            }
92        }
93
94        @Override
95        public void onLoaderReset(Loader<Cursor> loader) {
96        }
97    };
98
99    public JoinContactListFragment() {
100        setPhotoLoaderEnabled(true);
101        setSectionHeaderDisplayEnabled(true);
102        setVisibleScrollbarEnabled(false);
103        setQuickContactEnabled(false);
104        setListType(ListEvent.ListType.PICK_JOIN);
105        setLogListEvents(true);
106    }
107
108    public void setOnContactPickerActionListener(OnContactPickerActionListener listener) {
109        mListener = listener;
110    }
111
112    @Override
113    protected void startLoading() {
114        configureAdapter();
115
116        getLoaderManager().initLoader(DISPLAY_NAME_LOADER, null, mLoaderCallbacks);
117
118        // When this method is called, Uri to be used may be changed. We should use restartLoader()
119        // to load the parameter again.
120        getLoaderManager().restartLoader(JoinContactListAdapter.PARTITION_ALL_CONTACTS,
121                null, mLoaderCallbacks);
122    }
123
124    private void onContactListLoaded(Cursor suggestionsCursor, Cursor allContactsCursor) {
125        JoinContactListAdapter adapter = getAdapter();
126        adapter.setSuggestionsCursor(suggestionsCursor);
127        setVisibleScrollbarEnabled(true);
128        onPartitionLoaded(JoinContactListAdapter.PARTITION_ALL_CONTACTS, allContactsCursor);
129    }
130
131    private void showTargetContactName(String displayName) {
132        Activity activity = getActivity();
133        TextView blurbView = (TextView) activity.findViewById(R.id.join_contact_blurb);
134        final String name = !TextUtils.isEmpty(displayName) ? displayName
135            : activity.getString(R.string.missing_name);
136        String blurb = activity.getString(R.string.blurbJoinContactDataWith, name);
137        blurbView.setText(blurb);
138    }
139
140    public void setTargetContactId(long targetContactId) {
141        mTargetContactId = targetContactId;
142    }
143
144    @Override
145    public JoinContactListAdapter createListAdapter() {
146        JoinContactListAdapter adapter = new JoinContactListAdapter(getActivity());
147        adapter.setPhotoPosition(ContactListItemView.getDefaultPhotoPosition(false /* opposite */));
148        return adapter;
149    }
150
151    @Override
152    protected void configureAdapter() {
153        super.configureAdapter();
154        JoinContactListAdapter adapter = getAdapter();
155        adapter.setTargetContactId(mTargetContactId);
156    }
157
158    @Override
159    protected View inflateView(LayoutInflater inflater, ViewGroup container) {
160        return inflater.inflate(R.layout.join_contact_picker_list_content, null);
161    }
162
163    @Override
164    protected void onItemClick(int position, long id) {
165        final Uri contactUri = getAdapter().getContactUri(position);
166        if (contactUri != null) mListener.onPickContactAction(contactUri);
167    }
168
169    @Override
170    public void onPickerResult(Intent data) {
171        final Uri contactUri = data.getData();
172        if (contactUri != null) mListener.onPickContactAction(contactUri);
173    }
174
175    @Override
176    public void onSaveInstanceState(Bundle outState) {
177        super.onSaveInstanceState(outState);
178        outState.putLong(KEY_TARGET_CONTACT_ID, mTargetContactId);
179    }
180
181    @Override
182    public void restoreSavedState(Bundle savedState) {
183        super.restoreSavedState(savedState);
184        if (savedState != null) {
185            mTargetContactId = savedState.getLong(KEY_TARGET_CONTACT_ID);
186        }
187    }
188
189    @Override
190    public void setQueryString(String queryString, boolean delaySelection) {
191        super.setQueryString(queryString, delaySelection);
192
193        setSearchMode(!TextUtils.isEmpty(queryString));
194    }
195}
196