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