PinLock

Additional

Language
Java
Version
v1.4.0 (Jun 17, 2016)
Created
Nov 20, 2015
Updated
Mar 28, 2017 (Retired)
Owner
Manu Sunny (manupsunny)
Contributor
Manu Sunny (manupsunny)
1
Activity
Badge
Generate
Download
Source code

PinLock

An Android library which can be used for implementing PIN lock mechanism in Android applications.

Usage

First, add PinLock dependency to your build.gradle. PinLock is available in both Maven and jCenter.

compile "com.manusunny:pinlock:+"
1. Setting PIN

Create a new class by extending SetPinActivity.java. And implement method onPinSet(),

public class SetPinActivitySample extends SetPinActivity {
    public void onPinSet(String pin){
        //Save 'pin' as SharedPreference
    }
}

That's it. Now you can start this activity using startActivityForResult(). And handle the results in onActivityResult(). Activity will return one of these result codes:

  1. PinListener.SUCCESS
  2. PinListener.CANCELLED
2. Confirming PIN

Create a new class by extending ConfirmPinActivity.java. And implement two methods,

public class ConfirmPinActivitySample extends SetPinActivity {
    @Override
    public boolean isPinCorrect(String pin) {
        String currentPin = // get it from SharedPreferences
        return pin.equals(currentPin);
    }

    @Override
    public void onForgotPin() {
        // handle forgot PIN scenario
    }
}

Now you can start this activity using startActivityForResult(). And handle the results in onActivityResult(). Activity will return one of these result codes:

  1. PinListener.SUCCESS
  2. PinListener.CANCELLED
  3. PinListener.FORGOT

Customizing the Plugin

1. Customizing Styles

All styles in this library are configurable. Give required styles in res/values/styles.xml

Example:

<resources>
    <style name="PinLock" >
        <item name="pinLength">4</item>
        <item name="backgroundColor">#FFFFFF</item>

        <item name="keypadButtonShape">@drawable/rectangle</item>
        <item name="keypadTextColor">#000000</item>
        <item name="keypadTextSize">20</item>
        <item name="keypadWidth">200dp</item>
        <item name="keypadHeight">230dp</item>
        <item name="keypadVerticalSpacing">2dp</item>
        <item name="keypadHorizontalSpacing">2dp</item>
        <item name="keypadClickAnimationDuration">100</item>

        <item name="cancelForgotTextColor">#000000</item>
        <item name="cancelForgotTextSize">14</item>

        <item name="infoTextColor">#000000</item>
        <item name="infoTextSize">20</item>

        <item name="vibrateOnClick">true</item>
        <item name="vibrateDuration">20</item>

        <item name="statusEmptyBackground">@drawable/dot_empty</item>
        <item name="statusFilledBackground">@drawable/dot_filled</item>
        <item name="statusDotDiameter">8dp</item>
        <item name="statusDotSpacing">10dp</item>
    </style>
</resources>
2. Customizing Labels

You can customize the messages and labels used in the library. Give new texts in res/values/strings.xml

Example:

<resources>
    <string name="message_enter_pin">Enter PIN</string>
    <string name="message_enter_new_pin">Enter new PIN</string>
    <string name="message_confirm_pin">Re enter PIN</string>
    <string name="message_invalid_pin">Invalid PIN. Try again</string>
    <string name="message_pin_mismatch">PIN mismatch. Try again</string>

    <string name="button_cancel">Cancel</string>
    <string name="button_forgot_pin">Forgot PIN</string>
</resources>

Supporting Multiple Languages

You can support multiple languages by providing texts in different languages. If texts are not available for the selected language, English will be used as the fallback language. Add res/values-<language_code> folder for each new language, and provide tranlated contents in strings.xml file.

Folder Structure :

|res
    |values       // Default language (en)
        |strings.xml
    |values-es    // Spanish
        |strings.xml
    |values-de    // German
        |strings.xml

Example:

Sample file for Spanish res/values-es/strings.xml

<resources>
    <string name="message_enter_pin">Ingrese el PIN</string>
    <string name="message_enter_new_pin">Ingrese un nuevo PIN</string>
    <string name="message_confirm_pin">Confirme el PIN</string>
    <string name="message_invalid_pin">PIN inválido. Intente de nuevo</string>
    <string name="message_pin_mismatch">El PIN no coincide. Intente de nuevo</string>

    <string name="button_cancel">Cancelar</string>
    <string name="button_forgot_pin">Olvidé mi PIN</string>
</resources>

A list of languages which are currently supported by Android along with their codes are available here.

License

Copyright 2016 © Manu Sunny

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Sample Screenshots

SetPin ConfirmPin ChangePin