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