From 429a136f054915dc3f94121d31fcff434ff78430 Mon Sep 17 00:00:00 2001 From: calderwoodra Date: Tue, 22 Aug 2017 11:34:13 -0700 Subject: Updated ContactsFragment to use composition. The following features are now configureable in ContactsFragment: - Presence of the "Create new contact" row at position 0 - The on click action when a user clicks on a row in contacts This change is being made so that this fragment can be reused in the add favorite screen in the new favorites fragment. For more context: https://docs.google.com/document/d/1lCjOgeYQXolrHW32Bgl-Vty_aIalQog_rog-EaO1bMA/edit#heading=h.1qre30w9h49i Bug: 36841782 Test: existing, updated PiperOrigin-RevId: 166089143 Change-Id: I567f4efb9c738f4fc629523e118e3cf116bf4ace --- .../dialer/app/list/DialtactsPagerAdapter.java | 5 +- .../dialer/contactsfragment/ContactViewHolder.java | 31 ++++++++--- .../dialer/contactsfragment/ContactsAdapter.java | 45 +++++++++++---- .../dialer/contactsfragment/ContactsFragment.java | 65 +++++++++++++++++++++- 4 files changed, 125 insertions(+), 21 deletions(-) diff --git a/java/com/android/dialer/app/list/DialtactsPagerAdapter.java b/java/com/android/dialer/app/list/DialtactsPagerAdapter.java index 822aa789f..d9cb0c1f6 100644 --- a/java/com/android/dialer/app/list/DialtactsPagerAdapter.java +++ b/java/com/android/dialer/app/list/DialtactsPagerAdapter.java @@ -31,6 +31,8 @@ import com.android.dialer.common.Assert; import com.android.dialer.common.LogUtil; import com.android.dialer.configprovider.ConfigProviderBindings; import com.android.dialer.contactsfragment.ContactsFragment; +import com.android.dialer.contactsfragment.ContactsFragment.ClickAction; +import com.android.dialer.contactsfragment.ContactsFragment.Header; import com.android.dialer.database.CallLogQueryHandler; import com.android.dialer.speeddial.SpeedDialFragment; import com.android.dialer.util.ViewUtil; @@ -120,7 +122,8 @@ public class DialtactsPagerAdapter extends FragmentPagerAdapter { case TAB_INDEX_ALL_CONTACTS: if (useNewContactsTab) { if (contactsFragment == null) { - contactsFragment = new ContactsFragment(); + contactsFragment = + ContactsFragment.newInstance(Header.ADD_CONTACT, ClickAction.OPEN_CONTACT_CARD); } return contactsFragment; } else { diff --git a/java/com/android/dialer/contactsfragment/ContactViewHolder.java b/java/com/android/dialer/contactsfragment/ContactViewHolder.java index 586e22aab..0597c2a7e 100644 --- a/java/com/android/dialer/contactsfragment/ContactViewHolder.java +++ b/java/com/android/dialer/contactsfragment/ContactViewHolder.java @@ -26,6 +26,7 @@ import android.view.View.OnClickListener; import android.widget.QuickContactBadge; import android.widget.TextView; import com.android.dialer.common.Assert; +import com.android.dialer.contactsfragment.ContactsFragment.ClickAction; import com.android.dialer.logging.InteractionEvent; import com.android.dialer.logging.Logger; @@ -36,17 +37,20 @@ final class ContactViewHolder extends RecyclerView.ViewHolder implements OnClick private final TextView name; private final QuickContactBadge photo; private final Context context; + private final @ClickAction int clickAction; private String headerText; private Uri contactUri; - public ContactViewHolder(View itemView) { + ContactViewHolder(View itemView, @ClickAction int clickAction) { super(itemView); + Assert.checkArgument(clickAction != ClickAction.INVALID, "Invalid click action."); context = itemView.getContext(); itemView.findViewById(R.id.click_target).setOnClickListener(this); - header = (TextView) itemView.findViewById(R.id.header); - name = (TextView) itemView.findViewById(R.id.contact_name); - photo = (QuickContactBadge) itemView.findViewById(R.id.photo); + header = itemView.findViewById(R.id.header); + name = itemView.findViewById(R.id.contact_name); + photo = itemView.findViewById(R.id.photo); + this.clickAction = clickAction; } /** @@ -85,9 +89,20 @@ final class ContactViewHolder extends RecyclerView.ViewHolder implements OnClick @Override public void onClick(View v) { - Logger.get(context) - .logInteraction(InteractionEvent.Type.OPEN_QUICK_CONTACT_FROM_CONTACTS_FRAGMENT_ITEM); - QuickContact.showQuickContact( - photo.getContext(), photo, contactUri, QuickContact.MODE_LARGE, null /* excludeMimes */); + switch (clickAction) { + case ClickAction.OPEN_CONTACT_CARD: + Logger.get(context) + .logInteraction(InteractionEvent.Type.OPEN_QUICK_CONTACT_FROM_CONTACTS_FRAGMENT_ITEM); + QuickContact.showQuickContact( + photo.getContext(), + photo, + contactUri, + QuickContact.MODE_LARGE, + null /* excludeMimes */); + break; + case ClickAction.INVALID: + default: + throw Assert.createIllegalStateFailException("Invalid click action."); + } } } diff --git a/java/com/android/dialer/contactsfragment/ContactsAdapter.java b/java/com/android/dialer/contactsfragment/ContactsAdapter.java index 1bd8e343a..13895313f 100644 --- a/java/com/android/dialer/contactsfragment/ContactsAdapter.java +++ b/java/com/android/dialer/contactsfragment/ContactsAdapter.java @@ -28,6 +28,8 @@ import android.view.View; import android.view.ViewGroup; import com.android.dialer.common.Assert; import com.android.dialer.contactphoto.ContactPhotoManager; +import com.android.dialer.contactsfragment.ContactsFragment.ClickAction; +import com.android.dialer.contactsfragment.ContactsFragment.Header; import com.android.dialer.lettertile.LetterTileDrawable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -47,6 +49,8 @@ final class ContactsAdapter extends RecyclerView.Adapter holderMap = new ArrayMap<>(); private final Context context; private final Cursor cursor; + private final @Header int header; + private final @ClickAction int clickAction; // List of contact sublist headers private final String[] headers; @@ -54,9 +58,12 @@ final class ContactsAdapter extends RecyclerView.AdapterCurrent example of this fragment are the contacts tab and in creating a new favorite + * contact. For example, the contacts tab we use: + * + *
    + *
  • {@link Header#ADD_CONTACT} to insert a header that allows users to add a contact + *
  • {@link ClickAction#OPEN_CONTACT_CARD} to open contact cards on click + *
+ * + * And for the add favorite contact screen we might use: + * + *
    + *
  • {@link Header#NONE} so that all rows are contacts (i.e. no header inserted) + *
  • {@link ClickAction#SET_RESULT_AND_FINISH} to send a selected contact to the previous + * activity. + *
+ * + * @param header determines the type of header inserted at position 0 in the contacts list + * @param clickAction defines the on click actions on rows that represent contacts + */ + public static ContactsFragment newInstance(@Header int header, @ClickAction int clickAction) { + Assert.checkArgument(clickAction != ClickAction.INVALID, "Invalid click action"); + ContactsFragment fragment = new ContactsFragment(); + Bundle args = new Bundle(); + args.putInt(EXTRA_HEADER, header); + args.putInt(EXTRA_CLICK_ACTION, clickAction); + fragment.setArguments(args); + return fragment; + } + @SuppressWarnings("WrongConstant") @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); contactsPrefs = new ContactsPreferences(getContext()); contactsPrefs.registerChangeListener(this); + header = getArguments().getInt(EXTRA_HEADER); + clickAction = getArguments().getInt(EXTRA_CLICK_ACTION); } @Nullable @@ -126,7 +189,7 @@ public class ContactsFragment extends Fragment } else { emptyContentView.setVisibility(View.GONE); recyclerView.setVisibility(View.VISIBLE); - adapter = new ContactsAdapter(getContext(), cursor); + adapter = new ContactsAdapter(getContext(), cursor, header, clickAction); manager = new LinearLayoutManager(getContext()) { @Override -- cgit v1.2.3