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.content.Intent;
     19 import android.net.Uri;
     20 import android.os.Bundle;
     21 import android.view.LayoutInflater;
     22 import android.view.View;
     23 import android.view.ViewGroup;
     24 import android.widget.AdapterView;
     25 
     26 import com.android.contacts.R;
     27 import com.android.contacts.common.list.ContactEntryListAdapter;
     28 import com.android.contacts.common.list.ContactEntryListFragment;
     29 import com.android.contacts.common.list.ContactListAdapter;
     30 import com.android.contacts.common.list.ContactListFilter;
     31 import com.android.contacts.common.list.DefaultContactListAdapter;
     32 import com.android.contacts.common.list.DirectoryListLoader;
     33 import com.android.contacts.common.list.ShortcutIntentBuilder;
     34 import com.android.contacts.common.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;
     35 
     36 /**
     37  * Fragment for the contact list used for browsing contacts (as compared to
     38  * picking a contact with one of the PICK or SHORTCUT intents).
     39  */
     40 public class ContactPickerFragment extends ContactEntryListFragment<ContactEntryListAdapter>
     41         implements OnShortcutIntentCreatedListener {
     42 
     43     private static final String KEY_EDIT_MODE = "editMode";
     44     private static final String KEY_CREATE_CONTACT_ENABLED = "createContactEnabled";
     45     private static final String KEY_SHORTCUT_REQUESTED = "shortcutRequested";
     46 
     47     private OnContactPickerActionListener mListener;
     48     private boolean mCreateContactEnabled;
     49     private boolean mEditMode;
     50     private boolean mShortcutRequested;
     51 
     52     public ContactPickerFragment() {
     53         setPhotoLoaderEnabled(true);
     54         setSectionHeaderDisplayEnabled(true);
     55         setVisibleScrollbarEnabled(true);
     56         setQuickContactEnabled(false);
     57         setDirectorySearchMode(DirectoryListLoader.SEARCH_MODE_CONTACT_SHORTCUT);
     58     }
     59 
     60     public void setOnContactPickerActionListener(OnContactPickerActionListener listener) {
     61         mListener = listener;
     62     }
     63 
     64     public boolean isCreateContactEnabled() {
     65         return mCreateContactEnabled;
     66     }
     67 
     68     public void setCreateContactEnabled(boolean flag) {
     69         this.mCreateContactEnabled = flag;
     70     }
     71 
     72     public boolean isEditMode() {
     73         return mEditMode;
     74     }
     75 
     76     public void setEditMode(boolean flag) {
     77         mEditMode = flag;
     78     }
     79 
     80     public void setShortcutRequested(boolean flag) {
     81         mShortcutRequested = flag;
     82     }
     83 
     84     @Override
     85     public void onSaveInstanceState(Bundle outState) {
     86         super.onSaveInstanceState(outState);
     87         outState.putBoolean(KEY_EDIT_MODE, mEditMode);
     88         outState.putBoolean(KEY_CREATE_CONTACT_ENABLED, mCreateContactEnabled);
     89         outState.putBoolean(KEY_SHORTCUT_REQUESTED, mShortcutRequested);
     90     }
     91 
     92     @Override
     93     public void restoreSavedState(Bundle savedState) {
     94         super.restoreSavedState(savedState);
     95 
     96         if (savedState == null) {
     97             return;
     98         }
     99 
    100         mEditMode = savedState.getBoolean(KEY_EDIT_MODE);
    101         mCreateContactEnabled = savedState.getBoolean(KEY_CREATE_CONTACT_ENABLED);
    102         mShortcutRequested = savedState.getBoolean(KEY_SHORTCUT_REQUESTED);
    103     }
    104 
    105     @Override
    106     protected void onCreateView(LayoutInflater inflater, ViewGroup container) {
    107         super.onCreateView(inflater, container);
    108         if (mCreateContactEnabled) {
    109             getListView().addHeaderView(inflater.inflate(R.layout.create_new_contact, null, false));
    110         }
    111     }
    112 
    113     @Override
    114     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    115         if (position == 0 && mCreateContactEnabled) {
    116             mListener.onCreateNewContactAction();
    117         } else {
    118             super.onItemClick(parent, view, position, id);
    119         }
    120     }
    121 
    122     @Override
    123     protected void onItemClick(int position, long id) {
    124         Uri uri;
    125         if (isLegacyCompatibilityMode()) {
    126             uri = ((LegacyContactListAdapter)getAdapter()).getPersonUri(position);
    127         } else {
    128             uri = ((ContactListAdapter)getAdapter()).getContactUri(position);
    129         }
    130         if (mEditMode) {
    131             editContact(uri);
    132         } else  if (mShortcutRequested) {
    133             ShortcutIntentBuilder builder = new ShortcutIntentBuilder(getActivity(), this);
    134             builder.createContactShortcutIntent(uri);
    135         } else {
    136             pickContact(uri);
    137         }
    138     }
    139 
    140     public void createNewContact() {
    141         mListener.onCreateNewContactAction();
    142     }
    143 
    144     public void editContact(Uri contactUri) {
    145         mListener.onEditContactAction(contactUri);
    146     }
    147 
    148     public void pickContact(Uri uri) {
    149         mListener.onPickContactAction(uri);
    150     }
    151 
    152     @Override
    153     protected ContactEntryListAdapter createListAdapter() {
    154         if (!isLegacyCompatibilityMode()) {
    155             DefaultContactListAdapter adapter = new DefaultContactListAdapter(getActivity());
    156             adapter.setFilter(ContactListFilter.createFilterWithType(
    157                     ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS));
    158             adapter.setSectionHeaderDisplayEnabled(true);
    159             adapter.setDisplayPhotos(true);
    160             adapter.setQuickContactEnabled(false);
    161             return adapter;
    162         } else {
    163             LegacyContactListAdapter adapter = new LegacyContactListAdapter(getActivity());
    164             adapter.setSectionHeaderDisplayEnabled(false);
    165             adapter.setDisplayPhotos(false);
    166             return adapter;
    167         }
    168     }
    169 
    170     @Override
    171     protected void configureAdapter() {
    172         super.configureAdapter();
    173 
    174         ContactEntryListAdapter adapter = getAdapter();
    175 
    176         // If "Create new contact" is shown, don't display the empty list UI
    177         adapter.setEmptyListEnabled(!isCreateContactEnabled());
    178     }
    179 
    180     @Override
    181     protected View inflateView(LayoutInflater inflater, ViewGroup container) {
    182         return inflater.inflate(R.layout.contact_picker_content, null);
    183     }
    184 
    185     @Override
    186     public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
    187         mListener.onShortcutIntentCreated(shortcutIntent);
    188     }
    189 
    190     @Override
    191     public void onPickerResult(Intent data) {
    192         mListener.onPickContactAction(data.getData());
    193     }
    194 }
    195