Skip to content

Commit 00b3784

Browse files
committed
TestDPC: Support changing profile name
Add UI for setting the profile name by calling DevicePolicyManager.setProfileName. Bug: 78929154 Test: Manual. Change-Id: Ic6f37a170ce08d800526d6225293017bd3721365
1 parent ec2b1e7 commit 00b3784

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
381381
private static final String SET_TIME_KEY = "set_time";
382382
private static final String SET_TIME_ZONE_KEY = "set_time_zone";
383383

384+
private static final String SET_PROFILE_NAME_KEY = "set_profile_name";
385+
384386
private static final String MANAGE_OVERRIDE_APN_KEY = "manage_override_apn";
385387

386388
private static final String MANAGED_SYSTEM_UPDATES_KEY = "managed_system_updates";
@@ -661,6 +663,8 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
661663
findPreference(SET_TIME_KEY).setOnPreferenceClickListener(this);
662664
findPreference(SET_TIME_ZONE_KEY).setOnPreferenceClickListener(this);
663665

666+
findPreference(SET_PROFILE_NAME_KEY).setOnPreferenceClickListener(this);
667+
664668
findPreference(MANAGE_OVERRIDE_APN_KEY).setOnPreferenceClickListener(this);
665669
findPreference(MANAGED_SYSTEM_UPDATES_KEY).setOnPreferenceClickListener(this);
666670

@@ -1126,6 +1130,9 @@ public void onPositiveButtonClicked(String[] lockTaskArray) {
11261130
case CROSS_PROFILE_CALENDAR_KEY:
11271131
showFragment(new CrossProfileCalendarFragment());
11281132
return true;
1133+
case SET_PROFILE_NAME_KEY:
1134+
showSetProfileNameDialog();
1135+
return true;
11291136
}
11301137
return false;
11311138
}
@@ -3399,7 +3406,7 @@ private void showSetScreenBrightnessDialog() {
33993406
* Shows a dialog that asks the user for a screen off timeout value, then sets this value as
34003407
* screen off timeout.
34013408
*/
3402-
@TargetApi(28)
3409+
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
34033410
private void showSetScreenOffTimeoutDialog() {
34043411
if (getActivity() == null || getActivity().isFinishing()) {
34053412
return;
@@ -3514,6 +3521,36 @@ private void showSetTimeZoneDialog() {
35143521
.show();
35153522
}
35163523

3524+
/**
3525+
* Shows a dialog that asks the user to set a profile name.
3526+
*/
3527+
@TargetApi(28)
3528+
private void showSetProfileNameDialog() {
3529+
if (getActivity() == null || getActivity().isFinishing()) {
3530+
return;
3531+
}
3532+
3533+
final View dialogView = getActivity().getLayoutInflater().inflate(
3534+
R.layout.simple_edittext, null);
3535+
final EditText profileNameEditText = (EditText) dialogView.findViewById(
3536+
R.id.input);
3537+
profileNameEditText.setText("");
3538+
3539+
new AlertDialog.Builder(getActivity())
3540+
.setTitle(R.string.set_profile_name)
3541+
.setView(dialogView)
3542+
.setPositiveButton(android.R.string.ok, (dialogInterface, i) -> {
3543+
final String newProfileName = profileNameEditText.getText().toString();
3544+
if (newProfileName.isEmpty()) {
3545+
showToast(R.string.no_profile_name);
3546+
return;
3547+
}
3548+
mDevicePolicyManager.setProfileName(mAdminComponentName, newProfileName);
3549+
})
3550+
.setNegativeButton(android.R.string.cancel, null)
3551+
.show();
3552+
}
3553+
35173554
private void chooseAccount() {
35183555
if (getActivity() == null || getActivity().isFinishing()) {
35193556
return;

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,10 @@
852852
<string name="no_timezone">Timezone not specified</string>
853853
<string name="invalid_timezone">Timezone id not valid</string>
854854

855+
<!-- Strings for setting profile name -->
856+
<string name="set_profile_name">Set profile name</string>
857+
<string name="no_profile_name">Missing profile name</string>
858+
855859
<!-- Strings for set system settings -->
856860
<string name="set_system_setting">Set system settings</string>
857861
<string name="auto_brightness">Auto brightness</string>

app/src/main/res/xml/device_policy_header.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
android:key="set_screen_off_timeout"
6363
android:title="@string/set_screen_off_timeout"
6464
testdpc:minSdkVersion="P" />
65+
<com.afwsamples.testdpc.common.preference.DpcPreference
66+
android:key="set_profile_name"
67+
android:title="@string/set_profile_name"
68+
testdpc:admin="deviceOwner|profileOwner"
69+
testdpc:minSdkVersion="L" />
6570
</PreferenceCategory>
6671

6772
<PreferenceCategory android:title="@string/override_apn_title">

0 commit comments

Comments
 (0)