blob: 5a9c9fd07f69659d52c874794c3771c2d3876aa1 [file] [log] [blame]
Gary Maia6c80b32016-09-30 16:34:55 -07001package com.android.contacts.editor;
2
3import android.app.AlertDialog;
4import android.app.Dialog;
5import android.app.DialogFragment;
6import android.content.Context;
7import android.content.DialogInterface;
Gary Maib9065dd2016-11-08 10:49:00 -08008import android.content.Intent;
Gary Maia6c80b32016-09-30 16:34:55 -07009import android.os.Bundle;
Gary Maiba10be22016-10-12 17:41:15 -070010import android.text.TextUtils;
Gary Maia6c80b32016-09-30 16:34:55 -070011import android.view.LayoutInflater;
12import android.view.View;
13import android.view.ViewGroup;
Gary Maia1721802016-11-01 17:55:31 -070014import android.widget.BaseAdapter;
Gary Maia6c80b32016-09-30 16:34:55 -070015import android.widget.ImageView;
Gary Maia1721802016-11-01 17:55:31 -070016import android.widget.ListAdapter;
Gary Maia6c80b32016-09-30 16:34:55 -070017import android.widget.TextView;
18
Gary Mai0a49afa2016-12-05 15:53:58 -080019import com.android.contacts.ContactPhotoManager;
Gary Maia6c80b32016-09-30 16:34:55 -070020import com.android.contacts.R;
Gary Maib9065dd2016-11-08 10:49:00 -080021import com.android.contacts.activities.ContactSelectionActivity;
Gary Mai0a49afa2016-12-05 15:53:58 -080022import com.android.contacts.editor.PickRawContactLoader.RawContact;
23import com.android.contacts.editor.PickRawContactLoader.RawContactsMetadata;
24import com.android.contacts.list.UiIntentActions;
Gary Mai69c182a2016-12-05 13:07:03 -080025import com.android.contacts.logging.EditorEvent;
26import com.android.contacts.logging.Logger;
27import com.android.contacts.model.AccountTypeManager;
28import com.android.contacts.model.account.AccountDisplayInfo;
29import com.android.contacts.model.account.AccountDisplayInfoFactory;
Marcus Hagerott4bd50d62016-12-15 15:52:22 -080030import com.android.contacts.model.account.AccountInfo;
Gary Mai69c182a2016-12-05 13:07:03 -080031import com.android.contacts.model.account.AccountType;
32import com.android.contacts.model.account.AccountWithDataSet;
33import com.android.contacts.model.account.GoogleAccountType;
34import com.android.contacts.preference.ContactsPreferences;
Gary Maia6c80b32016-09-30 16:34:55 -070035
36/**
Gary Mai5336e6e2016-10-23 14:17:03 -070037 * Should only be started from an activity that implements {@link PickRawContactListener}.
Gary Maia6c80b32016-09-30 16:34:55 -070038 * Dialog containing the raw contacts that make up a contact. On selection the editor is loaded
39 * for the chosen raw contact.
40 */
41public class PickRawContactDialogFragment extends DialogFragment {
Gary Maia1721802016-11-01 17:55:31 -070042 private static final String ARGS_RAW_CONTACTS_METADATA = "rawContactsMetadata";
Gary Maib9065dd2016-11-08 10:49:00 -080043 private static final int REQUEST_CODE_JOIN = 3;
Gary Mai445a7c62016-10-17 14:30:11 -070044
Gary Mai5336e6e2016-10-23 14:17:03 -070045 public interface PickRawContactListener {
46 void onPickRawContact(long rawContactId);
47 }
48
Gary Maia6c80b32016-09-30 16:34:55 -070049 /**
50 * Used to list the account info for the given raw contacts list.
51 */
Gary Maia1721802016-11-01 17:55:31 -070052 private final class RawContactAccountListAdapter extends BaseAdapter {
Gary Maia6c80b32016-09-30 16:34:55 -070053 private final LayoutInflater mInflater;
54 private final Context mContext;
Gary Maia1721802016-11-01 17:55:31 -070055 private final RawContactsMetadata mRawContactsMetadata;
Gary Maiba10be22016-10-12 17:41:15 -070056 private final AccountTypeManager mAccountTypeManager;
57 private final ContactsPreferences mPreferences;
Gary Maia6c80b32016-09-30 16:34:55 -070058
Gary Maia1721802016-11-01 17:55:31 -070059 public RawContactAccountListAdapter(Context context,
60 RawContactsMetadata rawContactsMetadata) {
Gary Maia6c80b32016-09-30 16:34:55 -070061 mContext = context;
62 mInflater = LayoutInflater.from(context);
Gary Maiba10be22016-10-12 17:41:15 -070063 mAccountTypeManager = AccountTypeManager.getInstance(context);
64 mPreferences = new ContactsPreferences(context);
Gary Maia1721802016-11-01 17:55:31 -070065 mRawContactsMetadata = rawContactsMetadata;
Gary Maia6c80b32016-09-30 16:34:55 -070066 }
67
68 @Override
Gary Maia1721802016-11-01 17:55:31 -070069 public int getCount() {
70 return mRawContactsMetadata.rawContacts.size();
71 }
Gary Maia6c80b32016-09-30 16:34:55 -070072
Gary Maia1721802016-11-01 17:55:31 -070073 @Override
74 public Object getItem(int position) {
75 return mRawContactsMetadata.rawContacts.get(position);
76 }
77
78 @Override
79 public long getItemId(int position) {
80 return mRawContactsMetadata.rawContacts.get(position).id;
81 }
82
83 @Override
84 public View getView(int position, View convertView, ViewGroup parent) {
85 final View view;
86 final RawContactViewHolder holder;
87 if (convertView == null) {
88 view = mInflater.inflate(R.layout.raw_contact_list_item, parent, false);
89 holder = new RawContactViewHolder();
90 holder.displayName = (TextView) view.findViewById(R.id.display_name);
91 holder.accountName = (TextView) view.findViewById(R.id.account_name);
92 holder.accountIcon = (ImageView) view.findViewById(R.id.account_icon);
93 holder.photo = (ImageView) view.findViewById(R.id.photo);
94 view.setTag(holder);
95 } else {
96 view = convertView;
97 holder = (RawContactViewHolder) view.getTag();
98 }
99 final RawContact rawContact = mRawContactsMetadata.rawContacts.get(position);
100 final AccountType account = mAccountTypeManager.getAccountType(rawContact.accountType,
101 rawContact.accountDataSet);
102
103 String displayName =
Gary Maiba10be22016-10-12 17:41:15 -0700104 mPreferences.getDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY
Gary Mai3a533282016-11-08 19:02:26 +0000105 ? rawContact.displayName : rawContact.displayNameAlt;
Gary Maia6c80b32016-09-30 16:34:55 -0700106
Gary Maiba10be22016-10-12 17:41:15 -0700107 if (TextUtils.isEmpty(displayName)) {
108 displayName = mContext.getString(R.string.missing_name);
109 }
Gary Mai27e8c3a2016-10-27 10:27:46 -0700110 holder.displayName.setText(displayName);
Gary Maiba10be22016-10-12 17:41:15 -0700111
112 final String accountDisplayLabel;
113
114 // Use the same string as editor if it's an editable user profile raw contact.
Gary Maia1721802016-11-01 17:55:31 -0700115 if (mRawContactsMetadata.isUserProfile && account.areContactsWritable()) {
Marcus Hagerott4bd50d62016-12-15 15:52:22 -0800116 final AccountInfo accountInfo =
117 AccountTypeManager.getInstance(getContext()).getAccountInfoForAccount(
Gary Maia1721802016-11-01 17:55:31 -0700118 new AccountWithDataSet(rawContact.accountName,
119 rawContact.accountType, rawContact.accountDataSet));
Gary Maiba10be22016-10-12 17:41:15 -0700120 accountDisplayLabel = EditorUiUtils.getAccountHeaderLabelForMyProfile(mContext,
Marcus Hagerott4bd50d62016-12-15 15:52:22 -0800121 accountInfo);
Gary Maia1721802016-11-01 17:55:31 -0700122 } else if (GoogleAccountType.ACCOUNT_TYPE.equals(rawContact.accountType)
Gary Maiba10be22016-10-12 17:41:15 -0700123 && account.dataSet == null) {
124 // Focus Google accounts have the account name shown
Gary Maia1721802016-11-01 17:55:31 -0700125 accountDisplayLabel = rawContact.accountName;
Gary Maiba10be22016-10-12 17:41:15 -0700126 } else {
127 accountDisplayLabel = account.getDisplayLabel(mContext).toString();
128 }
Gary Mai27e8c3a2016-10-27 10:27:46 -0700129
130 holder.accountName.setText(accountDisplayLabel);
131 holder.accountIcon.setImageDrawable(account.getDisplayIcon(mContext));
Gary Maia6c80b32016-09-30 16:34:55 -0700132 final ContactPhotoManager.DefaultImageRequest
133 request = new ContactPhotoManager.DefaultImageRequest(
Gary Maia1721802016-11-01 17:55:31 -0700134 displayName, String.valueOf(rawContact.id), /* isCircular = */ true);
Gary Mai27e8c3a2016-10-27 10:27:46 -0700135
Gary Mai3a533282016-11-08 19:02:26 +0000136 ContactPhotoManager.getInstance(mContext).loadThumbnail(holder.photo,
137 rawContact.photoId,
Gary Maia6c80b32016-09-30 16:34:55 -0700138 /* darkTheme = */ false,
139 /* isCircular = */ true,
140 request);
Gary Mai27e8c3a2016-10-27 10:27:46 -0700141 return view;
Gary Maia6c80b32016-09-30 16:34:55 -0700142 }
143
Gary Mai27e8c3a2016-10-27 10:27:46 -0700144 class RawContactViewHolder {
145 TextView displayName;
146 TextView accountName;
147 ImageView accountIcon;
148 ImageView photo;
149 }
Gary Maia6c80b32016-09-30 16:34:55 -0700150 }
151
Gary Maia1721802016-11-01 17:55:31 -0700152 private ListAdapter mAdapter;
Gary Maib9065dd2016-11-08 10:49:00 -0800153 private boolean mShouldFinishActivity = true;
Gary Maia6c80b32016-09-30 16:34:55 -0700154
Gary Maia1721802016-11-01 17:55:31 -0700155 public static PickRawContactDialogFragment getInstance(RawContactsMetadata metadata) {
Gary Maia6c80b32016-09-30 16:34:55 -0700156 final PickRawContactDialogFragment fragment = new PickRawContactDialogFragment();
Gary Mai445a7c62016-10-17 14:30:11 -0700157 final Bundle args = new Bundle();
Gary Maia1721802016-11-01 17:55:31 -0700158 args.putParcelable(ARGS_RAW_CONTACTS_METADATA, metadata);
Gary Mai445a7c62016-10-17 14:30:11 -0700159 fragment.setArguments(args);
Gary Maia6c80b32016-09-30 16:34:55 -0700160 return fragment;
161 }
162
163 @Override
164 public Dialog onCreateDialog(Bundle savedInstanceState) {
Gary Mai5336e6e2016-10-23 14:17:03 -0700165 if (!(getActivity() instanceof PickRawContactListener)) {
166 throw new IllegalArgumentException(
167 "Host activity doesn't implement PickRawContactListener");
168 }
Gary Maia1721802016-11-01 17:55:31 -0700169 final Bundle args = getArguments();
170 if (args == null) {
171 throw new IllegalArgumentException("Dialog created with no arguments");
172 }
173
174 final RawContactsMetadata metadata = args.getParcelable(ARGS_RAW_CONTACTS_METADATA);
175 if (metadata == null) {
176 throw new IllegalArgumentException("Dialog created with null RawContactsMetadata");
177 }
178
Gary Maia6c80b32016-09-30 16:34:55 -0700179 final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Gary Maia1721802016-11-01 17:55:31 -0700180 mAdapter = new RawContactAccountListAdapter(getContext(), metadata);
Gary Maib9065dd2016-11-08 10:49:00 -0800181 if (metadata.showReadOnly) {
182 builder.setTitle(R.string.contact_editor_pick_linked_contact_dialog_title);
Gary Mai59ebb1b2016-11-29 17:08:47 -0800183 // Only provide link editing options for non-user profile contacts.
184 if (!metadata.isUserProfile) {
185 builder.setPositiveButton(R.string.contact_editor_add_linked_contact,
186 new DialogInterface.OnClickListener() {
187 @Override
188 public void onClick(DialogInterface dialog, int which) {
189 mShouldFinishActivity = false;
190 final Intent intent = new Intent(getActivity(),
191 ContactSelectionActivity.class);
192 intent.setAction(UiIntentActions.PICK_JOIN_CONTACT_ACTION);
193 intent.putExtra(UiIntentActions.TARGET_CONTACT_ID_EXTRA_KEY,
194 metadata.contactId);
195 getActivity().startActivityForResult(intent, REQUEST_CODE_JOIN);
196 }
197 });
198 builder.setNegativeButton(R.string.contact_editor_unlink_contacts,
199 new DialogInterface.OnClickListener() {
200 @Override
201 public void onClick(DialogInterface dialog, int which) {
202 mShouldFinishActivity = false;
203 final SplitContactConfirmationDialogFragment splitDialog = new
204 SplitContactConfirmationDialogFragment();
205 splitDialog.show(getActivity().getFragmentManager(),
206 SplitContactConfirmationDialogFragment.TAG);
207 }
208 });
209 }
Gary Maib9065dd2016-11-08 10:49:00 -0800210 } else {
211 builder.setTitle(R.string.contact_editor_pick_raw_contact_to_edit_dialog_title);
212 }
Gary Maiea2f3572016-10-11 17:30:28 -0700213 builder.setAdapter(mAdapter, new DialogInterface.OnClickListener() {
Gary Maia6c80b32016-09-30 16:34:55 -0700214 @Override
215 public void onClick(DialogInterface dialog, int which) {
Gary Maiea2f3572016-10-11 17:30:28 -0700216 final long rawContactId = mAdapter.getItemId(which);
Gary Mai5336e6e2016-10-23 14:17:03 -0700217 ((PickRawContactListener) getActivity()).onPickRawContact(rawContactId);
Gary Maia6c80b32016-09-30 16:34:55 -0700218 }
219 });
220 builder.setCancelable(true);
yaolu341d1042016-11-07 15:46:46 -0800221 if (savedInstanceState == null) {
222 Logger.logEditorEvent(EditorEvent.EventType.SHOW_RAW_CONTACT_PICKER,
223 /* numberRawContacts */ mAdapter.getCount());
224 }
Gary Maia6c80b32016-09-30 16:34:55 -0700225 return builder.create();
226 }
227
228 @Override
229 public void onDismiss(DialogInterface dialog) {
230 super.onDismiss(dialog);
Gary Maib9065dd2016-11-08 10:49:00 -0800231 if (mShouldFinishActivity) {
232 finishActivity();
233 }
Gary Maia6c80b32016-09-30 16:34:55 -0700234 }
235
Gary Mai445a7c62016-10-17 14:30:11 -0700236 @Override
Walter Jang68de0a52016-10-29 15:52:54 -0700237 public Context getContext() {
238 return getActivity();
239 }
240
Gary Maia6c80b32016-09-30 16:34:55 -0700241 private void finishActivity() {
242 if (getActivity() != null && !getActivity().isFinishing()) {
243 getActivity().finish();
244 }
245 }
246}