下面列出了android.app.KeyguardManager#isKeyguardSecure ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void showAuthenticationScreen() {
KeyguardManager keyguardManager = ContextCompat
.getSystemService(this, KeyguardManager.class);
if (keyguardManager == null
|| android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
return;
}
if (keyguardManager.isKeyguardSecure()) {
Intent intent = keyguardManager
.createConfirmDeviceCredentialIntent(mPromptInfo.getTitle(), mPromptInfo.getDescription());
this.startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
} else {
// Show a message that the user hasn't set up a lock screen.
finishWithError(PluginError.BIOMETRIC_SCREEN_GUARD_UNSECURED);
}
}
/**
* 判断是否支持指纹(先用Android M的方式来判断)
*/
private boolean isSupportFingerprint() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Toast.makeText(this, "您的系统版本过低,不支持指纹功能", Toast.LENGTH_SHORT).show();
return false;
} else {
KeyguardManager keyguardManager = getSystemService(KeyguardManager.class);
FingerprintManager fingerprintManager = getSystemService(FingerprintManager.class);
if (!fingerprintManager.isHardwareDetected()) {
Toast.makeText(this, "您的手机不支持指纹功能", Toast.LENGTH_SHORT).show();
return false;
} else if (!keyguardManager.isKeyguardSecure()) {
Toast.makeText(this, "您还未设置锁屏,请先设置锁屏并添加一个指纹", Toast.LENGTH_SHORT).show();
return false;
} else if (!fingerprintManager.hasEnrolledFingerprints()) {
Toast.makeText(this, "您至少需要在系统设置中添加一个指纹", Toast.LENGTH_SHORT).show();
return false;
}
}
return true;
}
/** Check is permissions granted for biometric things. */
public static boolean isPermissionsGranted(@NonNull final Context context) {
// before api23 no permissions for biometric, no hardware == no permissions
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return false;
}
final KeyguardManager km =
(KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if( !km.isKeyguardSecure() ) return false;
// api28+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
return context.checkSelfPermission(Manifest.permission.USE_BIOMETRIC) == PERMISSION_GRANTED;
}
// before api28
return context.checkSelfPermission(Manifest.permission.USE_FINGERPRINT) == PERMISSION_GRANTED;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
Button purchaseButton = (Button) findViewById(R.id.purchase_button);
if (!mKeyguardManager.isKeyguardSecure()) {
// Show a message that the user hasn't set up a lock screen.
Toast.makeText(this,
"Secure lock screen hasn't set up.\n"
+ "Go to 'Settings -> Security -> Screenlock' to set up a lock screen",
Toast.LENGTH_LONG).show();
purchaseButton.setEnabled(false);
return;
}
createKey();
findViewById(R.id.purchase_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Test to encrypt something. It might fail if the timeout expired (30s).
tryEncrypt();
}
});
}
@RequiresApi(api = Build.VERSION_CODES.M)
public static boolean checkFinger(Context ctx) {
// Keyguard Manager
KeyguardManager keyguardManager = (KeyguardManager) ctx.getSystemService(KEYGUARD_SERVICE);
// Fingerprint Manager
FingerprintManager fingerprintManager = (FingerprintManager) ctx.getSystemService(FINGERPRINT_SERVICE);
try {
// Check if the fingerprint sensor is present
if (!fingerprintManager.isHardwareDetected()) {
// Update the UI with a message
StringUtils.showToast(ctx, ctx.getString(R.string.fp_not_supported));
return false;
}
if (!fingerprintManager.hasEnrolledFingerprints()) {
StringUtils.showToast(ctx, ctx.getString(R.string.fp_not_configured));
return false;
}
if (!keyguardManager.isKeyguardSecure()) {
StringUtils.showToast(ctx, ctx.getString(R.string.fp_not_enabled_sls));
return false;
}
} catch (SecurityException se) {
se.printStackTrace();
}
return true;
}
/** Start an activity. Returns true if successful. */
@SuppressWarnings("deprecation")
protected boolean startActivity(Intent intent) {
Context context = getContext();
if (null == context || null == intent) return false;
// Disable the Keyguard if necessary.
KeyguardManager mKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (mKM.isKeyguardLocked() && !mKM.isKeyguardSecure()) {
mKeyguardLock = mKM.newKeyguardLock(getName());
mKeyguardLock.disableKeyguard();
}
try {
// Necessary because we're in a background Service!
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
return true;
}
} catch (ActivityNotFoundException anfe) {
LOGE("VolumePanel", "Error launching Intent: " + intent.toString(), anfe);
} catch (SecurityException se) {
LOGE("VolumePanel", "Error launching Intent: " + intent.toString(), se);
Toast.makeText(context, R.string.permission_error, Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
public boolean canGoForward() {
Constants.AuthMethod authMethod = selectionMapping.get(selection.getSelectedItemPosition());
if (authMethod == Constants.AuthMethod.PIN || authMethod == Constants.AuthMethod.PASSWORD) {
String password = passwordInput.getText().toString();
String confirm = passwordConfirm.getText().toString();
if (! password.isEmpty()) {
if (password.length() < minLength) {
updateWarning(lengthWarning);
return false;
} else {
if (! confirm.isEmpty() && confirm.equals(password)) {
hideWarning();
return true;
} else {
updateWarning(confirmPasswordWarning);
return false;
}
}
} else {
updateWarning(noPasswordWarning);
return false;
}
} else if (authMethod == Constants.AuthMethod.DEVICE) {
KeyguardManager km = (KeyguardManager) getContext().getSystemService(KEYGUARD_SERVICE);
if (! km.isKeyguardSecure()) {
updateWarning(R.string.settings_toast_auth_device_not_secure);
return false;
}
hideWarning();
return true;
} else {
hideWarning();
return true;
}
}
private void saveValues() {
byte[] newKey = null;
if (settings.getEncryption() == EncryptionType.PASSWORD) {
if (value == AuthMethod.NONE || value == AuthMethod.DEVICE) {
UIHelper.showGenericDialog(getContext(), R.string.settings_dialog_title_error, R.string.settings_dialog_msg_auth_invalid_with_encryption);
return;
}
}
if (value == AuthMethod.DEVICE) {
KeyguardManager km = (KeyguardManager) getContext().getSystemService(KEYGUARD_SERVICE);
if (! km.isKeyguardSecure()) {
Toast.makeText(getContext(), R.string.settings_toast_auth_device_not_secure, Toast.LENGTH_LONG).show();
return;
}
}
if (value == AuthMethod.PASSWORD || value == AuthMethod.PIN) {
String password = passwordInput.getText().toString();
if (!password.isEmpty()) {
newKey = settings.setAuthCredentials(password);
} else {
return;
}
}
if (settings.getEncryption() == EncryptionType.PASSWORD) {
if (newKey == null || encryptionChangeCallback == null)
return;
if (! encryptionChangeCallback.testEncryptionChange(newKey))
return;
}
persistString(value.toString().toLowerCase());
setSummary(entries.get(entryValues.indexOf(value)));
}
public static boolean isDeviceSupported(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return false;
}
FingerprintManager fingerprintManager = context.getSystemService(FingerprintManager.class);
KeyguardManager keyguardManager = context.getSystemService(KeyguardManager.class);
return (keyguardManager != null) && (fingerprintManager != null) &&
keyguardManager.isKeyguardSecure() &&
fingerprintManager.isHardwareDetected() &&
fingerprintManager.hasEnrolledFingerprints();
}
@ReactMethod(isBlockingSynchronousMethod = true)
public boolean isPinOrFingerprintSetSync() {
KeyguardManager keyguardManager = (KeyguardManager) getReactApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
if (keyguardManager != null) {
return keyguardManager.isKeyguardSecure();
}
System.err.println("Unable to determine keyguard status. KeyguardManager was null");
return false;
}
public boolean isStandardKeyguardState() {
boolean isStandardKeyguqrd = false;
KeyguardManager keyManager = (KeyguardManager) mContext.getSystemService(mContext.KEYGUARD_SERVICE);
if (null != keyManager) {
isStandardKeyguqrd = keyManager.isKeyguardSecure();
}
return isStandardKeyguqrd;
}
private void initializeVisibility() {
KeyguardManager keyguardManager = (KeyguardManager) getContext().getSystemService(Context.KEYGUARD_SERVICE);
SwitchPreferenceCompat screenLockPreference = (SwitchPreferenceCompat) findPreference(Prefs.SCREEN_LOCK);
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP || keyguardManager == null || !keyguardManager.isKeyguardSecure()) {
screenLockPreference.setChecked(false);
screenLockPreference.setEnabled(false);
}
if (!screenLockPreference.isChecked()) {
manageScreenLockChildren(false);
}
}
public static boolean areCredentialsAvailable(Context context) {
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (keyguardManager != null) {
return keyguardManager.isKeyguardSecure();
} else {
Log.e(TAG, "Keyguard manager is null");
return false;
}
}
/** Start an activity. Returns true if successful. */
@SuppressWarnings("deprecation")
protected boolean startActivity(Intent intent) {
Context context = getContext();
if (null == context || null == intent) return false;
// Disable the Keyguard if necessary.
KeyguardManager mKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (mKM.isKeyguardLocked() && !mKM.isKeyguardSecure()) {
mKeyguardLock = mKM.newKeyguardLock(getName());
mKeyguardLock.disableKeyguard();
}
try {
// Necessary because we're in a background Service!
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
return true;
}
} catch (ActivityNotFoundException anfe) {
LOGE("VolumePanel", "Error launching Intent: " + intent.toString(), anfe);
} catch (SecurityException se) {
LOGE("VolumePanel", "Error launching Intent: " + intent.toString(), se);
Toast.makeText(context, R.string.permission_error, Toast.LENGTH_SHORT).show();
}
return false;
}
/**
* Returns whether the device is currently locked and requires a PIN,
* pattern or password to unlock.
*/
@SuppressLint("NewApi")
public static boolean isDeviceLocked(@NonNull KeyguardManager km) {
return Device.hasLollipopMR1Api() ? km.isDeviceLocked() : km.isKeyguardSecure();
}
public static boolean isKeyguardSecure(Context context) {
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
return keyguardManager.isKeyguardSecure();
}
public static boolean isKeyguardSecure(Context context) {
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
return keyguardManager.isKeyguardSecure();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (DEBUG) Log.d(TAG, "Creating keyguard activity...");
mTimeout.registerListener(this);
mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
registerScreenEventsReceiver();
int flags = 0;
// Handle intents
final Intent intent = getIntent();
if (intent != null) {
if (hasWakeUpExtra(intent)) flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
mExtraCause = intent.getIntExtra(EXTRA_CAUSE, 0);
}
// FIXME: Android dev team broke the DISMISS_KEYGUARD flag.
// https://code.google.com/p/android-developer-preview/issues/detail?id=1902
if (Device.hasLollipopApi()
&& !Device.hasMarshmallowApi() // Should be fine now
&& !mKeyguardManager.isKeyguardSecure()) {
getWindow().addFlags(flags);
requestDismissSystemKeyguard();
} else {
getWindow().addFlags(flags |
// Show activity above the system keyguard.
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
// Update system ui visibility: hide nav bar and optionally the
// status bar.
final View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener(
mSystemUiListener = new View.OnSystemUiVisibilityChangeListener() {
public final void onSystemUiVisibilityChange(int f) {
setSystemUiVisibilityFake();
decorView.postDelayed(new Runnable() {
@Override
public void run() {
int visibility = SYSTEM_UI_BASIC_FLAGS
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
if (getConfig().isFullScreen()) {
visibility |= View.SYSTEM_UI_FLAG_FULLSCREEN;
}
decorView.setSystemUiVisibility(visibility);
}
}, 100);
}
}
);
}
public static boolean canUseFingerprint(boolean doSnackbar) {
BaseActivity activity = App.get().getCurrentForegroundActivity();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//Get an instance of KeyguardManager and FingerprintManager//
KeyguardManager keyguardManager =
(KeyguardManager) App.get().getSystemService(KEYGUARD_SERVICE);
FingerprintManager fingerprintManager = (FingerprintManager) App.get().getSystemService(Context.FINGERPRINT_SERVICE);
//Check whether the device has a fingerprint sensor//
if (!fingerprintManager.isHardwareDetected()) {
// If a fingerprint sensor isn’t available, then inform the user that they’ll be unable to use your app’s fingerprint functionality//
if (doSnackbar)
SnackbarUtil.showShort(activity, R.string.fingerprint_device_unsupported);
return false;
}
//Check whether the user has granted your app the USE_FINGERPRINT permission//
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
// If your app doesn't have this permission, then display the following text//
if (doSnackbar)
SnackbarUtil.showShort(activity, R.string.fingerprint_permission);
return false;
}
//Check that the user has registered at least one fingerprint//
if (!fingerprintManager.hasEnrolledFingerprints()) {
// If the user hasn’t configured any fingerprints, then display the following message//
if (doSnackbar)
SnackbarUtil.showShort(activity, R.string.fingerprint_unconfigured);
return false;
}
//Check that the lockscreen is secured//
if (!keyguardManager.isKeyguardSecure()) {
// If the user hasn’t secured their lockscreen with a PIN password or pattern, then display the following text//
if (doSnackbar)
SnackbarUtil.showShort(activity, R.string.fingerprint_noLock);
return false;
}
return true;
} else {
if (doSnackbar)
SnackbarUtil.showShort(activity, R.string.fingerprint_device_unsupported);
return false;
}
}
@TargetApi(Build.VERSION_CODES.M)
public static boolean canUseKeychainAuthentication(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return false;
KeyguardManager keyguardManager = context.getSystemService(KeyguardManager.class);
return keyguardManager.isKeyguardSecure();
}