Java Code Examples for android.app.KeyguardManager#isKeyguardSecure()

The following examples show how to use android.app.KeyguardManager#isKeyguardSecure() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VolumePanel.java    From Noyze with Apache License 2.0 7 votes vote down vote up
/** 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;
}
 
Example 2
Source File: FingerprintActivity.java    From AndroidSamples with Apache License 2.0 6 votes vote down vote up
/**
 * 判断是否支持指纹(先用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;
}
 
Example 3
Source File: MainActivity.java    From android-ConfirmCredential with Apache License 2.0 6 votes vote down vote up
@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();
        }
    });
}
 
Example 4
Source File: DeviceAvailability.java    From react-native-keychain with MIT License 6 votes vote down vote up
/** 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;
}
 
Example 5
Source File: BiometricActivity.java    From cordova-plugin-fingerprint-aio with MIT License 6 votes vote down vote up
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);
    }
}
 
Example 6
Source File: IntroScreenActivity.java    From andOTP with MIT License 5 votes vote down vote up
@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;
    }
}
 
Example 7
Source File: CredentialsPreference.java    From andOTP with MIT License 5 votes vote down vote up
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)));
}
 
Example 8
Source File: FingerprintHelper.java    From xmrwallet with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 9
Source File: RNDeviceModule.java    From react-native-device-info with MIT License 5 votes vote down vote up
@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;
}
 
Example 10
Source File: FingerPrint.java    From leafpicrevived with GNU General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example 11
Source File: LockscreenUtil.java    From Android-LockScreenSample-DisableHomeButtonKey with Apache License 2.0 5 votes vote down vote up
public boolean isStandardKeyguardState() {
    boolean isStandardKeyguqrd = false;
    KeyguardManager keyManager = (KeyguardManager) mContext.getSystemService(mContext.KEYGUARD_SERVICE);
    if (null != keyManager) {
        isStandardKeyguqrd = keyManager.isKeyguardSecure();
    }

    return isStandardKeyguqrd;
}
 
Example 12
Source File: AppProtectionPreferenceFragment.java    From deltachat-android with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example 13
Source File: DeviceCredentialUtil.java    From nextcloud-notes with GNU General Public License v3.0 5 votes vote down vote up
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;
    }
}
 
Example 14
Source File: VolumePanel.java    From Noyze with Apache License 2.0 5 votes vote down vote up
/** 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;
}
 
Example 15
Source File: SmartLockHelper.java    From samples-android with Apache License 2.0 4 votes vote down vote up
public static boolean isKeyguardSecure(Context context) {
    KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    return keyguardManager.isKeyguardSecure();
}
 
Example 16
Source File: KeyguardActivity.java    From AcDisplay with GNU General Public License v2.0 4 votes vote down vote up
@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);
                }
            }
    );
}
 
Example 17
Source File: FingerprintUtil.java    From masterpassword with GNU General Public License v3.0 4 votes vote down vote up
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;
    }
}
 
Example 18
Source File: SharedPreferenceVaultFactory.java    From Android-Vault with Apache License 2.0 4 votes vote down vote up
@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();
}
 
Example 19
Source File: SmartLockHelper.java    From samples-android with Apache License 2.0 4 votes vote down vote up
public static boolean isKeyguardSecure(Context context) {
    KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    return keyguardManager.isKeyguardSecure();
}
 
Example 20
Source File: KeyguardUtils.java    From AcDisplay with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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();
}