@@ -112,6 +112,8 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
112112 int mPasswordOwner = -1 ;
113113 Handler mHandler = new Handler ();
114114
115+ long mLastMaximumTimeToLock = -1 ;
116+
115117 final HashMap <ComponentName , ActiveAdmin > mAdminMap
116118 = new HashMap <ComponentName , ActiveAdmin >();
117119 final ArrayList <ActiveAdmin > mAdminList
@@ -595,17 +597,18 @@ void removeActiveAdminLocked(final ComponentName adminReceiver) {
595597 new BroadcastReceiver () {
596598 @ Override
597599 public void onReceive (Context context , Intent intent ) {
598- synchronized (this ) {
600+ synchronized (DevicePolicyManagerService . this ) {
599601 boolean doProxyCleanup = admin .info .usesPolicy (
600602 DeviceAdminInfo .USES_POLICY_SETS_GLOBAL_PROXY );
601603 mAdminList .remove (admin );
602604 mAdminMap .remove (adminReceiver );
603605 validatePasswordOwnerLocked ();
604606 syncDeviceCapabilitiesLocked ();
605607 if (doProxyCleanup ) {
606- resetGlobalProxy ();
608+ resetGlobalProxyLocked ();
607609 }
608610 saveSettingsLocked ();
611+ updateMaximumTimeToLockLocked ();
609612 }
610613 }
611614 });
@@ -826,16 +829,7 @@ private void loadSettingsLocked() {
826829
827830 validatePasswordOwnerLocked ();
828831 syncDeviceCapabilitiesLocked ();
829-
830- long timeMs = getMaximumTimeToLock (null );
831- if (timeMs <= 0 ) {
832- timeMs = Integer .MAX_VALUE ;
833- }
834- try {
835- getIPowerManager ().setMaximumScreenOffTimeount ((int )timeMs );
836- } catch (RemoteException e ) {
837- Slog .w (TAG , "Failure talking with power manager" , e );
838- }
832+ updateMaximumTimeToLockLocked ();
839833 }
840834
841835 static void validateQualityConstant (int quality ) {
@@ -1606,25 +1600,38 @@ public void setMaximumTimeToLock(ComponentName who, long timeMs) {
16061600 DeviceAdminInfo .USES_POLICY_FORCE_LOCK );
16071601 if (ap .maximumTimeToUnlock != timeMs ) {
16081602 ap .maximumTimeToUnlock = timeMs ;
1603+ saveSettingsLocked ();
1604+ updateMaximumTimeToLockLocked ();
1605+ }
1606+ }
1607+ }
16091608
1610- long ident = Binder .clearCallingIdentity ();
1611- try {
1612- saveSettingsLocked ();
1609+ void updateMaximumTimeToLockLocked () {
1610+ long timeMs = getMaximumTimeToLock (null );
1611+ if (mLastMaximumTimeToLock == timeMs ) {
1612+ return ;
1613+ }
16131614
1614- timeMs = getMaximumTimeToLock (null );
1615- if (timeMs <= 0 ) {
1616- timeMs = Integer .MAX_VALUE ;
1617- }
1615+ long ident = Binder .clearCallingIdentity ();
1616+ try {
1617+ if (timeMs <= 0 ) {
1618+ timeMs = Integer .MAX_VALUE ;
1619+ } else {
1620+ // Make sure KEEP_SCREEN_ON is disabled, since that
1621+ // would allow bypassing of the maximum time to lock.
1622+ Settings .System .putInt (mContext .getContentResolver (),
1623+ Settings .System .STAY_ON_WHILE_PLUGGED_IN , 0 );
1624+ }
16181625
1619- try {
1620- getIPowerManager ().setMaximumScreenOffTimeount ((int )timeMs );
1621- } catch (RemoteException e ) {
1622- Slog .w (TAG , "Failure talking with power manager" , e );
1623- }
1624- } finally {
1625- Binder .restoreCallingIdentity (ident );
1626- }
1626+ mLastMaximumTimeToLock = timeMs ;
1627+
1628+ try {
1629+ getIPowerManager ().setMaximumScreenOffTimeount ((int )timeMs );
1630+ } catch (RemoteException e ) {
1631+ Slog .w (TAG , "Failure talking with power manager" , e );
16271632 }
1633+ } finally {
1634+ Binder .restoreCallingIdentity (ident );
16281635 }
16291636 }
16301637
@@ -1868,7 +1875,7 @@ public ComponentName setGlobalProxy(ComponentName who, String proxySpec,
18681875 // Reset the global proxy accordingly
18691876 // Do this using system permissions, as apps cannot write to secure settings
18701877 long origId = Binder .clearCallingIdentity ();
1871- resetGlobalProxy ();
1878+ resetGlobalProxyLocked ();
18721879 Binder .restoreCallingIdentity (origId );
18731880 return null ;
18741881 }
@@ -1892,20 +1899,20 @@ public ComponentName getGlobalProxyAdmin() {
18921899 return null ;
18931900 }
18941901
1895- private void resetGlobalProxy () {
1902+ private void resetGlobalProxyLocked () {
18961903 final int N = mAdminList .size ();
18971904 for (int i = 0 ; i < N ; i ++) {
18981905 ActiveAdmin ap = mAdminList .get (i );
18991906 if (ap .specifiesGlobalProxy ) {
1900- saveGlobalProxy (ap .globalProxySpec , ap .globalProxyExclusionList );
1907+ saveGlobalProxyLocked (ap .globalProxySpec , ap .globalProxyExclusionList );
19011908 return ;
19021909 }
19031910 }
19041911 // No device admins defining global proxies - reset global proxy settings to none
1905- saveGlobalProxy (null , null );
1912+ saveGlobalProxyLocked (null , null );
19061913 }
19071914
1908- private void saveGlobalProxy (String proxySpec , String exclusionList ) {
1915+ private void saveGlobalProxyLocked (String proxySpec , String exclusionList ) {
19091916 if (exclusionList == null ) {
19101917 exclusionList = "" ;
19111918 }
0 commit comments