• Stars
    star
    689
  • Rank 65,628 (Top 2 %)
  • Language
    Java
  • Created almost 10 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Material Design Pattern Lock with auth flow implementation

PatternLock

A Material Design Pattern Lock library with auth flow implementation.

Why PatterLock?

  • Battle-tested framework implementation with necessary modifications.
  • Supports XML attributes for customization.
  • Supports variable MxN pattern size.
  • Provides a drop-in implementation of the boilerplate create-and-confirm / verify flow, the same as the framework Settings app.
  • Provides a detailed example of integration in sample app source, e.g. implementing protected UI, theme switching.

Preview

Google Play

Sample APK

Design

This library aims to provide the basic but extensible building blocks for implementing pattern lock mechanism in an Android app. So the common usage will be extending the base Activity classes provided and overriding methods according to your need.

This library also aims to be elegant. Code taken from AOSP was slightly refactored and renamed to be clear, and the PatternView now utilizes the Android resource system for customization.

This library added support for variable pattern sizes, so that you can use pattern sizes other than framework's hard-coded 3x3 setup by setting app:pl_rowCount and app:pl_columnCount.

Integration

Gradle:

compile 'me.zhanghai.android.patternlock:library:2.1.2'

Usage

Here are some detailed usage documentation, and you can always refer to the sample app source for a working implementation.

Styling

First of all, you need to include the default styling in your theme, by adding:

<item name="patternViewStyle">@style/PatternView</item>
<!-- Or PatternView.Light, or your own style extending these two or not. -->

Or you can utilize the resource overriding trick by copying the layout/base_pattern_activity.xml from the library source to your application source, and modify it there (for instance PatternView attributes). Changes will override the original layout in this library.

Available PatternView attributes are, as in attrs.xml:

<declare-styleable name="PatternView">
    <!-- Defines the row count of the pattern. -->
    <attr name="pl_rowCount" format="integer" />
    <!-- Defines the column count of the pattern. -->
    <attr name="pl_columnCount" format="integer" />
    <!-- Defines the aspect to use when drawing PatternView. -->
    <attr name="pl_aspect">
        <!-- Square; the default value. -->
        <enum name="square" value="0" />
        <enum name="lock_width" value="1" />
        <enum name="lock_height" value="2" />
    </attr>
    <!-- Defines the regular pattern color. -->
    <attr name="pl_regularColor" format="color|reference" />
    <!-- Defines the error color. -->
    <attr name="pl_errorColor" format="color|reference" />
    <!-- Defines the success color. -->
    <attr name="pl_successColor" format="color|reference"/>
</declare-styleable>

And built-in styles, as in styles.xml:

<style name="Base.PatternView" parent="">
    <item name="pl_rowCount">3</item>
    <item name="pl_columnCount">3</item>
    <item name="pl_aspect">square</item>
</style>

<style name="PatternView" parent="Base.PatternView">
    <item name="pl_regularColor">?colorControlNormal</item>
    <item name="pl_errorColor">#fff4511e</item>
    <item name="pl_successColor">?colorControlActivated</item>
</style>

<style name="PatternView.Light" parent="Base.PatternView">
    <item name="pl_regularColor">?colorControlNormal</item>
    <item name="pl_errorColor">#fff4511e</item>
    <item name="pl_successColor">?colorControlActivated</item>
</style>

Implementing

As stated above, the common usage will be extending or checking result instead of giving Intent extras, because the actions to perform generally requires a Context or even a Activity, in which a simple callback can be difficult or even leaking Activity instances.

Set pattern activity example:

public class SampleSetPatternActivity extends SetPatternActivity {

    @Override
    protected void onSetPattern(List<PatternView.Cell> pattern) {
        String patternSha1 = PatternUtils.patternToSha1String(pattern);
        // TODO: Save patternSha1 in SharedPreferences.
    }
}

Confirm pattern activity example:

public class SampleConfirmPatternActivity extends ConfirmPatternActivity {

    @Override
    protected boolean isStealthModeEnabled() {
        // TODO: Return the value from SharedPreferences.
        return false;
    }

    @Override
    protected boolean isPatternCorrect(List<PatternView.Cell> pattern) {
        // TODO: Get saved pattern sha1.
        String patternSha1 = null;
        return TextUtils.equals(PatternUtils.patternToSha1String(pattern), patternSha1);
    }

    @Override
    protected void onForgotPassword() {

        startActivity(new Intent(this, YourResetPatternActivity.class));

        // Finish with RESULT_FORGOT_PASSWORD.
        super.onForgotPassword();
    }
}

Note that protected fields inherited from BasePatternActivity, such as mMessageText and mPatternView, are also there ready for your customization.

You can check out the sample app's PatternLockActivity for implementing a pattern-locked Activity.

