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