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