Skip to content

Commit 845a66f

Browse files
author
Rubin Xu
committed
Set auto time and timezone using new R APIs
COPE PO can only use the new APIs to set auto time and timezone. Fall back to the legacy global settings (if running on Q and before) Bug: 150121247 Test: manual Change-Id: I288a61da0b8f39565f2871b547dacff0bda236d5
1 parent c6ccd00 commit 845a66f

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,13 +1265,11 @@ public void onPositiveButtonClicked(String[] lockTaskArray) {
12651265
return true;
12661266
case SET_TIME_KEY:
12671267
// Disable auto time before we could set time manually.
1268-
mDevicePolicyManager.setGlobalSetting(mAdminComponentName,
1269-
Settings.Global.AUTO_TIME, "0");
1268+
setAutoTimeEnabled(false);
12701269
showSetTimeDialog();
12711270
return true;
12721271
case SET_TIME_ZONE_KEY:
1273-
mDevicePolicyManager.setGlobalSetting(mAdminComponentName,
1274-
Settings.Global.AUTO_TIME_ZONE, "0");
1272+
setAutoTimeZoneEnabled(false);
12751273
showSetTimeZoneDialog();
12761274
return true;
12771275
case MANAGE_OVERRIDE_APN_KEY:
@@ -4037,4 +4035,31 @@ private boolean isOrganizationOwnedDeviceWithManagedProfile() {
40374035
abstract static class ManageLockTaskListCallback {
40384036
public abstract void onPositiveButtonClicked(String[] lockTaskArray);
40394037
}
4038+
4039+
private void setAutoTimeEnabled(boolean enabled) {
4040+
try {
4041+
ReflectionUtil.invoke(mDevicePolicyManager, "setAutoTime",
4042+
new Class<?>[]{ComponentName.class, boolean.class},
4043+
mAdminComponentName, enabled);
4044+
} catch (ReflectionIsTemporaryException e) {
4045+
Log.e(TAG, "Error invoking setAutoTime", e);
4046+
// Prior to R, auto time is controlled by a global setting
4047+
mDevicePolicyManager.setGlobalSetting(mAdminComponentName,
4048+
Settings.Global.AUTO_TIME, enabled ? "1" : "0");
4049+
}
4050+
}
4051+
4052+
private void setAutoTimeZoneEnabled(boolean enabled) {
4053+
try {
4054+
ReflectionUtil.invoke(mDevicePolicyManager, "setAutoTimeZone",
4055+
new Class<?>[]{ComponentName.class, boolean.class},
4056+
mAdminComponentName, enabled);
4057+
} catch (ReflectionIsTemporaryException e) {
4058+
Log.e(TAG, "Error invoking setAutoTimeZone", e);
4059+
// Prior to R, auto timezone is controlled by a global setting
4060+
mDevicePolicyManager.setGlobalSetting(mAdminComponentName,
4061+
Settings.Global.AUTO_TIME_ZONE, enabled ? "1" : "0");
4062+
}
4063+
4064+
}
40404065
}

0 commit comments

Comments
 (0)