Skip to content

Commit 5e9057e

Browse files
committed
Merging - require passcode to change/remove passcode settings
2 parents 4bc2aa7 + c677fdc commit 5e9057e

21 files changed

Lines changed: 220 additions & 89 deletions

File tree

app/src/main/java/org/gnucash/android/ui/UxArgument.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@ public final class UxArgument {
3838
public static final String ORIGIN_ACCOUNT_UID = "origin_acccount_uid";
3939

4040
/**
41-
* Key for checking whether the passcode is enabled or not.
41+
* Key for checking whether the passcode is enabled or not
4242
*/
4343
public static final String ENABLED_PASSCODE = "enabled_passcode";
4444

4545
/**
46-
* Key for storing the passcode.
46+
* Key for disabling the passcode
47+
*/
48+
public static final String DISABLE_PASSCODE = "disable_passcode";
49+
50+
/**
51+
* Key for storing the passcode
4752
*/
4853
public static final String PASSCODE = "passcode";
4954

app/src/main/java/org/gnucash/android/ui/passcode/KeyboardFragment.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
*/
3535
public class KeyboardFragment extends SherlockFragment {
3636

37+
private static final int DELAY = 500;
38+
3739
private TextView pass1;
3840
private TextView pass2;
3941
private TextView pass3;
@@ -42,7 +44,7 @@ public class KeyboardFragment extends SherlockFragment {
4244
private int length = 0;
4345

4446
public interface OnPasscodeEnteredListener {
45-
public void onPasscodeEntered(String pass);
47+
void onPasscodeEntered(String pass);
4648
}
4749

4850
private OnPasscodeEnteredListener listener;
@@ -182,7 +184,7 @@ public void run() {
182184
pass4.setText(null);
183185
length = 0;
184186
}
185-
}, 500);
187+
}, DELAY);
186188
}
187189
}
188190

