Coder Social home page Coder Social logo

aritraroy / patternlockview Goto Github PK

View Code? Open in Web Editor NEW
2.7K 50.0 452.0 1.42 MB

An easy-to-use, customizable and Material Design ready Pattern Lock view for Android

License: Apache License 2.0

Java 100.00%
android android-library pattern-library material-design

patternlockview's Introduction

PatternLockView

PatternLockView

An easy-to-use, customizable, Material Design ready Pattern Lock view for Android.

Specs

Download API License Android Arsenal

This library allows you to implement pattern locking mechanism in your app easily and quickly. It is very easy to use and there are plenty of customization options available to change the functionality and look-and-feel of this view to match your needs.

It also supports RxJava 2 view bindings, so if you are a fan of reactive programming (just like me), you can get a stream of updates as the user draws the pattern.

PatternLockView PatternLockView

Download

This library is available in jCenter which is the default Maven repository used in Android Studio.

Gradle

dependencies {
    // other dependencies here
    
    compile 'com.andrognito.patternlockview:patternlockview:1.0.0'
    // Optional, for RxJava2 adapter
    compile 'com.andrognito.patternlockview:patternlockview-reactive:1.0.0'
}

Spread Some ❤️

GitHub stars GitHub followers
Twitter Follow

Usage

We recommend you to check the sample project to get a complete understanding of the library. The step-by-step implementation guide is as follows.

Step 1

Place the view in your XML layout file.

    <com.andrognito.patternlockview.PatternLockView
        android:id="@+id/pattern_lock_view"
        android:layout_width="280dp"
        android:layout_height="280dp"/>

This is enough to get the view rendered in your layout. But you would certainly want to add a callback listener to listen to pattern changes.

Step 2

Reference the view in code and add a listener to it.

mPatternLockView = (PatternLockView) findViewById(R.id.pattern_lock_view);
mPatternLockView.addPatternLockListener(mPatternLockViewListener);

Implement the listener interface as follows,

private PatternLockViewListener mPatternLockViewListener = new PatternLockViewListener() {
        @Override
        public void onStarted() {
            Log.d(getClass().getName(), "Pattern drawing started");
        }

        @Override
        public void onProgress(List<PatternLockView.Dot> progressPattern) {
            Log.d(getClass().getName(), "Pattern progress: " +
                    PatternLockUtils.patternToString(mPatternLockView, progressPattern));
        }

        @Override
        public void onComplete(List<PatternLockView.Dot> pattern) {
            Log.d(getClass().getName(), "Pattern complete: " +
                    PatternLockUtils.patternToString(mPatternLockView, pattern));
        }

        @Override
        public void onCleared() {
            Log.d(getClass().getName(), "Pattern has been cleared");
        }
    };

And that's it! Your PatternLockView is ready to rock. You might also want to remove the listeners when not needed, removePatternLockListener(mPatternLockViewListener);

Step 3 (Optional: ReactiveX Interface)

For the RxJava fanboys, this library supports RxJava 2 view bindings. You can subscribe to this view to get a stream of pattern change updates.

RxPatternLockView.patternChanges(mPatternLockView)
                .subscribe(new Consumer<PatternLockCompoundEvent>() {
                    @Override
                    public void accept(PatternLockCompoundEvent event) throws Exception {
                        if (event.getEventType() == PatternLockCompoundEvent.EventType.PATTERN_STARTED) {
                            Log.d(getClass().getName(), "Pattern drawing started");
                        } else if (event.getEventType() == PatternLockCompoundEvent.EventType.PATTERN_PROGRESS) {
                            Log.d(getClass().getName(), "Pattern progress: " +
                                    PatternLockUtils.patternToString(mPatternLockView, event.getPattern()));
                        } else if (event.getEventType() == PatternLockCompoundEvent.EventType.PATTERN_COMPLETE) {
                            Log.d(getClass().getName(), "Pattern complete: " +
                                    PatternLockUtils.patternToString(mPatternLockView, event.getPattern()));
                        } else if (event.getEventType() == PatternLockCompoundEvent.EventType.PATTERN_CLEARED) {
                            Log.d(getClass().getName(), "Pattern has been cleared");
                        }
                    }
                });

If you are not interested in getting the compound event, you should subscribe to patternComplete() and/or patternProgress() for the specific updates. Have a detailed look here.

Customization

There are several customization options available which you can use to completely change the look-and-feel and functionality of this view to match your needs.

XML (Quick and Easy)

