• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 package com.android.contacts.list;
17 
18 import android.app.Activity;
19 import android.app.LoaderManager.LoaderCallbacks;
20 import android.content.ContentUris;
21 import android.content.CursorLoader;
22 import android.content.Intent;
23 import android.content.Loader;
24 import android.database.Cursor;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.provider.ContactsContract.Contacts;
28 import android.text.TextUtils;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.TextView;
33 
34 import com.android.contacts.R;
35 import com.android.contacts.common.list.ContactEntryListFragment;
36 import com.android.contacts.list.JoinContactLoader.JoinContactLoaderResult;
37 
38 /**
39  * Fragment for the Join Contact list.
40  */
41 public 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                     Cursor suggestionsCursor = ((JoinContactLoaderResult) data).suggestionCursor;
84                     onContactListLoaded(suggestionsCursor, data);
85                     break;
86                 }
87             }
88         }
89 
90         @Override
91         public void onLoaderReset(Loader<Cursor> loader) {
92         }
93     };
94 
JoinContactListFragment()95     public JoinContactListFragment() {
96         setPhotoLoaderEnabled(true);
97         setSectionHeaderDisplayEnabled(true);
98         setVisibleScrollbarEnabled(false);
99         setQuickContactEnabled(false);
100     }
101 
setOnContactPickerActionListener(OnContactPickerActionListener listener)102     public void setOnContactPickerActionListener(OnContactPickerActionListener listener) {
103         mListener = listener;
104     }
105 
106     @Override
startLoading()107     protected void startLoading() {
108         configureAdapter();
109 
110         getLoaderManager().initLoader(DISPLAY_NAME_LOADER, null, mLoaderCallbacks);
111 
112         // When this method is called, Uri to be used may be changed. We should use restartLoader()
113         // to load the parameter again.
114         getLoaderManager().restartLoader(JoinContactListAdapter.PARTITION_ALL_CONTACTS,
115                 null, mLoaderCallbacks);
116     }
117 
onContactListLoaded(Cursor suggestionsCursor, Cursor allContactsCursor)118     private void onContactListLoaded(Cursor suggestionsCursor, Cursor allContactsCursor) {
119         JoinContactListAdapter adapter = getAdapter();
120         adapter.setSuggestionsCursor(suggestionsCursor);
121         setVisibleScrollbarEnabled(true);
122         onPartitionLoaded(JoinContactListAdapter.PARTITION_ALL_CONTACTS, allContactsCursor);
123     }
124 
showTargetContactName(String displayName)125     private void showTargetContactName(String displayName) {
126         Activity activity = getActivity();
127         TextView blurbView = (TextView) activity.findViewById(R.id.join_contact_blurb);
128         String blurb = activity.getString(R.string.blurbJoinContactDataWith, displayName);
129         blurbView.setText(blurb);
130     }
131 
setTargetContactId(long targetContactId)132     public void setTargetContactId(long targetContactId) {
133         mTargetContactId = targetContactId;
134     }
135 
136     @Override
createListAdapter()137     public JoinContactListAdapter createListAdapter() {
138         return new JoinContactListAdapter(getActivity());
139     }
140 
141     @Override
configureAdapter()142     protected void configureAdapter() {
143         super.configureAdapter();
144         JoinContactListAdapter adapter = getAdapter();
145         adapter.setTargetContactId(mTargetContactId);
146     }
147 
148     @Override
inflateView(LayoutInflater inflater, ViewGroup container)149     protected View inflateView(LayoutInflater inflater, ViewGroup container) {
150         return inflater.inflate(R.layout.join_contact_picker_list_content, null);
151     }
152 
153     @Override
onItemClick(int position, long id)154     protected void onItemClick(int position, long id) {
155         final Uri contactUri = getAdapter().getContactUri(position);
156         if (contactUri != null) mListener.onPickContactAction(contactUri);
157     }
158 
159     @Override
onPickerResult(Intent data)160     public void onPickerResult(Intent data) {
161         final Uri contactUri = data.getData();
162         if (contactUri != null) mListener.onPickContactAction(contactUri);
163     }
164 
165     @Override
onSaveInstanceState(Bundle outState)166     public void onSaveInstanceState(Bundle outState) {
167         super.onSaveInstanceState(outState);
168         outState.putLong(KEY_TARGET_CONTACT_ID, mTargetContactId);
169     }
170 
171     @Override
restoreSavedState(Bundle savedState)172     public void restoreSavedState(Bundle savedState) {
173         super.restoreSavedState(savedState);
174         if (savedState != null) {
175             mTargetContactId = savedState.getLong(KEY_TARGET_CONTACT_ID);
176         }
177     }
178 
179     @Override
setQueryString(String queryString, boolean delaySelection)180     public void setQueryString(String queryString, boolean delaySelection) {
181         super.setQueryString(queryString, delaySelection);
182 
183         setSearchMode(!TextUtils.isEmpty(queryString));
184     }
185 }
186