PFLockScreen Library for Android

Beauty Coder
Beauty Coder
Published in
4 min readMar 20, 2018

I created a PFLockScreen library — Lock screen library you can use in your application. It can be applications that encrypt some sensitive data like notes, pictures, etc or bank applications. For example, medical applications in Japan required additional security level as well.

If you would like to use it but have some issues or ideas fill free to create an issue on GitHub.

How the library works. We have typical UI to input pin code. That also allowed use fingerprint instead of a pin code.

Because AndroidKeystore doesn’t support custom pin for authorization I decided to have to parallel way to unlock app.

How library works inside

Using pin code.

If you don’t know how keysore and fingerprint API in Android works you can read about in my previous article

A user creates pin code. Using keystore and cipher object we encrypt the code. In a key generation, we set required authentication parameter to false. So we won’t need to use a fingerprint to unlock application (because in this case, we need to input pin code and use fingerprint at the same time).

Library encrypts pin code and returns an encrypted string.

For authorization user inputs pin code. The library compares inputted code with the encrypted one and if they match application is unlocked.

To use fingerprint we just will use FingerprintManager without a keystore. Because in our case password and fingerprint methods are separated we don’t actually need to encrypt/decrypt anything. So we just use Fingerprint API to confirm user’s identity. And if it’s confirmed the library returns unlocked state.

So the library can decrypt pin code without user’s fingerprint because we want to have two ways to unlock application

If you don’t need the UI part. You can use only encryption/decryption part of the library.

How to use the library

Add library to your project

allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
compile 'com.github.thealeksandr:PFLockScreen-Android:1.0.0-beta2'
}

Create pin code

Creating lock screen fragment in create mode.

PFLockScreenFragment fragment = new PFLockScreenFragment();
PFFLockScreenConfiguration.Builder builder = new PFFLockScreenConfiguration.Builder(this)
.setMode(PFFLockScreenConfiguration.MODE_CREATE);
fragment.setConfiguration(builder.build());
fragment.setCodeCreateListener(new PFLockScreenFragment.OnPFLockScreenCodeCreateListener() {
@Override
public void onCodeCreated(String encodedCode) {
//TODO: save somewhere;
}
});
//TODO: show fragment;

After user created pin code. The library will encode it using Android Key Store and return an encoded string in PFLockScreenFragment.OnPFLockScreenCodeCreateListener. All you have to do is save encoded string somewhere — database, SharedPreferences, Android Account etc.

Show authorization screen

Creating lock screen fragment in authorization mode is same as in creation mode, but instead of MODE_CREATE use MODE_AUTH.

PFFLockScreenConfiguration.Builder(this).setMode(PFFLockScreenConfiguration.MODE_AUTH);

Configure screen

PFFLockScreenConfiguration.Builder builder = new PFFLockScreenConfiguration.Builder(this)
.setTitle("Unlock")
.setUseFingerprint(true).
.setMode(PFFLockScreenConfiguration.MODE_AUTH)
.setCodeLength(6)
.setLeftButton("Can't remeber",
new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});

setTitle(String) — set custom string on the top of the screen. setUseFingerprint(boolean) — by default fingerprint button will be shown for all device 23+ with a fingerprint sensor. If you don’t want use fingerprint at all set false. setMode(PFLockScreenMode) — MODE_CREATE or MODE_AUTH. See details above. setCodeLength(int) — set the length of the pin code. By default, length is 4. Minimum length is 4. setLeftButton(String, View.OnClickListener) — set string for the left button and ClickListener.

Check if pin code encryption key exist

boolean isExist = PFFingerprintPinCodeHelper.getInstance().isPinCodeExist();

An encryption key is needed to encode/decode pin code and stored in Android KeyStore.

Delete pin code encryption key.

You need to delete encryption key if you delete/reset pin code.

PFFingerprintPinCodeHelper.getInstance().delete();

If you use library without UI. Encode Pin and Check Pin

You Don’t need to use this methods with library UI. Because this methods will be called automatically. But if you only want to use encryption/decryption part of the library use: java PFFingerprintPinCodeHelper.getInstance().encodePin(Context context, String pin);

PFFingerprintPinCodeHelper.getInstance().checkPin(Context context, String encodedPin, String pin);

UI Customization

You can customize buttons, backgrounds etc. To do that use attributes in your activity theme:

pf_key_button — style object for key buttons (0–9) pf_lock_screen — style object for the background. Use it to set custom background. pf_fingerprint_button — style object for fingerprint button. You can set custom drawable, paddings, etc. pf_delete_button — style object for delete/backspace button. You can set custom drawable, paddings, etc. pf_code_view — style object to customize code view. (The view from the top of the screen). The view itself is set of check boxes. To customize it use a custom selector with checked states.

Examples:

<style name=”AppTheme” parent=”Theme.AppCompat.Light.NoActionBar”>
<! — Some of your styles. →
<item name=”pf_key_button”>@style/MyLockScreenButtonStyle</item>
<item name=”pf_fingerprint_button”>@style/MyLockScreenFingerprintButtonStyle</item>
<item name=”pf_delete_button”>@style/MyLockScreenDeleteButtonStyle</item>
<item name=”pf_lock_screen”>@style/MyLockScreenStyle</item>
<item name=”pf_code_view”>@style/MyLockScreenCodeStyle</item>
</style>
<style name=”MyLockScreenStyle”>
<item name=”android:background”>@drawable/screen_background</item>
</style>
<style name=”MyLockScreenButtonStyle”>
<item name=”android:textColor”>@android:color/white</item>
<item name=”android:textSize”>18dp</item>
<item name=”android:foreground”>@drawable/key_foreground</item>
</style>
<style name=”MyLockScreenFingerprintButtonStyle”>
<item name=”android:src”>@drawable/fingerprint_icon</item>
</style>
<style name=”MyLockScreenDeleteButtonStyle”>
<item name=”android:src”>@drawable/delete_lockscreen</item>
</style>
<style name=”MyLockScreenCodeStyle”>
<item name=”android:button”>@drawable/code_selector</item>
</style>

The only important thing you can’t change right now is the size of keys. But it’s coming.

You can find library here PFLockScreen library.

If you like the article please press 👏 button.

--

--