Lock Pattern

2018. 11. 6. 13:34Android

반응형

o build.gradle

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


o Util.java

public class Util {

private static final String LOCK_PATTERN = "lockpattern";

private static Context mContext;

public static void init(Context appContext) {
mContext = appContext;
}

public static Context getContext(){
return mContext;
}

private static SharedPreferences getSharedPreferences() {
return mContext.getSharedPreferences("roundtalk", Context.MODE_PRIVATE);
}

public static void setLockPattern(String lockPattern) {
SharedPreferences.Editor editor = getSharedPreferences().edit();
editor.putString(LOCK_PATTERN, lockPattern).apply();
}

public static String getLockPattern() {
return getSharedPreferences().getString(LOCK_PATTERN, "");
}

}

o  LockPatternSetActivity.java

public class LockPatternSetActivity extends SetPatternActivity {
private static final String TAG = LockPatternSetActivity.class.getSimpleName();

@Override
protected void onSetPattern(List<PatternView.Cell> pattern) {
super.onSetPattern(pattern);
String patternSha1 = PatternUtils.patternToSha1String(pattern);
Log.d(TAG, "patternSha1 : "+Util.getLockPattern());
if(Util.getLockPattern().isEmpty())
Util.setLockPattern(patternSha1);
}
}

o LockPatternConfirmActivity.java

public class LockPatternConfirmActivity 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 = Util.getLockPattern();
if(TextUtils.equals(PatternUtils.patternToSha1String(pattern), patternSha1))
Toast.makeText(this, "Pattern is correct.", Toast.LENGTH_SHORT).show();

return TextUtils.equals(PatternUtils.patternToSha1String(pattern), patternSha1);
}

@Override
protected void onForgotPassword() {
// lock pattern forgot...reset pattern
startActivity(new Intent(this, LockPatternSetActivity.class));

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

}

o MainActivity.java

public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();


private Button lockpattern;


@TargetApi(Build.VERSION_CODES.P)
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


Util.init(this);

lockpattern = findViewById(R.id.lockpattern);
lockpattern.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(Util.getLockPattern().isEmpty())
startActivity(new Intent(MainActivity.this, LockPatternSetActivity.class));
else
startActivity(new Intent(MainActivity.this, LockPatternConfirmActivity.class));
}
});



}

}



반응형

'Android' 카테고리의 다른 글

안드로이드 아키텍처 컴포넌트  (0) 2018.12.30
roulette(룰렛)  (0) 2018.11.06
FingerPrint(지문인증)  (0) 2018.11.06
BarCode, QRCode&Scan  (0) 2018.11.06
NFC  (0) 2018.10.22