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