PatternLock or LockPattern?

The original view in AOSP is named LockPatternView, however I believe it is named so because it displays the (screen lock) pattern, instead of the pattern as a lock. Since this library is to provide the pattern as a locking mechanism, I'd prefer naming it PatternLock, and for simplicity, the view is renamed to PatternView.

Differences with android-lockpattern

I know there is already a library named android-lockpattern, and I tried it for a whole day before I started writing this "yet another".

So here are the major differences.

  • That project is prefixing its resources using alp_42447968, while this project is prefixing its resources using pl, and I prefer simplicity to that "security".

  • That project provides a bunch of mechanisms and extras for its Activity (and with some private methods :( ), none of which I found suitable for my use case, while this project provides a bunch of methods to override to meet your need.

  • That project has a demo but it is close-sourced, while this project provides an open source sample application with a working pattern lock mechanism implementation.

Legacy version

There was a legacy version of this library with Android Design (Holo) appearance and native Activity. If you still want that version, checkout this.

License

Copyright 2015 Zhang Hai

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.

More Repositories

1

MaterialFiles

Material Design file manager for Android
Kotlin
5,968
star
2

Douya

开源的 Material Design 豆瓣客户端(A Material Design app for douban.com)
Java
4,548
star
3

MaterialProgressBar

Material Design ProgressBar with consistent appearance
Java
2,215
star
4

MaterialRatingBar

Material Design RatingBar with better appearance
Java
1,961
star
5

MaterialColdStart

Utilize the window background during cold start time to make your app look faster.
Shell
1,225
star
6

AndroidFastScroll

Fast scroll for Android RecyclerView and more
Java
701
star
7

CustomTabsHelper

Custom tabs, made easy.
Java
386
star
8

Untracker

App to help you remove tracking information before sharing links
Kotlin
366
star
9

ComposePreference

Preference implementation for Jetpack Compose Material 3
Kotlin
182
star
10

archexp

浙江大学计算机体系结构课程实验
Verilog
166
star
11

AppIconLoader

Android app icon loader from AOSP iconloaderlib
Java
162
star
12

MaterialEditText

Material Design EditText with a delightful linear ripple in background
Java
146
star
13

zju-csse-undergraduate-design-latex-template

浙江大学计算机科学与技术、软件工程专业本科毕业设计 LaTeX 模板
TeX
138
star
14

SystemUiHelper

Helper for dealing with Android System UI visibility
Java
117
star
15

BeeShell

Java REPL on Android with BeanShell
Kotlin
107
star
16

EffortlessPermissions

An Android permission library extending Google's EasyPermissions with convenient additions.
Java
86
star
17

TextSelectionWebSearch

Add web search to text selection toolbar on Android
Java
84
star
18

gnome-shell-extension-transparent-top-bar

GNOME Shell extension that brings back the transparent top bar when free-floating in GNOME Shell 3.32
JavaScript
77
star
19

DouyaApiKey

豆芽 API Key 设置向导
Java
69
star
20

DoubanYearProgress

Year progress for Douban
JavaScript
63
star
21

AndroidUtil

Utilities to make the life of an Android developer easier
Java
59
star
22

libarchive-android

libarchive as an Android library
C
56
star
23

AndroidSVGScripts

Bash scripts to automate and extend the process of using SVG as Android image asset
Shell
56
star
24

AndroidRetroFile

Backport of java.nio.file API (JSR 203) for Android
Java
47
star
25

mipsasm

MIPS assembler and IDE
Java
46
star
26

MaterialPlayPauseDrawable

Material Design play/pause Drawable with animation
Java
37
star
27

libselinux-android

libselinux as an Android library
C
24
star
28

join-screenshots.zhanghai.me

Join screenshots with HTML5
HTML
23
star
29

opengl-c-solarsystem

Animated solar system model implemented with OpenGL and FreeGLUT in C
C
22
star
30

orgexp

Computer Organization Experiment, Shi Qingsong, Zhejiang University.
Verilog
21
star
31

pykeymacs

Emacs style keyboard macros implemented in Python
Python
20
star
32

vntools

Tools for working with visual novels
C++
18
star
33

filesystem-kt

Multiplatform Kotlin library for accessing files and file systems
Kotlin
17
star
34

deadbeef-gnome-mmkeys

Add GNOME multimedia keys support in DeadBeef player using DBus
C
16
star
35

collision-detection-bubbles

A collision detection and resolution experiment with Two.js
JavaScript
16
star
36

gtkmmproject

Gtkmm CLion Project
CMake
12
star
37

linenoise-android

Linenoise as an Android library
C
12
star
38

qt5project

Qt 5 CLion project
C++
10
star
39

Reflected

Java library for easy-to-use reflection
Java
9
star
40

AndroidLibraryTemplate

Android library template
9
star
41

SeekBarPreference

A Preference that shows a dialog with a SeekBar widget.
Java
9
star
42

Bytecoder

Android gradle plugin to generate bytecode for accessing any method or field
Java
9
star
43

Functional

Java library for JavaScript-like functional methods
Java
8
star
44

zhsh

A minimal interactive shell written in C.
C
8
star
45

DouyaCiBuilds

豆芽 CI 构建版本仓库
8
star
46

Palette

Sample application to showcase android.support.v7.graphics.Palette
Java
6
star
47

twitter-unwanted-follower-remover

Semi-automatic command line tool for removing unwanted followers on Twitter
JavaScript
5
star
48

particle-system-campfire

A particle system experiment with Two.js
HTML
5
star
49

AimBackend

Another Instant Messager app, backend.
JavaScript
5
star
50

AimAndroid

Another Instant Messager app, Android.
Java
5
star
51

kiscript

ECMAScript, kept simple.
C
5
star
52

ColorPicker

Android color picker from AOSP
Java
5
star
53

markdown-template

Zhang Hai's markdown template
JavaScript
4
star
54

gnome-shell-extension-es6-class-codemod

A jscodeshift transform that helps migrating GNOME Shell extensions to 3.32
JavaScript
4
star
55

Promise

Java library for JavaScript-like Promise
Java
4
star
56

ZJUWLANAutoLogin

A chrome extesion that enables auto login when redirected to login page on ZJUWLAN
JavaScript
4
star
57

resume.zhanghai.me

Zhang Hai's resume
HTML
4
star
58

gettext-po-parser

Gettext PO file parser
4
star
59

xkeymacs

X11 KEYboard MACroS
C
4
star
60

fpga-life-game

Conway's game of life implemented on FPGA with combinational logic
Verilog
3
star
61

zhclib

A C library written by me
C
3
star
62

translations.zhanghai.me

Search translations in open source projects
HTML
3
star
63

stone.zhanghai.me

(Zhang Hai's) The story of the stone
HTML
3
star
64

douya-website

Source of Zhang Hai's Douya project home
JavaScript
3
star
65

SsidTester

SSID tester for identifying ZJUWLAN difficulty. Also serves as a sample application for beginners.
Java
3
star
66

course_linux_programming

Linux programming course
C
3
star
67

zh-conf-backup

Backup script for Zhang Hai's configuration
Shell
3
star
68

Labyrinth

Labyrinth game with OpenGL
Java
3
star
69

resume-legacy

Source of Zhang Hai's resume, legacy version
CSS
2
star
70

slides.zhanghai.me

Zhang Hai's slides
HTML
2
star
71

douya.zhanghai.me

Zhang Hai's Douya
ApacheConf
2
star
72

api-functions-worth-calling

API - Functions worth calling
HTML
2
star
73

optical-system-mfc

Optical System with MFC
C
2
star
74

ExpressionEvaluator

Expression evaluator written in Java
Java
2
star
75

AimFrontend

Another Instant Messager app, frontend.
Vue
2
star
76

zh-minecraft-saves

My Minecraft saves.
Shell
2
star
77

FxPaint

Paint app written with JavaFx
Java
2
star
78

projects.zhanghai.me

Zhang Hai's projects
HTML
2
star
79

calc

C Topic Project - A calculator based on operator precedence
C
2
star
80

www.zhanghai.me

Zhang Hai's main site
HTML
2
star
81

aoe4-mod-superweapon-pvp

Add Siege Camp in campaign as a superweapon for the game (PvP version)
1
star
82

lms

C Topic Project - A simple Library Management System
C
1
star
83

SePolicyInject

Android library to inject rules into binary SELinux kernel policies
CMake
1
star
84

DataStructure

An object oriented implementation of fundamental data structures (and algorithms) using C++11
C++
1
star
85

WordList

A simple android application to display specially annotated English word lists
Java
1
star
86

optical-system-qt

Optical System with Qt
C
1
star
87

seafile-server-github-actions-build

Seafile Server built by GitHub Actions
1
star
88

reveal-md-template

Reveal.js template with reveal-md
Makefile
1
star
89

reveal-js-template

Zhang Hai's reveal.js template
Shell
1
star
90

ShowcaseView

A fork from legacy branch of https://github.com/amlcurran/ShowcaseView
Java
1
star
91

blog.zhanghai.me

Zhang Hai's blog
HTML
1
star
92

sevenzipjbind

Mirrored from git://sevenzipjbind.git.sourceforge.net/gitroot/sevenzipjbind/sevenzipjbind
C++
1
star
93

translations-index.zhanghai.me

Translations index
1
star
94

abnf-kt

ABNF parser generator for Kotlin
Kotlin
1
star