Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c7799159 authored by Anthony Hugh's avatar Anthony Hugh Committed by Android (Google) Code Review
Browse files

Merge "Refactor OnUsersUpdateListener"

parents c7729011 41676166
Loading
Loading
Loading
Loading
+35 −6
Original line number Diff line number Diff line
@@ -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
     */