[Android UI] how to unlock the android-lockpattern
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.
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
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 ciphertext = 3; // compare the tested ciphertext, representing a patternprivate String testChars = ciphertext; private Button openLockPatternBtn, compareButton, verifyModeButton; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. 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); // The SHA1 algorithm digest is used by default to enable custom resolution ////
//
// AlpSettings. 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 onAc TivityResult (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: 10 1b2a675e9fb9546336d5b9ef70418b594184f4} 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 represents the number of times the user tried the pattern */int retryCount = data. getIntExtra (LockPatternActivity. EXTRA_RETRY_COUNT, 0); Log. I (TAG, user attempted + 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
Pattern) {// This is just a simple demo. It uses two encryption methods. 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
Decrypt (Context context, char [] encryptedPattern) {List
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