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 |
@@ -478,6 +480,7 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
478
480
479
481
private DpcPreference mUserRestrictionsParentPreference ;
480
482
private DpcSwitchPreference mSuspendPersonalApps ;
483
+ private DpcEditTextPreference mProfileMaxTimeOff ;
481
484
482
485
private GetAccessibilityServicesTask mGetAccessibilityServicesTask = null ;
483
486
private GetInputMethodsTask mGetInputMethodsTask = null ;
@@ -734,6 +737,12 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
734
737
mSuspendPersonalApps .setCustomConstraint (
735
738
this ::validateProfileOwnerOfOrganizationOwnedDevice );
736
739
740
+ mProfileMaxTimeOff = (DpcEditTextPreference ) findPreference (PROFILE_MAX_TIME_OFF_KEY );
741
+ mProfileMaxTimeOff .setOnPreferenceChangeListener (this );
742
+ mProfileMaxTimeOff .setCustomConstraint (
743
+ this ::validateProfileOwnerOfOrganizationOwnedDevice );
744
+ maybeUpdateProfileMaxTimeOff ();
745
+
737
746
onCreateSetNewPasswordWithComplexityPreference ();
738
747
constrainSpecialCasePreferences ();
739
748
@@ -756,6 +765,14 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
756
765
reloadPersonalAppsSuspendedUi ();
757
766
}
758
767
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
+
759
776
@ TargetApi (Util .R_VERSION_CODE )
760
777
private void reloadPersonalAppsSuspendedUi () {
761
778
// TODO: nuke it when R sdk is available
@@ -773,7 +790,7 @@ int getPersonalAppsSuspensionReasons() {
773
790
"getPersonalAppsSuspendedReasons" , new Class <?>[]{ComponentName .class },
774
791
mAdminComponentName );
775
792
} catch (ReflectionIsTemporaryException e ) {
776
- Log . e ( TAG , "Error invoking getPersonalAppsSuspendedReasons" , e );
793
+ logAndShowToast ( "Error invoking getPersonalAppsSuspendedReasons" , e );
777
794
return 0 ;
778
795
}
779
796
}
@@ -785,10 +802,38 @@ void setPersonalAppsSuspended(boolean suspended) {
785
802
new Class <?>[]{ComponentName .class , boolean .class },
786
803
mAdminComponentName , suspended );
787
804
} 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 );
789
829
}
790
830
}
791
831
832
+ private void logAndShowToast (String message , Exception e ) {
833
+ Log .e (TAG , message , e );
834
+ showToast (message + ": " + e .getMessage ());
835
+ }
836
+
792
837
private void onCreateSetNewPasswordWithComplexityPreference () {
793
838
ListPreference complexityPref =
794
839
(ListPreference ) findPreference (SET_NEW_PASSWORD_WITH_COMPLEXITY );
@@ -1488,6 +1533,10 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
1488
1533
setPersonalAppsSuspended ((Boolean ) newValue );
1489
1534
reloadPersonalAppsSuspendedUi ();
1490
1535
return true ;
1536
+ case PROFILE_MAX_TIME_OFF_KEY :
1537
+ setManagedProfileMaximumTimeOff (Long .parseLong ((String ) newValue ));
1538
+ maybeUpdateProfileMaxTimeOff ();
1539
+ return true ;
1491
1540
}
1492
1541
return false ;
1493
1542
}
0 commit comments