خرید بک لینک

Vote count: 0

I made a Login. There are two ways to login.
1. Use Username and Password
2. Use Username and Fingerprint
When the User successfully loged in he will come to the HomeActivity.

Now I have the Problem that if the User didn't login with Username & Fingerprint the Fingerprint from my user will be checked in my HomeActivity again, when User is already loged in...So, I must disable the Fingerprint, but how?

That's a part of my LoginActivity:

public class LoginActivity extends AppCompatActivity {

    private KeyStore keyStore;
    private static final String KEY_NAME = "fingerprint";
    private Cipher cipher;
    private Button loginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        final FingerprintHandler mHandler = new FingerprintHandler(this, mListener);
        KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
        FingerprintManager fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);

        if (!fingerprintManager.isHardwareDetected()) {
            Toast.makeText(LoginActivity.this, "You're device doesn't hav a Fingerprintsensor!", Toast.LENGTH_LONG).show();
        } else {
            // Checks whether fingerprint permission is set on manifest
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(LoginActivity.this, "Fingerprint Authentication not enabled!", Toast.LENGTH_LONG).show();
            } else {
                // Check whether at least one fingerprint is registered
                if (!fingerprintManager.hasEnrolledFingerprints()) {
                    Toast.makeText(LoginActivity.this, "Register at least one fingerprint in Settings!", Toast.LENGTH_LONG).show();
                } else {
                    // Checks whether lock screen security is enabled or not
                    if (!keyguardManager.isKeyguardSecure()) {
                        Toast.makeText(LoginActivity.this, "Lock screen security not enabled in Settings", Toast.LENGTH_LONG).show();
                    } else {
                        generateKey();

                        if (cipherInit()) {
                            FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(cipher);
                            FingerprintHandler helper = new FingerprintHandler(this, mListener);
                            helper.startAuth(fingerprintManager, cryptoObject);
                        }
                    }
                }
            }
        }
loginButton = (Button) findViewById(R.id.button);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //do a few other things
                //login User 

                //!!IMPORTANT!!
                //now i must disable the fingerprint, because this user logged in with username & password and doesn't need the fingerprint...  

            }
        });
    }

    @TargetApi(Build.VERSION_CODES.M)
    protected void generateKey() {
        try {
            keyStore = KeyStore.getInstance("AndroidKeyStore");
        } catch (Exception e) {
            e.printStackTrace();
        }


        KeyGenerator keyGenerator;
        try {
            keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
        } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
            throw new RuntimeException("Failed to get KeyGenerator instance", e);
        }


        try {
            keyStore.load(null);
            keyGenerator.init(new
                    KeyGenParameterSpec.Builder(KEY_NAME,
                    KeyProperties.PURPOSE_ENCRYPT |
                            KeyProperties.PURPOSE_DECRYPT)
                    .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                    .setUserAuthenticationRequired(true)
                    .setEncryptionPaddings(
                            KeyProperties.ENCRYPTION_PADDING_PKCS7)
                    .build());
            keyGenerator.generateKey();
        } catch (NoSuchAlgorithmException |
                InvalidAlgorithmParameterException
                | CertificateException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    @TargetApi(Build.VERSION_CODES.M)
    public boolean cipherInit() {
        try {
            cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            throw new RuntimeException("Failed to get Cipher", e);
        }


        try {
            keyStore.load(null);
            SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, null);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            return true;
        } catch (KeyPermanentlyInvalidatedException e) {
            return false;
        } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
            throw new RuntimeException("Failed to init Cipher", e);
        }
    }



    public LoginListener mListener = new LoginListener() {
        @Override
        public void onLoginSuccess() {
            //do the login with my server
            //than go to HomeActivity
        }

        public boolean checkIfUsernameIsSet() {
            usernameField2 = (EditText) findViewById(R.id.editText8);
            if (usernameField2.getText().toString().isEmpty() == true) {
                return false;
            } else {
                return true;
            }
        }
    };
}

And here is my FingerprintHandler.java:

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.fingerprint.FingerprintManager;
import android.os.CancellationSignal;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;

public class FingerprintHandler extends FingerprintManager.AuthenticationCallback {

    private Context context;
    private LoginActivity.LoginListener mListener;

    // Constructor
    public FingerprintHandler(Context mContext, LoginActivity.LoginListener listener) {
        context = mContext;
        mListener = listener;
    }

    public void startAuth(FingerprintManager manager, FingerprintManager.CryptoObject cryptoObject) {
        CancellationSignal mCancellationSignal = new CancellationSignal();
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        manager.authenticate(cryptoObject, mCancellationSignal, 0, this, null);
    }

    @Override
    public void onAuthenticationError(int errMsgId, CharSequence errString) {
        Toast.makeText((Activity)context, "Fingerprint Authentication error.", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
        Toast.makeText((Activity)context, "Fingerprint Authentication help.", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onAuthenticationFailed() {
        if (mListener.checkIfUsernameIsSet()) {
            Toast.makeText((Activity) context, "Fingerprint Authentication failed.", Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText((Activity)context, "Please fill in Username", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
        if (mListener != null) {
            if (mListener.checkIfUsernameIsSet()) {
                mListener.onLoginSuccess();
            }
            else{
                Toast.makeText((Activity)context, "Please fill in Username", Toast.LENGTH_LONG).show();
            }
        }
        else{
            Toast.makeText((Activity)context, "Ups! Something went wrong.", Toast.LENGTH_LONG).show();
        }
    }
}
asked 35 secs ago

برچسب: نویسنده: استخدام کار تاريخ: دوشنبه 11 ارديبهشت 1396 ساعت: 15:51

صفحه بندی