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