Refactor night mode code a bit

This commit is contained in:
Alexander Bakker 2017-12-24 21:42:08 +01:00
parent a140ba8506
commit 782f24342c
7 changed files with 92 additions and 66 deletions

View file

@ -3,7 +3,6 @@ package me.impy.aegis;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -12,7 +11,7 @@ import android.widget.TextView;
import me.impy.aegis.crypto.KeyInfo; import me.impy.aegis.crypto.KeyInfo;
public class AddProfileActivity extends AppCompatActivity { public class AddProfileActivity extends AegisActivity {
private KeyProfile _keyProfile; private KeyProfile _keyProfile;
private EditText _profileName; private EditText _profileName;
@ -21,14 +20,10 @@ public class AddProfileActivity extends AppCompatActivity {
private TextView _textPeriod; private TextView _textPeriod;
private TextView _textOtp; private TextView _textOtp;
private AegisApplication _app;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
_app = (AegisApplication) getApplication();
setPreferredTheme();
setContentView(R.layout.activity_add_profile); setContentView(R.layout.activity_add_profile);
_profileName = findViewById(R.id.addProfileName); _profileName = findViewById(R.id.addProfileName);
@ -60,6 +55,15 @@ public class AddProfileActivity extends AppCompatActivity {
//_profileName.setText(_keyProfile.Info.getAccountName()); //_profileName.setText(_keyProfile.Info.getAccountName());
} }
@Override
protected void setPreferredTheme(boolean nightMode) {
if (nightMode) {
setTheme(R.style.AppTheme_Dark_TransparentActionBar);
} else {
setTheme(R.style.AppTheme_Default_TransparentActionBar);
}
}
private void initializeForm() { private void initializeForm() {
KeyInfo info = _keyProfile.getEntry().getInfo(); KeyInfo info = _keyProfile.getEntry().getInfo();
_profileName.setText(info.getAccountName()); _profileName.setText(info.getAccountName());
@ -81,12 +85,4 @@ public class AddProfileActivity extends AppCompatActivity {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
} }
private void setPreferredTheme() {
if (_app.getPreferences().getBoolean("pref_night_mode", false)) {
setTheme(R.style.AppTheme_Dark_TransparentActionBar);
} else {
setTheme(R.style.AppTheme_Default_TransparentActionBar);
}
}
} }

View file

@ -0,0 +1,23 @@
package me.impy.aegis;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public abstract class AegisActivity extends AppCompatActivity {
private AegisApplication _app;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_app = (AegisApplication) getApplication();
boolean nightMode = _app.getPreferences().getBoolean("pref_night_mode", false);
setPreferredTheme(nightMode);
}
protected AegisApplication getApp() {
return _app;
}
protected abstract void setPreferredTheme(boolean nightMode);
}

View file

