[Android UI] android-lockpattern unlocking implementation, androidui

Source: Internet
Author: User

[Android UI] android-lockpattern unlocking implementation, androidui

This article is for learning and sharing. If there are similarities, it is a coincidence that there are similarities?

Based on the principles of technology sharing and mutual learning, I would like to share the implementation of an open-source android pattern unlocking.

Project address: https://code.google.com/p/android-lockpattern/ (taking into account the wall to write this demo)

It is modified based on the source code of the pattern lock provided by android and can be called as an app.

Let's take a look at the implementation effect!


Let's start with the demo structure. First download the source code and use android-lockpattern as lib.


1. create a demo. android. lockpattern. lockPatternActivity. copy java to our project. It is a pattern activity. We can modify the logic in it and configure AndroidManifest. xml with the following configuration

<! -- To avoid activity being killed by the system due to screen rotation, add the following settings --> <activity android: name = "LockPatternActivity" android: configChanges = "orientation | screenSize | keyboard | keyboardHidden" android: screenOrientation = "user" android: theme = "@ style/alp.20.47968.theme. dark "> </activity>

2. let's take a look at the simplest and most affordable main Activity. There are three buttons: Create a new pattern, compare the pattern, and generate a random pattern. For specific functions and usage, see the code, I have already written it clearly.

/*** Demo Main Interface (habits) * @ author jan */public class MainActivity extends Activity implements OnClickListener {private static final String TAG = "MainActivity "; // request to create a new pattern private static final int REQ_CREATE_PATTERN = 1; // compare the existing pattern private static final int REQ_COMPARE_PATTERN = 2; // generate a random pattern (I feel useless (#--)/.) private static final int REQ_VERIFY_PATTERN = 3; // compare the test ciphertext, representing a patternprivate String testChars = "101b2a 675e9fb9546336d5b9ef70418b594184f4 "; private Button openLockPatternBtn, compareButton, verifyModeButton; @ Overrideprotected void onCreate (Bundle savedInstanceState. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // lockpattern will use Sharepreference to automatically save the ciphertext AlpSettings. security. setAutoSavePattern (this, true); // stealth mode: do not display the sketch line. AlpSettings is disabled by default. display. setStealthMode (this, false); // enable custom Mode By default, the SHA1 algorithm digest // <activity // android: name = "com. haibison. android. lockpattern. lockPatternActivity "// android: configChanges =" keyboard | keyboardHidden | orientation | screenSize "// android: screenOrientation =" user "// android: theme =" @ style/alp.20.47968.theme. dialog. dark "> // <meta-data // android: name =" encrypterClass "// android: value = "... full. qualified. name. to. your. LPEncrypter "/> // </activity> // AlpSettin Gs. security. setEncrypterClass (this, LPEncrypter. class); openLockPatternBtn = (Button) findViewById (R. id. open_button); openLockPatternBtn. setOnClickListener (this); compareButton = (Button) findViewById (R. id. compare_button); compareButton. setOnClickListener (this); verifyModeButton = (Button) findViewById (R. id. verify_button); verifyModeButton. setOnClickListener (this) ;}@ Overrideprotected void onActivityResu Lt (int requestCode, int resultCode, Intent data) {switch (requestCode) {case REQ_CREATE_PATTERN: if (resultCode = RESULT_ OK) {char [] pattern = data. getCharArrayExtra (LockPatternActivity. EXTRA_PATTERN); StringBuffer buffer = new StringBuffer (); for (char c: pattern) {buffer. append (c);} Log. I (TAG, "result =>" + buffer. toString (); Toast. makeText (this, "Message Digest:" + buffer, Toast. LENGTH_SHORT ). show (); // test: 101b2a67 5e9fb9546336d5b9ef70418b594184f4} break; case REQ_COMPARE_PATTERN:/** note! There are four possible returned results */switch (resultCode) {case RESULT_ OK: // The user passes the verification Log. d (TAG, "user passed"); break; case RESULT_CANCELED: // The user cancels the Log. d (TAG, "user canceled"); break; case LockPatternActivity. RESULT_FAILED: // Log of multiple failures. d (TAG, "user failed"); break; case LockPatternActivity. RESULT_FORGOT_PATTERN: // The user forgot the pattern and invoked your recovery Activity. log. d (TAG, "user forgot"); break;}/** in any case, EXTRA_RETRY_COUNT indicates the number of times the user tried the pattern */int retryCount = data. getIntExtra (LockPatternActivity. EXTRA_RETRY_COUNT, 0); Log. I (TAG, "user tried" + retryCount + "times"); break ;}@overridepublic void onClick (View v) {switch (v. getId () {// open a new pattern case R. id. open_button: Intent intent = new Intent (LockPatternActivity. ACTION_CREATE_PATTERN, null, this, LockPatternActivity. class); startActivityForResult (intent, REQ_CREATE_PATTERN); break; // compare the specified saved pattern with case R. id. compare_button: char [] savedPattern = testChars. toCharArray (); Intent compare = new Intent (LockPatternActivity. ACTION_COMPARE_PATTERN, null, this, LockPatternActivity. class); compare. putExtra (LockPatternActivity. EXTRA_PATTERN, savedPattern); startActivityForResult (compare, REQ_COMPARE_PATTERN); break; // case R. id. verify_button: // sets the number of times the verification is displayed. The default value is 4 times. display. setCaptchaWiredDots (this, 9); Intent verifyIntent = new Intent (LockPatternActivity. ACTION_VERIFY_CAPTCHA, null, this, LockPatternActivity. class); startActivityForResult (verifyIntent, REQ_VERIFY_PATTERN); break ;}}}
3. the default encryption method for the pattern lock is SHA1 (Secure Hash Algorithm). The legendary Secure Hash Algorithm is similar to MD5. If you need to implement your own encryption and decryption method, only haibison. android. lockpattern. util. IEncrypter interface, and then set: AlpSettings. security. setEncrypterClass (this, LPEncrypter. class );

/*** Provides a custom password adding method */public class LPEncrypter implements IEncrypter {@ Overridepublic char [] encrypt (Context context, List <Cell> pattern) {// This is just a simple demo. It uses a second encryption method. For more information, see. StringBuilder result = new StringBuilder (); for (Cell cell: pattern) result. append (Integer. toString (cell. getId () + 1 )). append ('-'); return result. substring (0, result. length ()-1 ). toCharArray () ;}@ Overridepublic List <Cell> decrypt (Context context, char [] encryptedPattern) {List <Cell> result = Lists. newArrayList (); String [] ids = new String (encryptedPattern ). split ("[^ 0-9]"); for (String id: ids) result. add (Cell. of (Integer. parseInt (id)-1); return result ;}}

4. It's easy to use. The specific modification depends on your requirements. We will not discuss it here, and finally leave the demo download link, Open the door




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.