blob: a55b49fe028d4783d71cbd3db2be98881176858c [file] [log] [blame]
Jim Millerab954542014-10-10 18:21:49 -07001/*
2 * Copyright (C) 2013 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
Jorim Jaggib10e33f2015-02-04 21:57:40 +010017package com.android.server.policy.keyguard;
Jim Millerab954542014-10-10 18:21:49 -070018
19import android.app.ActivityManager;
20import android.content.Context;
21import android.os.RemoteException;
Brian Young9272dab2018-02-23 18:04:20 +000022import android.os.ServiceManager;
23import android.security.IKeystoreService;
Jim Millerab954542014-10-10 18:21:49 -070024import android.util.Slog;
25
26import com.android.internal.policy.IKeyguardService;
27import com.android.internal.policy.IKeyguardStateCallback;
28import com.android.internal.widget.LockPatternUtils;
29
Jorim Jaggi84dc08a2015-09-11 17:45:22 -070030import java.io.PrintWriter;
31
Jim Millerab954542014-10-10 18:21:49 -070032/**
33 * Maintains a cached copy of Keyguard's state.
34 * @hide
35 */
36public class KeyguardStateMonitor extends IKeyguardStateCallback.Stub {
37 private static final String TAG = "KeyguardStateMonitor";
38
39 // These cache the current state of Keyguard to improve performance and avoid deadlock. After
40 // Keyguard changes its state, it always triggers a layout in window manager. Because
41 // IKeyguardStateCallback is synchronous and because these states are declared volatile, it's
42 // guaranteed that window manager picks up the new state all the time in the layout caused by
Adrian Roos02a28882015-06-03 17:06:46 -070043 // the state change of Keyguard. To be extra safe, assume most restrictive values until Keyguard
44 // tells us the actual value.
45 private volatile boolean mIsShowing = true;
46 private volatile boolean mSimSecure = true;
47 private volatile boolean mInputRestricted = true;
Adrian Roosd88eb262016-08-04 14:50:48 -070048 private volatile boolean mTrusted = false;
Jorim Jaggid11d1a92016-08-16 16:02:32 -070049 private volatile boolean mHasLockscreenWallpaper = false;
Jim Millerab954542014-10-10 18:21:49 -070050
Adrian Roosd6aa6cb2015-04-16 19:31:29 -070051 private int mCurrentUserId;
52
Jim Millerab954542014-10-10 18:21:49 -070053 private final LockPatternUtils mLockPatternUtils;
Jorim Jaggie69c9312016-10-31 18:24:38 -070054 private final StateCallback mCallback;
Jim Millerab954542014-10-10 18:21:49 -070055
Brian Young9272dab2018-02-23 18:04:20 +000056 IKeystoreService mKeystoreService;
57
Jorim Jaggie69c9312016-10-31 18:24:38 -070058 public KeyguardStateMonitor(Context context, IKeyguardService service, StateCallback callback) {
Jim Millerab954542014-10-10 18:21:49 -070059 mLockPatternUtils = new LockPatternUtils(context);
Adrian Roosd6aa6cb2015-04-16 19:31:29 -070060 mCurrentUserId = ActivityManager.getCurrentUser();
Jorim Jaggie69c9312016-10-31 18:24:38 -070061 mCallback = callback;
Jorim Jaggife762342016-10-13 14:33:27 +020062
Brian Young9272dab2018-02-23 18:04:20 +000063 mKeystoreService = IKeystoreService.Stub.asInterface(ServiceManager
64 .getService("android.security.keystore"));
65
Jim Millerab954542014-10-10 18:21:49 -070066 try {
67 service.addStateMonitorCallback(this);
68 } catch (RemoteException e) {
69 Slog.w(TAG, "Remote Exception", e);
70 }
71 }
72
73 public boolean isShowing() {
74 return mIsShowing;
75 }
76
Jim Millere4044bb2016-05-10 18:38:25 -070077 public boolean isSecure(int userId) {
78 return mLockPatternUtils.isSecure(userId) || mSimSecure;
Jim Millerab954542014-10-10 18:21:49 -070079 }
80
81 public boolean isInputRestricted() {
82 return mInputRestricted;
83 }
84
Adrian Roosd88eb262016-08-04 14:50:48 -070085 public boolean isTrusted() {
86 return mTrusted;
87 }
88
Jorim Jaggid11d1a92016-08-16 16:02:32 -070089 public boolean hasLockscreenWallpaper() {
90 return mHasLockscreenWallpaper;
91 }
92
Jim Millerab954542014-10-10 18:21:49 -070093 @Override // Binder interface
94 public void onShowingStateChanged(boolean showing) {
95 mIsShowing = showing;
Brian Young9272dab2018-02-23 18:04:20 +000096
Adrian Roos4163d622018-05-22 16:56:35 +020097 mCallback.onShowingChanged();
Janis Danisevskis01c3c2f2018-10-10 09:32:39 -070098 int retry = 2;
99 while (retry > 0) {
100 try {
101 mKeystoreService.onKeyguardVisibilityChanged(showing, mCurrentUserId);
102 break;
103 } catch (RemoteException e) {
104 if (retry == 2) {
105 Slog.w(TAG, "Error informing keystore of screen lock. Keystore may have died"
106 + " -> refreshing service token and retrying");
107 mKeystoreService = IKeystoreService.Stub.asInterface(ServiceManager
108 .getService("android.security.keystore"));
109 } else {
110 Slog.e(TAG, "Error informing keystore of screen lock after retrying once", e);
111 }
112 --retry;
113 }
Brian Young9272dab2018-02-23 18:04:20 +0000114 }
Jim Millerab954542014-10-10 18:21:49 -0700115 }
116
117 @Override // Binder interface
118 public void onSimSecureStateChanged(boolean simSecure) {
119 mSimSecure = simSecure;
120 }
121
Adrian Roosd6aa6cb2015-04-16 19:31:29 -0700122 public synchronized void setCurrentUser(int userId) {
123 mCurrentUserId = userId;
124 }
125
126 private synchronized int getCurrentUser() {
127 return mCurrentUserId;
Jim Millerab954542014-10-10 18:21:49 -0700128 }
129
130 @Override // Binder interface
131 public void onInputRestrictedStateChanged(boolean inputRestricted) {
132 mInputRestricted = inputRestricted;
133 }
Jorim Jaggi84dc08a2015-09-11 17:45:22 -0700134
Adrian Roosd88eb262016-08-04 14:50:48 -0700135 @Override // Binder interface
136 public void onTrustedChanged(boolean trusted) {
137 mTrusted = trusted;
Jorim Jaggie69c9312016-10-31 18:24:38 -0700138 mCallback.onTrustedChanged();
Adrian Roosd88eb262016-08-04 14:50:48 -0700139 }
140
Jorim Jaggid11d1a92016-08-16 16:02:32 -0700141 @Override // Binder interface
142 public void onHasLockscreenWallpaperChanged(boolean hasLockscreenWallpaper) {
143 mHasLockscreenWallpaper = hasLockscreenWallpaper;
144 }
145
Jorim Jaggie69c9312016-10-31 18:24:38 -0700146 public interface StateCallback {
147 void onTrustedChanged();
Adrian Roos4163d622018-05-22 16:56:35 +0200148 void onShowingChanged();
Jorim Jaggie69c9312016-10-31 18:24:38 -0700149 }
150
Jorim Jaggi84dc08a2015-09-11 17:45:22 -0700151 public void dump(String prefix, PrintWriter pw) {
152 pw.println(prefix + TAG);
153 prefix += " ";
154 pw.println(prefix + "mIsShowing=" + mIsShowing);
155 pw.println(prefix + "mSimSecure=" + mSimSecure);
156 pw.println(prefix + "mInputRestricted=" + mInputRestricted);
Adrian Roosd88eb262016-08-04 14:50:48 -0700157 pw.println(prefix + "mTrusted=" + mTrusted);
Jorim Jaggi84dc08a2015-09-11 17:45:22 -0700158 pw.println(prefix + "mCurrentUserId=" + mCurrentUserId);
159 }
Brian Young36716eb2018-02-23 18:04:20 +0000160}