From d4ad5497020322248e740c2c1400483e2be2828b Mon Sep 17 00:00:00 2001 From: Alex Chau Date: Wed, 23 Jan 2019 13:39:37 +0000 Subject: [PATCH] Add back @NonNull to DevicePolicyManager.wipeData - Added WIPE_SILENTLY to skip notifying user Bug: 114711242 Test: cts-tradefed run singleCommand cts -m CtsDevicePolicyManagerTestCases --test com.android.cts.devicepolicy.ManagedProfileTest#testWipeDataWithoutReason Change-Id: I163e0f3e1ab1e6feef668070a73b1f86db9691b3 --- api/current.txt | 3 +- .../app/admin/DevicePolicyManager.java | 28 +++++++++++++------ .../DevicePolicyManagerService.java | 8 ++++-- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/api/current.txt b/api/current.txt index 442be31569e8d..d7cbe722327c8 100644 --- a/api/current.txt +++ b/api/current.txt @@ -6787,7 +6787,7 @@ package android.app.admin { method public void uninstallCaCert(@Nullable android.content.ComponentName, byte[]); method public boolean updateOverrideApn(@NonNull android.content.ComponentName, int, @NonNull android.telephony.data.ApnSetting); method public void wipeData(int); - method public void wipeData(int, CharSequence); + method public void wipeData(int, @NonNull CharSequence); field public static final String ACTION_ADD_DEVICE_ADMIN = "android.app.action.ADD_DEVICE_ADMIN"; field public static final String ACTION_ADMIN_POLICY_COMPLIANCE = "android.app.action.ADMIN_POLICY_COMPLIANCE"; field public static final String ACTION_APPLICATION_DELEGATION_SCOPES_CHANGED = "android.app.action.APPLICATION_DELEGATION_SCOPES_CHANGED"; @@ -6930,6 +6930,7 @@ package android.app.admin { field public static final int WIPE_EUICC = 4; // 0x4 field public static final int WIPE_EXTERNAL_STORAGE = 1; // 0x1 field public static final int WIPE_RESET_PROTECTION_DATA = 2; // 0x2 + field public static final int WIPE_SILENTLY = 8; // 0x8 } public abstract static class DevicePolicyManager.InstallUpdateCallback { diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java index 7cc953cc254f7..f8f6862798a7d 100644 --- a/core/java/android/app/admin/DevicePolicyManager.java +++ b/core/java/android/app/admin/DevicePolicyManager.java @@ -3955,6 +3955,10 @@ public class DevicePolicyManager { */ public static final int WIPE_EUICC = 0x0004; + /** + * Flag for {@link #wipeData(int)}: won't show reason for wiping to the user. + */ + public static final int WIPE_SILENTLY = 0x0008; /** * Ask that all user data be wiped. If called as a secondary user, the user will be removed and @@ -3966,9 +3970,10 @@ public class DevicePolicyManager { * be able to call this method; if it has not, a security exception will be thrown. * * @param flags Bit mask of additional options: currently supported flags are - * {@link #WIPE_EXTERNAL_STORAGE} and {@link #WIPE_RESET_PROTECTION_DATA}. + * {@link #WIPE_EXTERNAL_STORAGE}, {@link #WIPE_RESET_PROTECTION_DATA}, + * {@link #WIPE_EUICC} and {@link #WIPE_SILENTLY}. * @throws SecurityException if the calling application does not own an active administrator - * that uses {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA} + * that uses {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA} */ public void wipeData(int flags) { throwIfParentInstance("wipeData"); @@ -3988,16 +3993,21 @@ public class DevicePolicyManager { * be able to call this method; if it has not, a security exception will be thrown. * * @param flags Bit mask of additional options: currently supported flags are - * {@link #WIPE_EXTERNAL_STORAGE} and {@link #WIPE_RESET_PROTECTION_DATA}. + * {@link #WIPE_EXTERNAL_STORAGE}, {@link #WIPE_RESET_PROTECTION_DATA} and + * {@link #WIPE_EUICC}. * @param reason a string that contains the reason for wiping data, which can be - * presented to the user. If the string is null or empty, user won't be notified. + * presented to the user. * @throws SecurityException if the calling application does not own an active administrator - * that uses {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA} - * @throws IllegalArgumentException if the input reason string is null or empty. + * that uses {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA} + * @throws IllegalArgumentException if the input reason string is null or empty, or if + * {@link #WIPE_SILENTLY} is set. */ - public void wipeData(int flags, CharSequence reason) { + public void wipeData(int flags, @NonNull CharSequence reason) { throwIfParentInstance("wipeData"); - wipeDataInternal(flags, reason != null ? reason.toString() : null); + Preconditions.checkNotNull(reason, "reason string is null"); + Preconditions.checkStringNotEmpty(reason, "reason string is empty"); + Preconditions.checkArgument((flags & WIPE_SILENTLY) == 0, "WIPE_SILENTLY cannot be set"); + wipeDataInternal(flags, reason.toString()); } /** @@ -4008,7 +4018,7 @@ public class DevicePolicyManager { * @see #wipeData(int, CharSequence) * @hide */ - private void wipeDataInternal(int flags, String wipeReasonForUser) { + private void wipeDataInternal(int flags, @NonNull String wipeReasonForUser) { if (mService != null) { try { mService.wipeDataWithReason(flags, wipeReasonForUser); diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java index fb7e47d70d64b..38846433ad31a 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java @@ -70,6 +70,7 @@ import static android.app.admin.DevicePolicyManager.PROFILE_KEYGUARD_FEATURES_AF import static android.app.admin.DevicePolicyManager.WIPE_EUICC; import static android.app.admin.DevicePolicyManager.WIPE_EXTERNAL_STORAGE; import static android.app.admin.DevicePolicyManager.WIPE_RESET_PROTECTION_DATA; +import static android.app.admin.DevicePolicyManager.WIPE_SILENTLY; import static android.content.pm.PackageManager.MATCH_UNINSTALLED_PACKAGES; import static android.provider.Settings.Global.PRIVATE_DNS_MODE; import static android.provider.Settings.Global.PRIVATE_DNS_SPECIFIER; @@ -6362,7 +6363,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { } } - private void forceWipeUser(int userId, String wipeReasonForUser) { + private void forceWipeUser(int userId, String wipeReasonForUser, boolean wipeSilently) { boolean success = false; try { IActivityManager am = mInjector.getIActivityManager(); @@ -6373,7 +6374,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { success = mUserManagerInternal.removeUserEvenWhenDisallowed(userId); if (!success) { Slog.w(LOG_TAG, "Couldn't remove user " + userId); - } else if (isManagedProfile(userId) && !TextUtils.isEmpty(wipeReasonForUser)) { + } else if (isManagedProfile(userId) && !wipeSilently) { sendWipeProfileNotification(wipeReasonForUser); } } catch (RemoteException re) { @@ -6388,6 +6389,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { if (!mHasFeature) { return; } + Preconditions.checkStringNotEmpty(wipeReasonForUser, "wipeReasonForUser is null or empty"); enforceFullCrossUsersPermission(mInjector.userHandleGetCallingUserId()); final ActiveAdmin admin; @@ -6447,7 +6449,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { internalReason, /*wipeEuicc=*/ (flags & WIPE_EUICC) != 0); } else { - forceWipeUser(userId, wipeReasonForUser); + forceWipeUser(userId, wipeReasonForUser, (flags & WIPE_SILENTLY) != 0); } } finally { mInjector.binderRestoreCallingIdentity(ident);