mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 19:29:06 +01:00
phase 2: helpers and app bus
This commit is contained in:
@@ -16,14 +16,10 @@ import android.widget.TextView;
|
||||
import android.widget.TextView.OnEditorActionListener;
|
||||
|
||||
import org.briarproject.R;
|
||||
import org.briarproject.android.event.PasswordValidateEvent;
|
||||
import org.briarproject.android.helper.PasswordHelper;
|
||||
import org.briarproject.android.util.AndroidUtils;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.CryptoExecutor;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
import org.briarproject.api.db.DatabaseConfig;
|
||||
import org.briarproject.util.StringUtils;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import org.briarproject.api.event.Event;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
@@ -34,28 +30,22 @@ import static android.view.View.VISIBLE;
|
||||
|
||||
public class PasswordActivity extends BaseActivity {
|
||||
|
||||
@Inject @CryptoExecutor protected Executor cryptoExecutor;
|
||||
private Button signInButton;
|
||||
private ProgressBar progress;
|
||||
private TextInputLayout input;
|
||||
private EditText password;
|
||||
|
||||
private byte[] encrypted;
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@Inject protected volatile CryptoComponent crypto;
|
||||
@Inject protected volatile DatabaseConfig databaseConfig;
|
||||
@Inject
|
||||
PasswordHelper passwordHelper;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle state) {
|
||||
super.onCreate(state);
|
||||
|
||||
String hex = getEncryptedDatabaseKey();
|
||||
if (hex == null || !databaseConfig.databaseExists()) {
|
||||
if (!passwordHelper.initialized()) {
|
||||
clearSharedPrefsAndDeleteEverything();
|
||||
return;
|
||||
}
|
||||
encrypted = StringUtils.fromHexString(hex);
|
||||
|
||||
setContentView(R.layout.activity_password);
|
||||
signInButton = (Button) findViewById(R.id.btn_sign_in);
|
||||
@@ -66,8 +56,7 @@ public class PasswordActivity extends BaseActivity {
|
||||
@Override
|
||||
public boolean onEditorAction(TextView v, int actionId,
|
||||
KeyEvent event) {
|
||||
hideSoftKeyboard(password);
|
||||
validatePassword(encrypted, password.getText());
|
||||
validatePassword();
|
||||
return true;
|
||||
}
|
||||
});
|
||||
@@ -103,7 +92,7 @@ public class PasswordActivity extends BaseActivity {
|
||||
}
|
||||
|
||||
private void clearSharedPrefsAndDeleteEverything() {
|
||||
clearSharedPrefs();
|
||||
passwordHelper.clearPrefs();
|
||||
AndroidUtils.deleteAppData(this);
|
||||
setResult(RESULT_CANCELED);
|
||||
startActivity(new Intent(this, SetupActivity.class));
|
||||
@@ -111,7 +100,7 @@ public class PasswordActivity extends BaseActivity {
|
||||
}
|
||||
|
||||
public void onSignInClick(View v) {
|
||||
validatePassword(encrypted, password.getText());
|
||||
validatePassword();
|
||||
}
|
||||
|
||||
public void onForgottenPasswordClick(View v) {
|
||||
@@ -132,47 +121,37 @@ public class PasswordActivity extends BaseActivity {
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private void validatePassword(final byte[] encrypted, Editable e) {
|
||||
private void validatePassword() {
|
||||
hideSoftKeyboard(password);
|
||||
// Replace the button with a progress bar
|
||||
signInButton.setVisibility(INVISIBLE);
|
||||
progress.setVisibility(VISIBLE);
|
||||
// Decrypt the database key in a background thread
|
||||
final String password = e.toString();
|
||||
cryptoExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
byte[] key = crypto.decryptWithPassword(encrypted, password);
|
||||
if (key == null) {
|
||||
tryAgain();
|
||||
} else {
|
||||
databaseConfig.setEncryptionKey(new SecretKey(key));
|
||||
setResultAndFinish();
|
||||
}
|
||||
}
|
||||
});
|
||||
passwordHelper.validatePassword(password.getText().toString());
|
||||
}
|
||||
|
||||
private void tryAgain() {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
AndroidUtils.setError(input, getString(R.string.try_again),
|
||||
true);
|
||||
signInButton.setVisibility(VISIBLE);
|
||||
progress.setVisibility(INVISIBLE);
|
||||
password.setText("");
|
||||
|
||||
// show the keyboard again
|
||||
showSoftKeyboard(password);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setResultAndFinish() {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
super.eventOccurred(e);
|
||||
if (e instanceof PasswordValidateEvent) {
|
||||
boolean validated = ((PasswordValidateEvent)e).passwordValidated();
|
||||
if (validated) {
|
||||
setResult(RESULT_OK);
|
||||
finish();
|
||||
}
|
||||
});
|
||||
else {
|
||||
tryAgain();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void tryAgain() {
|
||||
AndroidUtils.setError(input, getString(R.string.try_again),
|
||||
true);
|
||||
signInButton.setVisibility(VISIBLE);
|
||||
progress.setVisibility(INVISIBLE);
|
||||
password.setText("");
|
||||
|
||||
// show the keyboard again
|
||||
showSoftKeyboard(password);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user