107
107
import com .afwsamples .testdpc .common .UserArrayAdapter ;
108
108
import com .afwsamples .testdpc .common .Util ;
109
109
import com .afwsamples .testdpc .common .preference .CustomConstraint ;
110
+ import com .afwsamples .testdpc .common .preference .DpcEditTextPreference ;
110
111
import com .afwsamples .testdpc .common .preference .DpcPreference ;
111
112
import com .afwsamples .testdpc .common .preference .DpcPreferenceBase ;
112
113
import com .afwsamples .testdpc .common .preference .DpcPreferenceHelper ;
@@ -395,6 +396,7 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
395
396
private static final String SET_LOCATION_ENABLED_KEY = "set_location_enabled" ;
396
397
private static final String SET_LOCATION_MODE_KEY = "set_location_mode" ;
397
398
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" ;
398
400
399
401
private static final String BATTERY_PLUGGED_ANY = Integer .toString (
400
402
BatteryManager .BATTERY_PLUGGED_AC |
@@ -480,6 +482,7 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
480
482
481
483
private DpcPreference mUserRestrictionsParentPreference ;
482
484
private DpcSwitchPreference mSuspendPersonalApps ;
485
+ private DpcEditTextPreference mProfileMaxTimeOff ;
483
486
484
487
private DpcSwitchPreference mLockdownAdminConfiguredNetworksPreference ;
485
488
@@ -748,6 +751,12 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
748
751
mSuspendPersonalApps .setCustomConstraint (
749
752
this ::validateProfileOwnerOfOrganizationOwnedDevice );
750
753
754
+ mProfileMaxTimeOff = (DpcEditTextPreference ) findPreference (PROFILE_MAX_TIME_OFF_KEY );
755
+ mProfileMaxTimeOff .setOnPreferenceChangeListener (this );
756
+ mProfileMaxTimeOff .setCustomConstraint (
757
+ this ::validateProfileOwnerOfOrganizationOwnedDevice );
758
+ maybeUpdateProfileMaxTimeOff ();
759
+
751
760
onCreateSetNewPasswordWithComplexityPreference ();
752
761
constrainSpecialCasePreferences ();
753
762
@@ -770,6 +779,14 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
770
779
reloadPersonalAppsSuspendedUi ();
771
780
}
772
781
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
+
773
790
@ TargetApi (Util .R_VERSION_CODE )
774
791
private void reloadPersonalAppsSuspendedUi () {
775
792
// TODO: nuke it when R sdk is available
@@ -787,7 +804,7 @@ int getPersonalAppsSuspensionReasons() {
787
804
"getPersonalAppsSuspendedReasons" , new Class <?>[]{ComponentName .class },
788
805
mAdminComponentName );
789
806
} catch (ReflectionIsTemporaryException e ) {
790
- Log . e ( TAG , "Error invoking getPersonalAppsSuspendedReasons" , e );
807
+ logAndShowToast ( "Error invoking getPersonalAppsSuspendedReasons" , e );
791
808
return 0 ;
792
809
}
793
810
}
@@ -799,10 +816,38 @@ void setPersonalAppsSuspended(boolean suspended) {
799
816
new Class <?>[]{ComponentName .class , boolean .class },
800
817
mAdminComponentName , suspended );
801
818
} 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 );
803
843
}
804
844
}
805
845
846
+ private void logAndShowToast (String message , Exception e ) {
847
+ Log .e (TAG , message , e );
848
+ showToast (message + ": " + e .getMessage ());
849
+ }
850
+
806
851
private void onCreateSetNewPasswordWithComplexityPreference () {
807
852
ListPreference complexityPref =
808
853
(ListPreference ) findPreference (SET_NEW_PASSWORD_WITH_COMPLEXITY );
@@ -1513,6 +1558,10 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
1513
1558
setPersonalAppsSuspended ((Boolean ) newValue );
1514
1559
reloadPersonalAppsSuspendedUi ();
1515
1560
return true ;
1561
+ case PROFILE_MAX_TIME_OFF_KEY :
1562
+ setManagedProfileMaximumTimeOff (Long .parseLong ((String ) newValue ));
1563
+ maybeUpdateProfileMaxTimeOff ();
1564
+ return true ;
1516
1565
}
1517
1566
return false ;
1518
1567
}
0 commit comments