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

Commit 7555f3e4 authored by Anthony Hugh's avatar Anthony Hugh
Browse files

Refactor getAllUsers

Refactoring API out of CarUserManagerHelper and into apps using the API.

Bug: 137136907
Test: atest CarSettingsRoboTests
Test: atest CarUserManagerHelperTest
Change-Id: I3f0c0c71e4f05de982e2af6ac95c4f77bef602fc
parent ffa0a6f4
Loading
Loading
Loading
Loading
+19 −3
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
@@ -52,6 +53,8 @@ import com.android.systemui.statusbar.phone.SystemUIDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Displays a GridLayout with icons for the users in the system to allow switching between users.
@@ -61,6 +64,7 @@ public class UserGridRecyclerView extends RecyclerView {
    private UserSelectionListener mUserSelectionListener;
    private UserAdapter mAdapter;
    private CarUserManagerHelper mCarUserManagerHelper;
    private UserManager mUserManager;
    private Context mContext;

    private final BroadcastReceiver mUserUpdateReceiver = new BroadcastReceiver() {
@@ -74,6 +78,7 @@ public class UserGridRecyclerView extends RecyclerView {
        super(context, attrs);
        mContext = context;
        mCarUserManagerHelper = new CarUserManagerHelper(mContext);
        mUserManager = UserManager.get(mContext);

        addItemDecoration(new ItemSpacingDecoration(context.getResources().getDimensionPixelSize(
                R.dimen.car_user_switcher_vertical_spacing_between_users)));
@@ -103,12 +108,23 @@ public class UserGridRecyclerView extends RecyclerView {
     * @return the adapter
     */
    public void buildAdapter() {
        List<UserRecord> userRecords = createUserRecords(mCarUserManagerHelper
                .getAllUsers());
        List<UserRecord> userRecords = createUserRecords(getAllUsers());
        mAdapter = new UserAdapter(mContext, userRecords);
        super.setAdapter(mAdapter);
    }

    private List<UserInfo> getAllUsers() {
        Stream<UserInfo> userListStream =
                mUserManager.getUsers(/* excludeDying= */ true).stream();

        if (UserManager.isHeadlessSystemUserMode()) {
            userListStream =
                    userListStream.filter(userInfo -> userInfo.id != UserHandle.USER_SYSTEM);
        }
        userListStream = userListStream.filter(userInfo -> userInfo.supportsSwitchToByUser());
        return userListStream.collect(Collectors.toList());
    }

    private List<UserRecord> createUserRecords(List<UserInfo> userInfoList) {
        List<UserRecord> userRecords = new ArrayList<>();

@@ -173,7 +189,7 @@ public class UserGridRecyclerView extends RecyclerView {

    private void onUsersUpdate() {
        mAdapter.clearUsers();
        mAdapter.updateUsers(createUserRecords(mCarUserManagerHelper.getAllUsers()));
        mAdapter.updateUsers(createUserRecords(getAllUsers()));
        mAdapter.notifyDataSetChanged();
    }