Merge "Add dialog for top switch on fully managed devices." into main
This commit is contained in:
@@ -15,20 +15,22 @@
|
|||||||
*/
|
*/
|
||||||
package com.android.settings.security;
|
package com.android.settings.security;
|
||||||
|
|
||||||
|
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.widget.CompoundButton;
|
import android.widget.CompoundButton;
|
||||||
import android.widget.CompoundButton.OnCheckedChangeListener;
|
import android.widget.CompoundButton.OnCheckedChangeListener;
|
||||||
|
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
import androidx.preference.Preference;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.Utils;
|
|
||||||
import com.android.settings.core.TogglePreferenceController;
|
import com.android.settings.core.TogglePreferenceController;
|
||||||
import com.android.settings.widget.SettingsMainSwitchPreference;
|
import com.android.settings.widget.SettingsMainSwitchPreference;
|
||||||
|
import com.android.settingslib.RestrictedLockUtils;
|
||||||
|
import com.android.settingslib.RestrictedLockUtilsInternal;
|
||||||
|
|
||||||
/** Preference controller for content protection toggle switch bar. */
|
/** Preference controller for content protection toggle switch bar. */
|
||||||
public class ContentProtectionTogglePreferenceController extends TogglePreferenceController
|
public class ContentProtectionTogglePreferenceController extends TogglePreferenceController
|
||||||
@@ -37,9 +39,9 @@ public class ContentProtectionTogglePreferenceController extends TogglePreferenc
|
|||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static final String KEY_CONTENT_PROTECTION_PREFERENCE = "content_protection_user_consent";
|
static final String KEY_CONTENT_PROTECTION_PREFERENCE = "content_protection_user_consent";
|
||||||
|
|
||||||
private SettingsMainSwitchPreference mSwitchBar;
|
@Nullable private SettingsMainSwitchPreference mSwitchBar;
|
||||||
|
@Nullable private RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin;
|
||||||
private final ContentResolver mContentResolver;
|
private final ContentResolver mContentResolver;
|
||||||
private final boolean isFullyManagedDevice = Utils.getDeviceOwnerComponent(mContext) != null;
|
|
||||||
|
|
||||||
public ContentProtectionTogglePreferenceController(Context context, String preferenceKey) {
|
public ContentProtectionTogglePreferenceController(Context context, String preferenceKey) {
|
||||||
super(context, preferenceKey);
|
super(context, preferenceKey);
|
||||||
@@ -53,7 +55,7 @@ public class ContentProtectionTogglePreferenceController extends TogglePreferenc
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isChecked() {
|
public boolean isChecked() {
|
||||||
if (isFullyManagedDevice) {
|
if (mEnforcedAdmin != null) {
|
||||||
// If fully managed device, it should always unchecked
|
// If fully managed device, it should always unchecked
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -70,12 +72,25 @@ public class ContentProtectionTogglePreferenceController extends TogglePreferenc
|
|||||||
@Override
|
@Override
|
||||||
public void displayPreference(PreferenceScreen screen) {
|
public void displayPreference(PreferenceScreen screen) {
|
||||||
super.displayPreference(screen);
|
super.displayPreference(screen);
|
||||||
|
final Preference preference = screen.findPreference(getPreferenceKey());
|
||||||
|
|
||||||
mSwitchBar = screen.findPreference(getPreferenceKey());
|
if (preference instanceof SettingsMainSwitchPreference) {
|
||||||
|
mSwitchBar = (SettingsMainSwitchPreference) preference;
|
||||||
mSwitchBar.addOnSwitchChangeListener(this);
|
mSwitchBar.addOnSwitchChangeListener(this);
|
||||||
if (isFullyManagedDevice) {
|
}
|
||||||
// If fully managed device, the switch bar is greyed out
|
}
|
||||||
mSwitchBar.setEnabled(false);
|
|
||||||
|
/**
|
||||||
|
* Temporary workaround for SettingsMainSwitchPreference.setDisabledByAdmin without user
|
||||||
|
* restriction.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void updateState(Preference preference) {
|
||||||
|
super.updateState(preference);
|
||||||
|
// Assign the value to mEnforcedAdmin since it's needed in isChecked()
|
||||||
|
mEnforcedAdmin = getEnforcedAdmin();
|
||||||
|
if (mSwitchBar != null && mEnforcedAdmin != null) {
|
||||||
|
mSwitchBar.setDisabledByAdmin(mEnforcedAdmin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,4 +105,9 @@ public class ContentProtectionTogglePreferenceController extends TogglePreferenc
|
|||||||
public int getSliceHighlightMenuRes() {
|
public int getSliceHighlightMenuRes() {
|
||||||
return R.string.menu_key_security;
|
return R.string.menu_key_security;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
protected RestrictedLockUtils.EnforcedAdmin getEnforcedAdmin() {
|
||||||
|
return RestrictedLockUtilsInternal.getDeviceOwner(mContext);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -78,7 +78,7 @@ public class SettingsMainSwitchPreference extends TwoStatePreference implements
|
|||||||
holder.setDividerAllowedAbove(false);
|
holder.setDividerAllowedAbove(false);
|
||||||
holder.setDividerAllowedBelow(false);
|
holder.setDividerAllowedBelow(false);
|
||||||
|
|
||||||
if (mRestrictedHelper != null) {
|
if (mEnforcedAdmin == null && mRestrictedHelper != null) {
|
||||||
mEnforcedAdmin = mRestrictedHelper.checkRestrictionEnforced();
|
mEnforcedAdmin = mRestrictedHelper.checkRestrictionEnforced();
|
||||||
}
|
}
|
||||||
mMainSwitchBar = (SettingsMainSwitchBar) holder.findViewById(R.id.main_switch_bar);
|
mMainSwitchBar = (SettingsMainSwitchBar) holder.findViewById(R.id.main_switch_bar);
|
||||||
|
@@ -13,16 +13,18 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.android.settings.security;
|
package com.android.settings.security;
|
||||||
|
|
||||||
import static com.android.settings.security.ContentProtectionTogglePreferenceController.KEY_CONTENT_PROTECTION_PREFERENCE;
|
import static com.android.settings.security.ContentProtectionTogglePreferenceController.KEY_CONTENT_PROTECTION_PREFERENCE;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.content.ComponentName;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.platform.test.flag.junit.SetFlagsRule;
|
import android.platform.test.flag.junit.SetFlagsRule;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
@@ -32,6 +34,7 @@ import androidx.test.core.app.ApplicationProvider;
|
|||||||
|
|
||||||
import com.android.settings.testutils.shadow.ShadowUtils;
|
import com.android.settings.testutils.shadow.ShadowUtils;
|
||||||
import com.android.settings.widget.SettingsMainSwitchPreference;
|
import com.android.settings.widget.SettingsMainSwitchPreference;
|
||||||
|
import com.android.settingslib.RestrictedLockUtils;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -55,8 +58,9 @@ public class ContentProtectionTogglePreferenceControllerTest {
|
|||||||
@Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
|
@Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
|
||||||
@Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule();
|
@Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule();
|
||||||
|
|
||||||
@Mock private PreferenceScreen mScreen;
|
@Mock private PreferenceScreen mMockScreen;
|
||||||
|
|
||||||
|
private RestrictedLockUtils.EnforcedAdmin mAdmin;
|
||||||
private SettingsMainSwitchPreference mSwitchPreference;
|
private SettingsMainSwitchPreference mSwitchPreference;
|
||||||
private final Context mContext = ApplicationProvider.getApplicationContext();
|
private final Context mContext = ApplicationProvider.getApplicationContext();
|
||||||
private ContentProtectionTogglePreferenceController mController;
|
private ContentProtectionTogglePreferenceController mController;
|
||||||
@@ -65,9 +69,10 @@ public class ContentProtectionTogglePreferenceControllerTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mController = new ContentProtectionTogglePreferenceController(mContext, "key");
|
mController = new TestContentProtectionTogglePreferenceController();
|
||||||
mSwitchPreference = new SettingsMainSwitchPreference(mContext);
|
mSwitchPreference = new SettingsMainSwitchPreference(mContext);
|
||||||
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mSwitchPreference);
|
when(mMockScreen.findPreference(mController.getPreferenceKey()))
|
||||||
|
.thenReturn(mSwitchPreference);
|
||||||
mSettingBackupValue = getContentProtectionGlobalSetting();
|
mSettingBackupValue = getContentProtectionGlobalSetting();
|
||||||
Settings.Global.putInt(mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, 0);
|
Settings.Global.putInt(mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, 0);
|
||||||
}
|
}
|
||||||
@@ -78,6 +83,7 @@ public class ContentProtectionTogglePreferenceControllerTest {
|
|||||||
mContext.getContentResolver(),
|
mContext.getContentResolver(),
|
||||||
KEY_CONTENT_PROTECTION_PREFERENCE,
|
KEY_CONTENT_PROTECTION_PREFERENCE,
|
||||||
mSettingBackupValue);
|
mSettingBackupValue);
|
||||||
|
ShadowUtils.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -85,6 +91,49 @@ public class ContentProtectionTogglePreferenceControllerTest {
|
|||||||
assertThat(mController.isAvailable()).isTrue();
|
assertThat(mController.isAvailable()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void displayPreference() {
|
||||||
|
setUpFullyManagedMode();
|
||||||
|
SettingsMainSwitchPreference mockSwitchPreference =
|
||||||
|
mock(SettingsMainSwitchPreference.class);
|
||||||
|
when(mMockScreen.findPreference(any())).thenReturn(mockSwitchPreference);
|
||||||
|
when(mockSwitchPreference.getKey()).thenReturn(mController.getPreferenceKey());
|
||||||
|
|
||||||
|
mController = new TestContentProtectionTogglePreferenceController();
|
||||||
|
mController.displayPreference(mMockScreen);
|
||||||
|
|
||||||
|
assertThat(mockSwitchPreference).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateState_notFullyManagedMode_enabled() {
|
||||||
|
SettingsMainSwitchPreference mockSwitchPreference =
|
||||||
|
mock(SettingsMainSwitchPreference.class);
|
||||||
|
when(mMockScreen.findPreference(any())).thenReturn(mockSwitchPreference);
|
||||||
|
when(mockSwitchPreference.getKey()).thenReturn(mController.getPreferenceKey());
|
||||||
|
|
||||||
|
mController = new TestContentProtectionTogglePreferenceController();
|
||||||
|
mController.displayPreference(mMockScreen);
|
||||||
|
mController.updateState(mockSwitchPreference);
|
||||||
|
|
||||||
|
verify(mockSwitchPreference, never()).setDisabledByAdmin(any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateState_fullyManagedMode_disabled() {
|
||||||
|
setUpFullyManagedMode();
|
||||||
|
SettingsMainSwitchPreference mockSwitchPreference =
|
||||||
|
mock(SettingsMainSwitchPreference.class);
|
||||||
|
when(mMockScreen.findPreference(any())).thenReturn(mockSwitchPreference);
|
||||||
|
when(mockSwitchPreference.getKey()).thenReturn(mController.getPreferenceKey());
|
||||||
|
|
||||||
|
mController = new TestContentProtectionTogglePreferenceController();
|
||||||
|
mController.displayPreference(mMockScreen);
|
||||||
|
mController.updateState(mockSwitchPreference);
|
||||||
|
|
||||||
|
verify(mockSwitchPreference).setDisabledByAdmin(mAdmin);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isChecked_settingTurnOn() {
|
public void isChecked_settingTurnOn() {
|
||||||
Settings.Global.putInt(mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, 1);
|
Settings.Global.putInt(mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, 1);
|
||||||
@@ -94,15 +143,18 @@ public class ContentProtectionTogglePreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isChecked_fullyManagedMode_settingTurnOff() {
|
public void isChecked_fullyManagedMode_settingTurnOff() {
|
||||||
final ComponentName componentName =
|
setUpFullyManagedMode();
|
||||||
ComponentName.unflattenFromString("com.android.test/.DeviceAdminReceiver");
|
|
||||||
ShadowUtils.setDeviceOwnerComponent(componentName);
|
|
||||||
Settings.Global.putInt(mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, 1);
|
Settings.Global.putInt(mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, 1);
|
||||||
|
SettingsMainSwitchPreference mockSwitchPreference =
|
||||||
|
mock(SettingsMainSwitchPreference.class);
|
||||||
|
when(mMockScreen.findPreference(any())).thenReturn(mockSwitchPreference);
|
||||||
|
when(mockSwitchPreference.getKey()).thenReturn(mController.getPreferenceKey());
|
||||||
|
|
||||||
ContentProtectionTogglePreferenceController controller =
|
mController = new TestContentProtectionTogglePreferenceController();
|
||||||
new ContentProtectionTogglePreferenceController(mContext, "key");
|
mController.displayPreference(mMockScreen);
|
||||||
|
mController.updateState(mockSwitchPreference);
|
||||||
|
|
||||||
assertThat(controller.isChecked()).isFalse();
|
assertThat(mController.isChecked()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -122,7 +174,6 @@ public class ContentProtectionTogglePreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onSwitchChanged_switchChecked_manuallyEnabled() {
|
public void onSwitchChanged_switchChecked_manuallyEnabled() {
|
||||||
mController.displayPreference(mScreen);
|
|
||||||
mController.setChecked(false);
|
mController.setChecked(false);
|
||||||
|
|
||||||
mController.onCheckedChanged(/* switchView= */ null, /* isChecked= */ true);
|
mController.onCheckedChanged(/* switchView= */ null, /* isChecked= */ true);
|
||||||
@@ -132,8 +183,6 @@ public class ContentProtectionTogglePreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onSwitchChanged_switchUnchecked_manuallyDisabled() {
|
public void onSwitchChanged_switchUnchecked_manuallyDisabled() {
|
||||||
mController.displayPreference(mScreen);
|
|
||||||
|
|
||||||
mController.onCheckedChanged(/* switchView= */ null, /* isChecked= */ false);
|
mController.onCheckedChanged(/* switchView= */ null, /* isChecked= */ false);
|
||||||
|
|
||||||
assertThat(getContentProtectionGlobalSetting()).isEqualTo(-1);
|
assertThat(getContentProtectionGlobalSetting()).isEqualTo(-1);
|
||||||
@@ -143,4 +192,21 @@ public class ContentProtectionTogglePreferenceControllerTest {
|
|||||||
return Settings.Global.getInt(
|
return Settings.Global.getInt(
|
||||||
mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, 0);
|
mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setUpFullyManagedMode() {
|
||||||
|
mAdmin = new RestrictedLockUtils.EnforcedAdmin();
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TestContentProtectionTogglePreferenceController
|
||||||
|
extends ContentProtectionTogglePreferenceController {
|
||||||
|
|
||||||
|
TestContentProtectionTogglePreferenceController() {
|
||||||
|
super(ContentProtectionTogglePreferenceControllerTest.this.mContext, "key");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected RestrictedLockUtils.EnforcedAdmin getEnforcedAdmin() {
|
||||||
|
return mAdmin;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user