Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 1 | package com.android.contacts.editor; |
| 2 | |
| 3 | import android.app.AlertDialog; |
| 4 | import android.app.Dialog; |
| 5 | import android.app.DialogFragment; |
| 6 | import android.content.Context; |
| 7 | import android.content.DialogInterface; |
Gary Mai | b9065dd | 2016-11-08 10:49:00 -0800 | [diff] [blame] | 8 | import android.content.Intent; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 9 | import android.os.Bundle; |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 10 | import android.text.TextUtils; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 11 | import android.view.LayoutInflater; |
| 12 | import android.view.View; |
| 13 | import android.view.ViewGroup; |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 14 | import android.widget.BaseAdapter; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 15 | import android.widget.ImageView; |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 16 | import android.widget.ListAdapter; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 17 | import android.widget.TextView; |
| 18 | |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 19 | import com.android.contacts.ContactPhotoManager; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 20 | import com.android.contacts.R; |
Gary Mai | b9065dd | 2016-11-08 10:49:00 -0800 | [diff] [blame] | 21 | import com.android.contacts.activities.ContactSelectionActivity; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 22 | import com.android.contacts.editor.PickRawContactLoader.RawContact; |
| 23 | import com.android.contacts.editor.PickRawContactLoader.RawContactsMetadata; |
| 24 | import com.android.contacts.list.UiIntentActions; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 25 | import com.android.contacts.logging.EditorEvent; |
| 26 | import com.android.contacts.logging.Logger; |
| 27 | import com.android.contacts.model.AccountTypeManager; |
| 28 | import com.android.contacts.model.account.AccountDisplayInfo; |
| 29 | import com.android.contacts.model.account.AccountDisplayInfoFactory; |
| 30 | import com.android.contacts.model.account.AccountType; |
| 31 | import com.android.contacts.model.account.AccountWithDataSet; |
| 32 | import com.android.contacts.model.account.GoogleAccountType; |
| 33 | import com.android.contacts.preference.ContactsPreferences; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
Gary Mai | 5336e6e | 2016-10-23 14:17:03 -0700 | [diff] [blame] | 36 | * Should only be started from an activity that implements {@link PickRawContactListener}. |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 37 | * Dialog containing the raw contacts that make up a contact. On selection the editor is loaded |
| 38 | * for the chosen raw contact. |
| 39 | */ |
| 40 | public class PickRawContactDialogFragment extends DialogFragment { |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 41 | private static final String ARGS_RAW_CONTACTS_METADATA = "rawContactsMetadata"; |
Gary Mai | b9065dd | 2016-11-08 10:49:00 -0800 | [diff] [blame] | 42 | private static final int REQUEST_CODE_JOIN = 3; |
Gary Mai | 445a7c6 | 2016-10-17 14:30:11 -0700 | [diff] [blame] | 43 | |
Gary Mai | 5336e6e | 2016-10-23 14:17:03 -0700 | [diff] [blame] | 44 | public interface PickRawContactListener { |
| 45 | void onPickRawContact(long rawContactId); |
| 46 | } |
| 47 | |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Used to list the account info for the given raw contacts list. |
| 50 | */ |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 51 | private final class RawContactAccountListAdapter extends BaseAdapter { |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 52 | private final LayoutInflater mInflater; |
| 53 | private final Context mContext; |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 54 | private final RawContactsMetadata mRawContactsMetadata; |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 55 | private final AccountDisplayInfoFactory mAccountDisplayInfoFactory; |
| 56 | private final AccountTypeManager mAccountTypeManager; |
| 57 | private final ContactsPreferences mPreferences; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 58 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 59 | public RawContactAccountListAdapter(Context context, |
| 60 | RawContactsMetadata rawContactsMetadata) { |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 61 | mContext = context; |
| 62 | mInflater = LayoutInflater.from(context); |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 63 | mAccountDisplayInfoFactory = AccountDisplayInfoFactory.forWritableAccounts(context); |
| 64 | mAccountTypeManager = AccountTypeManager.getInstance(context); |
| 65 | mPreferences = new ContactsPreferences(context); |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 66 | mRawContactsMetadata = rawContactsMetadata; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 70 | public int getCount() { |
| 71 | return mRawContactsMetadata.rawContacts.size(); |
| 72 | } |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 73 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 74 | @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 Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 105 | mPreferences.getDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY |
Gary Mai | 3a53328 | 2016-11-08 19:02:26 +0000 | [diff] [blame] | 106 | ? rawContact.displayName : rawContact.displayNameAlt; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 107 | |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 108 | if (TextUtils.isEmpty(displayName)) { |
| 109 | displayName = mContext.getString(R.string.missing_name); |
| 110 | } |
Gary Mai | 27e8c3a | 2016-10-27 10:27:46 -0700 | [diff] [blame] | 111 | holder.displayName.setText(displayName); |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 112 | |
| 113 | final String accountDisplayLabel; |
| 114 | |
| 115 | // Use the same string as editor if it's an editable user profile raw contact. |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 116 | if (mRawContactsMetadata.isUserProfile && account.areContactsWritable()) { |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 117 | final AccountDisplayInfo displayInfo = |
| 118 | mAccountDisplayInfoFactory.getAccountDisplayInfo( |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 119 | new AccountWithDataSet(rawContact.accountName, |
| 120 | rawContact.accountType, rawContact.accountDataSet)); |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 121 | accountDisplayLabel = EditorUiUtils.getAccountHeaderLabelForMyProfile(mContext, |
| 122 | displayInfo); |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 123 | } else if (GoogleAccountType.ACCOUNT_TYPE.equals(rawContact.accountType) |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 124 | && account.dataSet == null) { |
| 125 | // Focus Google accounts have the account name shown |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 126 | accountDisplayLabel = rawContact.accountName; |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 127 | } else { |
| 128 | accountDisplayLabel = account.getDisplayLabel(mContext).toString(); |
| 129 | } |
Gary Mai | 27e8c3a | 2016-10-27 10:27:46 -0700 | [diff] [blame] | 130 | |
| 131 | holder.accountName.setText(accountDisplayLabel); |
| 132 | holder.accountIcon.setImageDrawable(account.getDisplayIcon(mContext)); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 133 | final ContactPhotoManager.DefaultImageRequest |
| 134 | request = new ContactPhotoManager.DefaultImageRequest( |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 135 | displayName, String.valueOf(rawContact.id), /* isCircular = */ true); |
Gary Mai | 27e8c3a | 2016-10-27 10:27:46 -0700 | [diff] [blame] | 136 | |
Gary Mai | 3a53328 | 2016-11-08 19:02:26 +0000 | [diff] [blame] | 137 | ContactPhotoManager.getInstance(mContext).loadThumbnail(holder.photo, |
| 138 | rawContact.photoId, |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 139 | /* darkTheme = */ false, |
| 140 | /* isCircular = */ true, |
| 141 | request); |
Gary Mai | 27e8c3a | 2016-10-27 10:27:46 -0700 | [diff] [blame] | 142 | return view; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Gary Mai | 27e8c3a | 2016-10-27 10:27:46 -0700 | [diff] [blame] | 145 | class RawContactViewHolder { |
| 146 | TextView displayName; |
| 147 | TextView accountName; |
| 148 | ImageView accountIcon; |
| 149 | ImageView photo; |
| 150 | } |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 153 | private ListAdapter mAdapter; |
Gary Mai | b9065dd | 2016-11-08 10:49:00 -0800 | [diff] [blame] | 154 | private boolean mShouldFinishActivity = true; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 155 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 156 | public static PickRawContactDialogFragment getInstance(RawContactsMetadata metadata) { |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 157 | final PickRawContactDialogFragment fragment = new PickRawContactDialogFragment(); |
Gary Mai | 445a7c6 | 2016-10-17 14:30:11 -0700 | [diff] [blame] | 158 | final Bundle args = new Bundle(); |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 159 | args.putParcelable(ARGS_RAW_CONTACTS_METADATA, metadata); |
Gary Mai | 445a7c6 | 2016-10-17 14:30:11 -0700 | [diff] [blame] | 160 | fragment.setArguments(args); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 161 | return fragment; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
Gary Mai | 5336e6e | 2016-10-23 14:17:03 -0700 | [diff] [blame] | 166 | if (!(getActivity() instanceof PickRawContactListener)) { |
| 167 | throw new IllegalArgumentException( |
| 168 | "Host activity doesn't implement PickRawContactListener"); |
| 169 | } |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 170 | 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 Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 180 | final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 181 | mAdapter = new RawContactAccountListAdapter(getContext(), metadata); |
Gary Mai | b9065dd | 2016-11-08 10:49:00 -0800 | [diff] [blame] | 182 | if (metadata.showReadOnly) { |
| 183 | builder.setTitle(R.string.contact_editor_pick_linked_contact_dialog_title); |
Gary Mai | 59ebb1b | 2016-11-29 17:08:47 -0800 | [diff] [blame] | 184 | // 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 Mai | b9065dd | 2016-11-08 10:49:00 -0800 | [diff] [blame] | 211 | } else { |
| 212 | builder.setTitle(R.string.contact_editor_pick_raw_contact_to_edit_dialog_title); |
| 213 | } |
Gary Mai | ea2f357 | 2016-10-11 17:30:28 -0700 | [diff] [blame] | 214 | builder.setAdapter(mAdapter, new DialogInterface.OnClickListener() { |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 215 | @Override |
| 216 | public void onClick(DialogInterface dialog, int which) { |
Gary Mai | ea2f357 | 2016-10-11 17:30:28 -0700 | [diff] [blame] | 217 | final long rawContactId = mAdapter.getItemId(which); |
Gary Mai | 5336e6e | 2016-10-23 14:17:03 -0700 | [diff] [blame] | 218 | ((PickRawContactListener) getActivity()).onPickRawContact(rawContactId); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 219 | } |
| 220 | }); |
| 221 | builder.setCancelable(true); |
yaolu | 341d104 | 2016-11-07 15:46:46 -0800 | [diff] [blame] | 222 | if (savedInstanceState == null) { |
| 223 | Logger.logEditorEvent(EditorEvent.EventType.SHOW_RAW_CONTACT_PICKER, |
| 224 | /* numberRawContacts */ mAdapter.getCount()); |
| 225 | } |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 226 | return builder.create(); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void onDismiss(DialogInterface dialog) { |
| 231 | super.onDismiss(dialog); |
Gary Mai | b9065dd | 2016-11-08 10:49:00 -0800 | [diff] [blame] | 232 | if (mShouldFinishActivity) { |
| 233 | finishActivity(); |
| 234 | } |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Gary Mai | 445a7c6 | 2016-10-17 14:30:11 -0700 | [diff] [blame] | 237 | @Override |
Walter Jang | 68de0a5 | 2016-10-29 15:52:54 -0700 | [diff] [blame] | 238 | public Context getContext() { |
| 239 | return getActivity(); |
| 240 | } |
| 241 | |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 242 | private void finishActivity() { |
| 243 | if (getActivity() != null && !getActivity().isFinishing()) { |
| 244 | getActivity().finish(); |
| 245 | } |
| 246 | } |
| 247 | } |