Skip to content

Commit d25ecd6

Browse files
authored
Add "Remove profile owner"
1 parent e2ede3c commit d25ecd6

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
338338
private static final String RELAUNCH_IN_LOCK_TASK = "relaunch_in_lock_task";
339339
private static final String REMOVE_ALL_CERTIFICATES_KEY = "remove_all_ca_certificates";
340340
private static final String REMOVE_DEVICE_OWNER_KEY = "remove_device_owner";
341+
private static final String REMOVE_PROFILE_OWNER_KEY = "remove_profile_owner";
341342
private static final String REMOVE_KEY_CERTIFICATE_KEY = "remove_key_certificate";
342343
private static final String REMOVE_USER_KEY = "remove_user";
343344
private static final String SWITCH_USER_KEY = "switch_user";
@@ -685,6 +686,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
685686
findPreference(REMOVE_MANAGED_PROFILE_KEY).setOnPreferenceClickListener(this);
686687
findPreference(FACTORY_RESET_DEVICE_KEY).setOnPreferenceClickListener(this);
687688
findPreference(REMOVE_DEVICE_OWNER_KEY).setOnPreferenceClickListener(this);
689+
findPreference(REMOVE_PROFILE_OWNER_KEY).setOnPreferenceClickListener(this);
688690
mEnableBackupServicePreference = (DpcSwitchPreference) findPreference(ENABLE_BACKUP_SERVICE);
689691
mEnableBackupServicePreference.setOnPreferenceChangeListener(this);
690692
mEnableBackupServicePreference.setCustomConstraint(this::validateDeviceOwnerBeforeQ);
@@ -1069,6 +1071,9 @@ public boolean onPreferenceClick(Preference preference) {
10691071
} else if (REMOVE_DEVICE_OWNER_KEY.equals(key)) {
10701072
showRemoveDeviceOwnerPrompt();
10711073
return true;
1074+
} else if (REMOVE_PROFILE_OWNER_KEY.equals(key)) {
1075+
showRemoveProfileOwnerPrompt();
1076+
return true;
10721077
} else if (REQUEST_BUGREPORT_KEY.equals(key)) {
10731078
requestBugReport();
10741079
return true;
@@ -2149,6 +2154,26 @@ private void showRemoveDeviceOwnerPrompt() {
21492154
.show();
21502155
}
21512156

2157+
/** Shows a prompt to ask for confirmation on removing profile owner. */
2158+
private void showRemoveProfileOwnerPrompt() {
2159+
new AlertDialog.Builder(getActivity())
2160+
.setTitle(R.string.remove_profile_owner_title)
2161+
.setMessage(R.string.remove_device_owner_confirmation)
2162+
.setPositiveButton(
2163+
android.R.string.ok,
2164+
(d, i) ->
2165+
mDevicePolicyManagerGateway.clearProfileOwner(
2166+
(v) -> {
2167+
if (getActivity() != null && !getActivity().isFinishing()) {
2168+
showToast(R.string.profile_owner_removed);
2169+
getActivity().finish();
2170+
}
2171+
},
2172+
(e) -> onErrorLog("clearProfileOwner", e)))
2173+
.setNegativeButton(android.R.string.cancel, null)
2174+
.show();
2175+
}
2176+
21522177
/** Shows a message box with the device wifi mac address. */
21532178
@TargetApi(VERSION_CODES.N)
21542179
private void showWifiMacAddress() {

src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@
140140
<string name="remove_device_owner_confirmation">Policies and restrictions will continue to be
141141
active and may require a factory reset to clear.</string>
142142
<string name="device_owner_removed">This app is no longer a device owner.</string>
143+
<string name="remove_profile_owner">Remove this profile owner</string>
144+
<string name="remove_profile_owner_title">Remove profile owner?</string>
145+
<string name="profile_owner_removed">This app is no longer a profile owner.</string>
143146
<string name="set_factory_reset_protection_policy">Set factory reset protection policy</string>
144147
<string name="factory_reset_protection_policy">Factory reset protection policy</string>
145148
<string name="factory_reset_protection_policy_accounts">Account IDs</string>

src/main/res/xml/device_policy_header.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,11 @@
836836
android:title="@string/remove_device_owner"
837837
testdpc:admin="deviceOwner"
838838
testdpc:minSdkVersion="L" />
839+
<com.afwsamples.testdpc.common.preference.DpcPreference
840+
android:key="remove_profile_owner"
841+
android:title="@string/remove_profile_owner"
842+
testdpc:admin="profileOwner|orgOwnedProfileOwner"
843+
testdpc:minSdkVersion="L" />
839844
<com.afwsamples.testdpc.common.preference.DpcPreference
840845
android:key="reboot"
841846
android:title="@string/reboot"

0 commit comments

Comments
 (0)