Skip to content

Commit 400dc8c

Browse files
jscott1989pfmaggi
authored andcommitted
No public description
PiperOrigin-RevId: 640863854
1 parent dc8b794 commit 400dc8c

File tree

8 files changed

+85
-65
lines changed

8 files changed

+85
-65
lines changed

src/main/AndroidManifest.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
<uses-permission android:name="android.permission.MANAGE_DEVICE_POLICY_VPN"/>
6464
<uses-permission android:name="android.permission.MANAGE_DEVICE_POLICY_WALLPAPER"/>
6565
<uses-permission android:name="android.permission.MANAGE_DEVICE_POLICY_WIFI"/>
66-
<uses-permission android:name="android.permission.MANAGE_DEVICE_POLICY_CAMERA_TOGGLE"/>
6766
<uses-permission android:name="android.permission.UPDATE_DEVICE_MANAGEMENT_RESOURCES"/>
6867

6968
<uses-feature android:name="android.hardware.wifi" android:required="false" />

src/main/java/com/afwsamples/testdpc/DevicePolicyManagerGateway.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ void setUserRestriction(
246246
/** See {@link android.app.admin.DevicePolicyManager#wipeData(int)}. */
247247
void wipeData(int flags, @NonNull Consumer<Void> onSuccess, @NonNull Consumer<Exception> onError);
248248

249+
/** See {@link android.app.admin.DevicePolicyManager#wipeDevice(int)}. */
250+
void wipeDevice(
251+
int flags, @NonNull Consumer<Void> onSuccess, @NonNull Consumer<Exception> onError);
252+
249253
/**
250254
* See {@link
251255
* android.app.admin.DevicePolicyManager#requestBugreport(android.content.ComponentName)}.

src/main/java/com/afwsamples/testdpc/DevicePolicyManagerGatewayImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.afwsamples.testdpc;
1818

19+
import android.annotation.TargetApi;
1920
import android.app.admin.DevicePolicyManager;
2021
import android.app.admin.NetworkEvent;
2122
import android.app.admin.SecurityLog.SecurityEvent;
@@ -27,6 +28,7 @@
2728
import android.content.pm.PackageManager.NameNotFoundException;
2829
import android.graphics.Bitmap;
2930
import android.location.LocationManager;
31+
import android.os.Build.VERSION_CODES;
3032
import android.os.Bundle;
3133
import android.os.PersistableBundle;
3234
import android.os.Process;
@@ -436,6 +438,19 @@ public void wipeData(int flags, Consumer<Void> onSuccess, Consumer<Exception> on
436438
}
437439
}
438440

441+
@Override
442+
@TargetApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
443+
public void wipeDevice(int flags, Consumer<Void> onSuccess, Consumer<Exception> onError) {
444+
Log.d(TAG, "wipeDevice(" + flags + ")");
445+
446+
try {
447+
mDevicePolicyManager.wipeDevice(flags);
448+
onSuccess.accept(null);
449+
} catch (Exception e) {
450+
onError.accept(e);
451+
}
452+
}
453+
439454
@Override
440455
public void requestBugreport(Consumer<Void> onSuccess, Consumer<Exception> onError) {
441456
Log.d(TAG, "requestBugreport(");

src/main/java/com/afwsamples/testdpc/policy/PolicyManagementFragment.java

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,8 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
390390
private static final String UNSUSPEND_APPS_KEY = "unsuspend_apps";
391391
private static final String CLEAR_APP_DATA_KEY = "clear_app_data";
392392
private static final String KEEP_UNINSTALLED_PACKAGES = "keep_uninstalled_packages";
393-
private static final String WIPE_DATA_KEY = "wipe_data";
393+
private static final String REMOVE_MANAGED_PROFILE_KEY = "remove_managed_profile";
394+
private static final String FACTORY_RESET_DEVICE_KEY = "factory_reset_device";
394395
private static final String CREATE_WIFI_CONFIGURATION_KEY = "create_wifi_configuration";
395396
private static final String CREATE_EAP_TLS_WIFI_CONFIGURATION_KEY =
396397
"create_eap_tls_wifi_configuration";
@@ -425,7 +426,6 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
425426
private static final String MANAGE_OVERRIDE_APN_KEY = "manage_override_apn";
426427
private static final String MANAGED_SYSTEM_UPDATES_KEY = "managed_system_updates";
427428
private static final String SET_PRIVATE_DNS_MODE_KEY = "set_private_dns_mode";
428-
private static final String FACTORY_RESET_ORG_OWNED_DEVICE = "factory_reset_org_owned_device";
429429
private static final String SET_FACTORY_RESET_PROTECTION_POLICY_KEY =
430430
"set_factory_reset_protection_policy";
431431
private static final String SET_LOCATION_ENABLED_KEY = "set_location_enabled";
@@ -682,7 +682,8 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
682682
mStayOnWhilePluggedInSwitchPreference =
683683
(SwitchPreference) findPreference(STAY_ON_WHILE_PLUGGED_IN);
684684
mStayOnWhilePluggedInSwitchPreference.setOnPreferenceChangeListener(this);
685-
findPreference(WIPE_DATA_KEY).setOnPreferenceClickListener(this);
685+
findPreference(REMOVE_MANAGED_PROFILE_KEY).setOnPreferenceClickListener(this);
686+
findPreference(FACTORY_RESET_DEVICE_KEY).setOnPreferenceClickListener(this);
686687
findPreference(REMOVE_DEVICE_OWNER_KEY).setOnPreferenceClickListener(this);
687688
mEnableBackupServicePreference = (DpcSwitchPreference) findPreference(ENABLE_BACKUP_SERVICE);
688689
mEnableBackupServicePreference.setOnPreferenceChangeListener(this);
@@ -810,7 +811,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
810811

811812
findPreference(CROSS_PROFILE_CALENDAR_KEY).setOnPreferenceClickListener(this);
812813
findPreference(ENTERPRISE_SLICE_KEY).setOnPreferenceClickListener(this);
813-
findPreference(FACTORY_RESET_ORG_OWNED_DEVICE).setOnPreferenceClickListener(this);
814814
findPreference(SET_FACTORY_RESET_PROTECTION_POLICY_KEY).setOnPreferenceClickListener(this);
815815
findPreference(SET_ORGANIZATION_ID_KEY).setOnPreferenceClickListener(this);
816816

@@ -1060,8 +1060,11 @@ public boolean onPreferenceClick(Preference preference) {
10601060
// no lock task present, ignore
10611061
}
10621062
return true;
1063-
} else if (WIPE_DATA_KEY.equals(key)) {
1064-
showWipeDataPrompt();
1063+
} else if (REMOVE_MANAGED_PROFILE_KEY.equals(key)) {
1064+
showWipeDataPrompt(/* wipeDevice */ false);
1065+
return true;
1066+
} else if (FACTORY_RESET_DEVICE_KEY.equals(key)) {
1067+
showWipeDataPrompt(/* wipeDevice */ true);
10651068
return true;
10661069
} else if (REMOVE_DEVICE_OWNER_KEY.equals(key)) {
10671070
showRemoveDeviceOwnerPrompt();
@@ -1403,9 +1406,6 @@ public void onPositiveButtonClicked(String[] lockTaskArray) {
14031406
} else if (SET_PROFILE_NAME_KEY.equals(key)) {
14041407
showSetProfileNameDialog();
14051408
return true;
1406-
} else if (FACTORY_RESET_ORG_OWNED_DEVICE.equals(key)) {
1407-
factoryResetOrgOwnedDevice();
1408-
return true;
14091409
} else if (SET_FACTORY_RESET_PROTECTION_POLICY_KEY.equals(key)) {
14101410
showFragment(new FactoryResetProtectionPolicyFragment());
14111411
return true;
@@ -2067,10 +2067,10 @@ public void onClick(DialogInterface dialogInterface, int which) {
20672067
}
20682068

20692069
/**
2070-
* Shows a prompt to ask for confirmation on wiping the data and also provide an option to set if
2071-
* external storage and factory reset protection data also needs to wiped.
2070+
* Shows a prompt to ask for confirmation on wiping the profile / device and also provide an
2071+
* option to set if external storage and factory reset protection data also needs to wiped.
20722072
*/
2073-
private void showWipeDataPrompt() {
2073+
private void showWipeDataPrompt(boolean wipeDevice) {
20742074
final LayoutInflater inflater = getActivity().getLayoutInflater();
20752075
final View dialogView = inflater.inflate(R.layout.wipe_data_dialog_prompt, null);
20762076
final CheckBox externalStorageCheckBox =
@@ -2079,7 +2079,10 @@ private void showWipeDataPrompt() {
20792079
(CheckBox) dialogView.findViewById(R.id.reset_protection_checkbox);
20802080

20812081
new AlertDialog.Builder(getActivity())
2082-
.setTitle(R.string.wipe_data_title)
2082+
.setTitle(
2083+
wipeDevice
2084+
? R.string.factory_reset_device_title
2085+
: R.string.remove_managed_profile_title)
20832086
.setView(dialogView)
20842087
.setPositiveButton(
20852088
android.R.string.ok,
@@ -2095,8 +2098,28 @@ public void onClick(DialogInterface dialogInterface, int i) {
20952098
(resetProtectionCheckBox.isChecked()
20962099
? DevicePolicyManager.WIPE_RESET_PROTECTION_DATA
20972100
: 0);
2098-
mDevicePolicyManagerGateway.wipeData(
2099-
flags, (v) -> onSuccessLog("wipeData"), (e) -> onErrorLog("wipeData", e));
2101+
if (wipeDevice) {
2102+
if (Util.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
2103+
// Since U, factory reset needs to use wipeDevice()
2104+
mDevicePolicyManagerGateway.wipeDevice(
2105+
flags, (v) -> onSuccessLog("wipeData"), (e) -> onErrorLog("wipeData", e));
2106+
} else if (mIsOrganizationOwnedProfileOwner) {
2107+
// Before U, factory reset by COPE goes via the parent DPM instance
2108+
DevicePolicyManagerGatewayImpl.forParentProfile(getActivity())
2109+
.wipeData(
2110+
/* flags= */ 0,
2111+
(v) -> onSuccessLog("wipeData"),
2112+
(e) -> onErrorLog("wipeData", e));
2113+
} else {
2114+
// Before U, factory reset by DO goes via the regular DPM instance
2115+
mDevicePolicyManagerGateway.wipeData(
2116+
flags, (v) -> onSuccessLog("wipeData"), (e) -> onErrorLog("wipeData", e));
2117+
}
2118+
} else {
2119+
// Wipe user
2120+
mDevicePolicyManagerGateway.wipeData(
2121+
flags, (v) -> onSuccessLog("wipeData"), (e) -> onErrorLog("wipeData", e));
2122+
}
21002123
}
21012124
})
21022125
.setNegativeButton(android.R.string.cancel, null)
@@ -4716,13 +4739,6 @@ private int validateAffiliatedUserAfterP() {
47164739
return NO_CUSTOM_CONSTRAINT;
47174740
}
47184741

4719-
@TargetApi(30)
4720-
private void factoryResetOrgOwnedDevice() {
4721-
DevicePolicyManagerGatewayImpl.forParentProfile(getActivity())
4722-
.wipeData(
4723-
/* flags= */ 0, (v) -> onSuccessLog("wipeData"), (e) -> onErrorLog("wipeData", e));
4724-
}
4725-
47264742
private boolean isOrganizationOwnedDevice() {
47274743
return mDevicePolicyManager.isDeviceOwnerApp(mPackageName)
47284744
|| (mDevicePolicyManager.isProfileOwnerApp(mPackageName)

src/main/java/com/afwsamples/testdpc/profilepolicy/ProfilePolicyManagementFragment.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@
5252
* DevicePolicyManager#clearCrossProfileIntentFilters(android.content.ComponentName)} 3) {@link
5353
* DevicePolicyManager#setCrossProfileCallerIdDisabled(android.content.ComponentName, boolean)} 4)
5454
* {@link DevicePolicyManager#getCrossProfileCallerIdDisabled(android.content.ComponentName)} 5)
55-
* {@link DevicePolicyManager#wipeData(int)} 6) {@link
56-
* DevicePolicyManager#addCrossProfileWidgetProvider(android.content.ComponentName, String)} 7)
57-
* {@link DevicePolicyManager#removeCrossProfileWidgetProvider(android.content.ComponentName,
58-
* String)} 8) {@link DevicePolicyManager#setBluetoothContactSharingDisabled(ComponentName,
55+
* {@link DevicePolicyManager#addCrossProfileWidgetProvider(android.content.ComponentName, String)}
56+
* 6) {@link DevicePolicyManager#removeCrossProfileWidgetProvider(android.content.ComponentName,
57+
* String)} 7) {@link DevicePolicyManager#setBluetoothContactSharingDisabled(ComponentName,
5958
* boolean)}
6059
*/
6160
public class ProfilePolicyManagementFragment extends BaseSearchablePolicyPreferenceFragment
@@ -78,7 +77,6 @@ public class ProfilePolicyManagementFragment extends BaseSearchablePolicyPrefere
7877
"disable_cross_profile_contacts_search";
7978
private static final String REMOVE_CROSS_PROFILE_APP_WIDGETS_KEY =
8079
"remove_cross_profile_app_widgets";
81-
private static final String REMOVE_PROFILE_KEY = "remove_profile";
8280
private static final String SET_ORGANIZATION_COLOR_KEY = "set_organization_color";
8381
private static final String SET_PROFILE_ORGANIZATION_NAME_KEY = "set_profile_organization_name";
8482

@@ -90,7 +88,6 @@ public class ProfilePolicyManagementFragment extends BaseSearchablePolicyPrefere
9088
private ComponentName mAdminComponentName;
9189
private Preference mAddCrossProfileIntentFilterPreference;
9290
private Preference mClearCrossProfileIntentFiltersPreference;
93-
private Preference mRemoveManagedProfilePreference;
9491
private Preference mAddCrossProfileAppWidgetsPreference;
9592
private Preference mRemoveCrossProfileAppWidgetsPreference;
9693
private SwitchPreference mDisableBluetoothContactSharingSwitchPreference;
@@ -117,8 +114,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
117114
mClearCrossProfileIntentFiltersPreference =
118115
findPreference(CLEAR_CROSS_PROFILE_INTENT_FILTERS_PREFERENCE_KEY);
119116
mClearCrossProfileIntentFiltersPreference.setOnPreferenceClickListener(this);
120-
mRemoveManagedProfilePreference = findPreference(REMOVE_PROFILE_KEY);
121-
mRemoveManagedProfilePreference.setOnPreferenceClickListener(this);
122117
mAddCrossProfileAppWidgetsPreference = findPreference(ADD_CROSS_PROFILE_APP_WIDGETS_KEY);
123118
mAddCrossProfileAppWidgetsPreference.setOnPreferenceClickListener(this);
124119
mRemoveCrossProfileAppWidgetsPreference = findPreference(REMOVE_CROSS_PROFILE_APP_WIDGETS_KEY);
@@ -157,14 +152,6 @@ public boolean onPreferenceClick(Preference preference) {
157152
mDevicePolicyManager.clearCrossProfileIntentFilters(mAdminComponentName);
158153
showToast(R.string.cross_profile_intent_filters_cleared);
159154
return true;
160-
case REMOVE_PROFILE_KEY:
161-
mRemoveManagedProfilePreference.setEnabled(false);
162-
mDevicePolicyManager.wipeData(0);
163-
showToast(R.string.removing_managed_profile);
164-
// Finish the activity because all other functions will not work after the managed
165-
// profile is removed.
166-
getActivity().finish();
167-
break;
168155
case ADD_CROSS_PROFILE_APP_WIDGETS_KEY:
169156
showDisabledAppWidgetList();
170157
return true;

src/main/res/values/strings.xml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,11 @@
135135
<string name="set_auto_time_required">Set auto (network) time required</string>
136136
<string name="set_auto_time">Set auto (network) time</string>
137137
<string name="set_auto_time_zone">Set auto (network) time zone</string>
138-
<string name="wipe_data">Wipe data</string>
139-
<string name="wipe_data_title">Wipe data?</string>
140-
<string name="wipe_data_confirmation">Are you sure you want to wipe the data?</string>
141-
<string name="wipe_data_include">Also wipe:</string>
142-
<string name="external_storage">External storage</string>
143-
<string name="reset_protection_data">Factory reset protection</string>
144138
<string name="remove_device_owner">Remove this device owner</string>
145139
<string name="remove_device_owner_title">Remove device owner?</string>
146140
<string name="remove_device_owner_confirmation">Policies and restrictions will continue to be
147141
active and may require a factory reset to clear.</string>
148142
<string name="device_owner_removed">This app is no longer a device owner.</string>
149-
<!-- Corporate-owned work profile falls under device owner management, for convenience. -->
150-
<string name="factory_reset_org_owned">Factory reset Org-Owned Work Profile device</string>
151143
<string name="set_factory_reset_protection_policy">Set factory reset protection policy</string>
152144
<string name="factory_reset_protection_policy">Factory reset protection policy</string>
153145
<string name="factory_reset_protection_policy_accounts">Accounts</string>
@@ -159,6 +151,18 @@
159151
<string name="cleared_factory_reset_protection_policy">Cleared factory reset protection policy</string>
160152
<string name="saved_factory_reset_protection_policy">Saved factory reset protection policy</string>
161153

154+
<!-- Strings for data wipe section-->
155+
<string name="data_wipe_section">Data wipe</string>
156+
<string name="remove_managed_profile">Remove work profile</string>
157+
<string name="remove_managed_profile_title">Remove work profile?</string>
158+
<string name="wipe_data_confirmation">Are you sure you want to wipe the data?</string>
159+
<string name="wipe_data_include">Also wipe:</string>
160+
<string name="external_storage">External storage</string>
161+
<string name="reset_protection_data">Factory reset protection</string>
162+
<string name="factory_reset_device">Factory reset device</string>
163+
<string name="factory_reset_device_title">Factory reset device?</string>
164+
165+
162166
<!-- Strings for device reboot -->
163167
<string name="reboot">Reboot device</string>
164168
<string name="reboot_error_msg">Cannot reboot device during an ongoing call.</string>
@@ -203,8 +207,6 @@
203207

204208
<!-- Stings for work profile management -->
205209
<string name="profile_management_title">Work profile management</string>
206-
<string name="remove_managed_profile">Remove work profile</string>
207-
<string name="removing_managed_profile">Removing work profile</string>
208210
<string name="lock_parent_key">Lock parent</string>
209211
<string name="evict_ce_key">Evict CE key</string>
210212

src/main/res/xml/device_policy_header.xml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -830,10 +830,6 @@
830830
android:title="@string/enable_usb_data_signaling"
831831
testdpc:admin="deviceOwner|orgOwnedProfileOwner"
832832
testdpc:minSdkVersion="S" />
833-
<com.afwsamples.testdpc.common.preference.DpcPreference
834-
android:key="wipe_data"
835-
android:title="@string/wipe_data"
836-
testdpc:minSdkVersion="L" />
837833
<com.afwsamples.testdpc.common.preference.DpcPreference
838834
android:key="remove_device_owner"
839835
android:title="@string/remove_device_owner"
@@ -849,11 +845,6 @@
849845
android:title="@string/set_factory_reset_protection_policy"
850846
testdpc:admin="deviceOwner|orgOwnedProfileOwner"
851847
testdpc:minSdkVersion="R" />
852-
<com.afwsamples.testdpc.common.preference.DpcPreference
853-
android:key="factory_reset_org_owned_device"
854-
android:title="@string/factory_reset_org_owned"
855-
testdpc:admin="orgOwnedProfileOwner"
856-
testdpc:minSdkVersion="R" />
857848
<com.afwsamples.testdpc.common.preference.DpcSwitchPreference
858849
android:key="suspend_personal_apps"
859850
android:title="@string/suspend_personal_apps"
@@ -869,6 +860,19 @@
869860
testdpc:minSdkVersion="R" />
870861
</PreferenceCategory>
871862

863+
<PreferenceCategory android:title="@string/data_wipe_section">
864+
<com.afwsamples.testdpc.common.preference.DpcPreference
865+
android:key="remove_managed_profile"
866+
android:title="@string/remove_managed_profile"
867+
testdpc:user="managedProfile"
868+
testdpc:minSdkVersion="L" />
869+
<com.afwsamples.testdpc.common.preference.DpcPreference
870+
android:key="factory_reset_device"
871+
android:title="@string/factory_reset_device"
872+
testdpc:admin="deviceOwner|orgOwnedProfileOwner"
873+
testdpc:minSdkVersion="L" />
874+
</PreferenceCategory>
875+
872876
<PreferenceCategory android:title="@string/transfer_ownership">
873877
<com.afwsamples.testdpc.common.preference.DpcPreference
874878
android:key="transfer_ownership_to_component"

src/main/res/xml/profile_policy_header.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,6 @@
6565
testdpc:minSdkVersion="N" />
6666
</PreferenceCategory>
6767

68-
<PreferenceCategory android:title="@string/profile_management_title">
69-
<com.afwsamples.testdpc.common.preference.DpcPreference
70-
android:key="remove_profile"
71-
android:title="@string/remove_managed_profile"
72-
testdpc:minSdkVersion="L" />
73-
</PreferenceCategory>
74-
7568
<PreferenceCategory android:title="@string/work_telephony_title">
7669
<com.afwsamples.testdpc.common.preference.DpcPreference
7770
android:key="managed_subscriptions"

0 commit comments

Comments
 (0)