From af96139ce2bb384036f85654441b248b3b4ef77c Mon Sep 17 00:00:00 2001 From: calderwoodra Date: Mon, 27 Nov 2017 19:39:32 -0800 Subject: Contact list no longer jumps to the top when the cp2 database updates. The contacts fragment recycler view was rebuilding the adapter and layout manager each time the Contacts.CONTENT_URI observered a content change. Now we simply update the adapter's cursor and notifyDataSetChanged. This keeps the recycler view in place through all changes (inserts, deletes and updates). This change also fixes another bug replated to contacts fragment not refreshing when the contacts permission was granted in another part of the UI. Bug: 64044576,67781478,68179085 Test: manual PiperOrigin-RevId: 177106761 Change-Id: Ib6a929efb047001d681cb008c3ede69860146836 --- .../dialer/contactsfragment/ContactsAdapter.java | 17 +++--- .../dialer/contactsfragment/ContactsFragment.java | 67 ++++++++++++++++------ 2 files changed, 60 insertions(+), 24 deletions(-) diff --git a/java/com/android/dialer/contactsfragment/ContactsAdapter.java b/java/com/android/dialer/contactsfragment/ContactsAdapter.java index 481574e0c..8f2120cd7 100644 --- a/java/com/android/dialer/contactsfragment/ContactsAdapter.java +++ b/java/com/android/dialer/contactsfragment/ContactsAdapter.java @@ -49,22 +49,24 @@ 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; - + private String[] headers = new String[0]; // Number of contacts that correspond to each header in {@code headers}. - private final int[] counts; + private int[] counts = new int[0]; + // Cursor with list of contacts + private Cursor cursor; - ContactsAdapter( - Context context, Cursor cursor, @Header int header, @ClickAction int clickAction) { + ContactsAdapter(Context context, @Header int header, @ClickAction int clickAction) { this.context = context; - this.cursor = cursor; this.header = header; this.clickAction = clickAction; + } + + void updateCursor(Cursor cursor) { + this.cursor = cursor; headers = cursor.getExtras().getStringArray(Contacts.EXTRA_ADDRESS_BOOK_INDEX_TITLES); counts = cursor.getExtras().getIntArray(Contacts.EXTRA_ADDRESS_BOOK_INDEX_COUNTS); if (counts != null) { @@ -78,6 +80,7 @@ final class ContactsAdapter extends RecyclerView.Adapter itemsShown) { + fastScroller.setVisibility(View.VISIBLE); + recyclerView.setOnScrollChangeListener(ContactsFragment.this); + } else { + fastScroller.setVisibility(View.GONE); + } + } + }; + recyclerView.setLayoutManager(manager); emptyContentView = view.findViewById(R.id.empty_list_view); emptyContentView.setImage(R.drawable.empty_contacts); @@ -187,6 +221,7 @@ public class ContactsFragment extends Fragment @Override public void onLoadFinished(Loader loader, Cursor cursor) { + LogUtil.enterBlock("ContactsFragment.onLoadFinished"); if (cursor == null || cursor.getCount() == 0) { emptyContentView.setDescription(R.string.all_contacts_empty); emptyContentView.setActionLabel(R.string.all_contacts_empty_add_contact_action); @@ -195,24 +230,8 @@ public class ContactsFragment extends Fragment } else { emptyContentView.setVisibility(View.GONE); recyclerView.setVisibility(View.VISIBLE); - adapter = new ContactsAdapter(getContext(), cursor, header, clickAction); - manager = - new LinearLayoutManager(getContext()) { - @Override - public void onLayoutChildren(Recycler recycler, State state) { - super.onLayoutChildren(recycler, state); - int itemsShown = findLastVisibleItemPosition() - findFirstVisibleItemPosition() + 1; - if (adapter.getItemCount() > itemsShown) { - fastScroller.setVisibility(View.VISIBLE); - recyclerView.setOnScrollChangeListener(ContactsFragment.this); - } else { - fastScroller.setVisibility(View.GONE); - } - } - }; + adapter.updateCursor(cursor); - recyclerView.setLayoutManager(manager); - recyclerView.setAdapter(adapter); PerformanceReport.logOnScrollStateChange(recyclerView); fastScroller.setup(adapter, manager); } @@ -311,4 +330,18 @@ public class ContactsFragment extends Fragment } } } + + @Override + public void onStart() { + super.onStart(); + PermissionsUtil.registerPermissionReceiver( + getActivity(), readContactsPermissionGrantedReceiver, READ_CONTACTS); + } + + @Override + public void onStop() { + PermissionsUtil.unregisterPermissionReceiver( + getActivity(), readContactsPermissionGrantedReceiver); + super.onStop(); + } } -- cgit v1.2.3