Home | History | Annotate | Download | only in list
      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                     Cursor suggestionsCursor = ((JoinContactLoaderResult) data).suggestionCursor;
     85                     onContactListLoaded(suggestionsCursor, data);
     86                     break;
     87                 }
     88             }
     89         }
     90 
     91         @Override
     92         public void onLoaderReset(Loader<Cursor> loader) {
     93         }
     94     };
     95 
     96     public JoinContactListFragment() {
     97         setPhotoLoaderEnabled(true);
     98         setSectionHeaderDisplayEnabled(true);
     99         setVisibleScrollbarEnabled(false);
    100         setQuickContactEnabled(false);
    101     }
    102 
    103     public void setOnContactPickerActionListener(OnContactPickerActionListener listener) {
    104         mListener = listener;
    105     }
    106 
    107     @Override
    108     protected void startLoading() {
    109         configureAdapter();
    110 
    111         getLoaderManager().initLoader(DISPLAY_NAME_LOADER, null, mLoaderCallbacks);
    112 
    113         // When this method is called, Uri to be used may be changed. We should use restartLoader()
    114         // to load the parameter again.
    115         getLoaderManager().restartLoader(JoinContactListAdapter.PARTITION_ALL_CONTACTS,
    116                 null, mLoaderCallbacks);
    117     }
    118 
    119     private void onContactListLoaded(Cursor suggestionsCursor, Cursor allContactsCursor) {
    120         JoinContactListAdapter adapter = getAdapter();
    121         adapter.setSuggestionsCursor(suggestionsCursor);
    122         setVisibleScrollbarEnabled(true);
    123         onPartitionLoaded(JoinContactListAdapter.PARTITION_ALL_CONTACTS, allContactsCursor);
    124     }
    125 
    126     private void showTargetContactName(String displayName) {
    127         Activity activity = getActivity();
    128         TextView blurbView = (TextView) activity.findViewById(R.id.join_contact_blurb);
    129         String blurb = activity.getString(R.string.blurbJoinContactDataWith, displayName);
    130         blurbView.setText(blurb);
    131     }
    132 
    133     public void setTargetContactId(long targetContactId) {
    134         mTargetContactId = targetContactId;
    135     }
    136 
    137     @Override
    138     public JoinContactListAdapter createListAdapter() {
    139         JoinContactListAdapter adapter = new JoinContactListAdapter(getActivity());
    140         adapter.setPhotoPosition(ContactListItemView.getDefaultPhotoPosition(true /* opposite */));
    141         return adapter;
    142     }
    143 
    144     @Override
    145     protected void configureAdapter() {
    146         super.configureAdapter();
    147         JoinContactListAdapter adapter = getAdapter();
    148         adapter.setTargetContactId(mTargetContactId);
    149     }
    150 
    151     @Override
    152     protected View inflateView(LayoutInflater inflater, ViewGroup container) {
    153         return inflater.inflate(R.layout.join_contact_picker_list_content, null);
    154     }
    155 
    156     @Override
    157     protected void onItemClick(int position, long id) {
    158         final Uri contactUri = getAdapter().getContactUri(position);
    159         if (contactUri != null) mListener.onPickContactAction(contactUri);
    160     }
    161 
    162     @Override
    163     public void onPickerResult(Intent data) {
    164         final Uri contactUri = data.getData();
    165         if (contactUri != null) mListener.onPickContactAction(contactUri);
    166     }
    167 
    168     @Override
    169     public void onSaveInstanceState(Bundle outState) {
    170         super.onSaveInstanceState(outState);
    171         outState.putLong(KEY_TARGET_CONTACT_ID, mTargetContactId);
    172     }
    173 
    174     @Override
    175     public void restoreSavedState(Bundle savedState) {
    176         super.restoreSavedState(savedState);
    177         if (savedState != null) {
    178             mTargetContactId = savedState.getLong(KEY_TARGET_CONTACT_ID);
    179         }
    180     }
    181 
    182     @Override
    183     public void setQueryString(String queryString, boolean delaySelection) {
    184         super.setQueryString(queryString, delaySelection);
    185 
    186         setSearchMode(!TextUtils.isEmpty(queryString));
    187     }
    188 }
    189