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 | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 80 | |
| 81 | if (!account.areContactsWritable()) { |
| 82 | displayName = mContext |
| 83 | .getString(R.string.contact_editor_pick_raw_contact_read_only, displayName); |
| 84 | view.setAlpha(.38f); |
| 85 | } else { |
| 86 | view.setAlpha(1f); |
| 87 | } |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 88 | final TextView nameView = (TextView) view.findViewById( |
| 89 | R.id.display_name); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 90 | nameView.setText(displayName); |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 91 | |
| 92 | final String accountDisplayLabel; |
| 93 | |
| 94 | // Use the same string as editor if it's an editable user profile raw contact. |
| 95 | if (mIsUserProfile && account.areContactsWritable()) { |
| 96 | final AccountDisplayInfo displayInfo = |
| 97 | mAccountDisplayInfoFactory.getAccountDisplayInfo( |
| 98 | new AccountWithDataSet(accountName, accountType, dataSet)); |
| 99 | accountDisplayLabel = EditorUiUtils.getAccountHeaderLabelForMyProfile(mContext, |
| 100 | displayInfo); |
| 101 | } |
| 102 | else if (GoogleAccountType.ACCOUNT_TYPE.equals(accountType) |
| 103 | && account.dataSet == null) { |
| 104 | // Focus Google accounts have the account name shown |
| 105 | accountDisplayLabel = accountName; |
| 106 | } else { |
| 107 | accountDisplayLabel = account.getDisplayLabel(mContext).toString(); |
| 108 | } |
| 109 | final TextView accountTextView = (TextView) view.findViewById( |
| 110 | R.id.account_name); |
| 111 | final ImageView accountIconView = (ImageView) view.findViewById( |
| 112 | R.id.account_icon); |
| 113 | accountTextView.setText(accountDisplayLabel); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 114 | accountIconView.setImageDrawable(account.getDisplayIcon(mContext)); |
| 115 | |
| 116 | final ContactPhotoManager.DefaultImageRequest |
| 117 | request = new ContactPhotoManager.DefaultImageRequest( |
| 118 | displayName, String.valueOf(rawContactId), /* isCircular = */ true); |
Gary Mai | ac70468 | 2016-10-11 15:29:41 -0700 | [diff] [blame] | 119 | final Uri photoUri = Uri.withAppendedPath( |
| 120 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId), |
| 121 | RawContacts.DisplayPhoto.CONTENT_DIRECTORY); |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 122 | final ImageView photoView = (ImageView) view.findViewById( |
| 123 | R.id.photo); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 124 | ContactPhotoManager.getInstance(mContext).loadDirectoryPhoto(photoView, |
Gary Mai | ac70468 | 2016-10-11 15:29:41 -0700 | [diff] [blame] | 125 | photoUri, |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 126 | /* darkTheme = */ false, |
| 127 | /* isCircular = */ true, |
| 128 | request); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
| 133 | return mInflater.inflate(R.layout.raw_contact_list_item, parent, false); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public long getItemId(int position) { |
| 138 | getCursor().moveToPosition(position); |
| 139 | return getCursor().getLong(PickRawContactLoader.RAW_CONTACT_ID); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // Cursor holding all raw contact rows for the given Contact. |
| 144 | private Cursor mCursor; |
Gary Mai | ea2f357 | 2016-10-11 17:30:28 -0700 | [diff] [blame] | 145 | private CursorAdapter mAdapter; |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 146 | private boolean mIsUserProfile; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 147 | |
Gary Mai | 5336e6e | 2016-10-23 14:17:03 -0700 | [diff] [blame^] | 148 | public static PickRawContactDialogFragment getInstance(Cursor cursor, boolean isUserProfile) { |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 149 | final PickRawContactDialogFragment fragment = new PickRawContactDialogFragment(); |
Gary Mai | 445a7c6 | 2016-10-17 14:30:11 -0700 | [diff] [blame] | 150 | final Bundle args = new Bundle(); |
Gary Mai | 445a7c6 | 2016-10-17 14:30:11 -0700 | [diff] [blame] | 151 | args.putBoolean(ARGS_IS_USER_PROFILE, isUserProfile); |
| 152 | fragment.setArguments(args); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 153 | fragment.setCursor(cursor); |
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) { |
Gary Mai | 5336e6e | 2016-10-23 14:17:03 -0700 | [diff] [blame^] | 159 | if (!(getActivity() instanceof PickRawContactListener)) { |
| 160 | throw new IllegalArgumentException( |
| 161 | "Host activity doesn't implement PickRawContactListener"); |
| 162 | } |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 163 | final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
Gary Mai | ea2f357 | 2016-10-11 17:30:28 -0700 | [diff] [blame] | 164 | mAdapter = new RawContactAccountListAdapter(getContext(), mCursor); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 165 | builder.setTitle(R.string.contact_editor_pick_raw_contact_dialog_title); |
Gary Mai | ea2f357 | 2016-10-11 17:30:28 -0700 | [diff] [blame] | 166 | builder.setAdapter(mAdapter, new DialogInterface.OnClickListener() { |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 167 | @Override |
| 168 | public void onClick(DialogInterface dialog, int which) { |
Gary Mai | ea2f357 | 2016-10-11 17:30:28 -0700 | [diff] [blame] | 169 | final long rawContactId = mAdapter.getItemId(which); |
Gary Mai | 5336e6e | 2016-10-23 14:17:03 -0700 | [diff] [blame^] | 170 | ((PickRawContactListener) getActivity()).onPickRawContact(rawContactId); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 171 | } |
| 172 | }); |
| 173 | builder.setCancelable(true); |
| 174 | return builder.create(); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void onDismiss(DialogInterface dialog) { |
| 179 | super.onDismiss(dialog); |
| 180 | mCursor = null; |
| 181 | finishActivity(); |
| 182 | } |
| 183 | |
Gary Mai | 445a7c6 | 2016-10-17 14:30:11 -0700 | [diff] [blame] | 184 | @Override |
| 185 | public void onCreate(Bundle savedInstanceState) { |
| 186 | super.onCreate(savedInstanceState); |
| 187 | |
| 188 | final Bundle args = getArguments(); |
| 189 | if (args != null) { |
Gary Mai | 445a7c6 | 2016-10-17 14:30:11 -0700 | [diff] [blame] | 190 | mIsUserProfile = args.getBoolean(ARGS_IS_USER_PROFILE); |
| 191 | } |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Gary Mai | ea2f357 | 2016-10-11 17:30:28 -0700 | [diff] [blame] | 194 | public void setCursor(Cursor cursor) { |
| 195 | if (mAdapter != null) { |
| 196 | mAdapter.swapCursor(cursor); |
| 197 | } |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 198 | mCursor = cursor; |
| 199 | } |
| 200 | |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 201 | private void finishActivity() { |
| 202 | if (getActivity() != null && !getActivity().isFinishing()) { |
| 203 | getActivity().finish(); |
| 204 | } |
| 205 | } |
| 206 | } |