app/src/main/java/org/gnucash/android/ui/passcode/PassLockActivity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ protected void onResume() {
4343
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
4444
GnuCashApplication.PASSCODE_SESSION_INIT_TIME = 0;
4545
}
46-
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
47-
if (sharedPreferences.getBoolean(UxArgument.ENABLED_PASSCODE, false) && !isSessionActive()) {
46+
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
47+
String passCode = prefs.getString(UxArgument.PASSCODE, "");
48+
if (prefs.getBoolean(UxArgument.ENABLED_PASSCODE, false) && !isSessionActive() && !passCode.trim().isEmpty()) {
4849
startActivity(new Intent(this, PasscodeLockScreenActivity.class)
4950
.setAction(getIntent().getAction())
5051
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)

app/src/main/java/org/gnucash/android/ui/passcode/PasscodeLockScreenActivity.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014 Oleksandr Tyshkovets <olexandr.tyshkovets@gmail.com>
2+
* Copyright (c) 2014 - 2015 Oleksandr Tyshkovets <olexandr.tyshkovets@gmail.com>
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,7 +49,12 @@ public void onPasscodeEntered(String pass) {
4949
.getString(UxArgument.PASSCODE, "");
5050
Log.d(TAG, "Passcode: " + passcode);
5151

52-
if (passcode.equals(pass)) {
52+
if (pass.equals(passcode)) {
53+
if (UxArgument.DISABLE_PASSCODE.equals(getIntent().getStringExtra(UxArgument.DISABLE_PASSCODE))) {
54+
setResult(RESULT_OK);
55+
finish();
56+
return;
57+
}
5358
GnuCashApplication.PASSCODE_SESSION_INIT_TIME = System.currentTimeMillis();
5459
startActivity(new Intent()
5560
.setClassName(this, getIntent().getStringExtra(UxArgument.PASSCODE_CLASS_CALLER))
@@ -64,10 +69,18 @@ public void onPasscodeEntered(String pass) {
6469

6570
@Override
6671
public void onBackPressed() {
72+
setResult(RESULT_CANCELED);
73+
74+
if (UxArgument.DISABLE_PASSCODE.equals(getIntent().getStringExtra(UxArgument.DISABLE_PASSCODE))) {
75+
finish();
76+
return;
77+
}
78+
6779
GnuCashApplication.PASSCODE_SESSION_INIT_TIME = System.currentTimeMillis() - GnuCashApplication.SESSION_TIMEOUT;
6880
startActivity(new Intent(Intent.ACTION_MAIN)
6981
.addCategory(Intent.CATEGORY_HOME)
70-
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
82+
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
83+
);
7184
}
7285

7386
}

app/src/main/java/org/gnucash/android/ui/passcode/PasscodePreferenceActivity.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014 Oleksandr Tyshkovets <olexandr.tyshkovets@gmail.com>
2+
* Copyright (c) 2014 - 2015 Oleksandr Tyshkovets <olexandr.tyshkovets@gmail.com>
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import android.content.Intent;
2020
import android.os.Bundle;
21+
import android.preference.PreferenceManager;
2122
import android.widget.TextView;
2223
import android.widget.Toast;
2324

@@ -33,29 +34,54 @@
3334
public class PasscodePreferenceActivity extends SherlockFragmentActivity
3435
implements KeyboardFragment.OnPasscodeEnteredListener {
3536

36-
private boolean reenter = false;
37-
private String passcode;
37+
private boolean mIsPassEnabled;
38+
private boolean mReenter = false;
39+
private String mPasscode;
40+
41+
private TextView mPassTextView;
3842

3943
@Override
4044
protected void onCreate(Bundle savedInstanceState) {
4145
super.onCreate(savedInstanceState);
4246
setContentView(R.layout.passcode_lockscreen);
47+
48+
mPassTextView = (TextView) findViewById(R.id.passcode_label);
49+
50+
mIsPassEnabled = PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
51+
.getBoolean(UxArgument.ENABLED_PASSCODE, false);
52+
53+
if (mIsPassEnabled) {
54+
mPassTextView.setText(R.string.label_old_passcode);
55+
}
4356
}
4457

4558
@Override
4659
public void onPasscodeEntered(String pass) {
47-
if (reenter) {
48-
if (passcode.equals(pass)) {
60+
String passCode = PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
61+
.getString(UxArgument.PASSCODE, "");
62+
63+
if (mIsPassEnabled) {
64+
if (pass.equals(passCode)) {
65+
mIsPassEnabled = false;
66+
mPassTextView.setText(R.string.label_new_passcode);
67+
} else {
68+
Toast.makeText(this, R.string.toast_wrong_passcode, Toast.LENGTH_SHORT).show();
69+
}
70+
return;
71+
}
72+
73+
if (mReenter) {
74+
if (mPasscode.equals(pass)) {
4975
setResult(RESULT_OK, new Intent().putExtra(UxArgument.PASSCODE, pass));
5076
finish();
5177
} else {
5278
Toast.makeText(this, R.string.toast_invalid_passcode_confirmation, Toast.LENGTH_LONG).show();
5379
}
5480
} else {
55-
passcode = pass;
56-
reenter = true;
57-
((TextView) findViewById(R.id.passcode_label)).setText(R.string.toast_confirm_passcode);
58-
Toast.makeText(this, R.string.toast_confirm_passcode, Toast.LENGTH_SHORT).show();
81+
mPasscode = pass;
82+
mReenter = true;
83+
mPassTextView.setText(R.string.label_confirm_passcode);
5984
}
6085
}
86+
6187
}

app/src/main/java/org/gnucash/android/ui/settings/PasscodePreferenceFragment.java

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014 Oleksandr Tyshkovets <olexandr.tyshkovets@gmail.com>
2+
* Copyright (c) 2014 - 2015 Oleksandr Tyshkovets <olexandr.tyshkovets@gmail.com>
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333

3434
import org.gnucash.android.R;
3535
import org.gnucash.android.ui.UxArgument;
36+
import org.gnucash.android.ui.passcode.PasscodeLockScreenActivity;
3637
import org.gnucash.android.ui.passcode.PasscodePreferenceActivity;
3738

3839
/**
@@ -43,12 +44,20 @@
4344
public class PasscodePreferenceFragment extends PreferenceFragment {
4445

4546
/**
46-
* * Request code for retrieving passcode to store
47+
* Request code for retrieving passcode to store
4748
*/
48-
public static final int PASSCODE_REQUEST_CODE = 2;
49+
public static final int PASSCODE_REQUEST_CODE = 0x2;
50+
/**
51+
* Request code for disabling passcode
52+
*/
53+
public static final int REQUEST_DISABLE_PASSCODE = 0x3;
54+
/**
55+
* Request code for changing passcode
56+
*/
57+
public static final int REQUEST_CHANGE_PASSCODE = 0x4;
4958

50-
private SharedPreferences.Editor editor;
51-
private CheckBoxPreference checkBoxPreference;
59+
private SharedPreferences.Editor mEditor;
60+
private CheckBoxPreference mCheckBoxPreference;
5261

5362
@Override
5463
public void onCreate(Bundle savedInstanceState) {
@@ -65,31 +74,31 @@ public void onCreate(Bundle savedInstanceState) {
6574
public void onResume() {
6675
super.onResume();
6776

68-
editor = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()).edit();
77+
mEditor = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()).edit();
6978
final Intent intent = new Intent(getActivity(), PasscodePreferenceActivity.class);
7079

71-
checkBoxPreference = (CheckBoxPreference) findPreference(getString(R.string.key_enable_passcode));
72-
final String passcodeEnabled = getString(R.string.title_passcode_enabled);
73-
final String passcodeDisabled = getString(R.string.title_passcode_disabled);
74-
checkBoxPreference.setTitle(checkBoxPreference.isChecked() ? passcodeEnabled : passcodeDisabled);
75-
checkBoxPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
76-
@Override
77-
public boolean onPreferenceChange(Preference preference, Object newValue) {
78-
if ((Boolean) newValue) {
79-
startActivityForResult(intent, PASSCODE_REQUEST_CODE);
80-
} else {
81-
checkBoxPreference.setTitle(passcodeDisabled);
82-
}
83-
editor.putBoolean(UxArgument.ENABLED_PASSCODE, (Boolean) newValue);
84-
editor.commit();
85-
return true;
86-
}
87-
});
80+
mCheckBoxPreference = (CheckBoxPreference) findPreference(getString(R.string.key_enable_passcode));
81+
mCheckBoxPreference.setTitle(mCheckBoxPreference.isChecked()
82+
? getString(R.string.title_passcode_enabled)
83+
: getString(R.string.title_passcode_disabled));
84+
mCheckBoxPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
85+
@Override
86+
public boolean onPreferenceChange(Preference preference, Object newValue) {
87+
if ((Boolean) newValue) {
88+
startActivityForResult(intent, PASSCODE_REQUEST_CODE);
89+
} else {
90+
Intent passIntent = new Intent(getActivity(), PasscodeLockScreenActivity.class);
91+
passIntent.putExtra(UxArgument.DISABLE_PASSCODE, UxArgument.DISABLE_PASSCODE);
92+
startActivityForResult(passIntent, REQUEST_DISABLE_PASSCODE);
93+
}
94+
return true;
95+
}
96+
});
8897
findPreference(getString(R.string.key_change_passcode))
8998
.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
9099
@Override
91100
public boolean onPreferenceClick(Preference preference) {
92-
startActivityForResult(intent, PASSCODE_REQUEST_CODE);
101+
startActivityForResult(intent, REQUEST_CHANGE_PASSCODE);
93102
return true;
94103
}
95104
});
@@ -99,15 +108,35 @@ public boolean onPreferenceClick(Preference preference) {
99108
public void onActivityResult(int requestCode, int resultCode, Intent data) {
100109
super.onActivityResult(requestCode, resultCode, data);
101110

102-
if (resultCode == Activity.RESULT_OK && requestCode == PASSCODE_REQUEST_CODE && data!= null) {
103-
editor.putString(UxArgument.PASSCODE, data.getStringExtra(UxArgument.PASSCODE));
104-
Toast.makeText(getActivity(), R.string.toast_passcode_set, Toast.LENGTH_SHORT).show();
105-
checkBoxPreference.setTitle(getString(R.string.title_passcode_enabled));
106-
} else {
107-
editor.putBoolean(UxArgument.ENABLED_PASSCODE, false);
108-
checkBoxPreference.setChecked(false);
111+
switch (requestCode) {
112+
case PASSCODE_REQUEST_CODE:
113+
if (resultCode == Activity.RESULT_OK && data != null) {
114+
mEditor.putString(UxArgument.PASSCODE, data.getStringExtra(UxArgument.PASSCODE));
115+
mEditor.putBoolean(UxArgument.ENABLED_PASSCODE, true);
116+
Toast.makeText(getActivity(), R.string.toast_passcode_set, Toast.LENGTH_SHORT).show();
117+
mCheckBoxPreference.setTitle(getString(R.string.title_passcode_enabled));
118+
}
119+
if (resultCode == Activity.RESULT_CANCELED) {
120+
mEditor.putBoolean(UxArgument.ENABLED_PASSCODE, false);
121+
mCheckBoxPreference.setChecked(false);
122+
mCheckBoxPreference.setTitle(getString(R.string.title_passcode_disabled));
123+
}
124+
break;
125+
case REQUEST_DISABLE_PASSCODE:
126+
boolean flag = resultCode != Activity.RESULT_OK;
127+
mEditor.putBoolean(UxArgument.ENABLED_PASSCODE, flag);
128+
mCheckBoxPreference.setChecked(flag);
129+
break;
130+
case REQUEST_CHANGE_PASSCODE:
131+
if (resultCode == Activity.RESULT_OK && data != null) {
132+
mEditor.putString(UxArgument.PASSCODE, data.getStringExtra(UxArgument.PASSCODE));
133+
mEditor.putBoolean(UxArgument.ENABLED_PASSCODE, true);
134+
Toast.makeText(getActivity(), R.string.toast_passcode_set, Toast.LENGTH_SHORT).show();
135+
mCheckBoxPreference.setTitle(getString(R.string.title_passcode_enabled));
136+
}
137+
break;
109138
}
110-
editor.commit();
139+
mEditor.commit();
111140
}
112141

113142
}

0 commit comments

Comments
 (0)