blob: 6b0051eb578585b4e9d99e0a652815bc3fbb718b [file] [log] [blame]
Gary Maia6c80b32016-09-30 16:34:55 -07001package com.android.contacts.editor;
2
3import android.app.AlertDialog;
4import android.app.Dialog;
5import android.app.DialogFragment;
Gary Maiac704682016-10-11 15:29:41 -07006import android.content.ContentUris;
Gary Maia6c80b32016-09-30 16:34:55 -07007import android.content.Context;
8import android.content.DialogInterface;
Gary Maia6c80b32016-09-30 16:34:55 -07009import android.database.Cursor;
10import android.net.Uri;
11import android.os.Bundle;
Gary Maiac704682016-10-11 15:29:41 -070012import android.provider.ContactsContract.RawContacts;
Gary Maiba10be22016-10-12 17:41:15 -070013import android.text.TextUtils;
Gary Maia6c80b32016-09-30 16:34:55 -070014import android.view.LayoutInflater;
15import android.view.View;
16import android.view.ViewGroup;
17import android.widget.CursorAdapter;
18import android.widget.ImageView;
19import android.widget.TextView;
20
21import com.android.contacts.R;
22import com.android.contacts.common.ContactPhotoManager;
23import com.android.contacts.common.model.AccountTypeManager;
Gary Maiba10be22016-10-12 17:41:15 -070024import com.android.contacts.common.model.account.AccountDisplayInfo;
25import com.android.contacts.common.model.account.AccountDisplayInfoFactory;
Gary Maia6c80b32016-09-30 16:34:55 -070026import com.android.contacts.common.model.account.AccountType;
Gary Maiba10be22016-10-12 17:41:15 -070027import com.android.contacts.common.model.account.AccountWithDataSet;
28import com.android.contacts.common.model.account.GoogleAccountType;
Gary Maia6c80b32016-09-30 16:34:55 -070029import com.android.contacts.common.preference.ContactsPreferences;
Gary Maia6c80b32016-09-30 16:34:55 -070030
31/**
Gary Mai5336e6e2016-10-23 14:17:03 -070032 * Should only be started from an activity that implements {@link PickRawContactListener}.
Gary Maia6c80b32016-09-30 16:34:55 -070033 * Dialog containing the raw contacts that make up a contact. On selection the editor is loaded
34 * for the chosen raw contact.
35 */
36public class PickRawContactDialogFragment extends DialogFragment {
Gary Mai445a7c62016-10-17 14:30:11 -070037 private static final String ARGS_IS_USER_PROFILE = "isUserProfile";
38
Gary Mai5336e6e2016-10-23 14:17:03 -070039 public interface PickRawContactListener {
40 void onPickRawContact(long rawContactId);
41 }
42
Gary Maia6c80b32016-09-30 16:34:55 -070043 /**
44 * Used to list the account info for the given raw contacts list.
45 */
Gary Maiba10be22016-10-12 17:41:15 -070046 private final class RawContactAccountListAdapter extends CursorAdapter {
Gary Maia6c80b32016-09-30 16:34:55 -070047 private final LayoutInflater mInflater;
48 private final Context mContext;
Gary Maiba10be22016-10-12 17:41:15 -070049 private final AccountDisplayInfoFactory mAccountDisplayInfoFactory;
50 private final AccountTypeManager mAccountTypeManager;
51 private final ContactsPreferences mPreferences;
Gary Maia6c80b32016-09-30 16:34:55 -070052
53 public RawContactAccountListAdapter(Context context, Cursor cursor) {
54 super(context, cursor, 0);
55 mContext = context;
56 mInflater = LayoutInflater.from(context);
Gary Maiba10be22016-10-12 17:41:15 -070057 mAccountDisplayInfoFactory = AccountDisplayInfoFactory.forWritableAccounts(context);
58 mAccountTypeManager = AccountTypeManager.getInstance(context);
59 mPreferences = new ContactsPreferences(context);
Gary Maia6c80b32016-09-30 16:34:55 -070060 }
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 Maiba10be22016-10-12 17:41:15 -070068 final AccountType account = mAccountTypeManager.getAccountType(accountType, dataSet);
Gary Maia6c80b32016-09-30 16:34:55 -070069
Gary Maia6c80b32016-09-30 16:34:55 -070070 final int displayNameColumn =
Gary Maiba10be22016-10-12 17:41:15 -070071 mPreferences.getDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY
Gary Maia6c80b32016-09-30 16:34:55 -070072 ? PickRawContactLoader.DISPLAY_NAME_PRIMARY
73 : PickRawContactLoader.DISPLAY_NAME_ALTERNATIVE;
Gary Maiba10be22016-10-12 17:41:15 -070074
Gary Maia6c80b32016-09-30 16:34:55 -070075 String displayName = cursor.getString(displayNameColumn);
76
Gary Maiba10be22016-10-12 17:41:15 -070077 if (TextUtils.isEmpty(displayName)) {
78 displayName = mContext.getString(R.string.missing_name);
79 }
Gary Mai27e8c3a2016-10-27 10:27:46 -070080 final RawContactViewHolder holder = (RawContactViewHolder) view.getTag();
81 holder.displayName.setText(displayName);
Gary Maiba10be22016-10-12 17:41:15 -070082
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 Mai27e8c3a2016-10-27 10:27:46 -0700100
101 holder.accountName.setText(accountDisplayLabel);
102 holder.accountIcon.setImageDrawable(account.getDisplayIcon(mContext));
Gary Maia6c80b32016-09-30 16:34:55 -0700103
104 final ContactPhotoManager.DefaultImageRequest
105 request = new ContactPhotoManager.DefaultImageRequest(
106 displayName, String.valueOf(rawContactId), /* isCircular = */ true);
Gary Maiac704682016-10-11 15:29:41 -0700107 final Uri photoUri = Uri.withAppendedPath(
108 ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId),
109 RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
Gary Mai27e8c3a2016-10-27 10:27:46 -0700110
111 ContactPhotoManager.getInstance(mContext).loadDirectoryPhoto(holder.photo,
Gary Maiac704682016-10-11 15:29:41 -0700112 photoUri,
Gary Maia6c80b32016-09-30 16:34:55 -0700113 /* darkTheme = */ false,
114 /* isCircular = */ true,
115 request);
116 }
117
118 @Override
119 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Gary Mai27e8c3a2016-10-27 10:27:46 -0700120 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 Maia6c80b32016-09-30 16:34:55 -0700128 }
129
130 @Override
131 public long getItemId(int position) {
132 getCursor().moveToPosition(position);
133 return getCursor().getLong(PickRawContactLoader.RAW_CONTACT_ID);
134 }
Gary Mai27e8c3a2016-10-27 10:27:46 -0700135
136 class RawContactViewHolder {
137 TextView displayName;
138 TextView accountName;
139 ImageView accountIcon;
140 ImageView photo;
141 }
Gary Maia6c80b32016-09-30 16:34:55 -0700142 }
143
144 // Cursor holding all raw contact rows for the given Contact.
145 private Cursor mCursor;
Gary Maiea2f3572016-10-11 17:30:28 -0700146 private CursorAdapter mAdapter;
Gary Maiba10be22016-10-12 17:41:15 -0700147 private boolean mIsUserProfile;
Gary Maia6c80b32016-09-30 16:34:55 -0700148
Gary Mai5336e6e2016-10-23 14:17:03 -0700149 public static PickRawContactDialogFragment getInstance(Cursor cursor, boolean isUserProfile) {
Gary Maia6c80b32016-09-30 16:34:55 -0700150 final PickRawContactDialogFragment fragment = new PickRawContactDialogFragment();
Gary Mai445a7c62016-10-17 14:30:11 -0700151 final Bundle args = new Bundle();
Gary Mai445a7c62016-10-17 14:30:11 -0700152 args.putBoolean(ARGS_IS_USER_PROFILE, isUserProfile);
153 fragment.setArguments(args);
Gary Maia6c80b32016-09-30 16:34:55 -0700154 fragment.setCursor(cursor);
Gary Maia6c80b32016-09-30 16:34:55 -0700155 return fragment;
156 }
157
158 @Override
159 public Dialog onCreateDialog(Bundle savedInstanceState) {
Gary Mai5336e6e2016-10-23 14:17:03 -0700160 if (!(getActivity() instanceof PickRawContactListener)) {
161 throw new IllegalArgumentException(
162 "Host activity doesn't implement PickRawContactListener");
163 }
Gary Maia6c80b32016-09-30 16:34:55 -0700164 final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Gary Maiea2f3572016-10-11 17:30:28 -0700165 mAdapter = new RawContactAccountListAdapter(getContext(), mCursor);
Gary Maia6c80b32016-09-30 16:34:55 -0700166 builder.setTitle(R.string.contact_editor_pick_raw_contact_dialog_title);
Gary Maiea2f3572016-10-11 17:30:28 -0700167 builder.setAdapter(mAdapter, new DialogInterface.OnClickListener() {
Gary Maia6c80b32016-09-30 16:34:55 -0700168 @Override
169 public void onClick(DialogInterface dialog, int which) {
Gary Maiea2f3572016-10-11 17:30:28 -0700170 final long rawContactId = mAdapter.getItemId(which);
Gary Mai5336e6e2016-10-23 14:17:03 -0700171 ((PickRawContactListener) getActivity()).onPickRawContact(rawContactId);
Gary Maia6c80b32016-09-30 16:34:55 -0700172 }
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 Mai445a7c62016-10-17 14:30:11 -0700185 @Override
186 public void onCreate(Bundle savedInstanceState) {
187 super.onCreate(savedInstanceState);
188
189 final Bundle args = getArguments();
190 if (args != null) {
Gary Mai445a7c62016-10-17 14:30:11 -0700191 mIsUserProfile = args.getBoolean(ARGS_IS_USER_PROFILE);
192 }
Gary Maia6c80b32016-09-30 16:34:55 -0700193 }
194
Walter Jang68de0a52016-10-29 15:52:54 -0700195 @Override
196 public Context getContext() {
197 return getActivity();
198 }
199
Gary Maiea2f3572016-10-11 17:30:28 -0700200 public void setCursor(Cursor cursor) {
201 if (mAdapter != null) {
202 mAdapter.swapCursor(cursor);
203 }
Gary Maia6c80b32016-09-30 16:34:55 -0700204 mCursor = cursor;
205 }
206
Gary Maia6c80b32016-09-30 16:34:55 -0700207 private void finishActivity() {
208 if (getActivity() != null && !getActivity().isFinishing()) {
209 getActivity().finish();
210 }
211 }
212}