JoinContactListFragment.java revision b22ca30163f5f99f07b906c20fd1a798541448a5
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.app.LoaderManager.LoaderCallbacks; 23import android.content.ContentUris; 24import android.content.CursorLoader; 25import android.content.Intent; 26import android.content.Loader; 27import android.database.Cursor; 28import android.os.Bundle; 29import android.provider.ContactsContract.Contacts; 30import android.view.LayoutInflater; 31import android.view.View; 32import android.view.ViewGroup; 33import android.widget.TextView; 34 35/** 36 * Fragment for the Join Contact list. 37 */ 38public class JoinContactListFragment extends ContactEntryListFragment<JoinContactListAdapter> { 39 40 private static final int DISPLAY_NAME_LOADER = -2; 41 42 private OnContactPickerActionListener mListener; 43 private long mTargetContactId; 44 private boolean mAllContactsListShown = false; 45 46 private 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 setAizyEnabled(mAllContactsListShown); 80 81 JoinContactListAdapter adapter = getAdapter(); 82 Cursor suggestionsCursor = ((JoinContactLoader)loader).getSuggestionsCursor(); 83 adapter.setSuggestionsCursor(suggestionsCursor); 84 onPartitionLoaded(JoinContactListAdapter.PARTITION_ALL_CONTACTS, data); 85 break; 86 } 87 } 88 } 89 }; 90 91 public JoinContactListFragment() { 92 setPhotoLoaderEnabled(true); 93 setSectionHeaderDisplayEnabled(true); 94 setAizyEnabled(false); 95 setQuickContactEnabled(false); 96 } 97 98 public void setOnContactPickerActionListener(OnContactPickerActionListener listener) { 99 mListener = listener; 100 } 101 102 @Override 103 protected void startLoading() { 104 configureAdapter(); 105 106 getLoaderManager().initLoader(DISPLAY_NAME_LOADER, null, mLoaderCallbacks); 107 getLoaderManager().initLoader(JoinContactListAdapter.PARTITION_ALL_CONTACTS, 108 null, mLoaderCallbacks); 109 } 110 111 private void showTargetContactName(String displayName) { 112 Activity activity = getActivity(); 113 TextView blurbView = (TextView)activity.findViewById(R.id.join_contact_blurb); 114 String blurb = activity.getString(R.string.blurbJoinContactDataWith, displayName); 115 blurbView.setText(blurb); 116 } 117 118 public void setTargetContactId(long targetContactId) { 119 mTargetContactId = targetContactId; 120 } 121 122 @Override 123 public JoinContactListAdapter createListAdapter() { 124 return new JoinContactListAdapter(getActivity()); 125 } 126 127 @Override 128 protected void configureAdapter() { 129 super.configureAdapter(); 130 JoinContactListAdapter adapter = getAdapter(); 131 adapter.setAllContactsListShown(mAllContactsListShown); 132 adapter.setTargetContactId(mTargetContactId); 133 } 134 135 @Override 136 protected View inflateView(LayoutInflater inflater, ViewGroup container) { 137 return inflater.inflate(R.layout.contacts_list_content_join, null); 138 } 139 140 @Override 141 protected void onItemClick(int position, long id) { 142 JoinContactListAdapter adapter = getAdapter(); 143 int partition = adapter.getPartitionForPosition(position); 144 if (partition == JoinContactListAdapter.PARTITION_SHOW_ALL_CONTACTS) { 145 mAllContactsListShown = true; 146 configureAdapter(); 147 getLoaderManager().restartLoader(JoinContactListAdapter.PARTITION_ALL_CONTACTS, 148 null, mLoaderCallbacks); 149 } else { 150 mListener.onPickContactAction(adapter.getContactUri(position)); 151 } 152 } 153 154 @Override 155 public void startSearch(String initialQuery) { 156 ContactsRequest request = new ContactsRequest(); 157 request.setActionCode(ContactsRequest.ACTION_PICK_CONTACT); 158 request.setDirectorySearchEnabled(false); 159 ContactsSearchManager.startSearchForResult(getActivity(), initialQuery, 160 ACTIVITY_REQUEST_CODE_PICKER, request); 161 } 162 163 @Override 164 public void onPickerResult(Intent data) { 165 mListener.onPickContactAction(data.getData()); 166 } 167} 168