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 | 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; |
| 24 | import com.android.contacts.common.model.account.AccountType; |
| 25 | import com.android.contacts.common.preference.ContactsPreferences; |
| 26 | import com.android.contacts.common.util.ImplicitIntentsUtil; |
| 27 | import com.android.contacts.common.util.MaterialColorMapUtils.MaterialPalette; |
| 28 | |
| 29 | /** |
| 30 | * Dialog containing the raw contacts that make up a contact. On selection the editor is loaded |
| 31 | * for the chosen raw contact. |
| 32 | */ |
| 33 | public class PickRawContactDialogFragment extends DialogFragment { |
| 34 | /** |
| 35 | * Used to list the account info for the given raw contacts list. |
| 36 | */ |
| 37 | private static final class RawContactAccountListAdapter extends CursorAdapter { |
| 38 | private final LayoutInflater mInflater; |
| 39 | private final Context mContext; |
| 40 | |
| 41 | public RawContactAccountListAdapter(Context context, Cursor cursor) { |
| 42 | super(context, cursor, 0); |
| 43 | mContext = context; |
| 44 | mInflater = LayoutInflater.from(context); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public void bindView(View view, Context context, Cursor cursor) { |
| 49 | final long rawContactId = cursor.getLong(PickRawContactLoader.RAW_CONTACT_ID); |
| 50 | final String accountName = cursor.getString(PickRawContactLoader.ACCOUNT_NAME); |
| 51 | final String accountType = cursor.getString(PickRawContactLoader.ACCOUNT_TYPE); |
| 52 | final String dataSet = cursor.getString(PickRawContactLoader.DATA_SET); |
| 53 | final AccountType account = AccountTypeManager.getInstance(mContext) |
| 54 | .getAccountType(accountType, dataSet); |
| 55 | |
| 56 | final ContactsPreferences prefs = new ContactsPreferences(mContext); |
| 57 | final int displayNameColumn = |
| 58 | prefs.getDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY |
| 59 | ? PickRawContactLoader.DISPLAY_NAME_PRIMARY |
| 60 | : PickRawContactLoader.DISPLAY_NAME_ALTERNATIVE; |
| 61 | String displayName = cursor.getString(displayNameColumn); |
| 62 | |
| 63 | final TextView nameView = (TextView) view.findViewById( |
| 64 | R.id.display_name); |
| 65 | final TextView accountTextView = (TextView) view.findViewById( |
| 66 | R.id.account_name); |
| 67 | final ImageView accountIconView = (ImageView) view.findViewById( |
| 68 | R.id.account_icon); |
| 69 | |
| 70 | if (!account.areContactsWritable()) { |
| 71 | displayName = mContext |
| 72 | .getString(R.string.contact_editor_pick_raw_contact_read_only, displayName); |
| 73 | view.setAlpha(.38f); |
| 74 | } else { |
| 75 | view.setAlpha(1f); |
| 76 | } |
| 77 | |
| 78 | nameView.setText(displayName); |
| 79 | accountTextView.setText(accountName); |
| 80 | accountIconView.setImageDrawable(account.getDisplayIcon(mContext)); |
| 81 | |
| 82 | final ContactPhotoManager.DefaultImageRequest |
| 83 | request = new ContactPhotoManager.DefaultImageRequest( |
| 84 | displayName, String.valueOf(rawContactId), /* isCircular = */ true); |
| 85 | final ImageView photoView = (ImageView) view.findViewById( |
| 86 | R.id.photo); |
Gary Mai | ac70468 | 2016-10-11 15:29:41 -0700 | [diff] [blame^] | 87 | final Uri photoUri = Uri.withAppendedPath( |
| 88 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId), |
| 89 | RawContacts.DisplayPhoto.CONTENT_DIRECTORY); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 90 | ContactPhotoManager.getInstance(mContext).loadDirectoryPhoto(photoView, |
Gary Mai | ac70468 | 2016-10-11 15:29:41 -0700 | [diff] [blame^] | 91 | photoUri, |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 92 | /* darkTheme = */ false, |
| 93 | /* isCircular = */ true, |
| 94 | request); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
| 99 | return mInflater.inflate(R.layout.raw_contact_list_item, parent, false); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public long getItemId(int position) { |
| 104 | getCursor().moveToPosition(position); |
| 105 | return getCursor().getLong(PickRawContactLoader.RAW_CONTACT_ID); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Cursor holding all raw contact rows for the given Contact. |
| 110 | private Cursor mCursor; |
| 111 | // Uri for the whole Contact. |
| 112 | private Uri mUri; |
| 113 | private MaterialPalette mMaterialPalette; |
| 114 | |
| 115 | public static PickRawContactDialogFragment getInstance(Uri uri, Cursor cursor, |
| 116 | MaterialPalette materialPalette) { |
| 117 | final PickRawContactDialogFragment fragment = new PickRawContactDialogFragment(); |
| 118 | fragment.setUri(uri); |
| 119 | fragment.setCursor(cursor); |
| 120 | fragment.setMaterialPalette(materialPalette); |
| 121 | return fragment; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 126 | final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
| 127 | final CursorAdapter adapter = new RawContactAccountListAdapter(getContext(), mCursor); |
| 128 | builder.setTitle(R.string.contact_editor_pick_raw_contact_dialog_title); |
| 129 | builder.setAdapter(adapter, new DialogInterface.OnClickListener() { |
| 130 | @Override |
| 131 | public void onClick(DialogInterface dialog, int which) { |
| 132 | final long rawContactId = adapter.getItemId(which); |
| 133 | final Intent intent = EditorIntents.createEditContactIntentForRawContact( |
| 134 | getActivity(), mUri, rawContactId, mMaterialPalette); |
| 135 | intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); |
| 136 | ImplicitIntentsUtil.startActivityInApp(getActivity(), intent); |
| 137 | } |
| 138 | }); |
| 139 | builder.setCancelable(true); |
| 140 | return builder.create(); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void onDismiss(DialogInterface dialog) { |
| 145 | super.onDismiss(dialog); |
| 146 | mCursor = null; |
| 147 | finishActivity(); |
| 148 | } |
| 149 | |
| 150 | private void setUri(Uri uri) { |
| 151 | mUri = uri; |
| 152 | } |
| 153 | |
| 154 | private void setCursor(Cursor cursor) { |
| 155 | mCursor = cursor; |
| 156 | } |
| 157 | |
| 158 | private void setMaterialPalette(MaterialPalette materialPalette) { |
| 159 | mMaterialPalette = materialPalette; |
| 160 | } |
| 161 | |
| 162 | private void finishActivity() { |
| 163 | if (getActivity() != null && !getActivity().isFinishing()) { |
| 164 | getActivity().finish(); |
| 165 | } |
| 166 | } |
| 167 | } |