() {
+ @Override
+ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+ View view = LayoutInflater.from(parent.getContext())
+ .inflate(R.layout.user_select_item, parent, false);
- @Override
- public boolean isEnabled(int position) {
- return true;
+ return new ViewHolder(view, onClickListener);
+ }
+
+ @Override
+ public void onBindViewHolder(ViewHolder holder, int position) {
+ UserAdapter.this.bindViewHolder(holder, position);
+ }
+
+ @Override
+ public int getItemCount() {
+ return getCount();
+ }
+ };
}
/**
- * Creates a {@link UserAdapter} if there is more than one
- * profile on the device.
+ * Creates a {@link UserAdapter} if there is more than one profile on the device.
*
- * The adapter can be used to populate a spinner that switches between the Settings
- * app on the different profiles.
+ *
The adapter can be used to populate a spinner that switches between the different
+ * profiles.
*
- * @return a {@link UserAdapter} or null if there is only one
- * profile.
+ * @return a {@link UserAdapter} or null if there is only one profile.
*/
public static UserAdapter createUserSpinnerAdapter(UserManager userManager, Context context) {
List userProfiles = userManager.getUserProfiles();
@@ -215,13 +183,60 @@ public class UserAdapter implements SpinnerAdapter, ListAdapter {
return createUserAdapter(userManager, context, userProfiles);
}
- public static UserAdapter createUserAdapter(
+ /**
+ * Creates a {@link RecyclerView} adapter which be used to populate a {@link RecyclerView} that
+ * select one of the different profiles.
+ */
+ public static RecyclerView.Adapter createUserRecycleViewAdapter(
+ Context context, List userProfiles, OnClickListener onClickListener) {
+ UserManager systemService = context.getSystemService(UserManager.class);
+ return createUserAdapter(systemService, context, userProfiles)
+ .createRecyclerViewAdapter(onClickListener);
+ }
+
+ private static UserAdapter createUserAdapter(
UserManager userManager, Context context, List userProfiles) {
ArrayList userDetails = new ArrayList<>(userProfiles.size());
- final int count = userProfiles.size();
- for (int i = 0; i < count; i++) {
- userDetails.add(new UserDetails(userProfiles.get(i), userManager, context));
+ for (UserHandle userProfile : userProfiles) {
+ userDetails.add(new UserDetails(userProfile, userManager, context));
}
return new UserAdapter(context, userDetails);
}
+
+ static class ViewHolder extends RecyclerView.ViewHolder {
+ private final ImageView mIconView;
+ private final TextView mTitleView;
+
+ private ViewHolder(View view) {
+ super(view);
+ mIconView = view.findViewById(android.R.id.icon);
+ mTitleView = view.findViewById(android.R.id.title);
+ }
+
+ private ViewHolder(View view, OnClickListener onClickListener) {
+ this(view);
+ View button = view.findViewById(R.id.button);
+ if (button != null) {
+ button.setOnClickListener(v -> onClickListener.onClick(getAdapterPosition()));
+ }
+ }
+
+ private ImageView getIconView() {
+ return mIconView;
+ }
+
+ private TextView getTitleView() {
+ return mTitleView;
+ }
+ }
+
+ /**
+ * Interface definition for a callback to be invoked when a user is clicked.
+ */
+ public interface OnClickListener {
+ /**
+ * Called when a user has been clicked.
+ */
+ void onClick(int position);
+ }
}
diff --git a/src/com/android/settings/privacy/EnableContentCaptureWithServiceSettingsPreferenceController.java b/src/com/android/settings/privacy/EnableContentCaptureWithServiceSettingsPreferenceController.java
index c3878d51b62..fcb2347f5b0 100644
--- a/src/com/android/settings/privacy/EnableContentCaptureWithServiceSettingsPreferenceController.java
+++ b/src/com/android/settings/privacy/EnableContentCaptureWithServiceSettingsPreferenceController.java
@@ -19,19 +19,18 @@ package com.android.settings.privacy;
import android.annotation.NonNull;
import android.content.ComponentName;
import android.content.Context;
-import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;
+import android.text.TextUtils;
import android.util.Log;
-import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.core.TogglePreferenceController;
-import com.android.settings.dashboard.profileselector.UserAdapter;
+import com.android.settings.dashboard.profileselector.ProfileSelectDialog;
import com.android.settings.utils.ContentCaptureUtils;
import java.util.ArrayList;
@@ -42,13 +41,9 @@ public final class EnableContentCaptureWithServiceSettingsPreferenceController
private static final String TAG = "ContentCaptureController";
- private final UserManager mUserManager;
-
public EnableContentCaptureWithServiceSettingsPreferenceController(@NonNull Context context,
@NonNull String key) {
super(context, key);
-
- mUserManager = UserManager.get(context);
}
@Override
@@ -74,11 +69,6 @@ public final class EnableContentCaptureWithServiceSettingsPreferenceController
Log.w(TAG, "No component name for custom service settings");
preference.setSelectable(false);
}
-
- preference.setOnPreferenceClickListener((pref) -> {
- ProfileSelectDialog.show(mContext, pref);
- return true;
- });
}
@Override
@@ -93,32 +83,30 @@ public final class EnableContentCaptureWithServiceSettingsPreferenceController
return R.string.menu_key_privacy;
}
- private static final class ProfileSelectDialog {
- public static void show(Context context, Preference pref) {
- final UserManager userManager = UserManager.get(context);
- final List userInfos = userManager.getUsers();
- final ArrayList userHandles = new ArrayList<>(userInfos.size());
- for (UserInfo info: userInfos) {
- userHandles.add(info.getUserHandle());
- }
- if (userHandles.size() == 1) {
- final Intent intent = pref.getIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
- context.startActivityAsUser(intent, userHandles.get(0));
- } else {
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
- UserAdapter adapter = UserAdapter.createUserAdapter(userManager, context,
- userHandles);
- builder.setTitle(com.android.settingslib.R.string.choose_profile)
- .setAdapter(adapter, (DialogInterface dialog, int which) -> {
- final UserHandle user = userHandles.get(which);
- // Show menu on top level items.
- final Intent intent = pref.getIntent()
- .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
- context.startActivityAsUser(intent, user);
- })
- .show();
- }
+ @Override
+ public boolean handlePreferenceTreeClick(Preference preference) {
+ if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
+ return false;
}
+ show(preference);
+ return true;
}
+ private void show(Preference preference) {
+ final UserManager userManager = UserManager.get(mContext);
+ final List userInfos = userManager.getUsers();
+ final ArrayList userHandles = new ArrayList<>(userInfos.size());
+ for (UserInfo info : userInfos) {
+ userHandles.add(info.getUserHandle());
+ }
+ final Intent intent = preference.getIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
+ if (userHandles.size() == 1) {
+ mContext.startActivityAsUser(intent, userHandles.get(0));
+ return;
+ }
+ ProfileSelectDialog.createDialog(mContext, userHandles, (int position) -> {
+ // Show menu on top level items.
+ mContext.startActivityAsUser(intent, userHandles.get(position));
+ }).show();
+ }
}
diff --git a/tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectDialogTest.java b/tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectDialogTest.java
index 53cddb1b056..e1cf52b995b 100644
--- a/tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectDialogTest.java
+++ b/tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectDialogTest.java
@@ -23,21 +23,29 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import android.app.Dialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;
+import android.widget.TextView;
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.settings.R;
import com.android.settingslib.drawer.ActivityTile;
import com.android.settingslib.drawer.CategoryKey;
import com.android.settingslib.drawer.Tile;
+import com.google.android.collect.Lists;
+
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
+import org.mockito.Spy;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@@ -46,8 +54,9 @@ public class ProfileSelectDialogTest {
private static final UserHandle NORMAL_USER = new UserHandle(1111);
private static final UserHandle REMOVED_USER = new UserHandle(2222);
- @Mock
- private Context mContext;
+ @Spy
+ private Context mContext = ApplicationProvider.getApplicationContext();
+
@Mock
private UserManager mUserManager;
@@ -91,4 +100,18 @@ public class ProfileSelectDialogTest {
verify(mUserManager, times(1)).getUserInfo(NORMAL_USER.getIdentifier());
verify(mUserManager, times(2)).getUserInfo(REMOVED_USER.getIdentifier());
}
+
+ @Test
+ public void createDialog_showsCorrectTitle() {
+ mContext.setTheme(R.style.Theme_AppCompat);
+
+ Dialog dialog = ProfileSelectDialog.createDialog(mContext, Lists.newArrayList(NORMAL_USER),
+ (position) -> {
+ });
+ dialog.show();
+
+ TextView titleView = dialog.findViewById(R.id.topPanel).findViewById(android.R.id.title);
+ assertThat(titleView.getText().toString()).isEqualTo(
+ mContext.getText(com.android.settingslib.R.string.choose_profile).toString());
+ }
}
diff --git a/tests/robotests/src/com/android/settings/dashboard/profileselector/UserAdapterTest.java b/tests/robotests/src/com/android/settings/dashboard/profileselector/UserAdapterTest.java
new file mode 100644
index 00000000000..aa7e30af809
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/dashboard/profileselector/UserAdapterTest.java
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.dashboard.profileselector;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.content.pm.UserInfo;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.widget.FrameLayout;
+import android.widget.TextView;
+
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.internal.widget.RecyclerView;
+import com.android.settingslib.R;
+
+import com.google.android.collect.Lists;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Spy;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.robolectric.RobolectricTestRunner;
+
+import java.util.ArrayList;
+
+@RunWith(RobolectricTestRunner.class)
+public class UserAdapterTest {
+ @Rule
+ public MockitoRule mRule = MockitoJUnit.rule();
+
+ private final int mPersonalUserId = UserHandle.myUserId();
+ private static final int WORK_USER_ID = 1;
+
+ @Mock
+ private UserManager mUserManager;
+
+ @Mock
+ private UserInfo mPersonalUserInfo;
+
+ @Mock
+ private UserInfo mWorkUserInfo;
+
+ @Mock
+ private UserAdapter.OnClickListener mOnClickListener;
+
+ @Spy
+ private Context mContext = ApplicationProvider.getApplicationContext();
+
+ @Before
+ public void setUp() {
+ when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
+ when(mUserManager.getUserInfo(mPersonalUserId)).thenReturn(mPersonalUserInfo);
+ when(mUserManager.getUserInfo(WORK_USER_ID)).thenReturn(mWorkUserInfo);
+ }
+
+ @Test
+ public void createUserSpinnerAdapter_singleProfile_returnsNull() {
+ when(mUserManager.getUserProfiles()).thenReturn(
+ Lists.newArrayList(UserHandle.of(mPersonalUserId)));
+
+ UserAdapter userSpinnerAdapter =
+ UserAdapter.createUserSpinnerAdapter(mUserManager, mContext);
+
+ assertThat(userSpinnerAdapter).isNull();
+ }
+
+ @Test
+ public void createUserSpinnerAdapter_twoProfiles_succeed() {
+ when(mUserManager.getUserProfiles()).thenReturn(
+ Lists.newArrayList(UserHandle.of(mPersonalUserId), UserHandle.of(WORK_USER_ID)));
+
+ UserAdapter userSpinnerAdapter =
+ UserAdapter.createUserSpinnerAdapter(mUserManager, mContext);
+
+ assertThat(userSpinnerAdapter.getCount()).isEqualTo(2);
+ assertThat(userSpinnerAdapter.getUserHandle(0).getIdentifier()).isEqualTo(mPersonalUserId);
+ assertThat(userSpinnerAdapter.getUserHandle(1).getIdentifier()).isEqualTo(WORK_USER_ID);
+ }
+
+ @Test
+ public void createUserRecycleViewAdapter_canBindViewHolderCorrectly() {
+ ArrayList userHandles =
+ Lists.newArrayList(UserHandle.of(mPersonalUserId), UserHandle.of(WORK_USER_ID));
+ FrameLayout parent = new FrameLayout(mContext);
+
+ RecyclerView.Adapter adapter =
+ UserAdapter.createUserRecycleViewAdapter(mContext, userHandles, mOnClickListener);
+ UserAdapter.ViewHolder holder = adapter.createViewHolder(parent, 0);
+ adapter.bindViewHolder(holder, 0);
+ holder.itemView.findViewById(R.id.button).performClick();
+
+ assertThat(adapter.getItemCount()).isEqualTo(2);
+ TextView textView = holder.itemView.findViewById(android.R.id.title);
+ assertThat(textView.getText().toString()).isEqualTo("Personal");
+ verify(mOnClickListener).onClick(anyInt());
+ }
+}