The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app; |
| 18 | |
Tor Norbye | 672055e | 2015-04-23 17:30:56 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
| 21 | import android.annotation.Nullable; |
Tor Norbye | 672055e | 2015-04-23 17:30:56 -0700 | [diff] [blame] | 22 | import android.annotation.RequiresPermission; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 23 | import android.annotation.SystemService; |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 24 | import android.app.trust.ITrustManager; |
| 25 | import android.content.Context; |
Jim Miller | 66093a9 | 2014-08-13 14:47:47 -0700 | [diff] [blame] | 26 | import android.content.Intent; |
Nancy Zheng | 43f166d | 2016-11-18 18:15:09 -0800 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
Nancy Zheng | e256a18 | 2016-11-21 15:46:08 -0800 | [diff] [blame] | 28 | import android.content.pm.ResolveInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.os.Binder; |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 30 | import android.os.Handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.os.IBinder; |
Nancy Zheng | 6111e23 | 2017-01-23 14:04:19 -0800 | [diff] [blame] | 32 | import android.os.Looper; |
Nancy Zheng | e256a18 | 2016-11-21 15:46:08 -0800 | [diff] [blame] | 33 | import android.os.RemoteException; |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 34 | import android.os.ServiceManager; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 35 | import android.os.ServiceManager.ServiceNotFoundException; |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 36 | import android.os.UserHandle; |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 37 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.view.IOnKeyguardExitResult; |
Nancy Zheng | e256a18 | 2016-11-21 15:46:08 -0800 | [diff] [blame] | 39 | import android.view.IWindowManager; |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 40 | import android.view.WindowManager.LayoutParams; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 41 | import android.view.WindowManagerGlobal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 43 | import com.android.internal.policy.IKeyguardDismissCallback; |
| 44 | |
Nancy Zheng | e256a18 | 2016-11-21 15:46:08 -0800 | [diff] [blame] | 45 | import java.util.List; |
| 46 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | /** |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 48 | * Class that can be used to lock and unlock the keyboard. The |
Jean-Michel Trivi | 37fde0a | 2012-05-24 17:13:06 -0700 | [diff] [blame] | 49 | * actual class to control the keyboard locking is |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | * {@link android.app.KeyguardManager.KeyguardLock}. |
| 51 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 52 | @SystemService(Context.KEYGUARD_SERVICE) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | public class KeyguardManager { |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 54 | |
| 55 | private static final String TAG = "KeyguardManager"; |
| 56 | |
| 57 | private final Context mContext; |
| 58 | private final IWindowManager mWM; |
| 59 | private final IActivityManager mAm; |
| 60 | private final ITrustManager mTrustManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | |
| 62 | /** |
Jim Miller | 66093a9 | 2014-08-13 14:47:47 -0700 | [diff] [blame] | 63 | * Intent used to prompt user for device credentials. |
| 64 | * @hide |
| 65 | */ |
| 66 | public static final String ACTION_CONFIRM_DEVICE_CREDENTIAL = |
| 67 | "android.app.action.CONFIRM_DEVICE_CREDENTIAL"; |
| 68 | |
| 69 | /** |
Clara Bayarri | b3987bd | 2015-11-18 16:39:34 -0800 | [diff] [blame] | 70 | * Intent used to prompt user for device credentials. |
| 71 | * @hide |
| 72 | */ |
| 73 | public static final String ACTION_CONFIRM_DEVICE_CREDENTIAL_WITH_USER = |
| 74 | "android.app.action.CONFIRM_DEVICE_CREDENTIAL_WITH_USER"; |
| 75 | |
| 76 | /** |
Jim Miller | 66093a9 | 2014-08-13 14:47:47 -0700 | [diff] [blame] | 77 | * A CharSequence dialog title to show to the user when used with a |
| 78 | * {@link #ACTION_CONFIRM_DEVICE_CREDENTIAL}. |
| 79 | * @hide |
| 80 | */ |
| 81 | public static final String EXTRA_TITLE = "android.app.extra.TITLE"; |
| 82 | |
| 83 | /** |
| 84 | * A CharSequence description to show to the user when used with |
| 85 | * {@link #ACTION_CONFIRM_DEVICE_CREDENTIAL}. |
| 86 | * @hide |
| 87 | */ |
| 88 | public static final String EXTRA_DESCRIPTION = "android.app.extra.DESCRIPTION"; |
| 89 | |
| 90 | /** |
| 91 | * Get an intent to prompt the user to confirm credentials (pin, pattern or password) |
| 92 | * for the current user of the device. The caller is expected to launch this activity using |
| 93 | * {@link android.app.Activity#startActivityForResult(Intent, int)} and check for |
| 94 | * {@link android.app.Activity#RESULT_OK} if the user successfully completes the challenge. |
| 95 | * |
| 96 | * @return the intent for launching the activity or null if no password is required. |
| 97 | **/ |
Jim Miller | bde3d18 | 2014-08-26 19:53:17 -0700 | [diff] [blame] | 98 | public Intent createConfirmDeviceCredentialIntent(CharSequence title, CharSequence description) { |
Clara Bayarri | b3987bd | 2015-11-18 16:39:34 -0800 | [diff] [blame] | 99 | if (!isDeviceSecure()) return null; |
Jim Miller | 66093a9 | 2014-08-13 14:47:47 -0700 | [diff] [blame] | 100 | Intent intent = new Intent(ACTION_CONFIRM_DEVICE_CREDENTIAL); |
| 101 | intent.putExtra(EXTRA_TITLE, title); |
| 102 | intent.putExtra(EXTRA_DESCRIPTION, description); |
Nancy Zheng | e256a18 | 2016-11-21 15:46:08 -0800 | [diff] [blame] | 103 | |
| 104 | // explicitly set the package for security |
| 105 | intent.setPackage(getSettingsPackageForIntent(intent)); |
Jim Miller | 66093a9 | 2014-08-13 14:47:47 -0700 | [diff] [blame] | 106 | return intent; |
| 107 | } |
| 108 | |
| 109 | /** |
Clara Bayarri | b3987bd | 2015-11-18 16:39:34 -0800 | [diff] [blame] | 110 | * Get an intent to prompt the user to confirm credentials (pin, pattern or password) |
| 111 | * for the given user. The caller is expected to launch this activity using |
| 112 | * {@link android.app.Activity#startActivityForResult(Intent, int)} and check for |
| 113 | * {@link android.app.Activity#RESULT_OK} if the user successfully completes the challenge. |
| 114 | * |
| 115 | * @return the intent for launching the activity or null if no password is required. |
| 116 | * |
| 117 | * @hide |
| 118 | */ |
| 119 | public Intent createConfirmDeviceCredentialIntent( |
| 120 | CharSequence title, CharSequence description, int userId) { |
| 121 | if (!isDeviceSecure(userId)) return null; |
| 122 | Intent intent = new Intent(ACTION_CONFIRM_DEVICE_CREDENTIAL_WITH_USER); |
| 123 | intent.putExtra(EXTRA_TITLE, title); |
| 124 | intent.putExtra(EXTRA_DESCRIPTION, description); |
| 125 | intent.putExtra(Intent.EXTRA_USER_ID, userId); |
Nancy Zheng | e256a18 | 2016-11-21 15:46:08 -0800 | [diff] [blame] | 126 | |
| 127 | // explicitly set the package for security |
| 128 | intent.setPackage(getSettingsPackageForIntent(intent)); |
| 129 | |
Clara Bayarri | b3987bd | 2015-11-18 16:39:34 -0800 | [diff] [blame] | 130 | return intent; |
| 131 | } |
| 132 | |
Nancy Zheng | e256a18 | 2016-11-21 15:46:08 -0800 | [diff] [blame] | 133 | private String getSettingsPackageForIntent(Intent intent) { |
| 134 | List<ResolveInfo> resolveInfos = mContext.getPackageManager() |
| 135 | .queryIntentActivities(intent, PackageManager.MATCH_SYSTEM_ONLY); |
| 136 | for (int i = 0; i < resolveInfos.size(); i++) { |
| 137 | return resolveInfos.get(i).activityInfo.packageName; |
| 138 | } |
| 139 | |
| 140 | return "com.android.settings"; |
| 141 | } |
| 142 | |
Clara Bayarri | b3987bd | 2015-11-18 16:39:34 -0800 | [diff] [blame] | 143 | /** |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 144 | * @deprecated Use {@link LayoutParams#FLAG_DISMISS_KEYGUARD} |
| 145 | * and/or {@link LayoutParams#FLAG_SHOW_WHEN_LOCKED} |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 146 | * instead; this allows you to seamlessly hide the keyguard as your application |
| 147 | * moves in and out of the foreground and does not require that any special |
| 148 | * permissions be requested. |
| 149 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | * Handle returned by {@link KeyguardManager#newKeyguardLock} that allows |
| 151 | * you to disable / reenable the keyguard. |
| 152 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 153 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | public class KeyguardLock { |
Kenny Root | d7d2d43 | 2014-05-09 10:33:29 -0700 | [diff] [blame] | 155 | private final IBinder mToken = new Binder(); |
| 156 | private final String mTag; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | |
| 158 | KeyguardLock(String tag) { |
| 159 | mTag = tag; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Disable the keyguard from showing. If the keyguard is currently |
| 164 | * showing, hide it. The keyguard will be prevented from showing again |
| 165 | * until {@link #reenableKeyguard()} is called. |
| 166 | * |
| 167 | * A good place to call this is from {@link android.app.Activity#onResume()} |
| 168 | * |
Jim Miller | 66093a9 | 2014-08-13 14:47:47 -0700 | [diff] [blame] | 169 | * Note: This call has no effect while any {@link android.app.admin.DevicePolicyManager} |
Jim Miller | cb52cb5 | 2010-06-07 21:19:16 -0700 | [diff] [blame] | 170 | * is enabled that requires a password. |
Jim Miller | d6b5705 | 2010-06-07 17:52:42 -0700 | [diff] [blame] | 171 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | * @see #reenableKeyguard() |
| 173 | */ |
Tor Norbye | 672055e | 2015-04-23 17:30:56 -0700 | [diff] [blame] | 174 | @RequiresPermission(Manifest.permission.DISABLE_KEYGUARD) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | public void disableKeyguard() { |
| 176 | try { |
| 177 | mWM.disableKeyguard(mToken, mTag); |
| 178 | } catch (RemoteException ex) { |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Reenable the keyguard. The keyguard will reappear if the previous |
Jean-Michel Trivi | 37fde0a | 2012-05-24 17:13:06 -0700 | [diff] [blame] | 184 | * call to {@link #disableKeyguard()} caused it to be hidden. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | * |
Jim Miller | d6b5705 | 2010-06-07 17:52:42 -0700 | [diff] [blame] | 186 | * A good place to call this is from {@link android.app.Activity#onPause()} |
| 187 | * |
Jim Miller | cb52cb5 | 2010-06-07 21:19:16 -0700 | [diff] [blame] | 188 | * Note: This call has no effect while any {@link android.app.admin.DevicePolicyManager} |
| 189 | * is enabled that requires a password. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | * |
| 191 | * @see #disableKeyguard() |
| 192 | */ |
Tor Norbye | 672055e | 2015-04-23 17:30:56 -0700 | [diff] [blame] | 193 | @RequiresPermission(Manifest.permission.DISABLE_KEYGUARD) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | public void reenableKeyguard() { |
| 195 | try { |
| 196 | mWM.reenableKeyguard(mToken); |
| 197 | } catch (RemoteException ex) { |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 203 | * @deprecated Use {@link KeyguardDismissCallback} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | * Callback passed to {@link KeyguardManager#exitKeyguardSecurely} to notify |
| 205 | * caller of result. |
| 206 | */ |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 207 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | public interface OnKeyguardExitResult { |
| 209 | |
| 210 | /** |
| 211 | * @param success True if the user was able to authenticate, false if |
| 212 | * not. |
| 213 | */ |
| 214 | void onKeyguardExitResult(boolean success); |
| 215 | } |
| 216 | |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 217 | /** |
| 218 | * Callback passed to {@link KeyguardManager#dismissKeyguard} to notify caller of result. |
| 219 | */ |
| 220 | public static abstract class KeyguardDismissCallback { |
| 221 | |
| 222 | /** |
| 223 | * Called when dismissing Keyguard is currently not feasible, i.e. when Keyguard is not |
| 224 | * available, not showing or when the activity requesting the Keyguard dismissal isn't |
| 225 | * showing or isn't showing behind Keyguard. |
| 226 | */ |
| 227 | public void onDismissError() { } |
| 228 | |
| 229 | /** |
| 230 | * Called when dismissing Keyguard has succeeded and the device is now unlocked. |
| 231 | */ |
| 232 | public void onDismissSucceeded() { } |
| 233 | |
| 234 | /** |
| 235 | * Called when dismissing Keyguard has been cancelled, i.e. when the user cancelled the |
| 236 | * operation or the bouncer was hidden for some other reason. |
| 237 | */ |
| 238 | public void onDismissCancelled() { } |
| 239 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | |
Nancy Zheng | 40cd8e4 | 2016-11-21 12:58:16 -0800 | [diff] [blame] | 241 | KeyguardManager(Context context) throws ServiceNotFoundException { |
Nancy Zheng | 43f166d | 2016-11-18 18:15:09 -0800 | [diff] [blame] | 242 | mContext = context; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 243 | mWM = WindowManagerGlobal.getWindowManagerService(); |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 244 | mAm = ActivityManager.getService(); |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 245 | mTrustManager = ITrustManager.Stub.asInterface( |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 246 | ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 250 | * @deprecated Use {@link LayoutParams#FLAG_DISMISS_KEYGUARD} |
| 251 | * and/or {@link LayoutParams#FLAG_SHOW_WHEN_LOCKED} |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 252 | * instead; this allows you to seamlessly hide the keyguard as your application |
| 253 | * moves in and out of the foreground and does not require that any special |
| 254 | * permissions be requested. |
| 255 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | * Enables you to lock or unlock the keyboard. Get an instance of this class by |
Jim Miller | 66093a9 | 2014-08-13 14:47:47 -0700 | [diff] [blame] | 257 | * calling {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | * This class is wrapped by {@link android.app.KeyguardManager KeyguardManager}. |
| 259 | * @param tag A tag that informally identifies who you are (for debugging who |
| 260 | * is disabling he keyguard). |
| 261 | * |
| 262 | * @return A {@link KeyguardLock} handle to use to disable and reenable the |
| 263 | * keyguard. |
| 264 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 265 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | public KeyguardLock newKeyguardLock(String tag) { |
| 267 | return new KeyguardLock(tag); |
| 268 | } |
| 269 | |
| 270 | /** |
Mike Lockwood | 520d8bc | 2011-02-18 13:23:13 -0500 | [diff] [blame] | 271 | * Return whether the keyguard is currently locked. |
| 272 | * |
Jean-Michel Trivi | 37fde0a | 2012-05-24 17:13:06 -0700 | [diff] [blame] | 273 | * @return true if keyguard is locked. |
Mike Lockwood | 520d8bc | 2011-02-18 13:23:13 -0500 | [diff] [blame] | 274 | */ |
| 275 | public boolean isKeyguardLocked() { |
| 276 | try { |
Mike Lockwood | 5053124 | 2011-02-26 11:23:49 -0500 | [diff] [blame] | 277 | return mWM.isKeyguardLocked(); |
Mike Lockwood | 520d8bc | 2011-02-18 13:23:13 -0500 | [diff] [blame] | 278 | } catch (RemoteException ex) { |
| 279 | return false; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
Adrian Roos | c39b4fc | 2015-04-28 15:48:00 -0700 | [diff] [blame] | 284 | * Return whether the keyguard is secured by a PIN, pattern or password or a SIM card |
| 285 | * is currently locked. |
Mike Lockwood | 520d8bc | 2011-02-18 13:23:13 -0500 | [diff] [blame] | 286 | * |
Adrian Roos | c39b4fc | 2015-04-28 15:48:00 -0700 | [diff] [blame] | 287 | * <p>See also {@link #isDeviceSecure()} which ignores SIM locked states. |
| 288 | * |
| 289 | * @return true if a PIN, pattern or password is set or a SIM card is locked. |
Mike Lockwood | 520d8bc | 2011-02-18 13:23:13 -0500 | [diff] [blame] | 290 | */ |
| 291 | public boolean isKeyguardSecure() { |
| 292 | try { |
| 293 | return mWM.isKeyguardSecure(); |
| 294 | } catch (RemoteException ex) { |
| 295 | return false; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | * If keyguard screen is showing or in restricted key input mode (i.e. in |
| 301 | * keyguard password emergency screen). When in such mode, certain keys, |
| 302 | * such as the Home key and the right soft keys, don't work. |
| 303 | * |
| 304 | * @return true if in keyguard restricted input mode. |
| 305 | * |
| 306 | * @see android.view.WindowManagerPolicy#inKeyguardRestrictedKeyInputMode |
| 307 | */ |
| 308 | public boolean inKeyguardRestrictedInputMode() { |
| 309 | try { |
| 310 | return mWM.inKeyguardRestrictedInputMode(); |
| 311 | } catch (RemoteException ex) { |
| 312 | return false; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /** |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 317 | * Returns whether the device is currently locked and requires a PIN, pattern or |
| 318 | * password to unlock. |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 319 | * |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 320 | * @return true if unlocking the device currently requires a PIN, pattern or |
| 321 | * password. |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 322 | */ |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 323 | public boolean isDeviceLocked() { |
Adrian Roos | 0b1853f | 2017-01-23 12:38:13 -0800 | [diff] [blame] | 324 | return isDeviceLocked(UserHandle.myUserId()); |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /** |
Adrian Roos | c39b4fc | 2015-04-28 15:48:00 -0700 | [diff] [blame] | 328 | * Per-user version of {@link #isDeviceLocked()}. |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 329 | * |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 330 | * @hide |
| 331 | */ |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 332 | public boolean isDeviceLocked(int userId) { |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 333 | try { |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 334 | return mTrustManager.isDeviceLocked(userId); |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 335 | } catch (RemoteException e) { |
| 336 | return false; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /** |
Adrian Roos | 8289368 | 2015-04-02 16:17:46 +0200 | [diff] [blame] | 341 | * Returns whether the device is secured with a PIN, pattern or |
| 342 | * password. |
| 343 | * |
Adrian Roos | c39b4fc | 2015-04-28 15:48:00 -0700 | [diff] [blame] | 344 | * <p>See also {@link #isKeyguardSecure} which treats SIM locked states as secure. |
| 345 | * |
Adrian Roos | 8289368 | 2015-04-02 16:17:46 +0200 | [diff] [blame] | 346 | * @return true if a PIN, pattern or password was set. |
| 347 | */ |
| 348 | public boolean isDeviceSecure() { |
Adrian Roos | 0b1853f | 2017-01-23 12:38:13 -0800 | [diff] [blame] | 349 | return isDeviceSecure(UserHandle.myUserId()); |
Adrian Roos | 8289368 | 2015-04-02 16:17:46 +0200 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /** |
Adrian Roos | c39b4fc | 2015-04-28 15:48:00 -0700 | [diff] [blame] | 353 | * Per-user version of {@link #isDeviceSecure()}. |
Adrian Roos | 8289368 | 2015-04-02 16:17:46 +0200 | [diff] [blame] | 354 | * |
Adrian Roos | 8289368 | 2015-04-02 16:17:46 +0200 | [diff] [blame] | 355 | * @hide |
| 356 | */ |
| 357 | public boolean isDeviceSecure(int userId) { |
| 358 | try { |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 359 | return mTrustManager.isDeviceSecure(userId); |
Adrian Roos | 8289368 | 2015-04-02 16:17:46 +0200 | [diff] [blame] | 360 | } catch (RemoteException e) { |
| 361 | return false; |
| 362 | } |
| 363 | } |
| 364 | |
Jeff Sharkey | 000ce80 | 2017-04-29 13:13:27 -0600 | [diff] [blame] | 365 | /** @removed */ |
Jorim Jaggi | f41e766 | 2017-04-27 14:30:39 +0200 | [diff] [blame] | 366 | @Deprecated |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 367 | public void dismissKeyguard(@NonNull Activity activity, |
| 368 | @Nullable KeyguardDismissCallback callback, @Nullable Handler handler) { |
Jorim Jaggi | f41e766 | 2017-04-27 14:30:39 +0200 | [diff] [blame] | 369 | requestDismissKeyguard(activity, callback); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * If the device is currently locked (see {@link #isKeyguardLocked()}, requests the Keyguard to |
| 374 | * be dismissed. |
| 375 | * <p> |
| 376 | * If the Keyguard is not secure or the device is currently in a trusted state, calling this |
| 377 | * method will immediately dismiss the Keyguard without any user interaction. |
| 378 | * <p> |
| 379 | * If the Keyguard is secure and the device is not in a trusted state, this will bring up the |
| 380 | * UI so the user can enter their credentials. |
| 381 | * |
| 382 | * @param activity The activity requesting the dismissal. The activity must be either visible |
| 383 | * by using {@link LayoutParams#FLAG_SHOW_WHEN_LOCKED} or must be in a state in |
| 384 | * which it would be visible if Keyguard would not be hiding it. If that's not |
| 385 | * the case, the request will fail immediately and |
| 386 | * {@link KeyguardDismissCallback#onDismissError} will be invoked. |
| 387 | * @param callback The callback to be called if the request to dismiss Keyguard was successful |
| 388 | * or {@code null} if the caller isn't interested in knowing the result. The |
| 389 | * callback will not be invoked if the activity was destroyed before the |
| 390 | * callback was received. |
| 391 | */ |
| 392 | public void requestDismissKeyguard(@NonNull Activity activity, |
| 393 | @Nullable KeyguardDismissCallback callback) { |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 394 | try { |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 395 | mAm.dismissKeyguard(activity.getActivityToken(), new IKeyguardDismissCallback.Stub() { |
| 396 | @Override |
| 397 | public void onDismissError() throws RemoteException { |
Jorim Jaggi | f41e766 | 2017-04-27 14:30:39 +0200 | [diff] [blame] | 398 | if (callback != null && !activity.isDestroyed()) { |
| 399 | activity.mHandler.post(callback::onDismissError); |
| 400 | } |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | @Override |
| 404 | public void onDismissSucceeded() throws RemoteException { |
Jorim Jaggi | f41e766 | 2017-04-27 14:30:39 +0200 | [diff] [blame] | 405 | if (callback != null && !activity.isDestroyed()) { |
| 406 | activity.mHandler.post(callback::onDismissSucceeded); |
| 407 | } |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | @Override |
| 411 | public void onDismissCancelled() throws RemoteException { |
Jorim Jaggi | f41e766 | 2017-04-27 14:30:39 +0200 | [diff] [blame] | 412 | if (callback != null && !activity.isDestroyed()) { |
| 413 | activity.mHandler.post(callback::onDismissCancelled); |
| 414 | } |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 415 | } |
| 416 | }); |
| 417 | } catch (RemoteException e) { |
| 418 | Log.i(TAG, "Failed to dismiss keyguard: " + e); |
Clara Bayarri | 56878a9 | 2015-10-29 15:43:55 +0000 | [diff] [blame] | 419 | } |
Clara Bayarri | 56878a9 | 2015-10-29 15:43:55 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Adrian Roos | 8289368 | 2015-04-02 16:17:46 +0200 | [diff] [blame] | 422 | /** |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 423 | * @deprecated Use {@link LayoutParams#FLAG_DISMISS_KEYGUARD} |
| 424 | * and/or {@link LayoutParams#FLAG_SHOW_WHEN_LOCKED} |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 425 | * instead; this allows you to seamlessly hide the keyguard as your application |
| 426 | * moves in and out of the foreground and does not require that any special |
| 427 | * permissions be requested. |
| 428 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | * Exit the keyguard securely. The use case for this api is that, after |
| 430 | * disabling the keyguard, your app, which was granted permission to |
| 431 | * disable the keyguard and show a limited amount of information deemed |
| 432 | * safe without the user getting past the keyguard, needs to navigate to |
| 433 | * something that is not safe to view without getting past the keyguard. |
| 434 | * |
| 435 | * This will, if the keyguard is secure, bring up the unlock screen of |
| 436 | * the keyguard. |
| 437 | * |
| 438 | * @param callback Let's you know whether the operation was succesful and |
| 439 | * it is safe to launch anything that would normally be considered safe |
| 440 | * once the user has gotten past the keyguard. |
| 441 | */ |
Dianne Hackborn | 9567a66 | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 442 | @Deprecated |
Tor Norbye | 672055e | 2015-04-23 17:30:56 -0700 | [diff] [blame] | 443 | @RequiresPermission(Manifest.permission.DISABLE_KEYGUARD) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | public void exitKeyguardSecurely(final OnKeyguardExitResult callback) { |
| 445 | try { |
| 446 | mWM.exitKeyguardSecurely(new IOnKeyguardExitResult.Stub() { |
| 447 | public void onKeyguardExitResult(boolean success) throws RemoteException { |
Jim Miller | a999d46 | 2013-10-30 13:58:11 -0700 | [diff] [blame] | 448 | if (callback != null) { |
| 449 | callback.onKeyguardExitResult(success); |
| 450 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | } |
| 452 | }); |
| 453 | } catch (RemoteException e) { |
| 454 | |
| 455 | } |
| 456 | } |
| 457 | } |