Home | History | Annotate | Download | only in list
      1 /*
      2  * Copyright (C) 2011 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 com.android.contacts.ContactPhotoManager;
     19 import com.android.contacts.ContactTileLoaderFactory;
     20 import com.android.contacts.R;
     21 import com.android.contacts.list.ContactTileAdapter.DisplayType;
     22 
     23 import android.app.Activity;
     24 import android.app.Fragment;
     25 import android.app.LoaderManager;
     26 import android.app.LoaderManager.LoaderCallbacks;
     27 import android.content.CursorLoader;
     28 import android.content.Loader;
     29 import android.content.res.Resources;
     30 import android.database.Cursor;
     31 import android.graphics.Rect;
     32 import android.net.Uri;
     33 import android.os.Bundle;
     34 import android.view.LayoutInflater;
     35 import android.view.View;
     36 import android.view.ViewGroup;
     37 import android.widget.ListView;
     38 import android.widget.TextView;
     39 
     40 /**
     41  * Fragment containing a list of starred contacts followed by a list of frequently contacted.
     42  *
     43  * TODO: Make this an abstract class so that the favorites, frequent, and group list functionality
     44  * can be separated out. This will make it easier to customize any of those lists if necessary
     45  * (i.e. adding header views to the ListViews in the fragment). This work was started
     46  * by creating {@link ContactTileFrequentFragment}.
     47  */
     48 public class ContactTileListFragment extends Fragment {
     49     private static final String TAG = ContactTileListFragment.class.getSimpleName();
     50 
     51     public interface Listener {
     52         public void onContactSelected(Uri contactUri, Rect targetRect);
     53     }
     54 
     55     private static int LOADER_CONTACTS = 1;
     56 
     57     private Listener mListener;
     58     private ContactTileAdapter mAdapter;
     59     private DisplayType mDisplayType;
     60     private TextView mEmptyView;
     61     private ListView mListView;
     62 
     63     @Override
     64     public void onAttach(Activity activity) {
     65         super.onAttach(activity);
     66 
     67         Resources res = getResources();
     68         int columnCount = res.getInteger(R.integer.contact_tile_column_count);
     69 
     70         mAdapter = new ContactTileAdapter(activity, mAdapterListener,
     71                 columnCount, mDisplayType);
     72         mAdapter.setPhotoLoader(ContactPhotoManager.getInstance(activity));
     73     }
     74 
     75     @Override
     76     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     77             Bundle savedInstanceState) {
     78         return inflateAndSetupView(inflater, container, savedInstanceState,
     79                 R.layout.contact_tile_list);
     80     }
     81 
     82     protected View inflateAndSetupView(LayoutInflater inflater, ViewGroup container,
     83             Bundle savedInstanceState, int layoutResourceId) {
     84         View listLayout = inflater.inflate(layoutResourceId, container, false);
     85 
     86         mEmptyView = (TextView) listLayout.findViewById(R.id.contact_tile_list_empty);
     87         mListView = (ListView) listLayout.findViewById(R.id.contact_tile_list);
     88 
     89         mListView.setItemsCanFocus(true);
     90         mListView.setAdapter(mAdapter);
     91         return listLayout;
     92     }
     93 
     94     @Override
     95     public void onStart() {
     96         super.onStart();
     97         // TODO: Use initLoader?
     98         getLoaderManager().restartLoader(LOADER_CONTACTS, null, mContactTileLoaderListener);
     99     }
    100 
    101     public void setColumnCount(int columnCount) {
    102         mAdapter.setColumnCount(columnCount);
    103     }
    104 
    105     public void setDisplayType(DisplayType displayType) {
    106         mDisplayType = displayType;
    107         mAdapter.setDisplayType(mDisplayType);
    108     }
    109 
    110     public void enableQuickContact(boolean enableQuickContact) {
    111         mAdapter.enableQuickContact(enableQuickContact);
    112     }
    113 
    114     private final LoaderManager.LoaderCallbacks<Cursor> mContactTileLoaderListener =
    115             new LoaderCallbacks<Cursor>() {
    116 
    117         @Override
    118         public CursorLoader onCreateLoader(int id, Bundle args) {
    119             switch (mDisplayType) {
    120               case STARRED_ONLY:
    121                   return ContactTileLoaderFactory.createStarredLoader(getActivity());
    122               case STREQUENT:
    123                   return ContactTileLoaderFactory.createStrequentLoader(getActivity());
    124               case STREQUENT_PHONE_ONLY:
    125                   return ContactTileLoaderFactory.createStrequentPhoneOnlyLoader(getActivity());
    126               case FREQUENT_ONLY:
    127                   return ContactTileLoaderFactory.createFrequentLoader(getActivity());
    128               default:
    129                   throw new IllegalStateException(
    130                       "Unrecognized DisplayType " + mDisplayType);
    131             }
    132         }
    133 
    134         @Override
    135         public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    136             mAdapter.setContactCursor(data);
    137             mEmptyView.setText(getEmptyStateText());
    138             mListView.setEmptyView(mEmptyView);
    139         }
    140 
    141         @Override
    142         public void onLoaderReset(Loader<Cursor> loader) {}
    143     };
    144 
    145     private String getEmptyStateText() {
    146         String emptyText;
    147         switch (mDisplayType) {
    148             case STREQUENT:
    149             case STREQUENT_PHONE_ONLY:
    150             case STARRED_ONLY:
    151                 emptyText = getString(R.string.listTotalAllContactsZeroStarred);
    152                 break;
    153             case FREQUENT_ONLY:
    154             case GROUP_MEMBERS:
    155                 emptyText = getString(R.string.noContacts);
    156                 break;
    157             default:
    158                 throw new IllegalArgumentException("Unrecognized DisplayType " + mDisplayType);
    159         }
    160         return emptyText;
    161     }
    162 
    163     public void setListener(Listener listener) {
    164         mListener = listener;
    165     }
    166 
    167     private ContactTileAdapter.Listener mAdapterListener =
    168             new ContactTileAdapter.Listener() {
    169         @Override
    170         public void onContactSelected(Uri contactUri, Rect targetRect) {
    171             if (mListener != null) {
    172                 mListener.onContactSelected(contactUri, targetRect);
    173             }
    174         }
    175     };
    176 }
    177