Consistent styling for setup and password activities.

Also removed some unused code from BaseActivity.
This commit is contained in:
akwizgran
2016-01-08 13:21:20 +00:00
parent 90214c7f93
commit 7e48267f92
5 changed files with 67 additions and 73 deletions

View File

@@ -40,7 +40,6 @@
android:imeOptions="actionDone" android:imeOptions="actionDone"
android:inputType="textPassword" android:inputType="textPassword"
android:maxLines="1" /> android:maxLines="1" />
</android.support.design.widget.TextInputLayout> </android.support.design.widget.TextInputLayout>
<Button <Button
@@ -61,7 +60,6 @@
android:layout_alignTop="@id/btn_sign_in" android:layout_alignTop="@id/btn_sign_in"
android:layout_alignBottom="@id/btn_sign_in" android:layout_alignBottom="@id/btn_sign_in"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:gravity="center"
android:visibility="invisible" /> android:visibility="invisible" />
<TextView <TextView

View File

@@ -5,10 +5,9 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<LinearLayout <RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" android:orientation="vertical"
android:paddingBottom="@dimen/margin_activity_vertical" android:paddingBottom="@dimen/margin_activity_vertical"
android:paddingEnd="@dimen/margin_activity_horizontal" android:paddingEnd="@dimen/margin_activity_horizontal"
@@ -18,9 +17,11 @@
android:paddingTop="@dimen/margin_activity_vertical"> android:paddingTop="@dimen/margin_activity_vertical">
<TextView <TextView
android:id="@+id/nickname_title"
style="@style/BriarTextTitle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:layout_centerHorizontal="true"
android:text="@string/choose_nickname" android:text="@string/choose_nickname"
android:textSize="@dimen/text_size_medium"/> android:textSize="@dimen/text_size_medium"/>
@@ -28,6 +29,8 @@
android:id="@+id/nickname_entry_wrapper" android:id="@+id/nickname_entry_wrapper"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/nickname_title"
app:errorEnabled="true"> app:errorEnabled="true">
<EditText <EditText
@@ -39,9 +42,12 @@
</android.support.design.widget.TextInputLayout> </android.support.design.widget.TextInputLayout>
<TextView <TextView
android:id="@+id/password_title"
style="@style/BriarTextTitle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:layout_centerHorizontal="true"
android:layout_below="@id/nickname_entry_wrapper"
android:text="@string/choose_password" android:text="@string/choose_password"
android:textSize="@dimen/text_size_medium"/> android:textSize="@dimen/text_size_medium"/>
@@ -49,6 +55,8 @@
android:id="@+id/password_entry_wrapper" android:id="@+id/password_entry_wrapper"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/password_title"
app:errorEnabled="true"> app:errorEnabled="true">
<EditText <EditText
@@ -60,9 +68,12 @@
</android.support.design.widget.TextInputLayout> </android.support.design.widget.TextInputLayout>
<TextView <TextView
android:id="@+id/password_confirm_title"
style="@style/BriarTextTitle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:layout_centerHorizontal="true"
android:layout_below="@id/password_entry_wrapper"
android:text="@string/confirm_password" android:text="@string/confirm_password"
android:textSize="@dimen/text_size_medium"/> android:textSize="@dimen/text_size_medium"/>
@@ -70,6 +81,8 @@
android:id="@+id/password_confirm_wrapper" android:id="@+id/password_confirm_wrapper"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/password_confirm_title"
app:errorEnabled="true"> app:errorEnabled="true">
<EditText <EditText
@@ -85,20 +98,31 @@
android:id="@+id/strength_meter" android:id="@+id/strength_meter"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/password_confirm_wrapper"
android:visibility="invisible"/> android:visibility="invisible"/>
<Button <Button
android:id="@+id/create_account" android:id="@+id/create_account"
style="@style/BriarButton.Default"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/strength_meter"
android:layout_marginTop="@dimen/margin_medium"
android:enabled="false" android:enabled="false"
android:text="@string/create_account_button"/> android:text="@string/create_account_button"/>
<ProgressBar <ProgressBar
android:id="@+id/progress_wheel" android:id="@+id/progress_wheel"
style="?android:attr/progressBarStyleInverse"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:indeterminate="true" android:layout_alignTop="@id/create_account"
android:visibility="gone"/> android:layout_alignBottom="@id/create_account"
</LinearLayout> android:layout_centerHorizontal="true"
android:visibility="invisible" />
</RelativeLayout>
</ScrollView> </ScrollView>

View File

