ContactLoaderFragment.java revision 2eb969cc399d87b659a45568fa951d394c216917
12eb969cc399d87b659a45568fa951d394c216917Katherine Kuan/* 22eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * Copyright (C) 2011 The Android Open Source Project 32eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * 42eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * Licensed under the Apache License, Version 2.0 (the "License"); 52eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * you may not use this file except in compliance with the License. 62eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * You may obtain a copy of the License at 72eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * 82eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * http://www.apache.org/licenses/LICENSE-2.0 92eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * 102eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * Unless required by applicable law or agreed to in writing, software 112eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * distributed under the License is distributed on an "AS IS" BASIS, 122eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * See the License for the specific language governing permissions and 142eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * limitations under the License 152eb969cc399d87b659a45568fa951d394c216917Katherine Kuan */ 162eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 172eb969cc399d87b659a45568fa951d394c216917Katherine Kuanpackage com.android.contacts.detail; 182eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 192eb969cc399d87b659a45568fa951d394c216917Katherine Kuanimport com.android.contacts.ContactLoader; 202eb969cc399d87b659a45568fa951d394c216917Katherine Kuanimport com.android.contacts.R; 212eb969cc399d87b659a45568fa951d394c216917Katherine Kuanimport com.android.internal.util.Objects; 222eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 232eb969cc399d87b659a45568fa951d394c216917Katherine Kuanimport android.app.Activity; 242eb969cc399d87b659a45568fa951d394c216917Katherine Kuanimport android.app.Fragment; 252eb969cc399d87b659a45568fa951d394c216917Katherine Kuanimport android.app.LoaderManager; 262eb969cc399d87b659a45568fa951d394c216917Katherine Kuanimport android.app.LoaderManager.LoaderCallbacks; 272eb969cc399d87b659a45568fa951d394c216917Katherine Kuanimport android.content.Context; 282eb969cc399d87b659a45568fa951d394c216917Katherine Kuanimport android.content.Loader; 292eb969cc399d87b659a45568fa951d394c216917Katherine Kuanimport android.net.Uri; 302eb969cc399d87b659a45568fa951d394c216917Katherine Kuanimport android.os.Bundle; 312eb969cc399d87b659a45568fa951d394c216917Katherine Kuanimport android.util.Log; 322eb969cc399d87b659a45568fa951d394c216917Katherine Kuanimport android.view.LayoutInflater; 332eb969cc399d87b659a45568fa951d394c216917Katherine Kuanimport android.view.View; 342eb969cc399d87b659a45568fa951d394c216917Katherine Kuanimport android.view.ViewGroup; 352eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 362eb969cc399d87b659a45568fa951d394c216917Katherine Kuan/** 372eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * This is an invisible worker {@link Fragment} that loads the contact details for the contact card. 382eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * The data is then passed to the listener, who can then pass the data to other {@link View}s. 392eb969cc399d87b659a45568fa951d394c216917Katherine Kuan */ 402eb969cc399d87b659a45568fa951d394c216917Katherine Kuanpublic class ContactLoaderFragment extends Fragment { 412eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 422eb969cc399d87b659a45568fa951d394c216917Katherine Kuan private static final String TAG = ContactLoaderFragment.class.getSimpleName(); 432eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 442eb969cc399d87b659a45568fa951d394c216917Katherine Kuan /** 452eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * This is a listener to the {@link ContactLoaderFragment} and will be notified when the 462eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * contact details have finished loading. 472eb969cc399d87b659a45568fa951d394c216917Katherine Kuan */ 482eb969cc399d87b659a45568fa951d394c216917Katherine Kuan public static interface ContactLoaderFragmentListener { 492eb969cc399d87b659a45568fa951d394c216917Katherine Kuan public void onDetailsLoaded(ContactLoader.Result result); 502eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 512eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 522eb969cc399d87b659a45568fa951d394c216917Katherine Kuan private static final int LOADER_DETAILS = 1; 532eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 542eb969cc399d87b659a45568fa951d394c216917Katherine Kuan private static final String KEY_CONTACT_URI = "contactUri"; 552eb969cc399d87b659a45568fa951d394c216917Katherine Kuan private static final String LOADER_ARG_CONTACT_URI = "contactUri"; 562eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 572eb969cc399d87b659a45568fa951d394c216917Katherine Kuan private Context mContext; 582eb969cc399d87b659a45568fa951d394c216917Katherine Kuan private Uri mLookupUri; 592eb969cc399d87b659a45568fa951d394c216917Katherine Kuan private ContactLoaderFragmentListener mListener; 602eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 612eb969cc399d87b659a45568fa951d394c216917Katherine Kuan private ContactLoader.Result mContactData; 622eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 632eb969cc399d87b659a45568fa951d394c216917Katherine Kuan public ContactLoaderFragment() { 642eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 652eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 662eb969cc399d87b659a45568fa951d394c216917Katherine Kuan @Override 672eb969cc399d87b659a45568fa951d394c216917Katherine Kuan public void onCreate(Bundle savedInstanceState) { 682eb969cc399d87b659a45568fa951d394c216917Katherine Kuan super.onCreate(savedInstanceState); 692eb969cc399d87b659a45568fa951d394c216917Katherine Kuan if (savedInstanceState != null) { 702eb969cc399d87b659a45568fa951d394c216917Katherine Kuan mLookupUri = savedInstanceState.getParcelable(KEY_CONTACT_URI); 712eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 722eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 732eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 742eb969cc399d87b659a45568fa951d394c216917Katherine Kuan @Override 752eb969cc399d87b659a45568fa951d394c216917Katherine Kuan public void onSaveInstanceState(Bundle outState) { 762eb969cc399d87b659a45568fa951d394c216917Katherine Kuan super.onSaveInstanceState(outState); 772eb969cc399d87b659a45568fa951d394c216917Katherine Kuan outState.putParcelable(KEY_CONTACT_URI, mLookupUri); 782eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 792eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 802eb969cc399d87b659a45568fa951d394c216917Katherine Kuan @Override 812eb969cc399d87b659a45568fa951d394c216917Katherine Kuan public void onAttach(Activity activity) { 822eb969cc399d87b659a45568fa951d394c216917Katherine Kuan super.onAttach(activity); 832eb969cc399d87b659a45568fa951d394c216917Katherine Kuan mContext = activity; 842eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 852eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 862eb969cc399d87b659a45568fa951d394c216917Katherine Kuan @Override 872eb969cc399d87b659a45568fa951d394c216917Katherine Kuan public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { 882eb969cc399d87b659a45568fa951d394c216917Katherine Kuan // This is an empty view that is set to visibility gone. 892eb969cc399d87b659a45568fa951d394c216917Katherine Kuan return inflater.inflate(R.layout.contact_detail_loader_fragment, container, false); 902eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 912eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 922eb969cc399d87b659a45568fa951d394c216917Katherine Kuan @Override 932eb969cc399d87b659a45568fa951d394c216917Katherine Kuan public void onActivityCreated(Bundle savedInstanceState) { 942eb969cc399d87b659a45568fa951d394c216917Katherine Kuan super.onActivityCreated(savedInstanceState); 952eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 962eb969cc399d87b659a45568fa951d394c216917Katherine Kuan if (mLookupUri != null) { 972eb969cc399d87b659a45568fa951d394c216917Katherine Kuan Bundle args = new Bundle(); 982eb969cc399d87b659a45568fa951d394c216917Katherine Kuan args.putParcelable(LOADER_ARG_CONTACT_URI, mLookupUri); 992eb969cc399d87b659a45568fa951d394c216917Katherine Kuan getLoaderManager().initLoader(LOADER_DETAILS, args, mDetailLoaderListener); 1002eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 1012eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 1022eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 1032eb969cc399d87b659a45568fa951d394c216917Katherine Kuan public void loadUri(Uri lookupUri) { 1042eb969cc399d87b659a45568fa951d394c216917Katherine Kuan if (Objects.equal(lookupUri, mLookupUri)) { 1052eb969cc399d87b659a45568fa951d394c216917Katherine Kuan // Same URI, no need to load the data again 1062eb969cc399d87b659a45568fa951d394c216917Katherine Kuan return; 1072eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 1082eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 1092eb969cc399d87b659a45568fa951d394c216917Katherine Kuan mLookupUri = lookupUri; 1102eb969cc399d87b659a45568fa951d394c216917Katherine Kuan if (mLookupUri == null) { 1112eb969cc399d87b659a45568fa951d394c216917Katherine Kuan getLoaderManager().destroyLoader(LOADER_DETAILS); 1122eb969cc399d87b659a45568fa951d394c216917Katherine Kuan mContactData = null; 1132eb969cc399d87b659a45568fa951d394c216917Katherine Kuan if (mListener != null) { 1142eb969cc399d87b659a45568fa951d394c216917Katherine Kuan mListener.onDetailsLoaded(mContactData); 1152eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 1162eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } else if (getActivity() != null) { 1172eb969cc399d87b659a45568fa951d394c216917Katherine Kuan Bundle args = new Bundle(); 1182eb969cc399d87b659a45568fa951d394c216917Katherine Kuan args.putParcelable(LOADER_ARG_CONTACT_URI, mLookupUri); 1192eb969cc399d87b659a45568fa951d394c216917Katherine Kuan getLoaderManager().restartLoader(LOADER_DETAILS, args, mDetailLoaderListener); 1202eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 1212eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 1222eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 1232eb969cc399d87b659a45568fa951d394c216917Katherine Kuan public void setListener(ContactLoaderFragmentListener value) { 1242eb969cc399d87b659a45568fa951d394c216917Katherine Kuan mListener = value; 1252eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 1262eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 1272eb969cc399d87b659a45568fa951d394c216917Katherine Kuan /** 1282eb969cc399d87b659a45568fa951d394c216917Katherine Kuan * The listener for the detail loader 1292eb969cc399d87b659a45568fa951d394c216917Katherine Kuan */ 1302eb969cc399d87b659a45568fa951d394c216917Katherine Kuan private final LoaderManager.LoaderCallbacks<ContactLoader.Result> mDetailLoaderListener = 1312eb969cc399d87b659a45568fa951d394c216917Katherine Kuan new LoaderCallbacks<ContactLoader.Result>() { 1322eb969cc399d87b659a45568fa951d394c216917Katherine Kuan @Override 1332eb969cc399d87b659a45568fa951d394c216917Katherine Kuan public Loader<ContactLoader.Result> onCreateLoader(int id, Bundle args) { 1342eb969cc399d87b659a45568fa951d394c216917Katherine Kuan Uri lookupUri = args.getParcelable(LOADER_ARG_CONTACT_URI); 1352eb969cc399d87b659a45568fa951d394c216917Katherine Kuan return new ContactLoader(mContext, lookupUri, true /* loadGroupMetaData */); 1362eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 1372eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 1382eb969cc399d87b659a45568fa951d394c216917Katherine Kuan @Override 1392eb969cc399d87b659a45568fa951d394c216917Katherine Kuan public void onLoadFinished(Loader<ContactLoader.Result> loader, ContactLoader.Result data) { 1402eb969cc399d87b659a45568fa951d394c216917Katherine Kuan if (!mLookupUri.equals(data.getUri())) { 1412eb969cc399d87b659a45568fa951d394c216917Katherine Kuan return; 1422eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 1432eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 1442eb969cc399d87b659a45568fa951d394c216917Katherine Kuan if (data != ContactLoader.Result.NOT_FOUND && data != ContactLoader.Result.ERROR) { 1452eb969cc399d87b659a45568fa951d394c216917Katherine Kuan mContactData = data; 1462eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } else { 1472eb969cc399d87b659a45568fa951d394c216917Katherine Kuan Log.i(TAG, "No contact found: " + ((ContactLoader)loader).getLookupUri()); 1482eb969cc399d87b659a45568fa951d394c216917Katherine Kuan mContactData = null; 1492eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 1502eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 1512eb969cc399d87b659a45568fa951d394c216917Katherine Kuan if (mListener != null) { 1522eb969cc399d87b659a45568fa951d394c216917Katherine Kuan mListener.onDetailsLoaded(mContactData); 1532eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 1542eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 1552eb969cc399d87b659a45568fa951d394c216917Katherine Kuan 1562eb969cc399d87b659a45568fa951d394c216917Katherine Kuan public void onLoaderReset(Loader<ContactLoader.Result> loader) { 1572eb969cc399d87b659a45568fa951d394c216917Katherine Kuan mContactData = null; 1582eb969cc399d87b659a45568fa951d394c216917Katherine Kuan if (mListener != null) { 1592eb969cc399d87b659a45568fa951d394c216917Katherine Kuan mListener.onDetailsLoaded(mContactData); 1602eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 1612eb969cc399d87b659a45568fa951d394c216917Katherine Kuan } 1622eb969cc399d87b659a45568fa951d394c216917Katherine Kuan }; 1632eb969cc399d87b659a45568fa951d394c216917Katherine Kuan} 164