blob: aa3e0159c06b20f6b9198bd76bc383bbdf026755 [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;
Gary Maiac704682016-10-11 15:29:41 -07006import android.content.ContentUris;
Gary Maia6c80b32016-09-30 16:34:55 -07007import android.content.Context;
8import android.content.DialogInterface;
Gary Maia6c80b32016-09-30 16:34:55 -07009import android.net.Uri;
10import android.os.Bundle;
Gary Maiac704682016-10-11 15:29:41 -070011import android.provider.ContactsContract.RawContacts;
Gary Maiba10be22016-10-12 17:41:15 -070012import android.text.TextUtils;
Gary Maia6c80b32016-09-30 16:34:55 -070013import android.view.LayoutInflater;
14import android.view.View;
15import android.view.ViewGroup;
Gary Maia1721802016-11-01 17:55:31 -070016import android.widget.BaseAdapter;
Gary Maia6c80b32016-09-30 16:34:55 -070017import android.widget.ImageView;
Gary Maia1721802016-11-01 17:55:31 -070018import android.widget.ListAdapter;
Gary Maia6c80b32016-09-30 16:34:55 -070019import android.widget.TextView;
20
21import com.android.contacts.R;
22import com.android.contacts.common.ContactPhotoManager;
23import com.android.contacts.common.model.AccountTypeManager;
Gary Maiba10be22016-10-12 17:41:15 -070024import com.android.contacts.common.model.account.AccountDisplayInfo;
25import com.android.contacts.common.model.account.AccountDisplayInfoFactory;
Gary Maia6c80b32016-09-30 16:34:55 -070026import com.android.contacts.common.model.account.AccountType;
Gary Maiba10be22016-10-12 17:41:15 -070027import com.android.contacts.common.model.account.AccountWithDataSet;
28import com.android.contacts.common.model.account.GoogleAccountType;
Gary Maia6c80b32016-09-30 16:34:55 -070029import com.android.contacts.common.preference.ContactsPreferences;
Gary Maia1721802016-11-01 17:55:31 -070030import com.android.contacts.editor.PickRawContactLoader.RawContact;
31import com.android.contacts.editor.PickRawContactLoader.RawContactsMetadata;
Gary Maia6c80b32016-09-30 16:34:55 -070032
33/**
Gary Mai5336e6e2016-10-23 14:17:03 -070034 * Should only be started from an activity that implements {@link PickRawContactListener}.
Gary Maia6c80b32016-09-30 16:34:55 -070035 * Dialog containing the raw contacts that make up a contact. On selection the editor is loaded
36 * for the chosen raw contact.
37 */
38public class PickRawContactDialogFragment extends DialogFragment {
Gary Maia1721802016-11-01 17:55:31 -070039 private static final String ARGS_RAW_CONTACTS_METADATA = "rawContactsMetadata";
Gary Mai445a7c62016-10-17 14:30:11 -070040
Gary Mai5336e6e2016-10-23 14:17:03 -070041 public interface PickRawContactListener {
42 void onPickRawContact(long rawContactId);
43 }
44
Gary Maia6c80b32016-09-30 16:34:55 -070045 /**
46 * Used to list the account info for the given raw contacts list.
47 */
Gary Maia1721802016-11-01 17:55:31 -070048 private final class RawContactAccountListAdapter extends BaseAdapter {
Gary Maia6c80b32016-09-30 16:34:55 -070049 private final LayoutInflater mInflater;
50 private final Context mContext;
Gary Maia1721802016-11-01 17:55:31 -070051 private final RawContactsMetadata mRawContactsMetadata;
Gary Maiba10be22016-10-12 17:41:15 -070052 private final AccountDisplayInfoFactory mAccountDisplayInfoFactory;
53 private final AccountTypeManager mAccountTypeManager;
54 private final ContactsPreferences mPreferences;
Gary Maia6c80b32016-09-30 16:34:55 -070055
Gary Maia1721802016-11-01 17:55:31 -070056 public RawContactAccountListAdapter(Context context,
57 RawContactsMetadata rawContactsMetadata) {
Gary Maia6c80b32016-09-30 16:34:55 -070058 mContext = context;
59 mInflater = LayoutInflater.from(context);
Gary Maiba10be22016-10-12 17:41:15 -070060 mAccountDisplayInfoFactory = AccountDisplayInfoFactory.forWritableAccounts(context);
61 mAccountTypeManager = AccountTypeManager.getInstance(context);
62 mPreferences = new ContactsPreferences(context);
Gary Maia1721802016-11-01 17:55:31 -070063 mRawContactsMetadata = rawContactsMetadata;
Gary Maia6c80b32016-09-30 16:34:55 -070064 }
65
66 @Override
Gary Maia1721802016-11-01 17:55:31 -070067 public int getCount() {
68 return mRawContactsMetadata.rawContacts.size();
69 }
Gary Maia6c80b32016-09-30 16:34:55 -070070
Gary Maia1721802016-11-01 17:55:31 -070071 @Override
72 public Object getItem(int position) {
73 return mRawContactsMetadata.rawContacts.get(position);
74 }
75
76 @Override
77 public long getItemId(int position) {
78 return mRawContactsMetadata.rawContacts.get(position).id;
79 }
80
81 @Override
82 public View getView(int position, View convertView, ViewGroup parent) {
83 final View view;
84 final RawContactViewHolder holder;
85 if (convertView == null) {
86 view = mInflater.inflate(R.layout.raw_contact_list_item, parent, false);
87 holder = new RawContactViewHolder();
88 holder.displayName = (TextView) view.findViewById(R.id.display_name);
89 holder.accountName = (TextView) view.findViewById(R.id.account_name);
90 holder.accountIcon = (ImageView) view.findViewById(R.id.account_icon);
91 holder.photo = (ImageView) view.findViewById(R.id.photo);
92 view.setTag(holder);
93 } else {
94 view = convertView;
95 holder = (RawContactViewHolder) view.getTag();
96 }
97 final RawContact rawContact = mRawContactsMetadata.rawContacts.get(position);
98 final AccountType account = mAccountTypeManager.getAccountType(rawContact.accountType,
99 rawContact.accountDataSet);
100
101 String displayName =
Gary Maiba10be22016-10-12 17:41:15 -0700102 mPreferences.getDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY
Gary Maia1721802016-11-01 17:55:31 -0700103 ? rawContact.displayName : rawContact.displayNameAlt;
Gary Maia6c80b32016-09-30 16:34:55 -0700104
Gary Maiba10be22016-10-12 17:41:15 -0700105 if (TextUtils.isEmpty(displayName)) {
106 displayName = mContext.getString(R.string.missing_name);
107 }
Gary Mai27e8c3a2016-10-27 10:27:46 -0700108 holder.displayName.setText(displayName);
Gary Maiba10be22016-10-12 17:41:15 -0700109
110 final String accountDisplayLabel;
111
112 // Use the same string as editor if it's an editable user profile raw contact.
Gary Maia1721802016-11-01 17:55:31 -0700113 if (mRawContactsMetadata.isUserProfile && account.areContactsWritable()) {
Gary Maiba10be22016-10-12 17:41:15 -0700114 final AccountDisplayInfo displayInfo =
115 mAccountDisplayInfoFactory.getAccountDisplayInfo(
Gary Maia1721802016-11-01 17:55:31 -0700116 new AccountWithDataSet(rawContact.accountName,
117 rawContact.accountType, rawContact.accountDataSet));
Gary Maiba10be22016-10-12 17:41:15 -0700118 accountDisplayLabel = EditorUiUtils.getAccountHeaderLabelForMyProfile(mContext,
119 displayInfo);
Gary Maia1721802016-11-01 17:55:31 -0700120 } else if (GoogleAccountType.ACCOUNT_TYPE.equals(rawContact.accountType)
Gary Maiba10be22016-10-12 17:41:15 -0700121 && account.dataSet == null) {
122 // Focus Google accounts have the account name shown
Gary Maia1721802016-11-01 17:55:31 -0700123 accountDisplayLabel = rawContact.accountName;
Gary Maiba10be22016-10-12 17:41:15 -0700124 } else {
125 accountDisplayLabel = account.getDisplayLabel(mContext).toString();
126 }
Gary Mai27e8c3a2016-10-27 10:27:46 -0700127
128 holder.accountName.setText(accountDisplayLabel);
129 holder.accountIcon.setImageDrawable(account.getDisplayIcon(mContext));
Gary Maia6c80b32016-09-30 16:34:55 -0700130 final ContactPhotoManager.DefaultImageRequest
131 request = new ContactPhotoManager.DefaultImageRequest(
Gary Maia1721802016-11-01 17:55:31 -0700132 displayName, String.valueOf(rawContact.id), /* isCircular = */ true);
Gary Maiac704682016-10-11 15:29:41 -0700133 final Uri photoUri = Uri.withAppendedPath(
Gary Maia1721802016-11-01 17:55:31 -0700134 ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContact.id),
Gary Maiac704682016-10-11 15:29:41 -0700135 RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
Gary Mai27e8c3a2016-10-27 10:27:46 -0700136
137 ContactPhotoManager.getInstance(mContext).loadDirectoryPhoto(holder.photo,
Gary Maiac704682016-10-11 15:29:41 -0700138 photoUri,
Gary Maia6c80b32016-09-30 16:34:55 -0700139 /* darkTheme = */ false,
140 /* isCircular = */ true,
141 request);
Gary Maia6c80b32016-09-30 16:34:55 -0700142
Gary Mai27e8c3a2016-10-27 10:27:46 -0700143 return view;
Gary Maia6c80b32016-09-30 16:34:55 -0700144 }
145
Gary Mai27e8c3a2016-10-27 10:27:46 -0700146 class RawContactViewHolder {
147 TextView displayName;
148 TextView accountName;
149 ImageView accountIcon;
150 ImageView photo;
151 }
Gary Maia6c80b32016-09-30 16:34:55 -0700152 }
153
Gary Maia1721802016-11-01 17:55:31 -0700154 private ListAdapter mAdapter;
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);
182 builder.setTitle(R.string.contact_editor_pick_raw_contact_to_edit_dialog_title);
Gary Maiea2f3572016-10-11 17:30:28 -0700183 builder.setAdapter(mAdapter, new DialogInterface.OnClickListener() {
Gary Maia6c80b32016-09-30 16:34:55 -0700184 @Override
185 public void onClick(DialogInterface dialog, int which) {
Gary Maiea2f3572016-10-11 17:30:28 -0700186 final long rawContactId = mAdapter.getItemId(which);
Gary Mai5336e6e2016-10-23 14:17:03 -0700187 ((PickRawContactListener) getActivity()).onPickRawContact(rawContactId);
Gary Maia6c80b32016-09-30 16:34:55 -0700188 }
189 });
190 builder.setCancelable(true);
191 return builder.create();
192 }
193
194 @Override
195 public void onDismiss(DialogInterface dialog) {
196 super.onDismiss(dialog);
Gary Maia6c80b32016-09-30 16:34:55 -0700197 finishActivity();
198 }
199
Gary Mai445a7c62016-10-17 14:30:11 -0700200 @Override
Walter Jang68de0a52016-10-29 15:52:54 -0700201 public Context getContext() {
202 return getActivity();
203 }
204
Gary Maia6c80b32016-09-30 16:34:55 -0700205 private void finishActivity() {
206 if (getActivity() != null && !getActivity().isFinishing()) {
207 getActivity().finish();
208 }
209 }
210}