@ -4,7 +4,6 @@ import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.hardware.fingerprint.FingerprintManager; import android.hardware.fingerprint.FingerprintManager;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
@ -28,7 +27,7 @@ import me.impy.aegis.crypto.slots.SlotCollection;
import me.impy.aegis.helpers.FingerprintUiHelper; import me.impy.aegis.helpers.FingerprintUiHelper;
import me.impy.aegis.helpers.AuthHelper; import me.impy.aegis.helpers.AuthHelper;
public class AuthActivity extends AppCompatActivity implements FingerprintUiHelper.Callback, SlotCollectionTask.Callback { public class AuthActivity extends AegisActivity implements FingerprintUiHelper.Callback, SlotCollectionTask.Callback {
public static final int RESULT_OK = 0; public static final int RESULT_OK = 0;
public static final int RESULT_EXCEPTION = 1; public static final int RESULT_EXCEPTION = 1;
@ -76,6 +75,15 @@ public class AuthActivity extends AppCompatActivity implements FingerprintUiHelp
}); });
} }
@Override
protected void setPreferredTheme(boolean nightMode) {
if (nightMode) {
setTheme(R.style.AppTheme_Dark);
} else {
setTheme(R.style.AppTheme_Default);
}
}
private void showError() { private void showError() {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Decryption error"); builder.setTitle("Decryption error");

View file

@ -14,7 +14,6 @@ import android.os.Build;
import android.support.design.widget.BottomSheetDialog; import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
@ -41,7 +40,7 @@ import me.impy.aegis.importers.DatabaseImporter;
import me.impy.aegis.helpers.SimpleItemTouchHelperCallback; import me.impy.aegis.helpers.SimpleItemTouchHelperCallback;
import me.impy.aegis.util.ByteInputStream; import me.impy.aegis.util.ByteInputStream;
public class MainActivity extends AppCompatActivity implements KeyProfileAdapter.Listener { public class MainActivity extends AegisActivity implements KeyProfileAdapter.Listener {
// activity request codes // activity request codes
private static final int CODE_GET_KEYINFO = 0; private static final int CODE_GET_KEYINFO = 0;
private static final int CODE_ADD_KEYINFO = 1; private static final int CODE_ADD_KEYINFO = 1;
@ -68,14 +67,6 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
_app = (AegisApplication) getApplication(); _app = (AegisApplication) getApplication();
_db = _app.getDatabaseManager(); _db = _app.getDatabaseManager();
// set the theme
if (_app.getPreferences().getBoolean("pref_night_mode", false)) {
_nightMode = true;
setTheme(R.style.AppTheme_Dark_NoActionBar);
} else {
setPreferredTheme();
}
// set up the main view // set up the main view
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar); Toolbar toolbar = findViewById(R.id.toolbar);
@ -108,9 +99,7 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
try { try {
_db.load(); _db.load();
if (!_db.isUnlocked()) { if (!_db.isUnlocked()) {
Intent intent = new Intent(this, AuthActivity.class); startAuthActivity();
intent.putExtra("slots", _db.getFile().getSlots());
startActivityForResult(intent, CODE_DECRYPT);
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// start the intro if the db file was not found // start the intro if the db file was not found
@ -130,6 +119,16 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
} }
} }
@Override
protected void setPreferredTheme(boolean nightMode) {
if (nightMode) {
setTheme(R.style.AppTheme_Dark_NoActionBar);
} else if (_nightMode) {
setTheme(R.style.AppTheme_Default_NoActionBar);
}
_nightMode = nightMode;
}
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) { switch (requestCode) {
@ -353,7 +352,7 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Toast.makeText(this, "An error occurred while trying to load/decrypt the database", Toast.LENGTH_LONG).show(); Toast.makeText(this, "An error occurred while trying to load/decrypt the database", Toast.LENGTH_LONG).show();
recreate(); startAuthActivity();
return; return;
} }
@ -367,7 +366,7 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Toast.makeText(this, "An error occurred while trying to decrypt the database", Toast.LENGTH_LONG).show(); Toast.makeText(this, "An error occurred while trying to decrypt the database", Toast.LENGTH_LONG).show();
recreate(); startAuthActivity();
return; return;
} }
@ -400,7 +399,12 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
setPreferredTheme();
boolean nightMode = _app.getPreferences().getBoolean("pref_night_mode", false);
if (nightMode != _nightMode) {
setPreferredTheme(nightMode);
recreate();
}
} }
private BottomSheetDialog createBottomSheet(KeyProfile profile) { private BottomSheetDialog createBottomSheet(KeyProfile profile) {
@ -485,15 +489,19 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
e.printStackTrace(); e.printStackTrace();
Toast.makeText(this, "An error occurred while trying to lock the database", Toast.LENGTH_LONG).show(); Toast.makeText(this, "An error occurred while trying to lock the database", Toast.LENGTH_LONG).show();
} }
Intent intent = new Intent(this, AuthActivity.class); startAuthActivity();
intent.putExtra("slots", _db.getFile().getSlots());
startActivityForResult(intent, CODE_DECRYPT);
return true; return true;
default: default:
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
} }
private void startAuthActivity() {
Intent intent = new Intent(this, AuthActivity.class);
intent.putExtra("slots", _db.getFile().getSlots());
startActivityForResult(intent, CODE_DECRYPT);
}
private void initializeAppShortcuts() { private void initializeAppShortcuts() {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) { if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
return; return;
@ -523,23 +531,6 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
} }
} }
private void setPreferredTheme() {
boolean restart = false;
if (_app.getPreferences().getBoolean("pref_night_mode", false)) {
if (!_nightMode) {
setTheme(R.style.AppTheme_Dark_NoActionBar);
restart = true;
}
} else if (_nightMode) {
setTheme(R.style.AppTheme_Default_NoActionBar);
restart = true;
}
if (restart) {
recreate();
}
}
private void saveDatabase() { private void saveDatabase() {
try { try {
_db.save(); _db.save();
@ -564,7 +555,7 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
} }
private void updateLockIcon() { private void updateLockIcon() {
// hide the lock icon if the database is not encrypted // hide the lock icon if the database is not unlocked
if (_menu != null && _db.isUnlocked()) { if (_menu != null && _db.isUnlocked()) {
MenuItem item = _menu.findItem(R.id.action_lock); MenuItem item = _menu.findItem(R.id.action_lock);
item.setVisible(_db.getFile().isEncrypted()); item.setVisible(_db.getFile().isEncrypted());

View file

@ -7,23 +7,23 @@ import android.preference.PreferenceFragment;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.widget.Toast; import android.widget.Toast;
public class PreferencesActivity extends AppCompatActivity { public class PreferencesActivity extends AegisActivity {
public static final int ACTION_EXPORT = 0; public static final int ACTION_EXPORT = 0;
private AegisApplication _app; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new PreferencesFragment()).commit();
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { protected void setPreferredTheme(boolean nightMode) {
super.onCreate(savedInstanceState); if (nightMode) {
_app = (AegisApplication) getApplication();
if (_app.getPreferences().getBoolean("pref_night_mode", false)) {
setTheme(R.style.AppTheme_Dark); setTheme(R.style.AppTheme_Dark);
} else { } else {
setTheme(R.style.AppTheme_Default); setTheme(R.style.AppTheme_Default);
} }
getFragmentManager().beginTransaction().replace(android.R.id.content, new PreferencesFragment()).commit();
} }
public static class PreferencesFragment extends PreferenceFragment { public static class PreferencesFragment extends PreferenceFragment {

View file

@ -20,13 +20,13 @@ import me.impy.aegis.crypto.KeyInfo;
import me.impy.aegis.db.DatabaseEntry; import me.impy.aegis.db.DatabaseEntry;
import me.impy.aegis.helpers.SquareFinderView; import me.impy.aegis.helpers.SquareFinderView;
public class ScannerActivity extends Activity implements ZXingScannerView.ResultHandler { public class ScannerActivity extends AegisActivity implements ZXingScannerView.ResultHandler {
private static final int CODE_ASK_PERMS = 0; private static final int CODE_ASK_PERMS = 0;
private ZXingScannerView _scannerView; private ZXingScannerView _scannerView;
@Override @Override
public void onCreate(Bundle state) { protected void onCreate(Bundle state) {
super.onCreate(state); super.onCreate(state);
_scannerView = new ZXingScannerView(this) { _scannerView = new ZXingScannerView(this) {
@ -42,6 +42,11 @@ public class ScannerActivity extends Activity implements ZXingScannerView.Result
ActivityCompat.requestPermissions(ScannerActivity.this, new String[]{Manifest.permission.CAMERA}, CODE_ASK_PERMS); ActivityCompat.requestPermissions(ScannerActivity.this, new String[]{Manifest.permission.CAMERA}, CODE_ASK_PERMS);
} }
@Override
protected void setPreferredTheme(boolean nightMode) {
}
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();

View file

@ -15,6 +15,7 @@
tools:layout_editor_absoluteX="8dp"> tools:layout_editor_absoluteX="8dp">
<TextView <TextView
android:text="@string/authentication" android:text="@string/authentication"
android:textColor="?attr/primaryText"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textSize="24sp" android:textSize="24sp"
@ -28,7 +29,8 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/textView4" android:id="@+id/textView4"
android:text="@string/authentication_enter_password"/> android:text="@string/authentication_enter_password"
android:textColor="?attr/secondaryText"/>
<EditText <EditText
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -59,7 +61,8 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp" android:layout_marginLeft="15dp"
android:text="Touch sensor"/> android:text="Touch sensor"
android:textColor="?attr/secondaryText"/>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
</android.support.design.widget.CoordinatorLayout> </android.support.design.widget.CoordinatorLayout>