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.database.Cursor; |
| 10 | import android.net.Uri; |
| 11 | import android.os.Bundle; |
Gary Mai | ac70468 | 2016-10-11 15:29:41 -0700 | [diff] [blame] | 12 | import android.provider.ContactsContract.RawContacts; |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 13 | import android.text.TextUtils; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 14 | import android.view.LayoutInflater; |
| 15 | import android.view.View; |
| 16 | import android.view.ViewGroup; |
| 17 | import android.widget.CursorAdapter; |
| 18 | import android.widget.ImageView; |
| 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 | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
Gary Mai | 5336e6e | 2016-10-23 14:17:03 -0700 | [diff] [blame] | 32 | * Should only be started from an activity that implements {@link PickRawContactListener}. |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 33 | * Dialog containing the raw contacts that make up a contact. On selection the editor is loaded |
| 34 | * for the chosen raw contact. |
| 35 | */ |
| 36 | public class PickRawContactDialogFragment extends DialogFragment { |
Gary Mai | 445a7c6 | 2016-10-17 14:30:11 -0700 | [diff] [blame] | 37 | private static final String ARGS_IS_USER_PROFILE = "isUserProfile"; |
| 38 | |
Gary Mai | 5336e6e | 2016-10-23 14:17:03 -0700 | [diff] [blame] | 39 | public interface PickRawContactListener { |
| 40 | void onPickRawContact(long rawContactId); |
| 41 | } |
| 42 | |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Used to list the account info for the given raw contacts list. |
| 45 | */ |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 46 | private final class RawContactAccountListAdapter extends CursorAdapter { |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 47 | private final LayoutInflater mInflater; |
| 48 | private final Context mContext; |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 49 | private final AccountDisplayInfoFactory mAccountDisplayInfoFactory; |
| 50 | private final AccountTypeManager mAccountTypeManager; |
| 51 | private final ContactsPreferences mPreferences; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 52 | |
| 53 | public RawContactAccountListAdapter(Context context, Cursor cursor) { |
| 54 | super(context, cursor, 0); |
| 55 | mContext = context; |
| 56 | mInflater = LayoutInflater.from(context); |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 57 | mAccountDisplayInfoFactory = AccountDisplayInfoFactory.forWritableAccounts(context); |
| 58 | mAccountTypeManager = AccountTypeManager.getInstance(context); |
| 59 | mPreferences = new ContactsPreferences(context); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void bindView(View view, Context context, Cursor cursor) { |
| 64 | final long rawContactId = cursor.getLong(PickRawContactLoader.RAW_CONTACT_ID); |
| 65 | final String accountName = cursor.getString(PickRawContactLoader.ACCOUNT_NAME); |
| 66 | final String accountType = cursor.getString(PickRawContactLoader.ACCOUNT_TYPE); |
| 67 | final String dataSet = cursor.getString(PickRawContactLoader.DATA_SET); |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 68 | final AccountType account = mAccountTypeManager.getAccountType(accountType, dataSet); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 69 | |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 70 | final int displayNameColumn = |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 71 | mPreferences.getDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 72 | ? PickRawContactLoader.DISPLAY_NAME_PRIMARY |
| 73 | : PickRawContactLoader.DISPLAY_NAME_ALTERNATIVE; |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 74 | |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 75 | String displayName = cursor.getString(displayNameColumn); |
| 76 | |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 77 | if (TextUtils.isEmpty(displayName)) { |
| 78 | displayName = mContext.getString(R.string.missing_name); |
| 79 | } |
Gary Mai | 27e8c3a | 2016-10-27 10:27:46 -0700 | [diff] [blame^] | 80 | final RawContactViewHolder holder = (RawContactViewHolder) view.getTag(); |
| 81 | holder.displayName.setText(displayName); |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 82 | |
| 83 | final String accountDisplayLabel; |
| 84 | |
| 85 | // Use the same string as editor if it's an editable user profile raw contact. |
| 86 | if (mIsUserProfile && account.areContactsWritable()) { |
| 87 | final AccountDisplayInfo displayInfo = |
| 88 | mAccountDisplayInfoFactory.getAccountDisplayInfo( |
| 89 | new AccountWithDataSet(accountName, accountType, dataSet)); |
| 90 | accountDisplayLabel = EditorUiUtils.getAccountHeaderLabelForMyProfile(mContext, |
| 91 | displayInfo); |
| 92 | } |
| 93 | else if (GoogleAccountType.ACCOUNT_TYPE.equals(accountType) |
| 94 | && account.dataSet == null) { |
| 95 | // Focus Google accounts have the account name shown |
| 96 | accountDisplayLabel = accountName; |
| 97 | } else { |
| 98 | accountDisplayLabel = account.getDisplayLabel(mContext).toString(); |
| 99 | } |
Gary Mai | 27e8c3a | 2016-10-27 10:27:46 -0700 | [diff] [blame^] | 100 | |
| 101 | holder.accountName.setText(accountDisplayLabel); |
| 102 | holder.accountIcon.setImageDrawable(account.getDisplayIcon(mContext)); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 103 | |
| 104 | final ContactPhotoManager.DefaultImageRequest |
| 105 | request = new ContactPhotoManager.DefaultImageRequest( |
| 106 | displayName, String.valueOf(rawContactId), /* isCircular = */ true); |
Gary Mai | ac70468 | 2016-10-11 15:29:41 -0700 | [diff] [blame] | 107 | final Uri photoUri = Uri.withAppendedPath( |
| 108 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId), |
| 109 | RawContacts.DisplayPhoto.CONTENT_DIRECTORY); |
Gary Mai | 27e8c3a | 2016-10-27 10:27:46 -0700 | [diff] [blame^] | 110 | |
| 111 | ContactPhotoManager.getInstance(mContext).loadDirectoryPhoto(holder.photo, |
Gary Mai | ac70468 | 2016-10-11 15:29:41 -0700 | [diff] [blame] | 112 | photoUri, |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 113 | /* darkTheme = */ false, |
| 114 | /* isCircular = */ true, |
| 115 | request); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
Gary Mai | 27e8c3a | 2016-10-27 10:27:46 -0700 | [diff] [blame^] | 120 | final View view = mInflater.inflate(R.layout.raw_contact_list_item, parent, false); |
| 121 | final RawContactViewHolder holder = new RawContactViewHolder(); |
| 122 | holder.displayName = (TextView) view.findViewById(R.id.display_name); |
| 123 | holder.accountName = (TextView) view.findViewById(R.id.account_name); |
| 124 | holder.accountIcon = (ImageView) view.findViewById(R.id.account_icon); |
| 125 | holder.photo = (ImageView) view.findViewById(R.id.photo); |
| 126 | view.setTag(holder); |
| 127 | return view; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public long getItemId(int position) { |
| 132 | getCursor().moveToPosition(position); |
| 133 | return getCursor().getLong(PickRawContactLoader.RAW_CONTACT_ID); |
| 134 | } |
Gary Mai | 27e8c3a | 2016-10-27 10:27:46 -0700 | [diff] [blame^] | 135 | |
| 136 | class RawContactViewHolder { |
| 137 | TextView displayName; |
| 138 | TextView accountName; |
| 139 | ImageView accountIcon; |
| 140 | ImageView photo; |
| 141 | } |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // Cursor holding all raw contact rows for the given Contact. |
| 145 | private Cursor mCursor; |
Gary Mai | ea2f357 | 2016-10-11 17:30:28 -0700 | [diff] [blame] | 146 | private CursorAdapter mAdapter; |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 147 | private boolean mIsUserProfile; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 148 | |
Gary Mai | 5336e6e | 2016-10-23 14:17:03 -0700 | [diff] [blame] | 149 | public static PickRawContactDialogFragment getInstance(Cursor cursor, boolean isUserProfile) { |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 150 | final PickRawContactDialogFragment fragment = new PickRawContactDialogFragment(); |
Gary Mai | 445a7c6 | 2016-10-17 14:30:11 -0700 | [diff] [blame] | 151 | final Bundle args = new Bundle(); |
Gary Mai | 445a7c6 | 2016-10-17 14:30:11 -0700 | [diff] [blame] | 152 | args.putBoolean(ARGS_IS_USER_PROFILE, isUserProfile); |
| 153 | fragment.setArguments(args); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 154 | fragment.setCursor(cursor); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 155 | return fragment; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
Gary Mai | 5336e6e | 2016-10-23 14:17:03 -0700 | [diff] [blame] | 160 | if (!(getActivity() instanceof PickRawContactListener)) { |
| 161 | throw new IllegalArgumentException( |
| 162 | "Host activity doesn't implement PickRawContactListener"); |
| 163 | } |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 164 | final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
Gary Mai | ea2f357 | 2016-10-11 17:30:28 -0700 | [diff] [blame] | 165 | mAdapter = new RawContactAccountListAdapter(getContext(), mCursor); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 166 | builder.setTitle(R.string.contact_editor_pick_raw_contact_dialog_title); |
Gary Mai | ea2f357 | 2016-10-11 17:30:28 -0700 | [diff] [blame] | 167 | builder.setAdapter(mAdapter, new DialogInterface.OnClickListener() { |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 168 | @Override |
| 169 | public void onClick(DialogInterface dialog, int which) { |
Gary Mai | ea2f357 | 2016-10-11 17:30:28 -0700 | [diff] [blame] | 170 | final long rawContactId = mAdapter.getItemId(which); |
Gary Mai | 5336e6e | 2016-10-23 14:17:03 -0700 | [diff] [blame] | 171 | ((PickRawContactListener) getActivity()).onPickRawContact(rawContactId); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 172 | } |
| 173 | }); |
| 174 | builder.setCancelable(true); |
| 175 | return builder.create(); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void onDismiss(DialogInterface dialog) { |
| 180 | super.onDismiss(dialog); |
| 181 | mCursor = null; |
| 182 | finishActivity(); |
| 183 | } |
| 184 | |
Gary Mai | 445a7c6 | 2016-10-17 14:30:11 -0700 | [diff] [blame] | 185 | @Override |
| 186 | public void onCreate(Bundle savedInstanceState) { |
| 187 | super.onCreate(savedInstanceState); |
| 188 | |
| 189 | final Bundle args = getArguments(); |
| 190 | if (args != null) { |
Gary Mai | 445a7c6 | 2016-10-17 14:30:11 -0700 | [diff] [blame] | 191 | mIsUserProfile = args.getBoolean(ARGS_IS_USER_PROFILE); |
| 192 | } |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Gary Mai | ea2f357 | 2016-10-11 17:30:28 -0700 | [diff] [blame] | 195 | public void setCursor(Cursor cursor) { |
| 196 | if (mAdapter != null) { |
| 197 | mAdapter.swapCursor(cursor); |
| 198 | } |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 199 | mCursor = cursor; |
| 200 | } |
| 201 | |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 202 | private void finishActivity() { |
| 203 | if (getActivity() != null && !getActivity().isFinishing()) { |
| 204 | getActivity().finish(); |
| 205 | } |
| 206 | } |
| 207 | } |