| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app; |
| 18 |
|
| 19 |
import android.Manifest; |
| 20 |
import android.annotation.CallbackExecutor; |
| 21 |
import android.annotation.IntDef; |
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.Nullable; |
| 24 |
import android.annotation.RequiresFeature; |
| 25 |
import android.annotation.RequiresPermission; |
| 26 |
import android.annotation.SystemApi; |
| 27 |
import android.annotation.SystemService; |
| 28 |
import android.annotation.TestApi; |
| 29 |
import android.app.admin.DevicePolicyManager; |
| 30 |
import android.app.admin.DevicePolicyManager.PasswordComplexity; |
| 31 |
import android.app.admin.PasswordMetrics; |
| 32 |
import android.app.trust.ITrustManager; |
| 33 |
import android.compat.annotation.UnsupportedAppUsage; |
| 34 |
import android.content.Context; |
| 35 |
import android.content.Intent; |
| 36 |
import android.content.pm.PackageManager; |
| 37 |
import android.content.pm.ResolveInfo; |
| 38 |
import android.os.Binder; |
| 39 |
import android.os.Build; |
| 40 |
import android.os.IBinder; |
| 41 |
import android.os.RemoteException; |
| 42 |
import android.os.ServiceManager; |
| 43 |
import android.os.ServiceManager.ServiceNotFoundException; |
| 44 |
import android.os.UserHandle; |
| 45 |
import android.provider.Settings; |
| 46 |
import android.service.persistentdata.IPersistentDataBlockService; |
| 47 |
import android.util.ArrayMap; |
| 48 |
import android.util.Log; |
| 49 |
import android.view.IOnKeyguardExitResult; |
| 50 |
import android.view.IWindowManager; |
| 51 |
import android.view.WindowManager.LayoutParams; |
| 52 |
import android.view.WindowManagerGlobal; |
| 53 |
|
| 54 |
import com.android.internal.policy.IKeyguardDismissCallback; |
| 55 |
import com.android.internal.policy.IKeyguardLockedStateListener; |
| 56 |
import com.android.internal.util.Preconditions; |
| 57 |
import com.android.internal.widget.IWeakEscrowTokenActivatedListener; |
| 58 |
import com.android.internal.widget.IWeakEscrowTokenRemovedListener; |
| 59 |
import com.android.internal.widget.LockPatternUtils; |
| 60 |
import com.android.internal.widget.LockPatternView; |
| 61 |
import com.android.internal.widget.LockscreenCredential; |
| 62 |
import com.android.internal.widget.VerifyCredentialResponse; |
| 63 |
|
| 64 |
import java.nio.charset.Charset; |
| 65 |
import java.util.Arrays; |
| 66 |
import java.util.List; |
| 67 |
import java.util.Objects; |
| 68 |
import java.util.concurrent.Executor; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
@SystemService(Context.KEYGUARD_SERVICE) |
| 76 |
public class KeyguardManager { |
| 77 |
|
| 78 |
private static final String TAG = "KeyguardManager"; |
| 79 |
|
| 80 |
private final Context mContext; |
| 81 |
private final LockPatternUtils mLockPatternUtils; |
| 82 |
private final IWindowManager mWM; |
| 83 |
private final IActivityManager mAm; |
| 84 |
private final ITrustManager mTrustManager; |
| 85 |
private final INotificationManager mNotificationManager; |
| 86 |
private final ArrayMap<WeakEscrowTokenRemovedListener, IWeakEscrowTokenRemovedListener> |
| 87 |
mListeners = new ArrayMap<>(); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public static final String ACTION_CONFIRM_DEVICE_CREDENTIAL = |
| 94 |
"android.app.action.CONFIRM_DEVICE_CREDENTIAL"; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public static final String ACTION_CONFIRM_DEVICE_CREDENTIAL_WITH_USER = |
| 101 |
"android.app.action.CONFIRM_DEVICE_CREDENTIAL_WITH_USER"; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public static final String ACTION_CONFIRM_FRP_CREDENTIAL = |
| 108 |
"android.app.action.CONFIRM_FRP_CREDENTIAL"; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public static final String EXTRA_TITLE = "android.app.extra.TITLE"; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public static final String EXTRA_DESCRIPTION = "android.app.extra.DESCRIPTION"; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public static final String EXTRA_ALTERNATE_BUTTON_LABEL = |
| 130 |
"android.app.extra.ALTERNATE_BUTTON_LABEL"; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public static final int RESULT_ALTERNATE = 1; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public static final String EXTRA_DISALLOW_BIOMETRICS_IF_POLICY_EXISTS = "check_dpm"; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
@SystemApi |
| 157 |
public static final int PASSWORD = 0; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
@SystemApi |
| 166 |
public static final int PIN = 1; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
@SystemApi |
| 175 |
public static final int PATTERN = 2; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
@IntDef({ |
| 181 |
PASSWORD, |
| 182 |
PIN, |
| 183 |
PATTERN |
| 184 |
}) |
| 185 |
@interface LockTypes {} |
| 186 |
|
| 187 |
private final IKeyguardLockedStateListener mIKeyguardLockedStateListener = |
| 188 |
new IKeyguardLockedStateListener.Stub() { |
| 189 |
@Override |
| 190 |
public void onKeyguardLockedStateChanged(boolean isKeyguardLocked) { |
| 191 |
mKeyguardLockedStateListeners.forEach((listener, executor) -> { |
| 192 |
executor.execute( |
| 193 |
() -> listener.onKeyguardLockedStateChanged(isKeyguardLocked)); |
| 194 |
}); |
| 195 |
} |
| 196 |
}; |
| 197 |
private final ArrayMap<KeyguardLockedStateListener, Executor> |
| 198 |
mKeyguardLockedStateListeners = new ArrayMap<>(); |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
@Deprecated |
| 210 |
@RequiresFeature(PackageManager.FEATURE_SECURE_LOCK_SCREEN) |
| 211 |
public Intent createConfirmDeviceCredentialIntent(CharSequence title, |
| 212 |
CharSequence description) { |
| 213 |
if (!isDeviceSecure()) return null; |
| 214 |
Intent intent = new Intent(ACTION_CONFIRM_DEVICE_CREDENTIAL); |
| 215 |
intent.putExtra(EXTRA_TITLE, title); |
| 216 |
intent.putExtra(EXTRA_DESCRIPTION, description); |
| 217 |
|
| 218 |
|
| 219 |
intent.setPackage(getSettingsPackageForIntent(intent)); |
| 220 |
return intent; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public Intent createConfirmDeviceCredentialIntent( |
| 234 |
CharSequence title, CharSequence description, int userId) { |
| 235 |
if (!isDeviceSecure(userId)) return null; |
| 236 |
Intent intent = new Intent(ACTION_CONFIRM_DEVICE_CREDENTIAL_WITH_USER); |
| 237 |
intent.putExtra(EXTRA_TITLE, title); |
| 238 |
intent.putExtra(EXTRA_DESCRIPTION, description); |
| 239 |
intent.putExtra(Intent.EXTRA_USER_ID, userId); |
| 240 |
|
| 241 |
|
| 242 |
intent.setPackage(getSettingsPackageForIntent(intent)); |
| 243 |
|
| 244 |
return intent; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public Intent createConfirmDeviceCredentialIntent( |
| 261 |
CharSequence title, CharSequence description, int userId, |
| 262 |
boolean disallowBiometricsIfPolicyExists) { |
| 263 |
Intent intent = this.createConfirmDeviceCredentialIntent(title, description, userId); |
| 264 |
if (intent != null) { |
| 265 |
intent.putExtra(EXTRA_DISALLOW_BIOMETRICS_IF_POLICY_EXISTS, |
| 266 |
disallowBiometricsIfPolicyExists); |
| 267 |
} |
| 268 |
return intent; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
@RequiresFeature(PackageManager.FEATURE_SECURE_LOCK_SCREEN) |
| 289 |
@SystemApi |
| 290 |
public Intent createConfirmFactoryResetCredentialIntent( |
| 291 |
CharSequence title, CharSequence description, CharSequence alternateButtonLabel) { |
| 292 |
if (!LockPatternUtils.frpCredentialEnabled(mContext)) { |
| 293 |
Log.w(TAG, "Factory reset credentials not supported."); |
| 294 |
throw new UnsupportedOperationException("not supported on this device"); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
if (Settings.Global.getInt(mContext.getContentResolver(), |
| 299 |
Settings.Global.DEVICE_PROVISIONED, 0) != 0) { |
| 300 |
Log.e(TAG, "Factory reset credential cannot be verified after provisioning."); |
| 301 |
throw new IllegalStateException("must not be provisioned yet"); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
try { |
| 306 |
IPersistentDataBlockService pdb = IPersistentDataBlockService.Stub.asInterface( |
| 307 |
ServiceManager.getService(Context.PERSISTENT_DATA_BLOCK_SERVICE)); |
| 308 |
if (pdb == null) { |
| 309 |
Log.e(TAG, "No persistent data block service"); |
| 310 |
throw new UnsupportedOperationException("not supported on this device"); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
if (!pdb.hasFrpCredentialHandle()) { |
| 315 |
Log.i(TAG, "The persistent data block does not have a factory reset credential."); |
| 316 |
return null; |
| 317 |
} |
| 318 |
} catch (RemoteException e) { |
| 319 |
throw e.rethrowFromSystemServer(); |
| 320 |
} |
| 321 |
|
| 322 |
Intent intent = new Intent(ACTION_CONFIRM_FRP_CREDENTIAL); |
| 323 |
intent.putExtra(EXTRA_TITLE, title); |
| 324 |
intent.putExtra(EXTRA_DESCRIPTION, description); |
| 325 |
intent.putExtra(EXTRA_ALTERNATE_BUTTON_LABEL, alternateButtonLabel); |
| 326 |
|
| 327 |
|
| 328 |
intent.setPackage(getSettingsPackageForIntent(intent)); |
| 329 |
|
| 330 |
return intent; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
@RequiresFeature(PackageManager.FEATURE_SECURE_LOCK_SCREEN) |
| 345 |
@RequiresPermission(Manifest.permission.CONTROL_KEYGUARD_SECURE_NOTIFICATIONS) |
| 346 |
@SystemApi |
| 347 |
public void setPrivateNotificationsAllowed(boolean allow) { |
| 348 |
try { |
| 349 |
mNotificationManager.setPrivateNotificationsAllowed(allow); |
| 350 |
} catch (RemoteException e) { |
| 351 |
throw e.rethrowFromSystemServer(); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
@RequiresFeature(PackageManager.FEATURE_SECURE_LOCK_SCREEN) |
| 364 |
@RequiresPermission(Manifest.permission.CONTROL_KEYGUARD_SECURE_NOTIFICATIONS) |
| 365 |
@SystemApi |
| 366 |
public boolean getPrivateNotificationsAllowed() { |
| 367 |
try { |
| 368 |
return mNotificationManager.getPrivateNotificationsAllowed(); |
| 369 |
} catch (RemoteException e) { |
| 370 |
throw e.rethrowFromSystemServer(); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
private String getSettingsPackageForIntent(Intent intent) { |
| 375 |
List<ResolveInfo> resolveInfos = mContext.getPackageManager() |
| 376 |
.queryIntentActivities(intent, PackageManager.MATCH_SYSTEM_ONLY); |
| 377 |
for (int i = 0; i < resolveInfos.size(); i++) { |
| 378 |
return resolveInfos.get(i).activityInfo.packageName; |
| 379 |
} |
| 380 |
|
| 381 |
return "com.android.settings"; |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
@Deprecated |
| 395 |
public class KeyguardLock { |
| 396 |
private final IBinder mToken = new Binder(); |
| 397 |
private final String mTag; |
| 398 |
|
| 399 |
KeyguardLock(String tag) { |
| 400 |
mTag = tag; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
@RequiresPermission(Manifest.permission.DISABLE_KEYGUARD) |
| 416 |
public void disableKeyguard() { |
| 417 |
try { |
| 418 |
mWM.disableKeyguard(mToken, mTag, mContext.getUserId()); |
| 419 |
} catch (RemoteException ex) { |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
@RequiresPermission(Manifest.permission.DISABLE_KEYGUARD) |
| 435 |
public void reenableKeyguard() { |
| 436 |
try { |
| 437 |
mWM.reenableKeyguard(mToken, mContext.getUserId()); |
| 438 |
} catch (RemoteException ex) { |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
@Deprecated |
| 450 |
public interface OnKeyguardExitResult { |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
void onKeyguardExitResult(boolean success); |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
public static abstract class KeyguardDismissCallback { |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
public void onDismissError() { } |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
public void onDismissSucceeded() { } |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
public void onDismissCancelled() { } |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
@SystemApi |
| 492 |
public interface WeakEscrowTokenActivatedListener { |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
void onWeakEscrowTokenActivated(long handle, @NonNull UserHandle user); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
@SystemApi |
| 509 |
public interface WeakEscrowTokenRemovedListener { |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
void onWeakEscrowTokenRemoved(long handle, @NonNull UserHandle user); |
| 516 |
} |
| 517 |
|
| 518 |
KeyguardManager(Context context) throws ServiceNotFoundException { |
| 519 |
mContext = context; |
| 520 |
mLockPatternUtils = new LockPatternUtils(context); |
| 521 |
mWM = WindowManagerGlobal.getWindowManagerService(); |
| 522 |
mAm = ActivityManager.getService(); |
| 523 |
mTrustManager = ITrustManager.Stub.asInterface( |
| 524 |
ServiceManager.getServiceOrThrow(Context.TRUST_SERVICE)); |
| 525 |
mNotificationManager = INotificationManager.Stub.asInterface( |
| 526 |
ServiceManager.getServiceOrThrow(Context.NOTIFICATION_SERVICE)); |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
@Deprecated |
| 546 |
public KeyguardLock newKeyguardLock(String tag) { |
| 547 |
return new KeyguardLock(tag); |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
public boolean isKeyguardLocked() { |
| 556 |
try { |
| 557 |
return mWM.isKeyguardLocked(); |
| 558 |
} catch (RemoteException ex) { |
| 559 |
return false; |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
public boolean isKeyguardSecure() { |
| 572 |
try { |
| 573 |
return mWM.isKeyguardSecure(mContext.getUserId()); |
| 574 |
} catch (RemoteException ex) { |
| 575 |
return false; |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
public boolean inKeyguardRestrictedInputMode() { |
| 588 |
return isKeyguardLocked(); |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
public boolean isDeviceLocked() { |
| 599 |
return isDeviceLocked(mContext.getUserId()); |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 608 |
public boolean isDeviceLocked(int userId) { |
| 609 |
try { |
| 610 |
return mTrustManager.isDeviceLocked(userId, mContext.getAssociatedDisplayId()); |
| 611 |
} catch (RemoteException e) { |
| 612 |
return false; |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
public boolean isDeviceSecure() { |
| 625 |
return isDeviceSecure(mContext.getUserId()); |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 634 |
public boolean isDeviceSecure(int userId) { |
| 635 |
try { |
| 636 |
return mTrustManager.isDeviceSecure(userId, mContext.getAssociatedDisplayId()); |
| 637 |
} catch (RemoteException e) { |
| 638 |
return false; |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
public void requestDismissKeyguard(@NonNull Activity activity, |
| 666 |
@Nullable KeyguardDismissCallback callback) { |
| 667 |
requestDismissKeyguard(activity, null , callback); |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
@RequiresPermission(Manifest.permission.SHOW_KEYGUARD_MESSAGE) |
| 697 |
@SystemApi |
| 698 |
public void requestDismissKeyguard(@NonNull Activity activity, @Nullable CharSequence message, |
| 699 |
@Nullable KeyguardDismissCallback callback) { |
| 700 |
ActivityClient.getInstance().dismissKeyguard( |
| 701 |
activity.getActivityToken(), new IKeyguardDismissCallback.Stub() { |
| 702 |
@Override |
| 703 |
public void onDismissError() throws RemoteException { |
| 704 |
if (callback != null && !activity.isDestroyed()) { |
| 705 |
activity.mHandler.post(callback::onDismissError); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
@Override |
| 710 |
public void onDismissSucceeded() throws RemoteException { |
| 711 |
if (callback != null && !activity.isDestroyed()) { |
| 712 |
activity.mHandler.post(callback::onDismissSucceeded); |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
@Override |
| 717 |
public void onDismissCancelled() throws RemoteException { |
| 718 |
if (callback != null && !activity.isDestroyed()) { |
| 719 |
activity.mHandler.post(callback::onDismissCancelled); |
| 720 |
} |
| 721 |
} |
| 722 |
}, message); |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
@Deprecated |
| 746 |
@RequiresPermission(Manifest.permission.DISABLE_KEYGUARD) |
| 747 |
public void exitKeyguardSecurely(final OnKeyguardExitResult callback) { |
| 748 |
try { |
| 749 |
mWM.exitKeyguardSecurely(new IOnKeyguardExitResult.Stub() { |
| 750 |
public void onKeyguardExitResult(boolean success) throws RemoteException { |
| 751 |
if (callback != null) { |
| 752 |
callback.onKeyguardExitResult(success); |
| 753 |
} |
| 754 |
} |
| 755 |
}); |
| 756 |
} catch (RemoteException e) { |
| 757 |
|
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
private boolean checkInitialLockMethodUsage() { |
| 762 |
if (!hasPermission(Manifest.permission.SET_INITIAL_LOCK)) { |
| 763 |
throw new SecurityException("Requires SET_INITIAL_LOCK permission."); |
| 764 |
} |
| 765 |
return true; |
| 766 |
} |
| 767 |
|
| 768 |
private boolean hasPermission(String permission) { |
| 769 |
return PackageManager.PERMISSION_GRANTED == mContext.checkCallingOrSelfPermission( |
| 770 |
permission); |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
@RequiresPermission(Manifest.permission.SET_INITIAL_LOCK) |
| 785 |
@SystemApi |
| 786 |
public boolean isValidLockPasswordComplexity(@LockTypes int lockType, @NonNull byte[] password, |
| 787 |
@PasswordComplexity int complexity) { |
| 788 |
if (!checkInitialLockMethodUsage()) { |
| 789 |
return false; |
| 790 |
} |
| 791 |
complexity = PasswordMetrics.sanitizeComplexityLevel(complexity); |
| 792 |
|
| 793 |
DevicePolicyManager devicePolicyManager = |
| 794 |
(DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 795 |
PasswordMetrics adminMetrics = |
| 796 |
devicePolicyManager.getPasswordMinimumMetrics(mContext.getUserId()); |
| 797 |
|
| 798 |
boolean isPinOrPattern = lockType != PASSWORD; |
| 799 |
|
| 800 |
return PasswordMetrics.validatePassword( |
| 801 |
adminMetrics, complexity, isPinOrPattern, password).size() == 0; |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
@RequiresPermission(Manifest.permission.SET_INITIAL_LOCK) |
| 814 |
@SystemApi |
| 815 |
public int getMinLockLength(boolean isPin, @PasswordComplexity int complexity) { |
| 816 |
if (!checkInitialLockMethodUsage()) { |
| 817 |
return -1; |
| 818 |
} |
| 819 |
complexity = PasswordMetrics.sanitizeComplexityLevel(complexity); |
| 820 |
|
| 821 |
DevicePolicyManager devicePolicyManager = |
| 822 |
(DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 823 |
PasswordMetrics adminMetrics = |
| 824 |
devicePolicyManager.getPasswordMinimumMetrics(mContext.getUserId()); |
| 825 |
PasswordMetrics minMetrics = |
| 826 |
PasswordMetrics.applyComplexity(adminMetrics, isPin, complexity); |
| 827 |
return minMetrics.length; |
| 828 |
} |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
@RequiresPermission(Manifest.permission.SET_INITIAL_LOCK) |
| 844 |
@SystemApi |
| 845 |
public boolean setLock(@LockTypes int lockType, @NonNull byte[] password, |
| 846 |
@PasswordComplexity int complexity) { |
| 847 |
if (!checkInitialLockMethodUsage()) { |
| 848 |
return false; |
| 849 |
} |
| 850 |
|
| 851 |
int userId = mContext.getUserId(); |
| 852 |
if (isDeviceSecure(userId)) { |
| 853 |
Log.e(TAG, "Password already set, rejecting call to setLock"); |
| 854 |
return false; |
| 855 |
} |
| 856 |
if (!isValidLockPasswordComplexity(lockType, password, complexity)) { |
| 857 |
Log.e(TAG, "Password is not valid, rejecting call to setLock"); |
| 858 |
return false; |
| 859 |
} |
| 860 |
boolean success; |
| 861 |
try { |
| 862 |
LockscreenCredential credential = createLockscreenCredential( |
| 863 |
lockType, password); |
| 864 |
success = mLockPatternUtils.setLockCredential( |
| 865 |
credential, |
| 866 |
LockscreenCredential.createNone(), |
| 867 |
userId); |
| 868 |
} catch (Exception e) { |
| 869 |
Log.e(TAG, "Save lock exception", e); |
| 870 |
success = false; |
| 871 |
} finally { |
| 872 |
Arrays.fill(password, (byte) 0); |
| 873 |
} |
| 874 |
return success; |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
@RequiresFeature(PackageManager.FEATURE_AUTOMOTIVE) |
| 892 |
@RequiresPermission(Manifest.permission.MANAGE_WEAK_ESCROW_TOKEN) |
| 893 |
@SystemApi |
| 894 |
public long addWeakEscrowToken(@NonNull byte[] token, @NonNull UserHandle user, |
| 895 |
@NonNull @CallbackExecutor Executor executor, |
| 896 |
@NonNull WeakEscrowTokenActivatedListener listener) { |
| 897 |
Objects.requireNonNull(token, "Token cannot be null."); |
| 898 |
Objects.requireNonNull(user, "User cannot be null."); |
| 899 |
Objects.requireNonNull(executor, "Executor cannot be null."); |
| 900 |
Objects.requireNonNull(listener, "Listener cannot be null."); |
| 901 |
int userId = user.getIdentifier(); |
| 902 |
IWeakEscrowTokenActivatedListener internalListener = |
| 903 |
new IWeakEscrowTokenActivatedListener.Stub() { |
| 904 |
@Override |
| 905 |
public void onWeakEscrowTokenActivated(long handle, int userId) { |
| 906 |
UserHandle user = UserHandle.of(userId); |
| 907 |
final long restoreToken = Binder.clearCallingIdentity(); |
| 908 |
try { |
| 909 |
executor.execute(() -> listener.onWeakEscrowTokenActivated(handle, user)); |
| 910 |
} finally { |
| 911 |
Binder.restoreCallingIdentity(restoreToken); |
| 912 |
} |
| 913 |
Log.i(TAG, "Weak escrow token activated."); |
| 914 |
} |
| 915 |
}; |
| 916 |
return mLockPatternUtils.addWeakEscrowToken(token, userId, internalListener); |
| 917 |
} |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
@RequiresFeature(PackageManager.FEATURE_AUTOMOTIVE) |
| 927 |
@RequiresPermission(Manifest.permission.MANAGE_WEAK_ESCROW_TOKEN) |
| 928 |
@SystemApi |
| 929 |
public boolean removeWeakEscrowToken(long handle, @NonNull UserHandle user) { |
| 930 |
Objects.requireNonNull(user, "User cannot be null."); |
| 931 |
return mLockPatternUtils.removeWeakEscrowToken(handle, user.getIdentifier()); |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
@RequiresFeature(PackageManager.FEATURE_AUTOMOTIVE) |
| 939 |
@RequiresPermission(Manifest.permission.MANAGE_WEAK_ESCROW_TOKEN) |
| 940 |
@SystemApi |
| 941 |
public boolean isWeakEscrowTokenActive(long handle, @NonNull UserHandle user) { |
| 942 |
Objects.requireNonNull(user, "User cannot be null."); |
| 943 |
return mLockPatternUtils.isWeakEscrowTokenActive(handle, user.getIdentifier()); |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
@RequiresFeature(PackageManager.FEATURE_AUTOMOTIVE) |
| 951 |
@RequiresPermission(Manifest.permission.MANAGE_WEAK_ESCROW_TOKEN) |
| 952 |
@SystemApi |
| 953 |
public boolean isWeakEscrowTokenValid(long handle, @NonNull byte[] token, |
| 954 |
@NonNull UserHandle user) { |
| 955 |
Objects.requireNonNull(token, "Token cannot be null."); |
| 956 |
Objects.requireNonNull(user, "User cannot be null."); |
| 957 |
return mLockPatternUtils.isWeakEscrowTokenValid(handle, token, user.getIdentifier()); |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
@RequiresFeature(PackageManager.FEATURE_AUTOMOTIVE) |
| 967 |
@RequiresPermission(Manifest.permission.MANAGE_WEAK_ESCROW_TOKEN) |
| 968 |
@SystemApi |
| 969 |
public boolean registerWeakEscrowTokenRemovedListener( |
| 970 |
@NonNull @CallbackExecutor Executor executor, |
| 971 |
@NonNull WeakEscrowTokenRemovedListener listener) { |
| 972 |
Objects.requireNonNull(listener, "Listener cannot be null."); |
| 973 |
Objects.requireNonNull(executor, "Executor cannot be null."); |
| 974 |
Preconditions.checkArgument(!mListeners.containsKey(listener), |
| 975 |
"Listener already registered: %s", listener); |
| 976 |
IWeakEscrowTokenRemovedListener internalListener = |
| 977 |
new IWeakEscrowTokenRemovedListener.Stub() { |
| 978 |
@Override |
| 979 |
public void onWeakEscrowTokenRemoved(long handle, int userId) { |
| 980 |
UserHandle user = UserHandle.of(userId); |
| 981 |
final long token = Binder.clearCallingIdentity(); |
| 982 |
try { |
| 983 |
executor.execute(() -> listener.onWeakEscrowTokenRemoved(handle, user)); |
| 984 |
} finally { |
| 985 |
Binder.restoreCallingIdentity(token); |
| 986 |
} |
| 987 |
} |
| 988 |
}; |
| 989 |
if (mLockPatternUtils.registerWeakEscrowTokenRemovedListener(internalListener)) { |
| 990 |
mListeners.put(listener, internalListener); |
| 991 |
return true; |
| 992 |
} else { |
| 993 |
Log.e(TAG, "Listener failed to register"); |
| 994 |
return false; |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
@RequiresFeature(PackageManager.FEATURE_AUTOMOTIVE) |
| 1005 |
@RequiresPermission(Manifest.permission.MANAGE_WEAK_ESCROW_TOKEN) |
| 1006 |
@SystemApi |
| 1007 |
public boolean unregisterWeakEscrowTokenRemovedListener( |
| 1008 |
@NonNull WeakEscrowTokenRemovedListener listener) { |
| 1009 |
Objects.requireNonNull(listener, "Listener cannot be null."); |
| 1010 |
IWeakEscrowTokenRemovedListener internalListener = mListeners.get(listener); |
| 1011 |
Preconditions.checkArgument(internalListener != null, "Listener was not registered"); |
| 1012 |
if (mLockPatternUtils.unregisterWeakEscrowTokenRemovedListener(internalListener)) { |
| 1013 |
mListeners.remove(listener); |
| 1014 |
return true; |
| 1015 |
} else { |
| 1016 |
Log.e(TAG, "Listener failed to unregister."); |
| 1017 |
return false; |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
@TestApi |
| 1035 |
@RequiresPermission(anyOf = { |
| 1036 |
Manifest.permission.SET_AND_VERIFY_LOCKSCREEN_CREDENTIALS, |
| 1037 |
Manifest.permission.ACCESS_KEYGUARD_SECURE_STORAGE |
| 1038 |
}) |
| 1039 |
public boolean setLock(@LockTypes int newLockType, @Nullable byte[] newPassword, |
| 1040 |
@LockTypes int currentLockType, @Nullable byte[] currentPassword) { |
| 1041 |
final int userId = mContext.getUserId(); |
| 1042 |
LockscreenCredential currentCredential = createLockscreenCredential( |
| 1043 |
currentLockType, currentPassword); |
| 1044 |
LockscreenCredential newCredential = createLockscreenCredential( |
| 1045 |
newLockType, newPassword); |
| 1046 |
return mLockPatternUtils.setLockCredential(newCredential, currentCredential, userId); |
| 1047 |
} |
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
@TestApi |
| 1060 |
@RequiresPermission(anyOf = { |
| 1061 |
Manifest.permission.SET_AND_VERIFY_LOCKSCREEN_CREDENTIALS, |
| 1062 |
Manifest.permission.ACCESS_KEYGUARD_SECURE_STORAGE |
| 1063 |
}) |
| 1064 |
public boolean checkLock(@LockTypes int lockType, @Nullable byte[] password) { |
| 1065 |
final LockscreenCredential credential = createLockscreenCredential( |
| 1066 |
lockType, password); |
| 1067 |
final VerifyCredentialResponse response = mLockPatternUtils.verifyCredential( |
| 1068 |
credential, mContext.getUserId(), 0); |
| 1069 |
if (response == null) { |
| 1070 |
return false; |
| 1071 |
} |
| 1072 |
return response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK; |
| 1073 |
} |
| 1074 |
|
| 1075 |
private LockscreenCredential createLockscreenCredential( |
| 1076 |
@LockTypes int lockType, @Nullable byte[] password) { |
| 1077 |
if (password == null) { |
| 1078 |
return LockscreenCredential.createNone(); |
| 1079 |
} |
| 1080 |
switch (lockType) { |
| 1081 |
case PASSWORD: |
| 1082 |
CharSequence passwordStr = new String(password, Charset.forName("UTF-8")); |
| 1083 |
return LockscreenCredential.createPassword(passwordStr); |
| 1084 |
case PIN: |
| 1085 |
CharSequence pinStr = new String(password); |
| 1086 |
return LockscreenCredential.createPin(pinStr); |
| 1087 |
case PATTERN: |
| 1088 |
List<LockPatternView.Cell> pattern = |
| 1089 |
LockPatternUtils.byteArrayToPattern(password); |
| 1090 |
return LockscreenCredential.createPattern(pattern); |
| 1091 |
default: |
| 1092 |
throw new IllegalArgumentException("Unknown lock type " + lockType); |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
@FunctionalInterface |
| 1100 |
public interface KeyguardLockedStateListener { |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
void onKeyguardLockedStateChanged(boolean isKeyguardLocked); |
| 1105 |
} |
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
@RequiresPermission(Manifest.permission.SUBSCRIBE_TO_KEYGUARD_LOCKED_STATE) |
| 1116 |
public void addKeyguardLockedStateListener(@NonNull @CallbackExecutor Executor executor, |
| 1117 |
@NonNull KeyguardLockedStateListener listener) { |
| 1118 |
synchronized (mKeyguardLockedStateListeners) { |
| 1119 |
mKeyguardLockedStateListeners.put(listener, executor); |
| 1120 |
if (mKeyguardLockedStateListeners.size() > 1) { |
| 1121 |
return; |
| 1122 |
} |
| 1123 |
try { |
| 1124 |
mWM.addKeyguardLockedStateListener(mIKeyguardLockedStateListener); |
| 1125 |
} catch (RemoteException e) { |
| 1126 |
throw e.rethrowFromSystemServer(); |
| 1127 |
} |
| 1128 |
} |
| 1129 |
} |
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
@RequiresPermission(Manifest.permission.SUBSCRIBE_TO_KEYGUARD_LOCKED_STATE) |
| 1140 |
public void removeKeyguardLockedStateListener(@NonNull KeyguardLockedStateListener listener) { |
| 1141 |
synchronized (mKeyguardLockedStateListeners) { |
| 1142 |
mKeyguardLockedStateListeners.remove(listener); |
| 1143 |
if (!mKeyguardLockedStateListeners.isEmpty()) { |
| 1144 |
return; |
| 1145 |
} |
| 1146 |
try { |
| 1147 |
mWM.removeKeyguardLockedStateListener(mIKeyguardLockedStateListener); |
| 1148 |
} catch (RemoteException e) { |
| 1149 |
throw e.rethrowFromSystemServer(); |
| 1150 |
} |
| 1151 |
} |
| 1152 |
} |
| 1153 |
} |