@@ -4,6 +4,7 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.view.View; import android.view.View;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
@@ -31,7 +32,6 @@ import roboguice.inject.RoboInjector;
import roboguice.util.RoboContext; import roboguice.util.RoboContext;
import static android.view.WindowManager.LayoutParams.FLAG_SECURE; import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
import static android.view.inputmethod.InputMethodManager.HIDE_IMPLICIT_ONLY;
import static android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT; import static android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT;
import static org.briarproject.android.TestingConstants.PREVENT_SCREENSHOTS; import static org.briarproject.android.TestingConstants.PREVENT_SCREENSHOTS;
@@ -39,7 +39,7 @@ public abstract class BaseActivity extends AppCompatActivity
implements RoboContext { implements RoboContext {
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 PREFS_KEY = "key";
private final HashMap<Key<?>, Object> scopedObjects = private final HashMap<Key<?>, Object> scopedObjects =
new HashMap<Key<?>, Object>(); new HashMap<Key<?>, Object>();
@@ -128,38 +128,24 @@ public abstract class BaseActivity extends AppCompatActivity
return scopedObjects; return scopedObjects;
} }
private SharedPreferences getBriarPrefs(String prefsName) { private SharedPreferences getSharedPrefs() {
return getSharedPreferences(prefsName, MODE_PRIVATE); return getSharedPreferences(PREFS_DB, MODE_PRIVATE);
} }
protected String getDbKeyInHex() { protected String getEncryptedDatabaseKey() {
return getBriarPrefs(PREFS_DB).getString(KEY_DB_KEY, null); return getSharedPrefs().getString(PREFS_KEY, null);
} }
private void clearPrefs(String prefsName) { protected void storeEncryptedDatabaseKey(final String hex) {
SharedPreferences.Editor editor = getBriarPrefs(prefsName).edit(); SharedPreferences.Editor editor = getSharedPrefs().edit();
editor.clear(); editor.putString(PREFS_KEY, hex);
editor.apply(); editor.apply();
} }
protected void clearDbPrefs() { protected void clearSharedPrefs() {
clearPrefs(PREFS_DB); SharedPreferences.Editor editor = getSharedPrefs().edit();
} editor.clear();
editor.apply();
protected void gotoAndFinish(Class classInstance, int resultCode) {
if (resultCode != Integer.MIN_VALUE)
setResult(resultCode);
startActivity(new Intent(this, classInstance));
finish();
}
protected void gotoAndFinish(Class classInstance) {
gotoAndFinish(classInstance, Integer.MIN_VALUE);
}
protected void toggleSoftKeyboard() {
Object o = getSystemService(INPUT_METHOD_SERVICE);
((InputMethodManager) o).toggleSoftInput(HIDE_IMPLICIT_ONLY, 0);
} }
protected void showSoftKeyboard(View view) { protected void showSoftKeyboard(View view) {
@@ -168,8 +154,8 @@ public abstract class BaseActivity extends AppCompatActivity
} }
protected void hideSoftKeyboard(View view) { protected void hideSoftKeyboard(View view) {
IBinder token = view.getWindowToken();
Object o = getSystemService(INPUT_METHOD_SERVICE); Object o = getSystemService(INPUT_METHOD_SERVICE);
((InputMethodManager) o).hideSoftInputFromWindow(view.getWindowToken(), ((InputMethodManager) o).hideSoftInputFromWindow(token, 0);
0);
} }
} }

View File

