Merge "Disable user switching when the device is factory resetting." into sc-v2-dev
This commit is contained in:
54
services/core/java/com/android/server/FactoryResetter.java
Normal file
54
services/core/java/com/android/server/FactoryResetter.java
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* 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.server;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
|
||||||
|
import com.android.internal.util.Preconditions;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO(b/225012970): add javadoc from {@code com.android.server.devicepolicy.FactoryResetter}
|
||||||
|
*/
|
||||||
|
public final class FactoryResetter {
|
||||||
|
|
||||||
|
private static final AtomicBoolean sFactoryResetting = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether a factory reset is in progress.
|
||||||
|
*/
|
||||||
|
public static boolean isFactoryResetting() {
|
||||||
|
return sFactoryResetting.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated called by {@code com.android.server.devicepolicy.FactoryResetter}, won't be
|
||||||
|
* needed once that class logic is moved into this.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public static void setFactoryResetting(Context context) {
|
||||||
|
Preconditions.checkCallAuthorization(context.checkCallingOrSelfPermission(
|
||||||
|
android.Manifest.permission.MASTER_CLEAR) == PackageManager.PERMISSION_GRANTED);
|
||||||
|
sFactoryResetting.set(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private FactoryResetter() {
|
||||||
|
throw new UnsupportedOperationException("Provides only static methods");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -106,6 +106,7 @@ import com.android.internal.annotations.VisibleForTesting;
|
|||||||
import com.android.internal.util.ArrayUtils;
|
import com.android.internal.util.ArrayUtils;
|
||||||
import com.android.internal.util.FrameworkStatsLog;
|
import com.android.internal.util.FrameworkStatsLog;
|
||||||
import com.android.internal.widget.LockPatternUtils;
|
import com.android.internal.widget.LockPatternUtils;
|
||||||
|
import com.android.server.FactoryResetter;
|
||||||
import com.android.server.FgThread;
|
import com.android.server.FgThread;
|
||||||
import com.android.server.LocalServices;
|
import com.android.server.LocalServices;
|
||||||
import com.android.server.SystemServiceManager;
|
import com.android.server.SystemServiceManager;
|
||||||
@@ -1765,6 +1766,10 @@ class UserController implements Handler.Callback {
|
|||||||
Slogf.w(TAG, "Cannot switch to User #" + targetUserId + ": not a full user");
|
Slogf.w(TAG, "Cannot switch to User #" + targetUserId + ": not a full user");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (FactoryResetter.isFactoryResetting()) {
|
||||||
|
Slogf.w(TAG, "Cannot switch to User #" + targetUserId + ": factory reset in progress");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
boolean userSwitchUiEnabled;
|
boolean userSwitchUiEnabled;
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
if (!mInitialized) {
|
if (!mInitialized) {
|
||||||
|
|||||||
@@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package com.android.server.devicepolicy;
|
package com.android.server.devicepolicy;
|
||||||
|
|
||||||
|
import static com.android.server.FactoryResetter.setFactoryResetting;
|
||||||
|
|
||||||
import android.annotation.Nullable;
|
import android.annotation.Nullable;
|
||||||
import android.app.admin.DevicePolicySafetyChecker;
|
import android.app.admin.DevicePolicySafetyChecker;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@@ -36,7 +38,10 @@ import java.util.Objects;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Entry point for "factory reset" requests.
|
* Entry point for "factory reset" requests.
|
||||||
|
*
|
||||||
|
* @deprecated TODO(b/225012970): should be moved to {@code com.android.server.FactoryResetter}
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public final class FactoryResetter {
|
public final class FactoryResetter {
|
||||||
|
|
||||||
private static final String TAG = FactoryResetter.class.getSimpleName();
|
private static final String TAG = FactoryResetter.class.getSimpleName();
|
||||||
@@ -60,6 +65,8 @@ public final class FactoryResetter {
|
|||||||
Preconditions.checkCallAuthorization(mContext.checkCallingOrSelfPermission(
|
Preconditions.checkCallAuthorization(mContext.checkCallingOrSelfPermission(
|
||||||
android.Manifest.permission.MASTER_CLEAR) == PackageManager.PERMISSION_GRANTED);
|
android.Manifest.permission.MASTER_CLEAR) == PackageManager.PERMISSION_GRANTED);
|
||||||
|
|
||||||
|
setFactoryResetting(mContext);
|
||||||
|
|
||||||
if (mSafetyChecker == null) {
|
if (mSafetyChecker == null) {
|
||||||
factoryResetInternalUnchecked();
|
factoryResetInternalUnchecked();
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -14,11 +14,13 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// TODO(b/225012970): should be moved to com.android.server
|
||||||
package com.android.server.devicepolicy;
|
package com.android.server.devicepolicy;
|
||||||
|
|
||||||
import static com.android.dx.mockito.inline.extended.ExtendedMockito.doAnswer;
|
import static com.android.dx.mockito.inline.extended.ExtendedMockito.doAnswer;
|
||||||
import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
|
import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
|
||||||
import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
|
import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
|
||||||
|
import static com.android.server.FactoryResetter.isFactoryResetting;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
@@ -165,6 +167,7 @@ public final class FactoryResetterTest {
|
|||||||
.factoryReset();
|
.factoryReset();
|
||||||
|
|
||||||
assertThat(success).isTrue();
|
assertThat(success).isTrue();
|
||||||
|
assertThat(isFactoryResetting()).isTrue();
|
||||||
verifyWipeAdoptableStorageCalled();
|
verifyWipeAdoptableStorageCalled();
|
||||||
verifyWipeFactoryResetProtectionNotCalled();
|
verifyWipeFactoryResetProtectionNotCalled();
|
||||||
verifyRebootWipeUserDataMinimumArgsCalled();
|
verifyRebootWipeUserDataMinimumArgsCalled();
|
||||||
@@ -179,6 +182,7 @@ public final class FactoryResetterTest {
|
|||||||
.build().factoryReset();
|
.build().factoryReset();
|
||||||
|
|
||||||
assertThat(success).isTrue();
|
assertThat(success).isTrue();
|
||||||
|
assertThat(isFactoryResetting()).isTrue();
|
||||||
verifyWipeAdoptableStorageNotCalled();
|
verifyWipeAdoptableStorageNotCalled();
|
||||||
verifyWipeFactoryResetProtectionCalled();
|
verifyWipeFactoryResetProtectionCalled();
|
||||||
verifyRebootWipeUserDataMinimumArgsCalled();
|
verifyRebootWipeUserDataMinimumArgsCalled();
|
||||||
@@ -198,6 +202,7 @@ public final class FactoryResetterTest {
|
|||||||
.build().factoryReset();
|
.build().factoryReset();
|
||||||
|
|
||||||
assertThat(success).isTrue();
|
assertThat(success).isTrue();
|
||||||
|
assertThat(isFactoryResetting()).isTrue();
|
||||||
verifyWipeAdoptableStorageCalled();
|
verifyWipeAdoptableStorageCalled();
|
||||||
verifyWipeFactoryResetProtectionCalled();
|
verifyWipeFactoryResetProtectionCalled();
|
||||||
verifyRebootWipeUserDataAllArgsCalled();
|
verifyRebootWipeUserDataAllArgsCalled();
|
||||||
@@ -211,6 +216,7 @@ public final class FactoryResetterTest {
|
|||||||
.setSafetyChecker(mSafetyChecker).build().factoryReset();
|
.setSafetyChecker(mSafetyChecker).build().factoryReset();
|
||||||
|
|
||||||
assertThat(success).isFalse();
|
assertThat(success).isFalse();
|
||||||
|
assertThat(isFactoryResetting()).isTrue();
|
||||||
verifyWipeAdoptableStorageNotCalled();
|
verifyWipeAdoptableStorageNotCalled();
|
||||||
verifyWipeFactoryResetProtectionNotCalled();
|
verifyWipeFactoryResetProtectionNotCalled();
|
||||||
verifyRebootWipeUserDataNotCalled();
|
verifyRebootWipeUserDataNotCalled();
|
||||||
@@ -238,6 +244,7 @@ public final class FactoryResetterTest {
|
|||||||
.build().factoryReset();
|
.build().factoryReset();
|
||||||
|
|
||||||
assertThat(success).isFalse();
|
assertThat(success).isFalse();
|
||||||
|
assertThat(isFactoryResetting()).isTrue();
|
||||||
verifyWipeAdoptableStorageCalled();
|
verifyWipeAdoptableStorageCalled();
|
||||||
verifyWipeFactoryResetProtectionCalled();
|
verifyWipeFactoryResetProtectionCalled();
|
||||||
verifyRebootWipeUserDataAllArgsCalled();
|
verifyRebootWipeUserDataAllArgsCalled();
|
||||||
|
|||||||
Reference in New Issue
Block a user