2019-02-07 22:39:33 +01:00
|
|
|
package com.beemdevelopment.aegis.ui;
|
2017-12-24 21:42:08 +01:00
|
|
|
|
2019-04-08 23:13:11 +02:00
|
|
|
import android.content.Intent;
|
2019-06-22 09:58:35 +02:00
|
|
|
import android.content.res.Configuration;
|
2017-12-24 21:42:08 +01:00
|
|
|
import android.os.Bundle;
|
2018-05-09 15:49:32 +02:00
|
|
|
import android.view.WindowManager;
|
2019-04-16 22:57:13 +02:00
|
|
|
import android.widget.Toast;
|
2017-12-24 21:42:08 +01:00
|
|
|
|
2020-05-02 18:51:33 +02:00
|
|
|
import androidx.annotation.CallSuper;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
2019-02-07 22:39:33 +01:00
|
|
|
import com.beemdevelopment.aegis.AegisApplication;
|
|
|
|
import com.beemdevelopment.aegis.Preferences;
|
|
|
|
import com.beemdevelopment.aegis.R;
|
2019-03-28 00:54:30 +01:00
|
|
|
import com.beemdevelopment.aegis.Theme;
|
2020-05-02 18:51:33 +02:00
|
|
|
import com.beemdevelopment.aegis.vault.VaultManagerException;
|
2019-04-04 14:07:36 +02:00
|
|
|
|
2019-06-22 09:58:35 +02:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2019-05-12 17:12:09 +02:00
|
|
|
public abstract class AegisActivity extends AppCompatActivity implements AegisApplication.LockListener {
|
2019-05-26 21:45:36 +02:00
|
|
|
private boolean _resumed;
|
2017-12-24 21:42:08 +01:00
|
|
|
private AegisApplication _app;
|
2019-09-05 00:24:33 +02:00
|
|
|
private Theme _currentTheme;
|
2017-12-24 21:42:08 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
_app = (AegisApplication) getApplication();
|
|
|
|
|
2019-06-22 09:58:35 +02:00
|
|
|
// set the theme and locale before creating the activity
|
|
|
|
Preferences prefs = getPreferences();
|
|
|
|
setPreferredTheme(prefs.getCurrentTheme());
|
|
|
|
setLocale(prefs.getLocale());
|
2019-05-15 22:01:00 +02:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
2019-04-08 23:13:11 +02:00
|
|
|
// if the app was killed, relaunch MainActivity and close everything else
|
2019-05-12 17:12:09 +02:00
|
|
|
if (savedInstanceState != null && isOrphan()) {
|
2019-04-08 23:13:11 +02:00
|
|
|
Intent intent = new Intent(this, MainActivity.class);
|
2020-06-19 17:14:06 +02:00
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
2019-04-08 23:13:11 +02:00
|
|
|
startActivity(intent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-09 15:49:32 +02:00
|
|
|
// set FLAG_SECURE on the window of every AegisActivity
|
2018-05-11 20:08:51 +02:00
|
|
|
if (getPreferences().isSecureScreenEnabled()) {
|
2018-05-11 19:33:20 +02:00
|
|
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
|
|
|
|
}
|
2018-05-09 15:49:32 +02:00
|
|
|
|
2019-05-12 17:12:09 +02:00
|
|
|
// register a callback to listen for lock events
|
|
|
|
_app.registerLockListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
_app.unregisterLockListener(this);
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:45:36 +02:00
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
_resumed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
_resumed = false;
|
|
|
|
}
|
|
|
|
|
2019-05-12 17:12:09 +02:00
|
|
|
@CallSuper
|
|
|
|
@Override
|
|
|
|
public void onLocked() {
|
|
|
|
if (isOrphan()) {
|
|
|
|
setResult(RESULT_CANCELED, null);
|
|
|
|
finish();
|
|
|
|
}
|
2017-12-24 21:42:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected AegisApplication getApp() {
|
|
|
|
return _app;
|
|
|
|
}
|
|
|
|
|
2018-05-11 20:08:51 +02:00
|
|
|
protected Preferences getPreferences() {
|
|
|
|
return _app.getPreferences();
|
2018-05-11 15:12:36 +02:00
|
|
|
}
|
|
|
|
|
2019-03-28 00:54:30 +01:00
|
|
|
protected void setPreferredTheme(Theme theme) {
|
2020-04-20 15:29:41 +02:00
|
|
|
if (theme == Theme.SYSTEM || theme == Theme.SYSTEM_AMOLED) {
|
|
|
|
// set the theme based on the system theme
|
|
|
|
int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
|
|
|
|
switch (currentNightMode) {
|
|
|
|
case Configuration.UI_MODE_NIGHT_NO:
|
|
|
|
theme = Theme.LIGHT;
|
|
|
|
break;
|
|
|
|
case Configuration.UI_MODE_NIGHT_YES:
|
|
|
|
theme = theme == Theme.SYSTEM_AMOLED ? Theme.AMOLED : Theme.DARK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 00:24:33 +02:00
|
|
|
_currentTheme = theme;
|
|
|
|
|
2019-03-28 00:54:30 +01:00
|
|
|
switch (theme) {
|
|
|
|
case LIGHT:
|
|
|
|
setTheme(R.style.AppTheme);
|
|
|
|
break;
|
|
|
|
case DARK:
|
|
|
|
setTheme(R.style.AppTheme_Dark);
|
|
|
|
break;
|
|
|
|
case AMOLED:
|
|
|
|
setTheme(R.style.AppTheme_TrueBlack);
|
|
|
|
break;
|
2018-05-11 21:53:06 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-16 22:57:13 +02:00
|
|
|
|
2019-06-22 09:58:35 +02:00
|
|
|
protected void setLocale(Locale locale) {
|
|
|
|
Locale.setDefault(locale);
|
|
|
|
|
|
|
|
Configuration config = new Configuration();
|
|
|
|
config.locale = locale;
|
|
|
|
|
2020-01-12 16:45:05 +01:00
|
|
|
this.getResources().updateConfiguration(config, this.getResources().getDisplayMetrics());
|
2019-06-22 09:58:35 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 12:45:02 +02:00
|
|
|
protected boolean saveVault(boolean backup) {
|
2020-05-02 18:51:33 +02:00
|
|
|
try {
|
2020-06-13 12:45:02 +02:00
|
|
|
getApp().getVaultManager().save(backup);
|
2020-05-02 18:51:33 +02:00
|
|
|
return true;
|
|
|
|
} catch (VaultManagerException e) {
|
|
|
|
Toast.makeText(this, getString(R.string.saving_error), Toast.LENGTH_LONG).show();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:45:36 +02:00
|
|
|
/**
|
|
|
|
* Reports whether this Activity has been resumed. (i.e. onResume was called)
|
|
|
|
*/
|
|
|
|
protected boolean isOpen() {
|
|
|
|
return _resumed;
|
|
|
|
}
|
|
|
|
|
2019-05-12 17:12:09 +02:00
|
|
|
/**
|
|
|
|
* Reports whether this Activity instance has become an orphan. This can happen if
|
|
|
|
* the vault was locked by an external trigger while the Activity was still open.
|
|
|
|
*/
|
|
|
|
private boolean isOrphan() {
|
2020-06-18 17:21:08 +02:00
|
|
|
return !(this instanceof MainActivity) && !(this instanceof AuthActivity) && _app.isVaultLocked();
|
2019-05-12 17:12:09 +02:00
|
|
|
}
|
|
|
|
|
2019-09-05 00:24:33 +02:00
|
|
|
protected Theme getCurrentTheme() {
|
|
|
|
return _currentTheme;
|
|
|
|
}
|
2017-12-24 21:42:08 +01:00
|
|
|
}
|