@@ -38,7 +38,6 @@ public class PasswordActivity extends BaseActivity {
@Inject @CryptoExecutor private Executor cryptoExecutor; @Inject @CryptoExecutor private Executor cryptoExecutor;
private Button signInButton; private Button signInButton;
private ProgressBar progress; private ProgressBar progress;
private TextView title;
private TextInputLayout input; private TextInputLayout input;
private EditText password; private EditText password;
@@ -52,9 +51,9 @@ public class PasswordActivity extends BaseActivity {
public void onCreate(Bundle state) { public void onCreate(Bundle state) {
super.onCreate(state); super.onCreate(state);
String hex = getDbKeyInHex(); String hex = getEncryptedDatabaseKey();
if (hex == null || !databaseConfig.databaseExists()) { if (hex == null || !databaseConfig.databaseExists()) {
clearDbPrefs(); clearSharedPrefsAndDeleteDatabase();
return; return;
} }
encrypted = StringUtils.fromHexString(hex); encrypted = StringUtils.fromHexString(hex);
@@ -62,7 +61,6 @@ public class PasswordActivity extends BaseActivity {
setContentView(R.layout.activity_password); setContentView(R.layout.activity_password);
signInButton = (Button) findViewById(R.id.btn_sign_in); signInButton = (Button) findViewById(R.id.btn_sign_in);
progress = (ProgressBar) findViewById(R.id.progress_wheel); progress = (ProgressBar) findViewById(R.id.progress_wheel);
title = (TextView) findViewById(R.id.title_password);
input = (TextInputLayout) findViewById(R.id.password_layout); input = (TextInputLayout) findViewById(R.id.password_layout);
password = (EditText) findViewById(R.id.edit_password); password = (EditText) findViewById(R.id.edit_password);
password.setOnEditorActionListener(new OnEditorActionListener() { password.setOnEditorActionListener(new OnEditorActionListener() {
@@ -98,11 +96,12 @@ public class PasswordActivity extends BaseActivity {
startActivity(intent); startActivity(intent);
} }
@Override private void clearSharedPrefsAndDeleteDatabase() {
protected void clearDbPrefs() { clearSharedPrefs();
super.clearDbPrefs();
FileUtils.deleteFileOrDir(databaseConfig.getDatabaseDirectory()); FileUtils.deleteFileOrDir(databaseConfig.getDatabaseDirectory());
gotoAndFinish(SetupActivity.class, RESULT_CANCELED); setResult(RESULT_CANCELED);
startActivity(new Intent(this, SetupActivity.class));
finish();
} }
public void onSignInClick(View v) { public void onSignInClick(View v) {
@@ -115,12 +114,13 @@ public class PasswordActivity extends BaseActivity {
builder.setTitle(R.string.dialog_title_lost_password); builder.setTitle(R.string.dialog_title_lost_password);
builder.setMessage(R.string.dialog_message_lost_password); builder.setMessage(R.string.dialog_message_lost_password);
builder.setNegativeButton(R.string.no, null); builder.setNegativeButton(R.string.no, null);
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { builder.setPositiveButton(R.string.yes,
@Override new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { @Override
clearDbPrefs(); public void onClick(DialogInterface dialog, int which) {
} clearSharedPrefsAndDeleteDatabase();
}); }
});
AlertDialog dialog = builder.create(); AlertDialog dialog = builder.create();
dialog.show(); dialog.show();
} }

View File

@@ -1,8 +1,6 @@
package org.briarproject.android; package org.briarproject.android;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.TextInputLayout; import android.support.design.widget.TextInputLayout;
import android.text.Editable; import android.text.Editable;
@@ -38,7 +36,6 @@ import javax.inject.Inject;
import roboguice.inject.InjectView; import roboguice.inject.InjectView;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static android.view.View.GONE;
import static android.view.View.INVISIBLE; import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE; import static android.view.View.VISIBLE;
import static android.view.WindowManager.LayoutParams.FLAG_SECURE; import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
@@ -134,8 +131,8 @@ public class SetupActivity extends BaseActivity implements OnClickListener,
} }
public void onClick(View view) { public void onClick(View view) {
// Replace the feedback text and button with a progress bar // Replace the button with a progress bar
createAccountButton.setVisibility(GONE); createAccountButton.setVisibility(INVISIBLE);
progress.setVisibility(VISIBLE); progress.setVisibility(VISIBLE);
final String nickname = nicknameEntry.getText().toString(); final String nickname = nicknameEntry.getText().toString();
final String password = passwordEntry.getText().toString(); final String password = passwordEntry.getText().toString();
@@ -144,8 +141,8 @@ public class SetupActivity extends BaseActivity implements OnClickListener,
public void run() { public void run() {
SecretKey key = crypto.generateSecretKey(); SecretKey key = crypto.generateSecretKey();
databaseConfig.setEncryptionKey(key); databaseConfig.setEncryptionKey(key);
byte[] encrypted = encryptDatabaseKey(key, password); String hex = encryptDatabaseKey(key, password);
storeEncryptedDatabaseKey(encrypted); storeEncryptedDatabaseKey(hex);
LocalAuthor localAuthor = createLocalAuthor(nickname); LocalAuthor localAuthor = createLocalAuthor(nickname);
showDashboard(referenceManager.putReference(localAuthor, showDashboard(referenceManager.putReference(localAuthor,
LocalAuthor.class)); LocalAuthor.class));
@@ -153,24 +150,13 @@ public class SetupActivity extends BaseActivity implements OnClickListener,
}); });
} }
private void storeEncryptedDatabaseKey(final byte[] encrypted) { private String encryptDatabaseKey(SecretKey key, String password) {
long now = System.currentTimeMillis();
SharedPreferences prefs = getSharedPreferences("db", MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("key", StringUtils.toHexString(encrypted));
editor.commit();
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Key storage took " + duration + " ms");
}
private byte[] encryptDatabaseKey(SecretKey key, String password) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
byte[] encrypted = crypto.encryptWithPassword(key.getBytes(), password); byte[] encrypted = crypto.encryptWithPassword(key.getBytes(), password);
long duration = System.currentTimeMillis() - now; long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO)) if (LOG.isLoggable(INFO))
LOG.info("Key derivation took " + duration + " ms"); LOG.info("Key derivation took " + duration + " ms");
return encrypted; return StringUtils.toHexString(encrypted);
} }
private LocalAuthor createLocalAuthor(String nickname) { private LocalAuthor createLocalAuthor(String nickname) {