Skip to content

Commit 2efad22

Browse files
Pavel GrafovAndroid (Google) Code Review
authored andcommitted
Merge "Support for maximum profile time off." into ub-testdpc-rvc
2 parents fe57c98 + b868844 commit 2efad22

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 |
@@ -480,6 +482,7 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
480482

481483
private DpcPreference mUserRestrictionsParentPreference;
482484
private DpcSwitchPreference mSuspendPersonalApps;
485+
private DpcEditTextPreference mProfileMaxTimeOff;
483486

484487
private DpcSwitchPreference mLockdownAdminConfiguredNetworksPreference;
485488

@@ -748,6 +751,12 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
748751
mSuspendPersonalApps.setCustomConstraint(
749752
this::validateProfileOwnerOfOrganizationOwnedDevice);
750753

754+
mProfileMaxTimeOff = (DpcEditTextPreference) findPreference(PROFILE_MAX_TIME_OFF_KEY);
755+
mProfileMaxTimeOff.setOnPreferenceChangeListener(this);
756+
mProfileMaxTimeOff.setCustomConstraint(
757+
this::validateProfileOwnerOfOrganizationOwnedDevice);
758+
maybeUpdateProfileMaxTimeOff();
759+
751760
onCreateSetNewPasswordWithComplexityPreference();
752761
constrainSpecialCasePreferences();
753762

@@ -770,6 +779,14 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
770779
reloadPersonalAppsSuspendedUi();
771780
}
772781

782+
private void maybeUpdateProfileMaxTimeOff() {
783+
if (mProfileMaxTimeOff.isEnabled()) {
784+
final String currentValueAsString = Long.toString(getManagedProfileMaximumTimeOff());
785+
mProfileMaxTimeOff.setText(currentValueAsString);
786+
mProfileMaxTimeOff.setSummary(currentValueAsString);
787+
}
788+
}
789+
773790
@TargetApi(Util.R_VERSION_CODE)
774791
private void reloadPersonalAppsSuspendedUi() {
775792
// TODO: nuke it when R sdk is available
@@ -787,7 +804,7 @@ int getPersonalAppsSuspensionReasons() {
787804
"getPersonalAppsSuspendedReasons", new Class<?>[]{ComponentName.class},
788805
mAdminComponentName);
789806
} catch (ReflectionIsTemporaryException e) {
790-
Log.e(TAG, "Error invoking getPersonalAppsSuspendedReasons", e);
807+
logAndShowToast("Error invoking getPersonalAppsSuspendedReasons", e);
791808
return 0;
792809
}
793810
}
@@ -799,10 +816,38 @@ void setPersonalAppsSuspended(boolean suspended) {
799816
new Class<?>[]{ComponentName.class, boolean.class},
800817
mAdminComponentName, suspended);
801818
} catch (ReflectionIsTemporaryException e) {
802-
Log.e(TAG, "Error invoking setPersonalAppsSuspended", e);
819+
logAndShowToast("Error invoking setPersonalAppsSuspended", e);
820+
}
821+
}
822+
823+
//TODO: nuke it when R sdk is available.
824+
public long getManagedProfileMaximumTimeOff() {
825+
try {
826+
return (Long) ReflectionUtil.invoke(mDevicePolicyManager,
827+
"getManagedProfileMaximumTimeOff", new Class<?>[]{ComponentName.class},
828+
mAdminComponentName);
829+
} catch (ReflectionIsTemporaryException e) {
830+
logAndShowToast("Error invoking getManagedProfileMaximumTimeOff", e);
831+
return 0;
832+
}
833+
}
834+
835+
//TODO: nuke it when R sdk is available.
836+
public void setManagedProfileMaximumTimeOff(long timeoutMs) {
837+
try {
838+
ReflectionUtil.invoke(mDevicePolicyManager, "setManagedProfileMaximumTimeOff",
839+
new Class<?>[]{ComponentName.class, long.class},
840+
mAdminComponentName, timeoutMs);
841+
} catch (ReflectionIsTemporaryException e) {
842+
logAndShowToast("Error invoking setManagedProfileMaximumTimeOff", e);
803843
}
804844
}
805845

846+
private void logAndShowToast(String message, Exception e) {
847+
Log.e(TAG, message, e);
848+
showToast(message + ": " + e.getMessage());
849+
}
850+
806851
private void onCreateSetNewPasswordWithComplexityPreference() {
807852
ListPreference complexityPref =
808853
(ListPreference) findPreference(SET_NEW_PASSWORD_WITH_COMPLEXITY);
@@ -1513,6 +1558,10 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
15131558
setPersonalAppsSuspended((Boolean) newValue);
15141559
reloadPersonalAppsSuspendedUi();
15151560
return true;
1561+
case PROFILE_MAX_TIME_OFF_KEY:
1562+
setManagedProfileMaximumTimeOff(Long.parseLong((String) newValue));
1563+
maybeUpdateProfileMaxTimeOff();
1564+
return true;
15161565
}
15171566
return false;
15181567
}

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

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

625+
<!-- Maximum time work profile is allowed to be off -->
626+
<string name="profile_max_time_off">Work profile max time off (seconds)</string>
627+
625628
<!-- Enterprise security logging -->
626629
<string name="enable_security_logging">Enable security logging</string>
627630
<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
@@ -749,6 +749,14 @@
749749
android:title="@string/suspend_personal_apps"
750750
testdpc:admin="profileOwner"
751751
testdpc:minSdkVersion="R" />
752+
<com.afwsamples.testdpc.common.preference.DpcEditTextPreference
753+
android:defaultValue="0"
754+
android:dialogTitle="@string/profile_max_time_off"
755+
android:inputType="number"
756+
android:key="profile_max_time_off"
757+
android:title="@string/profile_max_time_off"
758+
testdpc:admin="profileOwner"
759+
testdpc:minSdkVersion="R" />
752760
</PreferenceCategory>
753761

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

0 commit comments

Comments
 (0)