Wiki
Clone wikiandroid-lockpattern / Quick-Use
The project is ended.
Old wiki pages can be found here: https://bitbucket.org/haibison/android-lockpattern/wiki/browse/
Integrating
-
Root
build.gradle
allprojects { repositories { ... // Please note that this URL only supports version 10+ maven { url 'https://haibison.bitbucket.io/maven-repos' } } }
-
app/build.gradle
(or similar)dependencies { ... implementation 'haibison.android:lockpattern:x.x.x' }
Remember to replace
x.x.x
with actual release version name.
Notes
All necessary component(s) are declared in the library manifest. They will be merged into your host project automatically by build tools. So you don't have to worry about them for now. But if you like to hack, we have some notes:
- There are many built-in themes, they start with
Alp_42447968.Theme.*
. You have to use one of them in order to let the library work properly. Because themes contain resources that the library needs.
Usage
Create new pattern
import haibison.android.lockpattern.LockPatternActivity;
...
// This is your preferred flag
private static final int REQ_CREATE_PATTERN = 1;
...
LockPatternActivity.IntentBuilder
.newPatternCreator(your-context)
.startForResult(your-activity-or-fragment, REQ_CREATE_PATTERN);
For more secure, you might want to use Encryption.
And to get the result
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_CREATE_PATTERN: {
if (resultCode == RESULT_OK) {
final char[] pattern = data.getCharArrayExtra(LockPatternActivity.EXTRA_PATTERN);
...
}
break;
}// REQ_CREATE_PATTERN
}
}
Let the user identify himself with lock pattern
...
// This is your preferred flag
private static final int REQ_ENTER_PATTERN = 2;
...
final char[] savedPattern = ...
LockPatternActivity.IntentBuilder
.newPatternComparator(your-context, savedPattern)
.startForResult(your-activity-or-fragment, REQ_ENTER_PATTERN);
and:
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_ENTER_PATTERN: {
// NOTE that there are 4 possible result codes!!!
switch (resultCode) {
case RESULT_OK:
// The user passed
break;
case RESULT_CANCELED:
// The user cancelled the task
break;
case LockPatternActivity.RESULT_FAILED:
// The user failed to enter the pattern
break;
case LockPatternActivity.RESULT_FORGOT_PATTERN:
// The user forgot the pattern and invoked your recovery Activity.
break;
}
// In any case, there's always a key EXTRA_RETRY_COUNT, which holds the number
// of tries that the user did.
final int retryCount = data.getIntExtra(LockPatternActivity.EXTRA_RETRY_COUNT, 0);
break;
}//REQ_ENTER_PATTERN
}
}
Notes
-
You can tell the library to store newly created pattern into
SharedPreferences
. So you don't have to specify pattern for actionACTION_COMPARE_PATTERN
. By default, this flag is off. To turn it on:import haibison.android.lockpattern.utils.AlpSettings; ... AlpSettings.Security.setAutoSavePattern(your-context, true);
-
ACTION_COMPARE_PATTERN
: there are 4 possible result codes like the sample above. ACTION_VERIFY_CAPTCHA
: there are 3 possible result codes like the sample above, excludingRESULT_FORGOT_PATTERN
.
Tips
Themes
-
To turn
LockPatternActivity
into a dialog, open yourAndroidManifest.xml
and add this:<?xml version="1.0" encoding="utf-8"?> <manifest package="..." xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"/> <application .../> <activity android:name="haibison.android.lockpattern.LockPatternActivity" android:theme="@style/Alp_42447968.Theme.Dialog.Dark" tools:replace="android:theme"/> ...
-
To change theme in runtime, you must set theme in
AndroidManifest.xml
like above. If you don't do that and set theme to dialog via code, the background ofLockPatternActivity
will be not transparent. A correct example:LockPatternActivity.IntentBuilder .newPatternCreator(your-context) .setTheme(R.style.Alp_42447968_Theme_Dialog_Dark) .startForResult(...);
Stealth Mode
In stealth mode, there will be no visible feedback as the user enters the pattern. To turn it on, use helper class AlpSettings.Display
. By default this mode is off.
import haibison.android.lockpattern.utils.AlpSettings;
...
AlpSettings.Display.setStealthMode(your-context, true);
CAPTCHA
To let the library generate a random CAPTCHA pattern and have the user verify it, you can use action ACTION_VERIFY_CAPTCHA
. Default wired dots is 4
, you can change it with helper class AlpSettings.Display
.
...
AlpSettings.Display.setCaptchaWiredDots(your-context, 9);
LockPatternActivity.IntentBuilder
.newCaptchaVerifier(your-context)
.startForResult(your-activity-or-fragment, your-request-code);
Forgot pattern?
If you use ACTION_COMPARE_PATTERN
, the user might forget his/her pattern. So, you can use a PendingIntent
of your Activity
to help the user recover the pattern when he/she needs. For example:
final PendingIntent piForgotPattern = ...
LockPatternActivity.IntentBuilder
.newPatternComparator(your-context, your-saved-pattern)
.setPendingIntentForgotPattern(piForgotPattern)
.startForResult(your-activity-or-fragment, your-request-code);
...
When the user taps the button Forgot pattern? (in LockPatternActivity
), the library makes a call to start your pending intent, then finishes itself with RESULT_FORGOT_PATTERN
.
Action Bar Icons
Version 3+ includes an icon set for action bar, which has dark and light icons:
If you use one of built-in themes (R.style.Alp_42447968_Theme_*
) for your activity, you can make a reference to the icon like:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_lockpattern"
android:icon="?attr/alp_42447968_ic_action_lockpattern"
... />
</menu>
Or if you use your own themes, use the icons directly instead of that reference. For example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme.Dark">
<item name="alp_ic_action_lockpattern">@drawable/alp_42447968_ic_action_lockpattern_light</item>
</style>
<style name="AppTheme.Light">
<item name="alp_ic_action_lockpattern">@drawable/alp_42447968_ic_action_lockpattern_dark</item>
</style>
</resources>
Other Notes
Default language
Default language is English. It is located at res/values/strings.xml
. Actually it is a copy of res/values-en/strings.xml
. So you can change it to your preferred language.
If you'd like to contribute your translation, we thank you :-)
AndroidManifest.xml
All configurations with helper class AlpSettings
(and all of its nested classes) must be called before you start LockPatternActivity
. But you can configure settings in an activity and call LockPatternActivity
in another activity.
You can also configure settings directly via your app's AndroidManifest.xml
.
Updated