From 4167616621bda5af041e36024074af6e2e040c0d Mon Sep 17 00:00:00 2001 From: Anthony Hugh Date: Tue, 1 Oct 2019 16:12:04 -0700 Subject: [PATCH] Refactor OnUsersUpdateListener OnUsersUpdateListener was a convenience listener to register for all possible user events. However, the API was not that customizable and so it tends to fire for too many events. Also, because it grouped so many intents together, listeners lost all possible metadata associated with the intents. Rather than maintain this large list, we will instead remove the API and have the callers register for these signals themselves. Bug: 137136907 Test: atest CarSettingsRoboTests Test: atest CarUserManagerHelperTest Change-Id: I02d5a03710e8ee12531518c8cbc718c0cb77b506 --- .../statusbar/car/UserGridRecyclerView.java | 41 ++++++++++++++++--- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/packages/CarSystemUI/src/com/android/systemui/statusbar/car/UserGridRecyclerView.java b/packages/CarSystemUI/src/com/android/systemui/statusbar/car/UserGridRecyclerView.java index 827a59eddf56d..743ab4737bfb7 100644 --- a/packages/CarSystemUI/src/com/android/systemui/statusbar/car/UserGridRecyclerView.java +++ b/packages/CarSystemUI/src/com/android/systemui/statusbar/car/UserGridRecyclerView.java @@ -23,13 +23,17 @@ import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; import android.car.userlib.CarUserManagerHelper; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; +import android.content.Intent; +import android.content.IntentFilter; import android.content.pm.UserInfo; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Rect; import android.os.AsyncTask; +import android.os.UserHandle; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; @@ -53,13 +57,19 @@ import java.util.List; * Displays a GridLayout with icons for the users in the system to allow switching between users. * One of the uses of this is for the lock screen in auto. */ -public class UserGridRecyclerView extends RecyclerView implements - CarUserManagerHelper.OnUsersUpdateListener { +public class UserGridRecyclerView extends RecyclerView { private UserSelectionListener mUserSelectionListener; private UserAdapter mAdapter; private CarUserManagerHelper mCarUserManagerHelper; private Context mContext; + private final BroadcastReceiver mUserUpdateReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + onUsersUpdate(); + } + }; + public UserGridRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; @@ -75,7 +85,7 @@ public class UserGridRecyclerView extends RecyclerView implements @Override public void onFinishInflate() { super.onFinishInflate(); - mCarUserManagerHelper.registerOnUsersUpdateListener(this); + registerForUserEvents(); } /** @@ -84,7 +94,7 @@ public class UserGridRecyclerView extends RecyclerView implements @Override public void onDetachedFromWindow() { super.onDetachedFromWindow(); - mCarUserManagerHelper.unregisterOnUsersUpdateListener(this); + unregisterForUserEvents(); } /** @@ -161,13 +171,32 @@ public class UserGridRecyclerView extends RecyclerView implements mUserSelectionListener = userSelectionListener; } - @Override - public void onUsersUpdate() { + private void onUsersUpdate() { mAdapter.clearUsers(); mAdapter.updateUsers(createUserRecords(mCarUserManagerHelper.getAllUsers())); mAdapter.notifyDataSetChanged(); } + private void registerForUserEvents() { + IntentFilter filter = new IntentFilter(); + filter.addAction(Intent.ACTION_USER_REMOVED); + filter.addAction(Intent.ACTION_USER_ADDED); + filter.addAction(Intent.ACTION_USER_INFO_CHANGED); + filter.addAction(Intent.ACTION_USER_SWITCHED); + filter.addAction(Intent.ACTION_USER_STOPPED); + filter.addAction(Intent.ACTION_USER_UNLOCKED); + mContext.registerReceiverAsUser( + mUserUpdateReceiver, + UserHandle.ALL, + filter, + /* broadcastPermission= */ null, + /* scheduler= */ null); + } + + private void unregisterForUserEvents() { + mContext.unregisterReceiver(mUserUpdateReceiver); + } + /** * Adapter to populate the grid layout with the available user profiles */