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 mPolicy.enableKeyguard(true); 72 // lazily evaluate this next time we're asked to disable keyguard 73 mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN; 74 break; 75 } 76 } 77 78 class KeyguardTokenWatcher extends TokenWatcher { 79 80 public KeyguardTokenWatcher(final Handler handler) { 81 super(handler, TAG); 82 } 83 84 @Override 85 public void acquired() { 86 // We fail safe and prevent disabling keyguard in the unlikely event this gets 87 // called before DevicePolicyManagerService has started. 88 if (mAllowDisableKeyguard == ALLOW_DISABLE_UNKNOWN) { 89 DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService( 90 Context.DEVICE_POLICY_SERVICE); 91 if (dpm != null) { 92 try { 93 mAllowDisableKeyguard = dpm.getPasswordQuality(null, 94 ActivityManagerNative.getDefault().getCurrentUser().id) 95 == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED ? 96 ALLOW_DISABLE_YES : ALLOW_DISABLE_NO; 97 } catch (RemoteException re) { 98 // Nothing much we can do 99 } 100 } 101 } 102 if (mAllowDisableKeyguard == ALLOW_DISABLE_YES) { 103 mPolicy.enableKeyguard(false); 104 } else { 105 Log.v(TAG, "Not disabling keyguard since device policy is enforced"); 106 } 107 } 108 109 @Override 110 public void released() { 111 mPolicy.enableKeyguard(true); 112 } 113 } 114 } 115