Home | History | Annotate | Download | only in wm
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.server.wm;
     18 
     19 import android.app.ActivityManagerNative;
     20 import android.app.admin.DevicePolicyManager;
     21 import android.content.Context;
     22 import android.os.Handler;
     23 import android.os.IBinder;
     24 import android.os.Message;
     25 import android.os.RemoteException;
     26 import android.os.TokenWatcher;
     27 import android.util.Log;
     28 import android.util.Pair;
     29 import android.view.WindowManagerPolicy;
     30 
     31 public class KeyguardDisableHandler extends Handler {
     32     private static final String TAG = "KeyguardDisableHandler";
     33 
     34     private static final int ALLOW_DISABLE_YES = 1;
     35     private static final int ALLOW_DISABLE_NO = 0;
     36     private static final int ALLOW_DISABLE_UNKNOWN = -1; // check with DevicePolicyManager
     37     private int mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN; // sync'd by mKeyguardTokenWatcher
     38 
     39     // Message.what constants
     40     static final int KEYGUARD_DISABLE = 1;
     41     static final int KEYGUARD_REENABLE = 2;
     42     static final int KEYGUARD_POLICY_CHANGED = 3;
     43 
     44     final Context mContext;
     45     final WindowManagerPolicy mPolicy;
     46     KeyguardTokenWatcher mKeyguardTokenWatcher;
     47 
     48     public KeyguardDisableHandler(final Context context, final WindowManagerPolicy policy) {
     49         mContext = context;
     50         mPolicy = policy;
     51     }
     52 
     53     @SuppressWarnings("unchecked")
     54     @Override
     55     public void handleMessage(Message msg) {
     56         if (mKeyguardTokenWatcher == null) {
     57             mKeyguardTokenWatcher = new KeyguardTokenWatcher(this);
     58         }
     59 
     60         switch (msg.what) {
     61             case KEYGUARD_DISABLE:
     62                 final Pair<IBinder, String> pair = (Pair<IBinder, String>)msg.obj;
     63                 mKeyguardTokenWatcher.acquire(pair.first, pair.second);
     64                 break;
     65 
     66             case KEYGUARD_REENABLE:
     67                 mKeyguardTokenWatcher.release((IBinder)msg.obj);
     68                 break;
     69 
     70             case KEYGUARD_POLICY_CHANGED:
     71                 mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN;
     72                 if (mKeyguardTokenWatcher.isAcquired()) {
     73                     // If we are currently disabled we need to know if the keyguard
     74                     // should be re-enabled, so determine the allow state immediately.
     75                     mKeyguardTokenWatcher.updateAllowState();
     76                     if (mAllowDisableKeyguard != ALLOW_DISABLE_YES) {
     77                         mPolicy.enableKeyguard(true);
     78                     }
     79                 } else {
     80                     // lazily evaluate this next time we're asked to disable keyguard
     81                     mPolicy.enableKeyguard(true);
     82                 }
     83                 break;
     84         }
     85     }
     86 
     87     class KeyguardTokenWatcher extends TokenWatcher {
     88 
     89         public KeyguardTokenWatcher(final Handler handler) {
     90             super(handler, TAG);
     91         }
     92 
     93         public void updateAllowState() {
     94             // We fail safe and prevent disabling keyguard in the unlikely event this gets
     95             // called before DevicePolicyManagerService has started.
     96             DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
     97                     Context.DEVICE_POLICY_SERVICE);
     98             if (dpm != null) {
     99                 try {
    100                     mAllowDisableKeyguard = dpm.getPasswordQuality(null,
    101                             ActivityManagerNative.getDefault().getCurrentUser().id)
    102                             == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED ?
    103                                     ALLOW_DISABLE_YES : ALLOW_DISABLE_NO;
    104                 } catch (RemoteException re) {
    105                     // Nothing much we can do
    106                 }
    107             }
    108         }
    109 
    110         @Override
    111         public void acquired() {
    112             if (mAllowDisableKeyguard == ALLOW_DISABLE_UNKNOWN) {
    113                 updateAllowState();
    114             }
    115             if (mAllowDisableKeyguard == ALLOW_DISABLE_YES) {
    116                 mPolicy.enableKeyguard(false);
    117             } else {
    118                 Log.v(TAG, "Not disabling keyguard since device policy is enforced");
    119             }
    120         }
    121 
    122         @Override
    123         public void released() {
    124             mPolicy.enableKeyguard(true);
    125         }
    126     }
    127 }
    128