JoinContactListFragment.java revision 1c5c8e23849ac3babb2bdec65ec63c190a5c40df
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.ContactsSearchManager;
19import com.android.contacts.R;
20
21import android.app.Activity;
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 OnContactPickerActionListener mListener;
42    private long mTargetContactId;
43    private boolean mAllContactsListShown = false;
44
45    public JoinContactListFragment() {
46        setPhotoLoaderEnabled(true);
47        setSectionHeaderDisplayEnabled(true);
48        setAizyEnabled(false);
49    }
50
51    public void setOnContactPickerActionListener(OnContactPickerActionListener listener) {
52        mListener = listener;
53    }
54
55    @Override
56    protected void onInitializeLoaders() {
57        super.onInitializeLoaders();
58        startLoading(DISPLAY_NAME_LOADER, null);
59    }
60
61    @Override
62    protected Loader<Cursor> startLoading(int id, Bundle args) {
63
64        // The first two partitions don't require loaders
65        if (id == JoinContactListAdapter.PARTITION_SUGGESTIONS ||
66                id == JoinContactListAdapter.PARTITION_SHOW_ALL_CONTACTS) {
67            return null;
68        }
69
70        return super.startLoading(id, args);
71    }
72
73    @Override
74    protected Loader<Cursor> onCreateLoader(int id, Bundle args) {
75        if (id == DISPLAY_NAME_LOADER) {
76            // Loader for the display name of the target contact
77            return new CursorLoader(getActivity(),
78                    ContentUris.withAppendedId(Contacts.CONTENT_URI, mTargetContactId),
79                    new String[] {Contacts.DISPLAY_NAME}, null, null, null);
80        } else if (id == JoinContactListAdapter.PARTITION_ALL_CONTACTS) {
81            JoinContactLoader loader = new JoinContactLoader(getActivity());
82            JoinContactListAdapter adapter = getAdapter();
83            if (adapter != null) {
84                adapter.configureLoader(loader, 0);
85            }
86            return loader;
87        } else {
88            throw new IllegalArgumentException("No loader for ID=" + id);
89        }
90    }
91
92    @Override
93    protected void onLoadFinished(Loader<Cursor> loader, Cursor data) {
94        if (loader.getId() == DISPLAY_NAME_LOADER) {
95            if (data != null && data.moveToFirst()) {
96                showTargetContactName(data.getString(0));
97            }
98        } else {
99            JoinContactListAdapter adapter = getAdapter();
100            Cursor suggestionsCursor = ((JoinContactLoader)loader).getSuggestionsCursor();
101            adapter.setSuggestionsCursor(suggestionsCursor);
102            super.onLoadFinished(loader, data);
103        }
104    }
105
106     private void showTargetContactName(String displayName) {
107        Activity activity = getActivity();
108        TextView blurbView = (TextView)activity.findViewById(R.id.join_contact_blurb);
109        String blurb = activity.getString(R.string.blurbJoinContactDataWith, displayName);
110        blurbView.setText(blurb);
111    }
112
113    public void setTargetContactId(long targetContactId) {
114        mTargetContactId = targetContactId;
115    }
116
117    @Override
118    public JoinContactListAdapter createListAdapter() {
119        return new JoinContactListAdapter(getActivity());
120    }
121
122    @Override
123    protected void configureAdapter() {
124        super.configureAdapter();
125        JoinContactListAdapter adapter = getAdapter();
126        adapter.setAllContactsListShown(mAllContactsListShown);
127        adapter.setTargetContactId(mTargetContactId);
128    }
129
130    @Override
131    protected View inflateView(LayoutInflater inflater, ViewGroup container) {
132        return inflater.inflate(R.layout.contacts_list_content_join, null);
133    }
134
135    @Override
136    protected void onItemClick(int position, long id) {
137        JoinContactListAdapter adapter = getAdapter();
138        int partition = adapter.getPartitionForPosition(position);
139        if (partition == JoinContactListAdapter.PARTITION_SHOW_ALL_CONTACTS) {
140            mAllContactsListShown = true;
141            reloadData();
142        } else {
143            mListener.onPickContactAction(adapter.getContactUri(position));
144        }
145    }
146
147    @Override
148    protected void reloadData() {
149        setAizyEnabled(mAllContactsListShown);
150        super.reloadData();
151    }
152
153    @Override
154    public void startSearch(String initialQuery) {
155        ContactsRequest request = new ContactsRequest();
156        request.setActionCode(ContactsRequest.ACTION_PICK_CONTACT);
157        request.setDirectorySearchEnabled(false);
158        ContactsSearchManager.startSearchForResult(getActivity(), initialQuery,
159                ACTIVITY_REQUEST_CODE_PICKER, request);
160    }
161
162    @Override
163    public void onPickerResult(Intent data) {
164        mListener.onPickContactAction(data.getData());
165    }
166}
167