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.os.UserHandle;
     28 import android.util.Log;
     29 import android.util.Pair;
     30 import android.view.WindowManagerPolicy;
     31 
     32 public class KeyguardDisableHandler extends Handler {
     33     private static final String TAG = "KeyguardDisableHandler";
     34 
     35     private static final int ALLOW_DISABLE_YES = 1;
     36     private static final int ALLOW_DISABLE_NO = 0;
     37     private static final int ALLOW_DISABLE_UNKNOWN = -1; // check with DevicePolicyManager
     38     private int mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN; // sync'd by mKeyguardTokenWatcher
     39 
     40     // Message.what constants
     41     static final int KEYGUARD_DISABLE = 1;
     42     static final int KEYGUARD_REENABLE = 2;
     43     static final int KEYGUARD_POLICY_CHANGED = 3;
     44 
     45     final Context mContext;
     46     final WindowManagerPolicy mPolicy;
     47     KeyguardTokenWatcher mKeyguardTokenWatcher;
     48 
     49     public KeyguardDisableHandler(final Context context, final WindowManagerPolicy policy) {
     50         mContext = context;
     51         mPolicy = policy;
     52     }
     53 
     54     @SuppressWarnings("unchecked")
     55     @Override
     56     public void handleMessage(Message msg) {
     57         if (mKeyguardTokenWatcher == null) {
     58             mKeyguardTokenWatcher = new KeyguardTokenWatcher(this);
     59         }
     60 
     61         switch (msg.what) {
     62             case KEYGUARD_DISABLE:
     63                 final Pair<IBinder, String> pair = (Pair<IBinder, String>)msg.obj;
     64                 mKeyguardTokenWatcher.acquire(pair.first, pair.second);
     65                 break;
     66 
     67             case KEYGUARD_REENABLE:
     68                 mKeyguardTokenWatcher.release((IBinder)msg.obj);
     69                 break;
     70 
     71             case KEYGUARD_POLICY_CHANGED:
     72                 mPolicy.enableKeyguard(true);
     73                 // lazily evaluate this next time we're asked to disable keyguard
     74                 mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN;
     75                 break;
     76         }
     77     }
     78 
     79     class KeyguardTokenWatcher extends TokenWatcher {
     80 
     81         public KeyguardTokenWatcher(final Handler handler) {
     82             super(handler, TAG);
     83         }
     84 
     85         @Override
     86         public void acquired() {
     87             // We fail safe and prevent disabling keyguard in the unlikely event this gets
     88             // called before DevicePolicyManagerService has started.
     89             if (mAllowDisableKeyguard == ALLOW_DISABLE_UNKNOWN) {
     90                 DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
     91                         Context.DEVICE_POLICY_SERVICE);
     92                 if (dpm != null) {
     93                     try {
     94                         mAllowDisableKeyguard = dpm.getPasswordQuality(null,
     95                                 ActivityManagerNative.getDefault().getCurrentUser().id)
     96                                 == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED ?
     97                                         ALLOW_DISABLE_YES : ALLOW_DISABLE_NO;
     98                     } catch (RemoteException re) {
     99                         // Nothing much we can do
    100                     }
    101                 }
    102             }
    103             if (mAllowDisableKeyguard == ALLOW_DISABLE_YES) {
    104                 mPolicy.enableKeyguard(false);
    105             } else {
    106                 Log.v(TAG, "Not disabling keyguard since device policy is enforced");
    107             }
    108         }
    109 
    110         @Override
    111         public void released() {
    112             mPolicy.enableKeyguard(true);
    113         }
    114     }
    115 }
    116