0

I'm following this tutorial.Right now, I'm trying to loop the fingerprint authentication part so that I can keep reauthenticate user fingerprint. I've tried to use thread in onStart() and onCreate() to while loop the authentication but the app is stuck in both cases.

Original code that can authenticate only one time

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

    fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
    keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);

    if (!keyguardManager.isKeyguardSecure()){
        Toast.makeText(this,
                "Lock screen security is not enable in Settings", Toast.LENGTH_LONG).show();
        return;
    }

    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED){
        Toast.makeText(this,
                "Fingerprint authentication permission is not enabled", Toast.LENGTH_LONG).show();
        return;
    }

    if (!fingerprintManager.hasEnrolledFingerprints()){
        Toast.makeText(this, "Register at least one fingerprint in Settings", Toast.LENGTH_LONG).show();
        return;
    }

    generateKey();
    if (cipherInit()){
        cryptoObject = new FingerprintManager.CryptoObject(cipher);
        FingerprintHandler helper = new FingerprintHandler(this);
        helper.startAuth(fingerprintManager, cryptoObject);

    }

}

Thread in onStart() / onCreate() that failed

    @Override
    protected void onStart() {
        super.onStart();
        new Thread(new Runnable(){
        public void run() {
            while(true)
                {
                    try {
                    Thread.sleep(50);
                    if (cipherInit()) {
                         cryptoObject = new FingerprintManager.CryptoObject(cipher);
                         FingerprintHandler helper = new FingerprintHandler(MainActivity.this);
                         helper.startAuth(fingerprintManager, cryptoObject);

                 }} catch (InterruptedException e){
                         e.printStackTrace();
            }
        }
    }
}).start();}

Other than using thread, I also tried to use AsyncTask to do the while loop for me. This is my attempt in creating the class. My problem is that the cipherInit() resides in MainActivity.java and how can I invoke the method from my Looping class?

Looping.java

    import android.hardware.fingerprint.FingerprintManager;
    import android.os.AsyncTask;

    public class Looping extends AsyncTask<Object,Void,Void> {
        FingerprintManager fingerprintManager;
        FingerprintManager.CryptoObject cryptoObject;
        Cipher cipher;
        @Override
        protected Void doInBackground(Void... arg0) {
            cipher = (Cipher) arg0[0];
            while(true) {
                if (cipherInit()) {
                    cryptoObject = new FingerprintManager.CryptoObject(cipher);
                    FingerprintHandler helper = new FingerprintHandler(MainActivity.this);
                    helper.startAuth(fingerprintManager, cryptoObject);

        }
    }
}}

MainActivity

            Looping loop = new Looping();
            loop.execute(cipher, null, null);

This is my first personal project and I'm still relatively new with the whole Android structure. I'll really appreciate any input from you all. Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Cliff
  • 1,468
  • 4
  • 14
  • 18

1 Answers1

0

You should not need the secondary thread or loop to do the authentication. The call to FingerprintManager.authenticate() is done in your FingerprintHandler (assuming you have the same code as the tutorial you cited). This is an asyc operation and the handler (FingerprintManager.AuthentciationCallback) is called back when the auth succeeds or fails. You need to take action based on that success/failure rather than poll in a while loop. That callback will occur on your main thread.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
  • Are you saying that I should have to "continue" or "loop" or "recontinue" my authentication in the `onAuthenticationSucceeded()` and `onAuthenticationFailed()` in the `FingerprintHandler`? How can I go along and reinvoke the whole thing or handle the callback? I'm not too familiar with this async callback. – Cliff Jul 29 '16 at 14:55
  • It's up to you to determine the action to be taken based on the callback method which is called. But it is an asynchronous operation, you should never just loop waiting on something. – Larry Schiefer Jul 29 '16 at 15:05