diff options
author | twyen <twyen@google.com> | 2017-11-29 20:49:06 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-11-29 20:49:06 +0000 |
commit | 002c66cda2a66ffdcde73442efb212e8341cb445 (patch) | |
tree | 1749d2cbf1a8636a2b5f79b88ff888e6700970f5 /java/com/android/dialer/contactsfragment/ContactsFragment.java | |
parent | 52dadec2337ec34791e1666921d70e2c2eb33834 (diff) | |
parent | 38db0c78bfc3330ac72e75a2b88397cf4b35145b (diff) |
Merge changes Ic61819b8,I4149abe1,Ic738a6dd,I297faa26,Ib6a929ef, ...
am: 38db0c78bf
Change-Id: I3857acc52182d915bef2ec6ab5e2e1479008da8d
Diffstat (limited to 'java/com/android/dialer/contactsfragment/ContactsFragment.java')
-rw-r--r-- | java/com/android/dialer/contactsfragment/ContactsFragment.java | 67 |
1 files changed, 50 insertions, 17 deletions
diff --git a/java/com/android/dialer/contactsfragment/ContactsFragment.java b/java/com/android/dialer/contactsfragment/ContactsFragment.java index a8daa546a..82b68b8ee 100644 --- a/java/com/android/dialer/contactsfragment/ContactsFragment.java +++ b/java/com/android/dialer/contactsfragment/ContactsFragment.java @@ -16,8 +16,13 @@ package com.android.dialer.contactsfragment; +import static android.Manifest.permission.READ_CONTACTS; + import android.app.Fragment; import android.app.LoaderManager.LoaderCallbacks; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; import android.content.Loader; import android.content.pm.PackageManager; import android.database.Cursor; @@ -80,6 +85,18 @@ public class ContactsFragment extends Fragment private static final String EXTRA_HEADER = "extra_header"; private static final String EXTRA_CLICK_ACTION = "extra_click_action"; + /** + * Listen to broadcast events about permissions in order to be notified if the READ_CONTACTS + * permission is granted via the UI in another fragment. + */ + private final BroadcastReceiver readContactsPermissionGrantedReceiver = + new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + getLoaderManager().initLoader(0, null, ContactsFragment.this); + } + }; + private FastScroller fastScroller; private TextView anchoredHeader; private RecyclerView recyclerView; @@ -146,6 +163,23 @@ public class ContactsFragment extends Fragment fastScroller = view.findViewById(R.id.fast_scroller); anchoredHeader = view.findViewById(R.id.header); recyclerView = view.findViewById(R.id.recycler_view); + adapter = new ContactsAdapter(getContext(), header, clickAction); + recyclerView.setAdapter(adapter); + 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); + } + } + }; + 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<Cursor> 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(); + } } |