mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 20:29:52 +01:00
code re-structure, mostly nitpicks :)
This commit is contained in:
@@ -13,49 +13,47 @@ import roboguice.activity.RoboActivity;
|
|||||||
|
|
||||||
public abstract class BaseActivity extends RoboActivity {
|
public abstract class BaseActivity extends RoboActivity {
|
||||||
|
|
||||||
private final static String PREFS_DB = "db";
|
private final static String PREFS_DB = "db";
|
||||||
private final static String KEY_DB_KEY = "key";
|
private final static String KEY_DB_KEY = "key";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
if (PREVENT_SCREENSHOTS) getWindow().addFlags(FLAG_SECURE);
|
if (PREVENT_SCREENSHOTS) getWindow().addFlags(FLAG_SECURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private SharedPreferences getBriarPrefs(String prefsName) {
|
private SharedPreferences getBriarPrefs(String prefsName) {
|
||||||
return getSharedPreferences(prefsName, MODE_PRIVATE);
|
return getSharedPreferences(prefsName, MODE_PRIVATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getDbKeyInHex() {
|
protected String getDbKeyInHex() {
|
||||||
return getBriarPrefs(PREFS_DB).getString(KEY_DB_KEY, null);
|
return getBriarPrefs(PREFS_DB).getString(KEY_DB_KEY, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void clearPrefs(String prefsName) {
|
private void clearPrefs(String prefsName) {
|
||||||
SharedPreferences.Editor editor = getBriarPrefs(prefsName).edit();
|
SharedPreferences.Editor editor = getBriarPrefs(prefsName).edit();
|
||||||
editor.clear();
|
editor.clear();
|
||||||
editor.apply();
|
editor.apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void clearDbPrefs() {
|
protected void clearDbPrefs() {
|
||||||
this.clearPrefs(PREFS_DB);
|
this.clearPrefs(PREFS_DB);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void gotoAndFinish(Class classInstance, int resultCode) {
|
protected void gotoAndFinish(Class classInstance, int resultCode) {
|
||||||
if (resultCode != Integer.MIN_VALUE)
|
if (resultCode != Integer.MIN_VALUE)
|
||||||
this.setResult(resultCode);
|
setResult(resultCode);
|
||||||
this.startActivity(new Intent(this, classInstance));
|
startActivity(new Intent(this, classInstance));
|
||||||
this.finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void gotoAndFinish(Class classInstance)
|
protected void gotoAndFinish(Class classInstance) {
|
||||||
{
|
gotoAndFinish(classInstance, Integer.MIN_VALUE);
|
||||||
this.gotoAndFinish(classInstance, Integer.MIN_VALUE);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
protected void hideSoftKeyboard()
|
protected void hideSoftKeyboard() {
|
||||||
{
|
Object o = getSystemService(INPUT_METHOD_SERVICE);
|
||||||
Object o = getSystemService(INPUT_METHOD_SERVICE);
|
((InputMethodManager) o).toggleSoftInput(HIDE_IMPLICIT_ONLY, 0);
|
||||||
((InputMethodManager) o).toggleSoftInput(HIDE_IMPLICIT_ONLY, 0);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,17 +3,16 @@ package org.briarproject.android;
|
|||||||
import static android.view.View.GONE;
|
import static android.view.View.GONE;
|
||||||
import static android.view.View.VISIBLE;
|
import static android.view.View.VISIBLE;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import org.briarproject.R;
|
import org.briarproject.R;
|
||||||
import org.briarproject.android.util.BriarIOUtils;
|
|
||||||
import org.briarproject.api.crypto.CryptoComponent;
|
import org.briarproject.api.crypto.CryptoComponent;
|
||||||
import org.briarproject.api.crypto.CryptoExecutor;
|
import org.briarproject.api.crypto.CryptoExecutor;
|
||||||
import org.briarproject.api.crypto.SecretKey;
|
import org.briarproject.api.crypto.SecretKey;
|
||||||
import org.briarproject.api.db.DatabaseConfig;
|
import org.briarproject.api.db.DatabaseConfig;
|
||||||
|
import org.briarproject.system.AndroidFileUtils;
|
||||||
import org.briarproject.util.StringUtils;
|
import org.briarproject.util.StringUtils;
|
||||||
|
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
@@ -32,10 +31,10 @@ import android.widget.TextView.OnEditorActionListener;
|
|||||||
public class PasswordActivity extends BaseActivity {
|
public class PasswordActivity extends BaseActivity {
|
||||||
|
|
||||||
@Inject @CryptoExecutor private Executor cryptoExecutor;
|
@Inject @CryptoExecutor private Executor cryptoExecutor;
|
||||||
private Button mSignInButton;
|
private Button signInButton;
|
||||||
private ProgressBar mProgress;
|
private ProgressBar progress;
|
||||||
private TextView mTitle;
|
private TextView title;
|
||||||
private EditText mPassword;
|
private EditText password;
|
||||||
|
|
||||||
private byte[] encrypted;
|
private byte[] encrypted;
|
||||||
|
|
||||||
@@ -47,23 +46,23 @@ public class PasswordActivity extends BaseActivity {
|
|||||||
public void onCreate(Bundle state) {
|
public void onCreate(Bundle state) {
|
||||||
super.onCreate(state);
|
super.onCreate(state);
|
||||||
|
|
||||||
String hex = this.getDbKeyInHex();
|
String hex = getDbKeyInHex();
|
||||||
if (hex == null || !databaseConfig.databaseExists()) {
|
if (hex == null || !databaseConfig.databaseExists()) {
|
||||||
this.clearDbPrefs();
|
clearDbPrefs();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.encrypted = StringUtils.fromHexString(hex);
|
encrypted = StringUtils.fromHexString(hex);
|
||||||
|
|
||||||
this.setContentView(R.layout.activity_password);
|
setContentView(R.layout.activity_password);
|
||||||
this.mSignInButton = (Button)findViewById(R.id.btn_sign_in);
|
signInButton = (Button)findViewById(R.id.btn_sign_in);
|
||||||
this.mProgress = (ProgressBar)findViewById(R.id.progress_wheel);
|
progress = (ProgressBar)findViewById(R.id.progress_wheel);
|
||||||
this.mTitle = (TextView)findViewById(R.id.title_password);
|
title = (TextView)findViewById(R.id.title_password);
|
||||||
this.mPassword = (EditText)findViewById(R.id.edit_password);
|
password = (EditText)findViewById(R.id.edit_password);
|
||||||
this.mPassword.setOnEditorActionListener(new OnEditorActionListener() {
|
password.setOnEditorActionListener(new OnEditorActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
||||||
if (actionId == EditorInfo.IME_ACTION_DONE) {
|
if (actionId == EditorInfo.IME_ACTION_DONE) {
|
||||||
validatePassword(encrypted, PasswordActivity.this.mPassword.getText());
|
validatePassword(encrypted, password.getText());
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -73,12 +72,12 @@ public class PasswordActivity extends BaseActivity {
|
|||||||
@Override
|
@Override
|
||||||
protected void clearDbPrefs() {
|
protected void clearDbPrefs() {
|
||||||
super.clearDbPrefs();
|
super.clearDbPrefs();
|
||||||
BriarIOUtils.deleteFileOrDir(databaseConfig.getDatabaseDirectory());
|
AndroidFileUtils.deleteFileOrDir(databaseConfig.getDatabaseDirectory());
|
||||||
this.gotoAndFinish(SetupActivity.class, RESULT_CANCELED);
|
gotoAndFinish(SetupActivity.class, RESULT_CANCELED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onSignInClick(View v) {
|
public void onSignInClick(View v) {
|
||||||
this.validatePassword(this.encrypted, this.mPassword.getText());
|
validatePassword(encrypted, password.getText());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onForgottenPasswordClick(View v) {
|
public void onForgottenPasswordClick(View v) {
|
||||||
@@ -90,7 +89,7 @@ public class PasswordActivity extends BaseActivity {
|
|||||||
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
|
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
PasswordActivity.this.clearDbPrefs();
|
clearDbPrefs();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
AlertDialog dialog = builder.create();
|
AlertDialog dialog = builder.create();
|
||||||
@@ -98,10 +97,10 @@ public class PasswordActivity extends BaseActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void validatePassword(final byte[] encrypted, Editable e) {
|
private void validatePassword(final byte[] encrypted, Editable e) {
|
||||||
this.hideSoftKeyboard();
|
hideSoftKeyboard();
|
||||||
// Replace the button with a progress bar
|
// Replace the button with a progress bar
|
||||||
this.mSignInButton.setVisibility(View.INVISIBLE);
|
signInButton.setVisibility(View.INVISIBLE);
|
||||||
this.mProgress.setVisibility(VISIBLE);
|
progress.setVisibility(VISIBLE);
|
||||||
// Decrypt the database key in a background thread
|
// Decrypt the database key in a background thread
|
||||||
final String password = e.toString();
|
final String password = e.toString();
|
||||||
cryptoExecutor.execute(new Runnable() {
|
cryptoExecutor.execute(new Runnable() {
|
||||||
@@ -120,10 +119,10 @@ public class PasswordActivity extends BaseActivity {
|
|||||||
private void tryAgain() {
|
private void tryAgain() {
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
PasswordActivity.this.mTitle.setText(R.string.try_again);
|
title.setText(R.string.try_again);
|
||||||
PasswordActivity.this.mSignInButton.setVisibility(VISIBLE);
|
signInButton.setVisibility(VISIBLE);
|
||||||
PasswordActivity.this.mProgress.setVisibility(GONE);
|
progress.setVisibility(GONE);
|
||||||
PasswordActivity.this.mPassword.setText("");
|
password.setText("");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
package org.briarproject.android.util;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by Ernir Erlingsson (ernir@ymirmobile.com) on 9.12.2015.
|
|
||||||
*/
|
|
||||||
public class BriarIOUtils {
|
|
||||||
|
|
||||||
public static void deleteFileOrDir(File f)
|
|
||||||
{
|
|
||||||
if (f.isFile())
|
|
||||||
f.delete();
|
|
||||||
else if (f.isDirectory())
|
|
||||||
for (File child : f.listFiles())
|
|
||||||
deleteFileOrDir(child);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -14,4 +14,12 @@ public class AndroidFileUtils implements FileUtils {
|
|||||||
public long getFreeSpace(File f) throws IOException {
|
public long getFreeSpace(File f) throws IOException {
|
||||||
return f.getUsableSpace();
|
return f.getUsableSpace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void deleteFileOrDir(File f) {
|
||||||
|
if (f.isFile())
|
||||||
|
f.delete();
|
||||||
|
else if (f.isDirectory())
|
||||||
|
for (File child : f.listFiles())
|
||||||
|
deleteFileOrDir(child);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user