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; |
Gary Mai | ac70468 | 2016-10-11 15:29:41 -0700 | [diff] [blame] | 6 | import android.content.ContentUris; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 7 | import android.content.Context; |
| 8 | import android.content.DialogInterface; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 9 | import android.net.Uri; |
| 10 | import android.os.Bundle; |
Gary Mai | ac70468 | 2016-10-11 15:29:41 -0700 | [diff] [blame] | 11 | import android.provider.ContactsContract.RawContacts; |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 12 | import android.text.TextUtils; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 13 | import android.view.LayoutInflater; |
| 14 | import android.view.View; |
| 15 | import android.view.ViewGroup; |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 16 | import android.widget.BaseAdapter; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 17 | import android.widget.ImageView; |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 18 | import android.widget.ListAdapter; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 19 | import android.widget.TextView; |
| 20 | |
| 21 | import com.android.contacts.R; |
| 22 | import com.android.contacts.common.ContactPhotoManager; |
| 23 | import com.android.contacts.common.model.AccountTypeManager; |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 24 | import com.android.contacts.common.model.account.AccountDisplayInfo; |
| 25 | import com.android.contacts.common.model.account.AccountDisplayInfoFactory; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 26 | import com.android.contacts.common.model.account.AccountType; |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 27 | import com.android.contacts.common.model.account.AccountWithDataSet; |
| 28 | import com.android.contacts.common.model.account.GoogleAccountType; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 29 | import com.android.contacts.common.preference.ContactsPreferences; |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 30 | import com.android.contacts.editor.PickRawContactLoader.RawContact; |
| 31 | import com.android.contacts.editor.PickRawContactLoader.RawContactsMetadata; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
Gary Mai | 5336e6e | 2016-10-23 14:17:03 -0700 | [diff] [blame] | 34 | * Should only be started from an activity that implements {@link PickRawContactListener}. |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 35 | * Dialog containing the raw contacts that make up a contact. On selection the editor is loaded |
| 36 | * for the chosen raw contact. |
| 37 | */ |
| 38 | public class PickRawContactDialogFragment extends DialogFragment { |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 39 | private static final String ARGS_RAW_CONTACTS_METADATA = "rawContactsMetadata"; |
Gary Mai | 445a7c6 | 2016-10-17 14:30:11 -0700 | [diff] [blame] | 40 | |
Gary Mai | 5336e6e | 2016-10-23 14:17:03 -0700 | [diff] [blame] | 41 | public interface PickRawContactListener { |
| 42 | void onPickRawContact(long rawContactId); |
| 43 | } |
| 44 | |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Used to list the account info for the given raw contacts list. |
| 47 | */ |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 48 | private final class RawContactAccountListAdapter extends BaseAdapter { |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 49 | private final LayoutInflater mInflater; |
| 50 | private final Context mContext; |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 51 | private final RawContactsMetadata mRawContactsMetadata; |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 52 | private final AccountDisplayInfoFactory mAccountDisplayInfoFactory; |
| 53 | private final AccountTypeManager mAccountTypeManager; |
| 54 | private final ContactsPreferences mPreferences; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 55 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 56 | public RawContactAccountListAdapter(Context context, |
| 57 | RawContactsMetadata rawContactsMetadata) { |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 58 | mContext = context; |
| 59 | mInflater = LayoutInflater.from(context); |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 60 | mAccountDisplayInfoFactory = AccountDisplayInfoFactory.forWritableAccounts(context); |
| 61 | mAccountTypeManager = AccountTypeManager.getInstance(context); |
| 62 | mPreferences = new ContactsPreferences(context); |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 63 | mRawContactsMetadata = rawContactsMetadata; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 67 | public int getCount() { |
| 68 | return mRawContactsMetadata.rawContacts.size(); |
| 69 | } |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 70 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 71 | @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 Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 102 | mPreferences.getDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 103 | ? rawContact.displayName : rawContact.displayNameAlt; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 104 | |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 105 | if (TextUtils.isEmpty(displayName)) { |
| 106 | displayName = mContext.getString(R.string.missing_name); |
| 107 | } |
Gary Mai | 27e8c3a | 2016-10-27 10:27:46 -0700 | [diff] [blame] | 108 | holder.displayName.setText(displayName); |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 109 | |
| 110 | final String accountDisplayLabel; |
| 111 | |
| 112 | // 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^] | 113 | if (mRawContactsMetadata.isUserProfile && account.areContactsWritable()) { |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 114 | final AccountDisplayInfo displayInfo = |
| 115 | mAccountDisplayInfoFactory.getAccountDisplayInfo( |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 116 | new AccountWithDataSet(rawContact.accountName, |
| 117 | rawContact.accountType, rawContact.accountDataSet)); |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 118 | accountDisplayLabel = EditorUiUtils.getAccountHeaderLabelForMyProfile(mContext, |
| 119 | displayInfo); |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 120 | } else if (GoogleAccountType.ACCOUNT_TYPE.equals(rawContact.accountType) |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 121 | && account.dataSet == null) { |
| 122 | // Focus Google accounts have the account name shown |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 123 | accountDisplayLabel = rawContact.accountName; |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 124 | } else { |
| 125 | accountDisplayLabel = account.getDisplayLabel(mContext).toString(); |
| 126 | } |
Gary Mai | 27e8c3a | 2016-10-27 10:27:46 -0700 | [diff] [blame] | 127 | |
| 128 | holder.accountName.setText(accountDisplayLabel); |
| 129 | holder.accountIcon.setImageDrawable(account.getDisplayIcon(mContext)); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 130 | final ContactPhotoManager.DefaultImageRequest |
| 131 | request = new ContactPhotoManager.DefaultImageRequest( |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 132 | displayName, String.valueOf(rawContact.id), /* isCircular = */ true); |
Gary Mai | ac70468 | 2016-10-11 15:29:41 -0700 | [diff] [blame] | 133 | final Uri photoUri = Uri.withAppendedPath( |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 134 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContact.id), |
Gary Mai | ac70468 | 2016-10-11 15:29:41 -0700 | [diff] [blame] | 135 | RawContacts.DisplayPhoto.CONTENT_DIRECTORY); |
Gary Mai | 27e8c3a | 2016-10-27 10:27:46 -0700 | [diff] [blame] | 136 | |
| 137 | ContactPhotoManager.getInstance(mContext).loadDirectoryPhoto(holder.photo, |
Gary Mai | ac70468 | 2016-10-11 15:29:41 -0700 | [diff] [blame] | 138 | photoUri, |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 139 | /* darkTheme = */ false, |
| 140 | /* isCircular = */ true, |
| 141 | request); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 142 | |
Gary Mai | 27e8c3a | 2016-10-27 10:27:46 -0700 | [diff] [blame] | 143 | return view; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Gary Mai | 27e8c3a | 2016-10-27 10:27:46 -0700 | [diff] [blame] | 146 | class RawContactViewHolder { |
| 147 | TextView displayName; |
| 148 | TextView accountName; |
| 149 | ImageView accountIcon; |
| 150 | ImageView photo; |
| 151 | } |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame^] | 154 | private ListAdapter mAdapter; |
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); |
| 182 | builder.setTitle(R.string.contact_editor_pick_raw_contact_to_edit_dialog_title); |
Gary Mai | ea2f357 | 2016-10-11 17:30:28 -0700 | [diff] [blame] | 183 | builder.setAdapter(mAdapter, new DialogInterface.OnClickListener() { |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 184 | @Override |
| 185 | public void onClick(DialogInterface dialog, int which) { |
Gary Mai | ea2f357 | 2016-10-11 17:30:28 -0700 | [diff] [blame] | 186 | final long rawContactId = mAdapter.getItemId(which); |
Gary Mai | 5336e6e | 2016-10-23 14:17:03 -0700 | [diff] [blame] | 187 | ((PickRawContactListener) getActivity()).onPickRawContact(rawContactId); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 188 | } |
| 189 | }); |
| 190 | builder.setCancelable(true); |
| 191 | return builder.create(); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public void onDismiss(DialogInterface dialog) { |
| 196 | super.onDismiss(dialog); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 197 | finishActivity(); |
| 198 | } |
| 199 | |
Gary Mai | 445a7c6 | 2016-10-17 14:30:11 -0700 | [diff] [blame] | 200 | @Override |
Walter Jang | 68de0a5 | 2016-10-29 15:52:54 -0700 | [diff] [blame] | 201 | public Context getContext() { |
| 202 | return getActivity(); |
| 203 | } |
| 204 | |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 205 | private void finishActivity() { |
| 206 | if (getActivity() != null && !getActivity().isFinishing()) { |
| 207 | getActivity().finish(); |
| 208 | } |
| 209 | } |
| 210 | } |