Craig Mautner | 5642a48 | 2012-08-23 12:16:53 -0700 | [diff] [blame] | 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 | |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 19 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 20 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 21 | |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 22 | import android.app.ActivityManager; |
Craig Mautner | 5642a48 | 2012-08-23 12:16:53 -0700 | [diff] [blame] | 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; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 28 | import android.os.RemoteException; |
Craig Mautner | 5642a48 | 2012-08-23 12:16:53 -0700 | [diff] [blame] | 29 | import android.os.TokenWatcher; |
| 30 | import android.util.Log; |
| 31 | import android.util.Pair; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 32 | |
| 33 | import com.android.server.policy.WindowManagerPolicy; |
Craig Mautner | 5642a48 | 2012-08-23 12:16:53 -0700 | [diff] [blame] | 34 | |
| 35 | public class KeyguardDisableHandler extends Handler { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 36 | private static final String TAG = TAG_WITH_CLASS_NAME ? "KeyguardDisableHandler" : TAG_WM; |
Craig Mautner | 5642a48 | 2012-08-23 12:16:53 -0700 | [diff] [blame] | 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: |
Craig Mautner | 5642a48 | 2012-08-23 12:16:53 -0700 | [diff] [blame] | 75 | mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN; |
Jason Monk | 4a3daac | 2014-12-09 14:32:02 -0500 | [diff] [blame] | 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 | } |
Craig Mautner | 5642a48 | 2012-08-23 12:16:53 -0700 | [diff] [blame] | 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | class KeyguardTokenWatcher extends TokenWatcher { |
| 92 | |
| 93 | public KeyguardTokenWatcher(final Handler handler) { |
| 94 | super(handler, TAG); |
| 95 | } |
| 96 | |
Jason Monk | 4a3daac | 2014-12-09 14:32:02 -0500 | [diff] [blame] | 97 | public void updateAllowState() { |
Craig Mautner | 5642a48 | 2012-08-23 12:16:53 -0700 | [diff] [blame] | 98 | // We fail safe and prevent disabling keyguard in the unlikely event this gets |
| 99 | // called before DevicePolicyManagerService has started. |
Jason Monk | 4a3daac | 2014-12-09 14:32:02 -0500 | [diff] [blame] | 100 | DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService( |
| 101 | Context.DEVICE_POLICY_SERVICE); |
| 102 | if (dpm != null) { |
| 103 | try { |
| 104 | mAllowDisableKeyguard = dpm.getPasswordQuality(null, |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 105 | ActivityManager.getService().getCurrentUser().id) |
Jason Monk | 4a3daac | 2014-12-09 14:32:02 -0500 | [diff] [blame] | 106 | == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED ? |
| 107 | ALLOW_DISABLE_YES : ALLOW_DISABLE_NO; |
| 108 | } catch (RemoteException re) { |
| 109 | // Nothing much we can do |
Craig Mautner | 5642a48 | 2012-08-23 12:16:53 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
Jason Monk | 4a3daac | 2014-12-09 14:32:02 -0500 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void acquired() { |
| 116 | if (mAllowDisableKeyguard == ALLOW_DISABLE_UNKNOWN) { |
| 117 | updateAllowState(); |
| 118 | } |
Craig Mautner | 5642a48 | 2012-08-23 12:16:53 -0700 | [diff] [blame] | 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 | } |