You can add various attributes to the PatternLockView from your XML layout.

  app:dotCount="3"                                        // Change the no.of dots in a row (or column)
  app:dotNormalSize="12dp"                                // Change the size of the dots in normal state
  app:dotSelectedSize="24dp"                              // Change the size of the dots in selected state
  app:pathWidth="4dp"                                     // Change the width of the path
  app:aspectRatioEnabled="true"                           // Set if the view should respect custom aspect ratio
  app:aspectRatio="square"                                // Set between "square", "width_bias", "height_bias"
  app:normalStateColor="@color/white"                     // Set the color of the pattern view in normal state
  app:correctStateColor="@color/primary"                  // Set the color of the pattern view in correct state
  app:wrongStateColor="@color/pomegranate"                // Set the color of the pattern view in error state     
  app:dotAnimationDuration="200"                          // Change the duration of the animating dots
  app:pathEndAnimationDuration="100"                      // Change the duration of the path end animaiton

JAVA (Programatically)

You can also programatically change the properties of the view, thereby having more control over it.

mPatternLockView.setViewMode(PatternLockView.PatternViewMode.CORRECT);       // Set the current viee more 
mPatternLockView.setInStealthMode(true);                                     // Set the pattern in stealth mode (pattern drawing is hidden)
mPatternLockView.setTactileFeedbackEnabled(true);                            // Enables vibration feedback when the pattern is drawn
mPatternLockView.setInputEnabled(false);                                     // Disables any input from the pattern lock view completely

mPatternLockView.setDotCount(3);
mPatternLockView.setDotNormalSize((int) ResourceUtils.getDimensionInPx(this, R.dimen.pattern_lock_dot_size));
mPatternLockView.setDotSelectedSize((int) ResourceUtils.getDimensionInPx(this, R.dimen.pattern_lock_dot_selected_size));
mPatternLockView.setPathWidth((int) ResourceUtils.getDimensionInPx(this, R.dimen.pattern_lock_path_width));
mPatternLockView.setAspectRatioEnabled(true);
mPatternLockView.setAspectRatio(PatternLockView.AspectRatio.ASPECT_RATIO_HEIGHT_BIAS); 
mPatternLockView.setNormalStateColor(ResourceUtils.getColor(this, R.color.white));
mPatternLockView.setCorrectStateColor(ResourceUtils.getColor(this, R.color.primary));
mPatternLockView.setWrongStateColor(ResourceUtils.getColor(this, R.color.pomegranate));
mPatternLockView.setDotAnimationDuration(150);
mPatternLockView.setPathEndAnimationDuration(100);

Contribution

This library is inspired from AOSP's LockPatternView. There are lots of improvements and customization options added so that you can get started without any hassle. If you find a bug or would like to improve any aspect of it, feel free to contribute with pull requests.

About The Author

Aritra Roy

Android & Backend Developer. Blogger. Designer. Fitness Enthusiast.

License

Copyright 2017 aritraroy

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.

patternlockview's People

Contributors

aritraroy avatar scottkennedy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

patternlockview's Issues

Build failed on Android Studio 4.0

Could not find com.android.support:appcompat-v7:25.3.0.
Searched in the following locations:
  - https://jcenter.bintray.com/com/android/support/appcompat-v7/25.3.0/appcompat-v7-25.3.0.pom

How to get the coordinates(X,Y) in correlation to root view

