blob: 6d6d4d6590de9f740d7eddaf38791dd1aff0c604 [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
19import com.android.contacts.R;
Gary Maib9065dd2016-11-08 10:49:00 -080020import com.android.contacts.activities.ContactSelectionActivity;
Gary Maia6c80b32016-09-30 16:34:55 -070021import com.android.contacts.common.ContactPhotoManager;
yaolu341d1042016-11-07 15:46:46 -080022import com.android.contacts.common.logging.EditorEvent;
23import com.android.contacts.common.logging.Logger;
Gary Maia6c80b32016-09-30 16:34:55 -070024import com.android.contacts.common.model.AccountTypeManager;
Gary Maiba10be22016-10-12 17:41:15 -070025import com.android.contacts.common.model.account.AccountDisplayInfo;
26import com.android.contacts.common.model.account.AccountDisplayInfoFactory;
Gary Maia6c80b32016-09-30 16:34:55 -070027import com.android.contacts.common.model.account.AccountType;
Gary Maiba10be22016-10-12 17:41:15 -070028import com.android.contacts.common.model.account.AccountWithDataSet;
29import com.android.contacts.common.model.account.GoogleAccountType;
Gary Maia6c80b32016-09-30 16:34:55 -070030import com.android.contacts.common.preference.ContactsPreferences;
Gary Maia1721802016-11-01 17:55:31 -070031import com.android.contacts.editor.PickRawContactLoader.RawContact;
32import com.android.contacts.editor.PickRawContactLoader.RawContactsMetadata;
Gary Maib9065dd2016-11-08 10:49:00 -080033import com.android.contacts.list.UiIntentActions;
Gary Maia6c80b32016-09-30 16:34:55 -070034
35/**
Gary Mai5336e6e2016-10-23 14:17:03 -070036 * Should only be started from an activity that implements {@link PickRawContactListener}.
Gary Maia6c80b32016-09-30 16:34:55 -070037 * Dialog containing the raw contacts that make up a contact. On selection the editor is loaded
38 * for the chosen raw contact.
39 */
40public class PickRawContactDialogFragment extends DialogFragment {
Gary Maia1721802016-11-01 17:55:31 -070041 private static final String ARGS_RAW_CONTACTS_METADATA = "rawContactsMetadata";
Gary Maib9065dd2016-11-08 10:49:00 -080042 private static final int REQUEST_CODE_JOIN = 3;
Gary Mai445a7c62016-10-17 14:30:11 -070043
Gary Mai5336e6e2016-10-23 14:17:03 -070044 public interface PickRawContactListener {
45 void onPickRawContact(long rawContactId);
46 }
47
Gary Maia6c80b32016-09-30 16:34:55 -070048 /**
49 * Used to list the account info for the given raw contacts list.
50 */
Gary Maia1721802016-11-01 17:55:31 -070051 private final class RawContactAccountListAdapter extends BaseAdapter {
Gary Maia6c80b32016-09-30 16:34:55 -070052 private final LayoutInflater mInflater;
53 private final Context mContext;
Gary Maia1721802016-11-01 17:55:31 -070054 private final RawContactsMetadata mRawContactsMetadata;
Gary Maiba10be22016-10-12 17:41:15 -070055 private final AccountDisplayInfoFactory mAccountDisplayInfoFactory;
56 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 mAccountDisplayInfoFactory = AccountDisplayInfoFactory.forWritableAccounts(context);
64 mAccountTypeManager = AccountTypeManager.getInstance(context);
65 mPreferences = new ContactsPreferences(context);
Gary Maia1721802016-11-01 17:55:31 -070066 mRawContactsMetadata = rawContactsMetadata;
Gary Maia6c80b32016-09-30 16:34:55 -070067 }
68
69 @Override
Gary Maia1721802016-11-01 17:55:31 -070070 public int getCount() {
71 return mRawContactsMetadata.rawContacts.size();
72 }
Gary Maia6c80b32016-09-30 16:34:55 -070073
Gary Maia1721802016-11-01 17:55:31 -070074 @Override
75 public Object getItem(int position) {
76 return mRawContactsMetadata.rawContacts.get(position);
77 }
78
79 @Override
80 public long getItemId(int position) {
81 return mRawContactsMetadata.rawContacts.get(position).id;
82 }
83
84 @Override
85 public View getView(int position, View convertView, ViewGroup parent) {
86 final View view;
87 final RawContactViewHolder holder;
88 if (convertView == null) {
89 view = mInflater.inflate(R.layout.raw_contact_list_item, parent, false);
90 holder = new RawContactViewHolder();
91 holder.displayName = (TextView) view.findViewById(R.id.display_name);
92 holder.accountName = (TextView) view.findViewById(R.id.account_name);
93 holder.accountIcon = (ImageView) view.findViewById(R.id.account_icon);
94 holder.photo = (ImageView) view.findViewById(R.id.photo);
95 view.setTag(holder);
96 } else {
97 view = convertView;
98 holder = (RawContactViewHolder) view.getTag();
99 }
100 final RawContact rawContact = mRawContactsMetadata.rawContacts.get(position);
101 final AccountType account = mAccountTypeManager.getAccountType(rawContact.accountType,
102 rawContact.accountDataSet);
103
104 String displayName =
Gary Maiba10be22016-10-12 17:41:15 -0700105 mPreferences.getDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY
Gary Mai3a533282016-11-08 19:02:26 +0000106 ? rawContact.displayName : rawContact.displayNameAlt;
Gary Maia6c80b32016-09-30 16:34:55 -0700107
Gary Maiba10be22016-10-12 17:41:15 -0700108 if (TextUtils.isEmpty(displayName)) {
109 displayName = mContext.getString(R.string.missing_name);
110 }
Gary Mai27e8c3a2016-10-27 10:27:46 -0700111 holder.displayName.setText(displayName);
Gary Maiba10be22016-10-12 17:41:15 -0700112
113 final String accountDisplayLabel;
114
115 // Use the same string as editor if it's an editable user profile raw contact.
Gary Maia1721802016-11-01 17:55:31 -0700116 if (mRawContactsMetadata.isUserProfile && account.areContactsWritable()) {
Gary Maiba10be22016-10-12 17:41:15 -0700117 final AccountDisplayInfo displayInfo =
118 mAccountDisplayInfoFactory.getAccountDisplayInfo(
Gary Maia1721802016-11-01 17:55:31 -0700119 new AccountWithDataSet(rawContact.accountName,
120 rawContact.accountType, rawContact.accountDataSet));
Gary Maiba10be22016-10-12 17:41:15 -0700121 accountDisplayLabel = EditorUiUtils.getAccountHeaderLabelForMyProfile(mContext,
122 displayInfo);
Gary Maia1721802016-11-01 17:55:31 -0700123 } else if (GoogleAccountType.ACCOUNT_TYPE.equals(rawContact.accountType)
Gary Maiba10be22016-10-12 17:41:15 -0700124 && account.dataSet == null) {
125 // Focus Google accounts have the account name shown
Gary Maia1721802016-11-01 17:55:31 -0700126 accountDisplayLabel = rawContact.accountName;
Gary Maiba10be22016-10-12 17:41:15 -0700127 } else {
128 accountDisplayLabel = account.getDisplayLabel(mContext).toString();
129 }
Gary Mai27e8c3a2016-10-27 10:27:46 -0700130
131 holder.accountName.setText(accountDisplayLabel);
132 holder.accountIcon.setImageDrawable(account.getDisplayIcon(mContext));
Gary Maia6c80b32016-09-30 16:34:55 -0700133 final ContactPhotoManager.DefaultImageRequest
134 request = new ContactPhotoManager.DefaultImageRequest(
Gary Maia1721802016-11-01 17:55:31 -0700135 displayName, String.valueOf(rawContact.id), /* isCircular = */ true);
Gary Mai27e8c3a2016-10-27 10:27:46 -0700136
Gary Mai3a533282016-11-08 19:02:26 +0000137 ContactPhotoManager.getInstance(mContext).loadThumbnail(holder.photo,
138 rawContact.photoId,
Gary Maia6c80b32016-09-30 16:34:55 -0700139 /* darkTheme = */ false,
140 /* isCircular = */ true,
141 request);
Gary Mai27e8c3a2016-10-27 10:27:46 -0700142 return view;
Gary Maia6c80b32016-09-30 16:34:55 -0700143 }
144
Gary Mai27e8c3a2016-10-27 10:27:46 -0700145 class RawContactViewHolder {
146 TextView displayName;
147 TextView accountName;
148 ImageView accountIcon;
149 ImageView photo;
150 }
Gary Maia6c80b32016-09-30 16:34:55 -0700151 }
152
Gary Maia1721802016-11-01 17:55:31 -0700153 private ListAdapter mAdapter;
Gary Maib9065dd2016-11-08 10:49:00 -0800154 private boolean mShouldFinishActivity = true;
Gary Maia6c80b32016-09-30 16:34:55 -0700155
Gary Maia1721802016-11-01 17:55:31 -0700156 public static PickRawContactDialogFragment getInstance(RawContactsMetadata metadata) {
Gary Maia6c80b32016-09-30 16:34:55 -0700157 final PickRawContactDialogFragment fragment = new PickRawContactDialogFragment();
Gary Mai445a7c62016-10-17 14:30:11 -0700158 final Bundle args = new Bundle();
Gary Maia1721802016-11-01 17:55:31 -0700159 args.putParcelable(ARGS_RAW_CONTACTS_METADATA, metadata);
Gary Mai445a7c62016-10-17 14:30:11 -0700160 fragment.setArguments(args);
Gary Maia6c80b32016-09-30 16:34:55 -0700161 return fragment;
162 }
163
164 @Override
165 public Dialog onCreateDialog(Bundle savedInstanceState) {
Gary Mai5336e6e2016-10-23 14:17:03 -0700166 if (!(getActivity() instanceof PickRawContactListener)) {
167 throw new IllegalArgumentException(
168 "Host activity doesn't implement PickRawContactListener");
169 }
Gary Maia1721802016-11-01 17:55:31 -0700170 final Bundle args = getArguments();
171 if (args == null) {
172 throw new IllegalArgumentException("Dialog created with no arguments");
173 }
174
175 final RawContactsMetadata metadata = args.getParcelable(ARGS_RAW_CONTACTS_METADATA);
176 if (metadata == null) {
177 throw new IllegalArgumentException("Dialog created with null RawContactsMetadata");
178 }
179
Gary Maia6c80b32016-09-30 16:34:55 -0700180 final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Gary Maia1721802016-11-01 17:55:31 -0700181 mAdapter = new RawContactAccountListAdapter(getContext(), metadata);
Gary Maib9065dd2016-11-08 10:49:00 -0800182 if (metadata.showReadOnly) {
183 builder.setTitle(R.string.contact_editor_pick_linked_contact_dialog_title);
Gary Mai59ebb1b2016-11-29 17:08:47 -0800184 // Only provide link editing options for non-user profile contacts.
185 if (!metadata.isUserProfile) {
186 builder.setPositiveButton(R.string.contact_editor_add_linked_contact,
187 new DialogInterface.OnClickListener() {
188 @Override
189 public void onClick(DialogInterface dialog, int which) {
190 mShouldFinishActivity = false;
191 final Intent intent = new Intent(getActivity(),
192 ContactSelectionActivity.class);
193 intent.setAction(UiIntentActions.PICK_JOIN_CONTACT_ACTION);
194 intent.putExtra(UiIntentActions.TARGET_CONTACT_ID_EXTRA_KEY,
195 metadata.contactId);
196 getActivity().startActivityForResult(intent, REQUEST_CODE_JOIN);
197 }
198 });
199 builder.setNegativeButton(R.string.contact_editor_unlink_contacts,
200 new DialogInterface.OnClickListener() {
201 @Override
202 public void onClick(DialogInterface dialog, int which) {
203 mShouldFinishActivity = false;
204 final SplitContactConfirmationDialogFragment splitDialog = new
205 SplitContactConfirmationDialogFragment();
206 splitDialog.show(getActivity().getFragmentManager(),
207 SplitContactConfirmationDialogFragment.TAG);
208 }
209 });
210 }
Gary Maib9065dd2016-11-08 10:49:00 -0800211 } else {
212 builder.setTitle(R.string.contact_editor_pick_raw_contact_to_edit_dialog_title);
213 }
Gary Maiea2f3572016-10-11 17:30:28 -0700214 builder.setAdapter(mAdapter, new DialogInterface.OnClickListener() {
Gary Maia6c80b32016-09-30 16:34:55 -0700215 @Override
216 public void onClick(DialogInterface dialog, int which) {
Gary Maiea2f3572016-10-11 17:30:28 -0700217 final long rawContactId = mAdapter.getItemId(which);
Gary Mai5336e6e2016-10-23 14:17:03 -0700218 ((PickRawContactListener) getActivity()).onPickRawContact(rawContactId);
Gary Maia6c80b32016-09-30 16:34:55 -0700219 }
220 });
221 builder.setCancelable(true);
yaolu341d1042016-11-07 15:46:46 -0800222 if (savedInstanceState == null) {
223 Logger.logEditorEvent(EditorEvent.EventType.SHOW_RAW_CONTACT_PICKER,
224 /* numberRawContacts */ mAdapter.getCount());
225 }
Gary Maia6c80b32016-09-30 16:34:55 -0700226 return builder.create();
227 }
228
229 @Override
230 public void onDismiss(DialogInterface dialog) {
231 super.onDismiss(dialog);
Gary Maib9065dd2016-11-08 10:49:00 -0800232 if (mShouldFinishActivity) {
233 finishActivity();
234 }
Gary Maia6c80b32016-09-30 16:34:55 -0700235 }
236
Gary Mai445a7c62016-10-17 14:30:11 -0700237 @Override
Walter Jang68de0a52016-10-29 15:52:54 -0700238 public Context getContext() {
239 return getActivity();
240 }
241
Gary Maia6c80b32016-09-30 16:34:55 -0700242 private void finishActivity() {
243 if (getActivity() != null && !getActivity().isFinishing()) {
244 getActivity().finish();
245 }
246 }
247}