2

I have been developing my personal android app to store my passwords. (Since lastpass is paid for mobile). I currently use simple password authentication, but i would love to be able to take advantage of my fingerprint scanner.

In file AndroidManifest.xml:

<uses-permission android:name="android.permission.USE_FINGERPRINT" />

In file MainActivity.java:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//Fingerprint API only available on from Android 6.0 (M)
FingerprintManager fingerprintManager = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
if (!fingerprintManager.isHardwareDetected()) { 
    // Device doesn't support fingerprint authentication     
} else if (!fingerprintManager.hasEnrolledFingerprints()) { 
    // User hasn't enrolled any fingerprints to authenticate with 
} else { 
    // Everything is ready for fingerprint authentication 
}
}

but how do i actually authenticate phone owner using his fingerprints?

UPDATE:

I used Lubomir Babev's answer and its perfect. You fill the two methods that you implement onAuthSucceded, onAuthFailed to handle if authorizartion was successful and i also had to add some permission checks, because Android studio made me do it

public void startListening() {
    if (isFingerScannerAvailableAndSet()) {
        try {
            if (ActivityCompat.checkSelfPermission(mContext.getApplicationContext(), Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
            }
            mFingerprintManager.authenticate(null, mCancellationSignal, 0 /* flags */, mAuthenticationCallback, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

and

 public void startListening() {
    if (isFingerScannerAvailableAndSet()) {
        try {
            if (ActivityCompat.checkSelfPermission(mContext.getApplicationContext(), Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
            }
            mFingerprintManager.authenticate(null, mCancellationSignal, 0 /* flags */, mAuthenticationCallback, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Da2da2
  • 57
  • 2
  • 3
  • 13
  • 1
    `lastpass is paid` - well, while reasoning is fully up to you I'd say you must not consider your time valuable at all. Last pass costs just $12 a **year**. – Marcin Orlowski Jul 29 '16 at 17:47
  • 1
    If you're only doing it to replace lastpass, have a look at keepass. – nexus_2006 Jul 29 '16 at 17:47
  • @MarcinOrlowski I know what you are saying, but since I'm learning android rn anyways, its a great opportunity for me to discover more –  Jul 29 '16 at 18:10

2 Answers2

1

I create a custom handler class for fingerprint event :

import android.content.Context;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.os.CancellationSignal;

public class FingerprintHandler {
    private Context                                     mContext;
    private FingerprintManager                          mFingerprintManager = null;
    private CancellationSignal                          mCancellationSignal;
    private FingerprintManager.AuthenticationCallback   mAuthenticationCallback;
    private OnAuthenticationSucceededListener           mSucceedListener;
    private OnAuthenticationErrorListener               mFailedListener;

    public interface OnAuthenticationSucceededListener {
        void onAuthSucceeded();
    }

    public interface OnAuthenticationErrorListener {
        void onAuthFailed();
    }

    public void setOnAuthenticationSucceededListener (OnAuthenticationSucceededListener listener){
        mSucceedListener = listener;
    }

    public void setOnAuthenticationFailedListener(OnAuthenticationErrorListener listener) {
        mFailedListener = listener;
    }

    public FingerprintHandler(Context context){
        mContext = context;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mFingerprintManager = context.getSystemService(FingerprintManager.class);
            mCancellationSignal = new CancellationSignal();

            mAuthenticationCallback = new FingerprintManager.AuthenticationCallback() {
                @Override
                public void onAuthenticationError(int errorCode, CharSequence errString) {
                    super.onAuthenticationError(errorCode, errString);
                }

                @Override
                public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
                    super.onAuthenticationHelp(helpCode, helpString);
                }

                @Override
                public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
                    super.onAuthenticationSucceeded(result);
                    if( mSucceedListener != null )
                        mSucceedListener.onAuthSucceeded();
                }

                @Override
                public void onAuthenticationFailed() {
                    super.onAuthenticationFailed();
                    if (mFailedListener != null)
                        mFailedListener.onAuthFailed();
                }
            };
        }
    }

    public void startListening(){
        if (isFingerScannerAvailableAndSet() ) {
            try{
                mFingerprintManager.authenticate(null, mCancellationSignal, 0 /* flags */, mAuthenticationCallback, null);
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public void stopListening(){
        if ( isFingerScannerAvailableAndSet() ) {
            try {
                mCancellationSignal.cancel();
                mCancellationSignal = null;
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public boolean isFingerScannerAvailableAndSet(){
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
            return false;
        if( mFingerprintManager == null )
            return false;
        if( !mFingerprintManager.isHardwareDetected() )
            return false;
        if( !mFingerprintManager.hasEnrolledFingerprints())
            return false;

        return true;
    }
}

Then in your activity implement

FingerprintHandler.OnAuthenticationSucceededListener, FingerprintHandler.OnAuthenticationErrorListener

Create fingerprint parameter :

private FingerprintHandler mFingerprintHandler;

After that init this fingerprint handler in onCreate method :

mFingerprintHandler = new FingerprintHandler(this);
mFingerprintHandler.setOnAuthenticationSucceededListener(this);
mFingerprintHandler.setOnAuthenticationFailedListener(this);

You can check if fingerprint is avaivable and set in your activity with this :

    if( mFingerprintHandler.isFingerScannerAvailableAndSet() ){
        // show image or text or do something 
    }

You can handle your fingerprint response in implemented methods :

@Override
public void onAuthSucceeded() {
     //fingerprint auth succeded go to next activity (or do something)
}


@Override
public void onAuthFailed() {
    //fingerpring auth failed, show error toast (or do something)
}

And you are ready ! :) Don't forget to stop and start listening the fingerprint in onPause and onResume methods :

@Override
public void onResume() {
    super.onResume();
    mFingerprintHandler.startListening();
}

@Override
public void onPause() {
    super.onPause();
    mFingerprintHandler.stopListening();
}

Happy codding :)))

Lubomir Babev
  • 1,892
  • 1
  • 12
  • 14
  • Holy.. you are my hero, @LubomirBabev Everything works as a breeze. Only thing that i had to do is to add some permission checks , but thats it.. Thanks a lot will use this in every app from now on –  Jul 29 '16 at 18:30
0

You can use this. It supports all the locking mechanisms (PIN, Pattern, Password, Fingerprint Scanner).

Intent credentialsIntent = null;
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP)
    credentialsIntent = keyguardManager.createConfirmDeviceCredentialIntent("title", "desc"), context.getString(R.string.verification_desc));

//If phone lock is set, launch the unlock screen
if (credentialsIntent != null) {
    ((Activity) context).startActivityForResult(credentialsIntent, CREDENTIALS_RESULT);
}
//Phone is not locked
else {
    doTheWork();
}  

@Override
public void onActivityResult(int requestCode) {
    if (requestCode == CREDENTIALS_RESULT) {
        doTheWork;
    } 
    else 
        Log.e("TA", "Error");
}
Prerak Sola
  • 9,517
  • 7
  • 36
  • 67