blob: da99e8005f38e3c451f02e2f894478f8444c17b3 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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
17package android.app;
18
Tor Norbye672055e2015-04-23 17:30:56 -070019import android.Manifest;
20import android.annotation.RequiresPermission;
Adrian Roosbcd07652014-10-22 16:57:16 +020021import android.app.trust.ITrustManager;
22import android.content.Context;
Jim Miller66093a92014-08-13 14:47:47 -070023import android.content.Intent;
Clara Bayarrib3987bd2015-11-18 16:39:34 -080024import android.content.pm.UserInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.os.Binder;
26import android.os.RemoteException;
27import android.os.IBinder;
Clara Bayarrib3987bd2015-11-18 16:39:34 -080028import android.os.IUserManager;
Adrian Roosbcd07652014-10-22 16:57:16 +020029import android.os.ServiceManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060030import android.os.ServiceManager.ServiceNotFoundException;
Adrian Roosbcd07652014-10-22 16:57:16 +020031import android.os.UserHandle;
Clara Bayarrib3987bd2015-11-18 16:39:34 -080032import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.view.IWindowManager;
34import android.view.IOnKeyguardExitResult;
Jeff Brown98365d72012-08-19 20:30:52 -070035import android.view.WindowManagerGlobal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37/**
Jim Miller66093a92014-08-13 14:47:47 -070038 * Class that can be used to lock and unlock the keyboard. Get an instance of this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 * class by calling {@link android.content.Context#getSystemService(java.lang.String)}
40 * with argument {@link android.content.Context#KEYGUARD_SERVICE}. The
Jean-Michel Trivi37fde0a2012-05-24 17:13:06 -070041 * actual class to control the keyboard locking is
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 * {@link android.app.KeyguardManager.KeyguardLock}.
43 */
44public class KeyguardManager {
45 private IWindowManager mWM;
Adrian Roosbcd07652014-10-22 16:57:16 +020046 private ITrustManager mTrustManager;
Clara Bayarrib3987bd2015-11-18 16:39:34 -080047 private IUserManager mUserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
49 /**
Jim Miller66093a92014-08-13 14:47:47 -070050 * Intent used to prompt user for device credentials.
51 * @hide
52 */
53 public static final String ACTION_CONFIRM_DEVICE_CREDENTIAL =
54 "android.app.action.CONFIRM_DEVICE_CREDENTIAL";
55
56 /**
Clara Bayarrib3987bd2015-11-18 16:39:34 -080057 * Intent used to prompt user for device credentials.
58 * @hide
59 */
60 public static final String ACTION_CONFIRM_DEVICE_CREDENTIAL_WITH_USER =
61 "android.app.action.CONFIRM_DEVICE_CREDENTIAL_WITH_USER";
62
63 /**
Jim Miller66093a92014-08-13 14:47:47 -070064 * A CharSequence dialog title to show to the user when used with a
65 * {@link #ACTION_CONFIRM_DEVICE_CREDENTIAL}.
66 * @hide
67 */
68 public static final String EXTRA_TITLE = "android.app.extra.TITLE";
69
70 /**
71 * A CharSequence description to show to the user when used with
72 * {@link #ACTION_CONFIRM_DEVICE_CREDENTIAL}.
73 * @hide
74 */
75 public static final String EXTRA_DESCRIPTION = "android.app.extra.DESCRIPTION";
76
77 /**
78 * Get an intent to prompt the user to confirm credentials (pin, pattern or password)
79 * for the current user of the device. The caller is expected to launch this activity using
80 * {@link android.app.Activity#startActivityForResult(Intent, int)} and check for
81 * {@link android.app.Activity#RESULT_OK} if the user successfully completes the challenge.
82 *
83 * @return the intent for launching the activity or null if no password is required.
84 **/
Jim Millerbde3d182014-08-26 19:53:17 -070085 public Intent createConfirmDeviceCredentialIntent(CharSequence title, CharSequence description) {
Clara Bayarrib3987bd2015-11-18 16:39:34 -080086 if (!isDeviceSecure()) return null;
Jim Miller66093a92014-08-13 14:47:47 -070087 Intent intent = new Intent(ACTION_CONFIRM_DEVICE_CREDENTIAL);
88 intent.putExtra(EXTRA_TITLE, title);
89 intent.putExtra(EXTRA_DESCRIPTION, description);
90 // For security reasons, only allow this to come from system settings.
91 intent.setPackage("com.android.settings");
92 return intent;
93 }
94
95 /**
Clara Bayarrib3987bd2015-11-18 16:39:34 -080096 * Get an intent to prompt the user to confirm credentials (pin, pattern or password)
97 * for the given user. The caller is expected to launch this activity using
98 * {@link android.app.Activity#startActivityForResult(Intent, int)} and check for
99 * {@link android.app.Activity#RESULT_OK} if the user successfully completes the challenge.
100 *
101 * @return the intent for launching the activity or null if no password is required.
102 *
103 * @hide
104 */
105 public Intent createConfirmDeviceCredentialIntent(
106 CharSequence title, CharSequence description, int userId) {
107 if (!isDeviceSecure(userId)) return null;
108 Intent intent = new Intent(ACTION_CONFIRM_DEVICE_CREDENTIAL_WITH_USER);
109 intent.putExtra(EXTRA_TITLE, title);
110 intent.putExtra(EXTRA_DESCRIPTION, description);
111 intent.putExtra(Intent.EXTRA_USER_ID, userId);
112 // For security reasons, only allow this to come from system settings.
113 intent.setPackage("com.android.settings");
114 return intent;
115 }
116
117 /**
Dianne Hackborn9567a662011-04-19 18:44:03 -0700118 * @deprecated Use {@link android.view.WindowManager.LayoutParams#FLAG_DISMISS_KEYGUARD}
119 * and/or {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED}
120 * instead; this allows you to seamlessly hide the keyguard as your application
121 * moves in and out of the foreground and does not require that any special
122 * permissions be requested.
123 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 * Handle returned by {@link KeyguardManager#newKeyguardLock} that allows
125 * you to disable / reenable the keyguard.
126 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700127 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 public class KeyguardLock {
Kenny Rootd7d2d432014-05-09 10:33:29 -0700129 private final IBinder mToken = new Binder();
130 private final String mTag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131
132 KeyguardLock(String tag) {
133 mTag = tag;
134 }
135
136 /**
137 * Disable the keyguard from showing. If the keyguard is currently
138 * showing, hide it. The keyguard will be prevented from showing again
139 * until {@link #reenableKeyguard()} is called.
140 *
141 * A good place to call this is from {@link android.app.Activity#onResume()}
142 *
Jim Miller66093a92014-08-13 14:47:47 -0700143 * Note: This call has no effect while any {@link android.app.admin.DevicePolicyManager}
Jim Millercb52cb52010-06-07 21:19:16 -0700144 * is enabled that requires a password.
Jim Millerd6b57052010-06-07 17:52:42 -0700145 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700146 * <p>This method requires the caller to hold the permission
147 * {@link android.Manifest.permission#DISABLE_KEYGUARD}.
148 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 * @see #reenableKeyguard()
150 */
Tor Norbye672055e2015-04-23 17:30:56 -0700151 @RequiresPermission(Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 public void disableKeyguard() {
153 try {
154 mWM.disableKeyguard(mToken, mTag);
155 } catch (RemoteException ex) {
156 }
157 }
158
159 /**
160 * Reenable the keyguard. The keyguard will reappear if the previous
Jean-Michel Trivi37fde0a2012-05-24 17:13:06 -0700161 * call to {@link #disableKeyguard()} caused it to be hidden.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 *
Jim Millerd6b57052010-06-07 17:52:42 -0700163 * A good place to call this is from {@link android.app.Activity#onPause()}
164 *
Jim Millercb52cb52010-06-07 21:19:16 -0700165 * Note: This call has no effect while any {@link android.app.admin.DevicePolicyManager}
166 * is enabled that requires a password.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700168 * <p>This method requires the caller to hold the permission
169 * {@link android.Manifest.permission#DISABLE_KEYGUARD}.
170 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 * @see #disableKeyguard()
172 */
Tor Norbye672055e2015-04-23 17:30:56 -0700173 @RequiresPermission(Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 public void reenableKeyguard() {
175 try {
176 mWM.reenableKeyguard(mToken);
177 } catch (RemoteException ex) {
178 }
179 }
180 }
181
182 /**
183 * Callback passed to {@link KeyguardManager#exitKeyguardSecurely} to notify
184 * caller of result.
185 */
186 public interface OnKeyguardExitResult {
187
188 /**
189 * @param success True if the user was able to authenticate, false if
190 * not.
191 */
192 void onKeyguardExitResult(boolean success);
193 }
194
195
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600196 KeyguardManager() throws ServiceNotFoundException {
Jeff Brown98365d72012-08-19 20:30:52 -0700197 mWM = WindowManagerGlobal.getWindowManagerService();
Adrian Roosbcd07652014-10-22 16:57:16 +0200198 mTrustManager = ITrustManager.Stub.asInterface(
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600199 ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE));
Clara Bayarrib3987bd2015-11-18 16:39:34 -0800200 mUserManager = IUserManager.Stub.asInterface(
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600201 ServiceManager.getServiceOrThrow(Context.USER_SERVICE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 }
203
204 /**
Dianne Hackborn9567a662011-04-19 18:44:03 -0700205 * @deprecated Use {@link android.view.WindowManager.LayoutParams#FLAG_DISMISS_KEYGUARD}
206 * and/or {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED}
207 * instead; this allows you to seamlessly hide the keyguard as your application
208 * moves in and out of the foreground and does not require that any special
209 * permissions be requested.
210 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 * Enables you to lock or unlock the keyboard. Get an instance of this class by
Jim Miller66093a92014-08-13 14:47:47 -0700212 * calling {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 * This class is wrapped by {@link android.app.KeyguardManager KeyguardManager}.
214 * @param tag A tag that informally identifies who you are (for debugging who
215 * is disabling he keyguard).
216 *
217 * @return A {@link KeyguardLock} handle to use to disable and reenable the
218 * keyguard.
219 */
Dianne Hackborn9567a662011-04-19 18:44:03 -0700220 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 public KeyguardLock newKeyguardLock(String tag) {
222 return new KeyguardLock(tag);
223 }
224
225 /**
Mike Lockwood520d8bc2011-02-18 13:23:13 -0500226 * Return whether the keyguard is currently locked.
227 *
Jean-Michel Trivi37fde0a2012-05-24 17:13:06 -0700228 * @return true if keyguard is locked.
Mike Lockwood520d8bc2011-02-18 13:23:13 -0500229 */
230 public boolean isKeyguardLocked() {
231 try {
Mike Lockwood50531242011-02-26 11:23:49 -0500232 return mWM.isKeyguardLocked();
Mike Lockwood520d8bc2011-02-18 13:23:13 -0500233 } catch (RemoteException ex) {
234 return false;
235 }
236 }
237
238 /**
Adrian Roosc39b4fc2015-04-28 15:48:00 -0700239 * Return whether the keyguard is secured by a PIN, pattern or password or a SIM card
240 * is currently locked.
Mike Lockwood520d8bc2011-02-18 13:23:13 -0500241 *
Adrian Roosc39b4fc2015-04-28 15:48:00 -0700242 * <p>See also {@link #isDeviceSecure()} which ignores SIM locked states.
243 *
244 * @return true if a PIN, pattern or password is set or a SIM card is locked.
Mike Lockwood520d8bc2011-02-18 13:23:13 -0500245 */
246 public boolean isKeyguardSecure() {
247 try {
248 return mWM.isKeyguardSecure();
249 } catch (RemoteException ex) {
250 return false;
251 }
252 }
253
254 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 * If keyguard screen is showing or in restricted key input mode (i.e. in
256 * keyguard password emergency screen). When in such mode, certain keys,
257 * such as the Home key and the right soft keys, don't work.
258 *
259 * @return true if in keyguard restricted input mode.
260 *
261 * @see android.view.WindowManagerPolicy#inKeyguardRestrictedKeyInputMode
262 */
263 public boolean inKeyguardRestrictedInputMode() {
264 try {
265 return mWM.inKeyguardRestrictedInputMode();
266 } catch (RemoteException ex) {
267 return false;
268 }
269 }
270
271 /**
Adrian Roos50bfeec2014-11-20 16:21:11 +0100272 * Returns whether the device is currently locked and requires a PIN, pattern or
273 * password to unlock.
Adrian Roosbcd07652014-10-22 16:57:16 +0200274 *
Adrian Roos50bfeec2014-11-20 16:21:11 +0100275 * @return true if unlocking the device currently requires a PIN, pattern or
276 * password.
Adrian Roosbcd07652014-10-22 16:57:16 +0200277 */
Adrian Roos50bfeec2014-11-20 16:21:11 +0100278 public boolean isDeviceLocked() {
279 return isDeviceLocked(UserHandle.getCallingUserId());
Adrian Roosbcd07652014-10-22 16:57:16 +0200280 }
281
282 /**
Adrian Roosc39b4fc2015-04-28 15:48:00 -0700283 * Per-user version of {@link #isDeviceLocked()}.
Adrian Roosbcd07652014-10-22 16:57:16 +0200284 *
Adrian Roosbcd07652014-10-22 16:57:16 +0200285 * @hide
286 */
Adrian Roos50bfeec2014-11-20 16:21:11 +0100287 public boolean isDeviceLocked(int userId) {
Clara Bayarri56878a92015-10-29 15:43:55 +0000288 ITrustManager trustManager = getTrustManager();
Adrian Roosbcd07652014-10-22 16:57:16 +0200289 try {
Clara Bayarri56878a92015-10-29 15:43:55 +0000290 return trustManager.isDeviceLocked(userId);
Adrian Roosbcd07652014-10-22 16:57:16 +0200291 } catch (RemoteException e) {
292 return false;
293 }
294 }
295
296 /**
Adrian Roos82893682015-04-02 16:17:46 +0200297 * Returns whether the device is secured with a PIN, pattern or
298 * password.
299 *
Adrian Roosc39b4fc2015-04-28 15:48:00 -0700300 * <p>See also {@link #isKeyguardSecure} which treats SIM locked states as secure.
301 *
Adrian Roos82893682015-04-02 16:17:46 +0200302 * @return true if a PIN, pattern or password was set.
303 */
304 public boolean isDeviceSecure() {
305 return isDeviceSecure(UserHandle.getCallingUserId());
306 }
307
308 /**
Adrian Roosc39b4fc2015-04-28 15:48:00 -0700309 * Per-user version of {@link #isDeviceSecure()}.
Adrian Roos82893682015-04-02 16:17:46 +0200310 *
Adrian Roos82893682015-04-02 16:17:46 +0200311 * @hide
312 */
313 public boolean isDeviceSecure(int userId) {
Clara Bayarri56878a92015-10-29 15:43:55 +0000314 ITrustManager trustManager = getTrustManager();
Adrian Roos82893682015-04-02 16:17:46 +0200315 try {
Clara Bayarri56878a92015-10-29 15:43:55 +0000316 return trustManager.isDeviceSecure(userId);
Adrian Roos82893682015-04-02 16:17:46 +0200317 } catch (RemoteException e) {
318 return false;
319 }
320 }
321
Clara Bayarri56878a92015-10-29 15:43:55 +0000322 private synchronized ITrustManager getTrustManager() {
323 if (mTrustManager == null) {
324 mTrustManager = ITrustManager.Stub.asInterface(
325 ServiceManager.getService(Context.TRUST_SERVICE));
326 }
327 return mTrustManager;
328 }
329
Adrian Roos82893682015-04-02 16:17:46 +0200330 /**
Dianne Hackborn9567a662011-04-19 18:44:03 -0700331 * @deprecated Use {@link android.view.WindowManager.LayoutParams#FLAG_DISMISS_KEYGUARD}
332 * and/or {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED}
333 * instead; this allows you to seamlessly hide the keyguard as your application
334 * moves in and out of the foreground and does not require that any special
335 * permissions be requested.
336 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 * Exit the keyguard securely. The use case for this api is that, after
338 * disabling the keyguard, your app, which was granted permission to
339 * disable the keyguard and show a limited amount of information deemed
340 * safe without the user getting past the keyguard, needs to navigate to
341 * something that is not safe to view without getting past the keyguard.
342 *
343 * This will, if the keyguard is secure, bring up the unlock screen of
344 * the keyguard.
345 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700346 * <p>This method requires the caller to hold the permission
347 * {@link android.Manifest.permission#DISABLE_KEYGUARD}.
348 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * @param callback Let's you know whether the operation was succesful and
350 * it is safe to launch anything that would normally be considered safe
351 * once the user has gotten past the keyguard.
352 */
Dianne Hackborn9567a662011-04-19 18:44:03 -0700353 @Deprecated
Tor Norbye672055e2015-04-23 17:30:56 -0700354 @RequiresPermission(Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 public void exitKeyguardSecurely(final OnKeyguardExitResult callback) {
356 try {
357 mWM.exitKeyguardSecurely(new IOnKeyguardExitResult.Stub() {
358 public void onKeyguardExitResult(boolean success) throws RemoteException {
Jim Millera999d462013-10-30 13:58:11 -0700359 if (callback != null) {
360 callback.onKeyguardExitResult(success);
361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 }
363 });
364 } catch (RemoteException e) {
365
366 }
367 }
368}