Search code examples
javaandroidandroid-fingerprint-api

android - How to call a class extending Asynctask in the Main Activity from FIngerPrintHelper class


I am implementing fingerprint authentication in my application. I have successfully authenticated fingerprint in the application. But the problem is, I want to call Asynctask class, which is inside the Main Activity, from a Finger Print Helper class.

Below is the code for FingerPrintHelper.java class:

public class FingerprintHandler extends FingerprintManager.AuthenticationCallback {


    private Context context;


    // Constructor
    public FingerprintHandler(Context mContext) {
        context = mContext;
    }


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


    @Override
    public void onAuthenticationError(int errMsgId, CharSequence errString) {
        this.update("Fingerprint Authentication error\n" + errString, false);
    }


    @Override
    public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
        this.update("Fingerprint Authentication help\n" + helpString, false);
    }


    @Override
    public void onAuthenticationFailed() {
        this.update("Fingerprint Authentication failed.", false);
        Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(1000);

        TextView lblFingerPrintError = (TextView) ((Activity)context).findViewById(R.id.lblFingerPrintError);
        lblFingerPrintError.setVisibility(View.VISIBLE);
        lblFingerPrintError.setText("Finger print did not match");
    }


    @Override
    public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
        this.update("Fingerprint Authentication succeeded.", true);


        context.startActivity(new Intent(context, HomePage.class));
    }


    public void update(String e, Boolean success){
        if(success){
            Log.i("WW", "Matched");
        }
    }
}

Inside the method, onAuthenticationSucceeded(), I want to call Asynctask Class which is the Main Activity.

Please respond if someone has the solution to this.

Thank You.


Solution

  • you can pass a callback to your calling activity to know about the authentication completion as follows.

    callback interface

        public interface CallBackInterface  {
        void onAuthenticationSucceed();
    }
    

    when you call FingerprintHandler from the activity just pass the reference using method or constructor like this.

    // Constructor
    public FingerprintHandler(Context mContext,CallBackInterface callback) {
        context = mContext;
        this.callback = callback;
    }
    

    now you can use this reference to notify the calling activity about the completion of the authentication as follows.

     @Override
        public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
            this.update("Fingerprint Authentication succeeded.", true);
            //notify the caller about success
            callback.onAuthenticationSucceed();
        }
    

    final code for FingerprintHandler as follows.

    public class FingerprintHandler extends FingerprintManager.AuthenticationCallback {
    
    
    private Context context;
    private CallBackInterface callback;
    
    // Constructor
    public FingerprintHandler(Context mContext, CallBackInterface callback) {
        context = mContext;
        this.callback = callback;
    }
    
    
    public void startAuth(FingerprintManager manager, FingerprintManager.CryptoObject cryptoObject) {
        CancellationSignal cancellationSignal = new CancellationSignal();
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        manager.authenticate(cryptoObject, cancellationSignal, 0, this, null);
    }
    
    
    @Override
    public void onAuthenticationError(int errMsgId, CharSequence errString) {
        this.update("Fingerprint Authentication error\n" + errString, false);
    }
    
    
    @Override
    public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
        this.update("Fingerprint Authentication help\n" + helpString, false);
    }
    
    
    @Override
    public void onAuthenticationFailed() {
        this.update("Fingerprint Authentication failed.", false);
        Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(1000);
    
        TextView lblFingerPrintError = (TextView) ((Activity)context).findViewById(R.id.lblFingerPrintError);
        lblFingerPrintError.setVisibility(View.VISIBLE);
        lblFingerPrintError.setText("Finger print did not match");
    }
    
    
    @Override
    public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
        this.update("Fingerprint Authentication succeeded.", true);
        //here notify the caller about the success
        callback.onAuthenticationSucceed();
    
       // context.startActivity(new Intent(context, HomePage.class));
    }
    
    
    public void update(String e, Boolean success){
        if(success){
            Log.i("WW", "Matched");
        }
    }
    

    }

    and in your activity where you have passed the reference using constructor you must have to override the onAuthenticationSucceed() so now call your async task here

    @Override
    public void onAuthenticationSucceed(){
      //here start your async task.
    }