Implementing a laptop with fingerprint encryption (Android) Part Two

Source: Internet
Author: User
Tags asymmetric encryption

The above basically completed the entire notebook Notes feature implementation, followed by the implementation of fingerprint identification encryption and some small considerations.

First of all to determine whether the phone has the fingerprint identification of the hardware function and whether the user to turn on fingerprint recognition.

     Public BooleanIsfinger () {//used to check for fingerprint recognition permissions        if(Checkcallingorselfpermission (Manifest.permission.USE_FINGERPRINT) = =packagemanager.permission_granted) {            if(!manager.ishardwaredetected ()) {Toast.maketext ( This, "No fingerprint identification module", Toast.length_short). Show (); return false; }            if(!mkeymanager.iskeyguardsecure ()) {Toast.maketext ( This, "No lock screen passcode is turned on", Toast.length_short). Show (); return false; }            if(!manager.hasenrolledfingerprints ()) {Toast.maketext ( This, "no fingerprint.", Toast.length_short). Show (); return false; }        }        return true; }

Start fingerprint recognition

     Public void startlistening (Fingerprintmanager.cryptoobject cryptoobject) {        ///Android Studio, Without this will be an error        if (activitycompat.checkselfpermission (This, Manifest.permission.USE_ Fingerprint)! = packagemanager.permission_granted) {            Toast.maketext (this, "No fingerprint recognition permission" , Toast.length_short). Show ();         }        (null);    }

First, check whether the user has given the software the right to use fingerprint recognition, and then make a fingerprint identification request. Where the authentication method requires five parameters, as follows

    @RequiresPermission (use_fingerprint)    publicvoid  Authenticate (@Nullable Cryptoobject Crypto, @Nullable cancellationsignal Cancel,            int  flags, @NonNull Authenticationcallback callback, @Nullable Handler Handler) {        authenticate (crypto, cancel, flags, callback, Handler , Userhandle.myuserid ());    }

The first argument is that the cryptographic object can use both symmetric and asymmetric encryption, or not use encryption directly with NULL

The second parameter is an object that is used to cancel the authentication, and the direct new Cancellationsignal () is available.

The third parameter is an optional flag, recommended to be 0

The fourth parameter is an object that accepts an authentication event

The fifth parameter is an optional callback event handler

The following is the handling of the fourth authentication event, with each result processed as follows

Fingerprintmanager.authenticationcallback mselfcancelled =NewFingerprintmanager.authenticationcallback () {@Override Public voidOnauthenticationerror (intErrorCode, Charsequence errstring) {            //But after multiple fingerprint password validation errors, enter this method, and the fingerprint verification cannot be called in a short timeToast.maketext (mainactivity. This, Errstring, Toast.length_short). Show ();        Showauthenticationscreen (); } @Override Public voidOnauthenticationhelp (intHelpcode, Charsequence helpstring) {//error occurred, may be problem with finger move too fast
Toast.maketext (mainactivity. This, helpstring, Toast.length_short). Show (); } @Override Public voidonauthenticationsucceeded (fingerprintmanager.authenticationresult result) {
Successful authentication, access to encrypted documents
Noteadapter.opennote (); } @Override Public voidonauthenticationfailed () {Toast.maketext (mainactivity. This, "Fingerprint recognition failed", Toast.length_short). Show (); } };

To prevent multiple validations from succeeding, but you want to unlock the lock screen password, you can use the

    Private voidShowauthenticationscreen () {Intent Intent= Mkeymanager.createconfirmdevicecredentialintent ("Finger", "fingerprint recognition failed too many times, please enter the lock screen password"); if(Intent! =NULL) {Startactivityforresult (intent, request_code_confirm_device_credentials); }} @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        if(Requestcode = =request_code_confirm_device_credentials) {            //Challenge completed, proceed with using cipher            if(ResultCode = =RESULT_OK)            {noteadapter.opennote (); } Else{Toast.maketext ( This, "Password verification failed", Toast.length_short). Show (); }        }    }

So the entire fingerprint unlock process is complete.

Here's a look at some of the areas of the project that need attention

1. About the new note refresh issue, before the noteactivity ondestory to store notes, in Noteadaper GetItemCount refresh the number of notes in the database, to display. However, when I went back to the main page, I found that the most new notes didn't appear on the list, because the activity lifecycle was recorded by log, It is found that the getitemcount of Noteadapter is executed earlier than the Ondestory function of Noteactivity and later than the OnPause function, so storenote is placed in OnPause instead of ondestory.

2. Modify the note can be directly in the mainactivity onresume function to write noetadapter.notifydatasetchanged on it.

2. When creating a new note, the user may simply accidentally click on the new note and don't want to add a new one, so you can make a simple decision when storing the note and whether the content is empty.

3. The main page of the note content display problem, you can set the following properties of TextView, to display some of the content can be

        android:maxlines= "1"        android:lines= "2"        android:ellipsize= "End"

At the same time, it is important to note that when loading the content of the encrypted information to be hidden, where I unified the encrypted information as "core secret!!!".

Implementing a laptop with fingerprint encryption (Android) Part Two

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.