Restrict screen saver settings searches to enabled users.
If a user can't enable screen saver, they shouldn't be able to accidentally find screen saver related settings in Settings. Bug: 260276394 Test: atest ScreenSaverPreferenceControllerTest Test: manually by settings up a second user on a device that supports screen saver, switching to that user, and making sure that searches for "screen saver" don't accidentally reveal settings that can't be accessed. Change-Id: If2f465b2acb548455f0868507cf47217ea1a626f
This commit is contained in:
@@ -1293,4 +1293,15 @@ public final class Utils extends com.android.settingslib.Utils {
|
|||||||
return context.createContextAsUser(mainUser, 0).getSystemService(UserManager.class)
|
return context.createContextAsUser(mainUser, 0).getSystemService(UserManager.class)
|
||||||
.isUserForeground();
|
.isUserForeground();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns if dreams are available to the current user.
|
||||||
|
*/
|
||||||
|
public static boolean areDreamsAvailableToCurrentUser(Context context) {
|
||||||
|
final boolean dreamsSupported = context.getResources().getBoolean(
|
||||||
|
com.android.internal.R.bool.config_dreamsSupported);
|
||||||
|
final boolean dreamsOnlyEnabledForDockUser = context.getResources().getBoolean(
|
||||||
|
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser);
|
||||||
|
return dreamsSupported && (!dreamsOnlyEnabledForDockUser || canCurrentUserDream(context));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -35,13 +35,7 @@ public class ScreenSaverPreferenceController extends BasePreferenceController im
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getAvailabilityStatus() {
|
public int getAvailabilityStatus() {
|
||||||
final boolean dreamsSupported = mContext.getResources().getBoolean(
|
return Utils.areDreamsAvailableToCurrentUser(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
|
||||||
com.android.internal.R.bool.config_dreamsSupported);
|
|
||||||
final boolean dreamsOnlyEnabledForDockUser = mContext.getResources().getBoolean(
|
|
||||||
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser);
|
|
||||||
return (dreamsSupported && (!dreamsOnlyEnabledForDockUser
|
|
||||||
|| Utils.canCurrentUserDream(mContext)))
|
|
||||||
? AVAILABLE : UNSUPPORTED_ON_DEVICE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -36,6 +36,7 @@ import androidx.preference.Preference;
|
|||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.Utils;
|
||||||
import com.android.settings.dashboard.DashboardFragment;
|
import com.android.settings.dashboard.DashboardFragment;
|
||||||
import com.android.settings.search.BaseSearchIndexProvider;
|
import com.android.settings.search.BaseSearchIndexProvider;
|
||||||
import com.android.settingslib.core.AbstractPreferenceController;
|
import com.android.settingslib.core.AbstractPreferenceController;
|
||||||
@@ -213,6 +214,17 @@ public class DreamSettings extends DashboardFragment implements OnMainSwitchChan
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||||
new BaseSearchIndexProvider(R.xml.dream_fragment_overview);
|
new SearchIndexProvider(R.xml.dream_fragment_overview);
|
||||||
|
|
||||||
|
static class SearchIndexProvider extends BaseSearchIndexProvider {
|
||||||
|
SearchIndexProvider(int xmlRes) {
|
||||||
|
super(xmlRes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean isPageSearchEnabled(Context context) {
|
||||||
|
return Utils.areDreamsAvailableToCurrentUser(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -18,13 +18,15 @@ package com.android.settings.display;
|
|||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
|
|
||||||
import androidx.test.core.app.ApplicationProvider;
|
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -32,14 +34,13 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.mockito.Spy;
|
|
||||||
|
|
||||||
@RunWith(AndroidJUnit4.class)
|
@RunWith(AndroidJUnit4.class)
|
||||||
public class ScreenSaverPreferenceControllerTest {
|
public class ScreenSaverPreferenceControllerTest {
|
||||||
@Spy
|
@Mock
|
||||||
private final Context mContext = ApplicationProvider.getApplicationContext();
|
private Context mContext;
|
||||||
@Spy
|
@Mock
|
||||||
private final Resources mResources = mContext.getResources();
|
private Resources mResources;
|
||||||
@Mock
|
@Mock
|
||||||
private UserManager mUserManager;
|
private UserManager mUserManager;
|
||||||
|
|
||||||
@@ -51,10 +52,14 @@ public class ScreenSaverPreferenceControllerTest {
|
|||||||
public void setup() {
|
public void setup() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
mController = new ScreenSaverPreferenceController(mContext, mPrefKey);
|
|
||||||
|
|
||||||
when(mContext.getResources()).thenReturn(mResources);
|
when(mContext.getResources()).thenReturn(mResources);
|
||||||
|
when(mContext.getSystemServiceName(UserManager.class))
|
||||||
|
.thenReturn(Context.USER_SERVICE);
|
||||||
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
|
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
|
||||||
|
when(mUserManager.getMainUser()).thenReturn(UserHandle.of(0));
|
||||||
|
when(mContext.createContextAsUser(any(), anyInt())).thenReturn(mContext);
|
||||||
|
|
||||||
|
mController = new ScreenSaverPreferenceController(mContext, mPrefKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -64,7 +69,7 @@ public class ScreenSaverPreferenceControllerTest {
|
|||||||
when(mResources.getBoolean(
|
when(mResources.getBoolean(
|
||||||
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser))
|
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser))
|
||||||
.thenReturn(false);
|
.thenReturn(false);
|
||||||
when(mUserManager.isMainUser()).thenReturn(true);
|
when(mUserManager.isUserForeground()).thenReturn(true);
|
||||||
assertTrue(mController.isAvailable());
|
assertTrue(mController.isAvailable());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +80,7 @@ public class ScreenSaverPreferenceControllerTest {
|
|||||||
when(mResources.getBoolean(
|
when(mResources.getBoolean(
|
||||||
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser))
|
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser))
|
||||||
.thenReturn(false);
|
.thenReturn(false);
|
||||||
when(mUserManager.isMainUser()).thenReturn(false);
|
when(mUserManager.isUserForeground()).thenReturn(false);
|
||||||
assertTrue(mController.isAvailable());
|
assertTrue(mController.isAvailable());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,7 +91,7 @@ public class ScreenSaverPreferenceControllerTest {
|
|||||||
when(mResources.getBoolean(
|
when(mResources.getBoolean(
|
||||||
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser))
|
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser))
|
||||||
.thenReturn(false);
|
.thenReturn(false);
|
||||||
when(mUserManager.isMainUser()).thenReturn(true);
|
when(mUserManager.isUserForeground()).thenReturn(true);
|
||||||
assertFalse(mController.isAvailable());
|
assertFalse(mController.isAvailable());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +102,7 @@ public class ScreenSaverPreferenceControllerTest {
|
|||||||
when(mResources.getBoolean(
|
when(mResources.getBoolean(
|
||||||
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser))
|
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser))
|
||||||
.thenReturn(true);
|
.thenReturn(true);
|
||||||
when(mUserManager.isMainUser()).thenReturn(true);
|
when(mUserManager.isUserForeground()).thenReturn(true);
|
||||||
assertTrue(mController.isAvailable());
|
assertTrue(mController.isAvailable());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,7 +113,7 @@ public class ScreenSaverPreferenceControllerTest {
|
|||||||
when(mResources.getBoolean(
|
when(mResources.getBoolean(
|
||||||
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser))
|
com.android.internal.R.bool.config_dreamsOnlyEnabledForDockUser))
|
||||||
.thenReturn(true);
|
.thenReturn(true);
|
||||||
when(mUserManager.isMainUser()).thenReturn(false);
|
when(mUserManager.isUserForeground()).thenReturn(false);
|
||||||
assertFalse(mController.isAvailable());
|
assertFalse(mController.isAvailable());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user