Skip to content

Commit b868844

Browse files
author
Pavel Grafov
committed
Support for maximum profile time off.
Bug: 143517719 Test: manual Change-Id: I492b9713325aa19f6f67300ba60412c0d0691e6a
1 parent a3c14c7 commit b868844

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

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

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
import com.afwsamples.testdpc.common.UserArrayAdapter;
108108
import com.afwsamples.testdpc.common.Util;
109109
import com.afwsamples.testdpc.common.preference.CustomConstraint;
110+
import com.afwsamples.testdpc.common.preference.DpcEditTextPreference;
110111
import com.afwsamples.testdpc.common.preference.DpcPreference;
111112
import com.afwsamples.testdpc.common.preference.DpcPreferenceBase;
112113
import com.afwsamples.testdpc.common.preference.DpcPreferenceHelper;
@@ -395,6 +396,7 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
395396
private static final String SET_LOCATION_ENABLED_KEY = "set_location_enabled";
396397
private static final String SET_LOCATION_MODE_KEY = "set_location_mode";
397398
private static final String SUSPEND_PERSONAL_APPS_KEY = "suspend_personal_apps";
399+
private static final String PROFILE_MAX_TIME_OFF_KEY = "profile_max_time_off";
398400

399401
private static final String BATTERY_PLUGGED_ANY = Integer.toString(
400402
BatteryManager.BATTERY_PLUGGED_AC |
@@ -478,6 +480,7 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
478480

479481
private DpcPreference mUserRestrictionsParentPreference;
480482
private DpcSwitchPreference mSuspendPersonalApps;
483+
private DpcEditTextPreference mProfileMaxTimeOff;
481484

482485
private GetAccessibilityServicesTask mGetAccessibilityServicesTask = null;
483486
private GetInputMethodsTask mGetInputMethodsTask = null;
@@ -734,6 +737,12 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
734737
mSuspendPersonalApps.setCustomConstraint(
735738
this::validateProfileOwnerOfOrganizationOwnedDevice);
736739

740+
mProfileMaxTimeOff = (DpcEditTextPreference) findPreference(PROFILE_MAX_TIME_OFF_KEY);
741+
mProfileMaxTimeOff.setOnPreferenceChangeListener(this);
742+
mProfileMaxTimeOff.setCustomConstraint(
743+
this::validateProfileOwnerOfOrganizationOwnedDevice);
744+
maybeUpdateProfileMaxTimeOff();
745+
737746
onCreateSetNewPasswordWithComplexityPreference();
738747
constrainSpecialCasePreferences();
739748

@@ -756,6 +765,14 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
756765
reloadPersonalAppsSuspendedUi();
757766
}
758767

768+
private void maybeUpdateProfileMaxTimeOff() {
769+
if (mProfileMaxTimeOff.isEnabled()) {
770+
final String currentValueAsString = Long.toString(getManagedProfileMaximumTimeOff());
771+
mProfileMaxTimeOff.setText(currentValueAsString);
772+
mProfileMaxTimeOff.setSummary(currentValueAsString);
773+
}
774+
}
775+
759776
@TargetApi(Util.R_VERSION_CODE)
760777
private void reloadPersonalAppsSuspendedUi() {
761778
// TODO: nuke it when R sdk is available
@@ -773,7 +790,7 @@ int getPersonalAppsSuspensionReasons() {
773790
"getPersonalAppsSuspendedReasons", new Class<?>[]{ComponentName.class},
774791
mAdminComponentName);
775792
} catch (ReflectionIsTemporaryException e) {
776-
Log.e(TAG, "Error invoking getPersonalAppsSuspendedReasons", e);
793+
logAndShowToast("Error invoking getPersonalAppsSuspendedReasons", e);
777794
return 0;
778795
}
779796
}
@@ -785,10 +802,38 @@ void setPersonalAppsSuspended(boolean suspended) {
785802
new Class<?>[]{ComponentName.class, boolean.class},
786803
mAdminComponentName, suspended);
787804
} catch (ReflectionIsTemporaryException e) {
788-
Log.e(TAG, "Error invoking setPersonalAppsSuspended", e);
805+
logAndShowToast("Error invoking setPersonalAppsSuspended", e);
806+
}
807+
}
808+
809+
//TODO: nuke it when R sdk is available.
810+
public long getManagedProfileMaximumTimeOff() {
811+
try {
812+
return (Long) ReflectionUtil.invoke(mDevicePolicyManager,
813+
"getManagedProfileMaximumTimeOff", new Class<?>[]{ComponentName.class},
814+
mAdminComponentName);
815+
} catch (ReflectionIsTemporaryException e) {
816+
logAndShowToast("Error invoking getManagedProfileMaximumTimeOff", e);
817+
return 0;
818+
}
819+
}
820+
821+
//TODO: nuke it when R sdk is available.
822+
public void setManagedProfileMaximumTimeOff(long timeoutMs) {
823+
try {
824+
ReflectionUtil.invoke(mDevicePolicyManager, "setManagedProfileMaximumTimeOff",
825+
new Class<?>[]{ComponentName.class, long.class},
826+
mAdminComponentName, timeoutMs);
827+
} catch (ReflectionIsTemporaryException e) {
828+
logAndShowToast("Error invoking setManagedProfileMaximumTimeOff", e);
789829
}
790830
}
791831

832+
private void logAndShowToast(String message, Exception e) {
833+
Log.e(TAG, message, e);
834+
showToast(message + ": " + e.getMessage());
835+
}
836+
792837
private void onCreateSetNewPasswordWithComplexityPreference() {
793838
ListPreference complexityPref =
794839
(ListPreference) findPreference(SET_NEW_PASSWORD_WITH_COMPLEXITY);
@@ -1488,6 +1533,10 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
14881533
setPersonalAppsSuspended((Boolean) newValue);
14891534
reloadPersonalAppsSuspendedUi();
14901535
return true;
1536+
case PROFILE_MAX_TIME_OFF_KEY:
1537+
setManagedProfileMaximumTimeOff(Long.parseLong((String) newValue));
1538+
maybeUpdateProfileMaxTimeOff();
1539+
return true;
14911540
}
14921541
return false;
14931542
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,9 @@
616616
<!-- Suspension of personal apps -->
617617
<string name="suspend_personal_apps">Suspend personal apps</string>
618618

619+
<!-- Maximum time work profile is allowed to be off -->
620+
<string name="profile_max_time_off">Work profile max time off (seconds)</string>
621+
619622
<!-- Enterprise security logging -->
620623
<string name="enable_security_logging">Enable security logging</string>
621624
<string name="request_security_logs">Request security logs</string>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,14 @@
739739
android:title="@string/suspend_personal_apps"
740740
testdpc:admin="profileOwner"
741741
testdpc:minSdkVersion="R" />
742+
<com.afwsamples.testdpc.common.preference.DpcEditTextPreference
743+
android:defaultValue="0"
744+
android:dialogTitle="@string/profile_max_time_off"
745+
android:inputType="number"
746+
android:key="profile_max_time_off"
747+
android:title="@string/profile_max_time_off"
748+
testdpc:admin="profileOwner"
749+
testdpc:minSdkVersion="R" />
742750
</PreferenceCategory>
743751

744752
<PreferenceCategory android:title="@string/transfer_ownership">

0 commit comments

Comments
 (0)