What i want is by clicking the area of the pattern lock component to trigger the OnTouchListener which i have set on the root view ( view.setOnTouchListener(new View.OnTouchListener() { } ) in order to get the coordinates of the selected spot . By doing so when i click any other component the listener triggers but when click the area of the pattern lock the listener doesn't trigger.
Note: I have also set the PatterLockViewListener.

Upload to Maven Central

Since JCenter is now removed in the latest Android Gradle Plugin, would you mind uploading this library to Maven Central instead?

How to use SetPattern method?

Actually on the wrong pattern entered we want to set the color of paths and dots to red.
How can we achieve this functionality?
and also SetPattern use.

How to clear Pattern after 5 Second.

After Wrong Pattern i can set Pattern color to Red but how can i clear the Pattern after 5 sec when it is wrong. I try Thread to seep 5 sec , it clear the pattern after 5 sec but the color of wrong is not change to red.

Add touch event listener

Hi,
I would like to add MotionEvent listener to PatternLockView in order to display X and Y touch screen position to user. How can I achieve that?

Vibration null pointer exception

I have got an exception from Firebase:

Fatal Exception: java.lang.NullPointerException
Attempt to read from field 'android.os.VibrationEffect com.android.server.VibratorService$Vibration.mEffect' on a null object reference
android.view.View.performHapticFeedback (View.java:23916)
com.andrognito.patternlockview.PatternLockView.detectAndAddHit (PatternLockView.java:814)
com.andrognito.patternlockview.PatternLockView.handleActionMove (PatternLockView.java:979)
com.andrognito.patternlockview.PatternLockView.onTouchEvent (PatternLockView.java:448)
android.view.View.dispatchTouchEvent (View.java:12629)
android.app.Activity.dispatchTouchEvent (Activity.java:3441)

Input delay on wrong pattern

Hi there,

great library!
I want to add a simple time delay to this view so it doesn't accept input for a certain amount of time after the pattern has been input wrong.
Is there an easy way to achieve this?

My current code with missing parts:

Single
  .fromCallable {
      patternLockView
              .setViewMode(PatternLockView.PatternViewMode.WRONG)
      // TODO: disable touch input
  }
  .delay(1, TimeUnit.SECONDS)
  .observeOn(Schedulers.io())
  .subscribeOn(AndroidSchedulers.mainThread())
  .subscribeBy(onSuccess = {
      patternLockView
              .clearPattern()
      // TODO: enable touch input
  })

java.util.ConcurrentModificationException

I am getting this exception on notifyListenersComplete patternLockView class

Fatal Exception: java.util.ConcurrentModificationException: at java.util.ArrayList$Itr.next(ArrayList.java:860) at com.andrognito.patternlockview.PatternLockView.notifyListenersComplete(PatternLockView.java:751) at com.andrognito.patternlockview.PatternLockView.notifyPatternDetected(PatternLockView.java:719) at com.andrognito.patternlockview.PatternLockView.handleActionUp(PatternLockView.java:1052) at com.andrognito.patternlockview.PatternLockView.onTouchEvent(PatternLockView.java:445) at android.view.View.dispatchTouchEvent(View.java:13488) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3088) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2781) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3088) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2781) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3088) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2781) at android.view.View.dispatchPointerEvent(View.java:13749) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5616) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5416) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4917) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4970) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4936) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5076) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4944) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5133) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4917) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4970) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4936) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4944) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4917) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7669) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7638) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7599) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7797) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:251) at android.os.MessageQueue.nativePollOnce(MessageQueue.java) at android.os.MessageQueue.next(MessageQueue.java:336) at android.os.Looper.loop(Looper.java:181) at android.app.ActivityThread.main(ActivityThread.java:7592) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

Results are not unique

If i use 4x4 or more dot, the patternlock result will be not unique , such as 12 5 is equal with 1 2 5. So it will be a good idea to use Hex in 4x4 or other encoding in more dot .

Dynamic height and width for the view

I'm making a Dialog with this pattern lock view inside of it but I would like to be able to easily adjust the pattern lock view's width and height depending on the containing Dialog's dimensions. Any way to do this? Seems like once the layout_width and layout_height are set in the layout file, they can't be changed anymore.
Thanks

Bug: unable to set pattern when dot index exceeds 9

There is a bug in the PatternLockUtils lib where stringToPattern() cannot convert every pattern combination.

Number values should not be saved in a string for pattern views with 4x4 dots and higher, because the tenth, eleventh and twelfth dot will be interpreted as "1", "0", "1", "1", and "1", "2", etc.

patternToString(), patternToMd5(), patternToSha1() should include a separator between each dot (a comma for example), so that we can differentiate between "1", "0", and "10".

bug_PatternLockView

I want to show multiple patterns

multiplePattern
I want to draw multiple patterns , i mean want to show multiple lines and dots, is it possible through this library, with different colors.

Attached a image for reference.

thanks :)

ArrayIndexOutOfBoundsException

Hi,

i tried your library but i got this error
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 at com.andrognito.patternlockview.PatternLockView$Dot.of(PatternLockView.java:1195) at com.andrognito.patternlockview.PatternLockView.checkForNewHit(PatternLockView.java:925) at com.andrognito.patternlockview.PatternLockView.detectAndAddHit(PatternLockView.java:784) at com.andrognito.patternlockview.PatternLockView.handleActionDown(PatternLockView.java:1080) at com.andrognito.patternlockview.PatternLockView.onTouchEvent(PatternLockView.java:442) at android.view.View.dispatchTouchEvent(View.java:11721) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2593) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2593) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2593) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2593) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2593) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2593) at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:445) at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1828) at android.app.Activity.dispatchTouchEvent(Activity.java:3292) at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68) at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:407) at android.view.View.dispatchPointerEvent(View.java:11960) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4776) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4590) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4181) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4147) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4274) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4155) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4331) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4181) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4147) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4155) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6642) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6616) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6577) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6745) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:325) at android.os.Looper.loop(Looper.java:142) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

this is my xml code
<com.andrognito.patternlockview.PatternLockView app:dotCount="3" app:aspectRatio="square" android:layout_gravity="center_horizontal" android:id="@+id/pattern_lock_view" android:layout_width="250dp" android:layout_height="250dp"/>

./gradlew installDebug fails

Parallel execution with configuration on demand is an incubating feature.

FAILURE: Build failed with an exception.

* Where:
Script 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle' line: 34

* What went wrong:
A problem occurred evaluating script.
> /Users/nik/tmp/PatternLockView/local.properties (No such file or directory)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.