Merge "Refactor OnUsersUpdateListener"

This commit is contained in:
Anthony Hugh
2019-10-03 20:31:20 +00:00
committed by Android (Google) Code Review

View File

@@ -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
*/