5555import android .app .ActivityManagerNative ;
5656import android .app .IActivityManager ;
5757import android .app .admin .DevicePolicyManager ;
58+ import android .content .BroadcastReceiver ;
5859import android .content .Context ;
60+ import android .content .Intent ;
61+ import android .content .IntentFilter ;
5962import android .content .pm .ActivityInfo ;
6063import android .content .pm .PackageManager ;
6164import android .content .res .CompatibilityInfo ;
@@ -235,11 +238,20 @@ public class WindowManagerService extends IWindowManager.Stub
235238 */
236239 private boolean mKeyguardDisabled = false ;
237240
241+ private static final int ALLOW_DISABLE_YES = 1 ;
242+ private static final int ALLOW_DISABLE_NO = 0 ;
243+ private static final int ALLOW_DISABLE_UNKNOWN = -1 ; // check with DevicePolicyManager
244+ private int mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN ; // sync'd by mKeyguardTokenWatcher
245+
238246 final TokenWatcher mKeyguardTokenWatcher = new TokenWatcher (
239247 new Handler (), "WindowManagerService.mKeyguardTokenWatcher" ) {
240248 public void acquired () {
241- mPolicy .enableKeyguard (false );
242- mKeyguardDisabled = true ;
249+ if (shouldAllowDisableKeyguard ()) {
250+ mPolicy .enableKeyguard (false );
251+ mKeyguardDisabled = true ;
252+ } else {
253+ Log .v (TAG , "Not disabling keyguard since device policy is enforced" );
254+ }
243255 }
244256 public void released () {
245257 mPolicy .enableKeyguard (true );
@@ -250,6 +262,18 @@ public void released() {
250262 }
251263 };
252264
265+ final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver () {
266+ @ Override
267+ public void onReceive (Context context , Intent intent ) {
268+ mPolicy .enableKeyguard (true );
269+ synchronized (mKeyguardTokenWatcher ) {
270+ // lazily evaluate this next time we're asked to disable keyguard
271+ mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN ;
272+ mKeyguardDisabled = false ;
273+ }
274+ }
275+ };
276+
253277 final Context mContext ;
254278
255279 final boolean mHaveInputMethods ;
@@ -610,6 +634,11 @@ private WindowManagerService(Context context, PowerManagerService pm,
610634 mTransitionAnimationScale = Settings .System .getFloat (context .getContentResolver (),
611635 Settings .System .TRANSITION_ANIMATION_SCALE , mTransitionAnimationScale );
612636
637+ // Track changes to DevicePolicyManager state so we can enable/disable keyguard.
638+ IntentFilter filter = new IntentFilter ();
639+ filter .addAction (DevicePolicyManager .ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED );
640+ mContext .registerReceiver (mBroadcastReceiver , filter );
641+
613642 int max_events_per_sec = 35 ;
614643 try {
615644 max_events_per_sec = Integer .parseInt (SystemProperties
@@ -4173,17 +4202,20 @@ public void moveAppTokensToBottom(List<IBinder> tokens) {
41734202 // Misc IWindowSession methods
41744203 // -------------------------------------------------------------
41754204
4176- private boolean allowDisableKeyguard ()
4205+ private boolean shouldAllowDisableKeyguard ()
41774206 {
4178- // We fail safe if this gets called before the service has started.
4179- boolean allow = false ;
4180- DevicePolicyManager dpm = (DevicePolicyManager ) mContext .getSystemService (
4181- Context .DEVICE_POLICY_SERVICE );
4182- if (dpm != null ) {
4183- allow = dpm .getPasswordQuality (null )
4184- == DevicePolicyManager .PASSWORD_QUALITY_UNSPECIFIED ;
4207+ // We fail safe and prevent disabling keyguard in the unlikely event this gets
4208+ // called before DevicePolicyManagerService has started.
4209+ if (mAllowDisableKeyguard == ALLOW_DISABLE_UNKNOWN ) {
4210+ DevicePolicyManager dpm = (DevicePolicyManager ) mContext .getSystemService (
4211+ Context .DEVICE_POLICY_SERVICE );
4212+ if (dpm != null ) {
4213+ mAllowDisableKeyguard = dpm .getPasswordQuality (null )
4214+ == DevicePolicyManager .PASSWORD_QUALITY_UNSPECIFIED ?
4215+ ALLOW_DISABLE_YES : ALLOW_DISABLE_NO ;
4216+ }
41854217 }
4186- return allow ;
4218+ return mAllowDisableKeyguard == ALLOW_DISABLE_YES ;
41874219 }
41884220
41894221 public void disableKeyguard (IBinder token , String tag ) {
@@ -4192,12 +4224,8 @@ public void disableKeyguard(IBinder token, String tag) {
41924224 throw new SecurityException ("Requires DISABLE_KEYGUARD permission" );
41934225 }
41944226
4195- if (allowDisableKeyguard ()) {
4196- synchronized (mKeyguardTokenWatcher ) {
4197- mKeyguardTokenWatcher .acquire (token , tag );
4198- }
4199- } else {
4200- Log .w (TAG , tag + ": disableKeyguard() ignored while DevicePolicyAmin is enabled." );
4227+ synchronized (mKeyguardTokenWatcher ) {
4228+ mKeyguardTokenWatcher .acquire (token , tag );
42014229 }
42024230 }
42034231
@@ -4207,29 +4235,25 @@ public void reenableKeyguard(IBinder token) {
42074235 throw new SecurityException ("Requires DISABLE_KEYGUARD permission" );
42084236 }
42094237
4210- if (allowDisableKeyguard ()) {
4211- synchronized (mKeyguardTokenWatcher ) {
4212- mKeyguardTokenWatcher .release (token );
4213-
4214- if (!mKeyguardTokenWatcher .isAcquired ()) {
4215- // If we are the last one to reenable the keyguard wait until
4216- // we have actaully finished reenabling until returning.
4217- // It is possible that reenableKeyguard() can be called before
4218- // the previous disableKeyguard() is handled, in which case
4219- // neither mKeyguardTokenWatcher.acquired() or released() would
4220- // be called. In that case mKeyguardDisabled will be false here
4221- // and we have nothing to wait for.
4222- while (mKeyguardDisabled ) {
4223- try {
4224- mKeyguardTokenWatcher .wait ();
4225- } catch (InterruptedException e ) {
4226- Thread .currentThread ().interrupt ();
4227- }
4238+ synchronized (mKeyguardTokenWatcher ) {
4239+ mKeyguardTokenWatcher .release (token );
4240+
4241+ if (!mKeyguardTokenWatcher .isAcquired ()) {
4242+ // If we are the last one to reenable the keyguard wait until
4243+ // we have actually finished reenabling until returning.
4244+ // It is possible that reenableKeyguard() can be called before
4245+ // the previous disableKeyguard() is handled, in which case
4246+ // neither mKeyguardTokenWatcher.acquired() or released() would
4247+ // be called. In that case mKeyguardDisabled will be false here
4248+ // and we have nothing to wait for.
4249+ while (mKeyguardDisabled ) {
4250+ try {
4251+ mKeyguardTokenWatcher .wait ();
4252+ } catch (InterruptedException e ) {
4253+ Thread .currentThread ().interrupt ();
42284254 }
42294255 }
42304256 }
4231- } else {
4232- Log .w (TAG , "reenableKeyguard() ignored while DevicePolicyAmin is enabled." );
42334257 }
42344258 }
42354259
0 commit comments