2019-02-07 22:39:33 +01:00
|
|
|
package com.beemdevelopment.aegis.ui;
|
2017-12-24 21:42:08 +01:00
|
|
|
|
2020-12-27 17:37:00 +01:00
|
|
|
import android.content.ActivityNotFoundException;
|
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-12-27 17:37:00 +01:00
|
|
|
import androidx.annotation.CallSuper;
|
|
|
|
import androidx.annotation.Nullable;
|
2020-05-02 18:51:33 +02:00
|
|
|
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-07-11 18:42:44 +02:00
|
|
|
import com.beemdevelopment.aegis.ThemeMap;
|
2021-01-27 13:54:11 +01:00
|
|
|
import com.beemdevelopment.aegis.ui.dialogs.Dialogs;
|
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;
|
2020-07-11 18:42:44 +02:00
|
|
|
import java.util.Map;
|
2019-06-22 09:58:35 +02:00
|
|
|
|
2019-05-12 17:12:09 +02:00
|
|
|
public abstract class AegisActivity extends AppCompatActivity implements AegisApplication.LockListener {
|
2017-12-24 21:42:08 +01:00
|
|
|
private AegisApplication _app;
|
|
|
|
|
|
|
|
@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();
|
2020-07-11 18:42:44 +02:00
|
|
|
onSetTheme();
|
2019-06-22 09:58:35 +02:00
|
|
|
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);
|
2020-08-12 21:33:25 +02:00
|
|
|
finish();
|
2019-04-08 23:13:11 +02:00
|
|
|
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
|
2021-01-19 20:26:45 +01:00
|
|
|
@CallSuper
|
2019-05-12 17:12:09 +02:00
|
|
|
protected void onDestroy() {
|
|
|
|
_app.unregisterLockListener(this);
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2020-12-27 17:37:00 +01:00
|
|
|
@CallSuper
|
2020-12-23 11:38:57 +01:00
|
|
|
@Override
|
2020-12-27 17:37:00 +01:00
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
2020-12-23 11:38:57 +01:00
|
|
|
_app.setBlockAutoLock(false);
|
|
|
|
}
|
|
|
|
|
2020-12-27 17:37:00 +01:00
|
|
|
@Override
|
|
|
|
public void startActivityForResult(Intent intent, int requestCode, Bundle bundle) {
|
|
|
|
if (isAutoLockBypassedForAction(intent.getAction())) {
|
|
|
|
_app.setBlockAutoLock(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
super.startActivityForResult(intent, requestCode, bundle);
|
|
|
|
} catch (ActivityNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
if (isDocsAction(intent.getAction())) {
|
|
|
|
Dialogs.showErrorDialog(this, R.string.documentsui_error, e);
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:45:36 +02:00
|
|
|
@Override
|
2020-08-12 21:33:25 +02:00
|
|
|
public void onLocked(boolean userInitiated) {
|
|
|
|
setResult(RESULT_CANCELED, null);
|
|
|
|
finishAndRemoveTask();
|
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
|
|
|
}
|
|
|
|
|
2020-07-11 18:42:44 +02:00
|
|
|
/**
|
|
|
|
* Called when the activity is expected to set its theme.
|
|
|
|
*/
|
|
|
|
protected void onSetTheme() {
|
|
|
|
setTheme(ThemeMap.DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the theme of the activity. The actual style that is set is picked from the
|
|
|
|
* given map, based on the theme configured by the user.
|
|
|
|
*/
|
|
|
|
protected void setTheme(Map<Theme, Integer> themeMap) {
|
|
|
|
int theme = themeMap.get(getConfiguredTheme());
|
|
|
|
setTheme(theme);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Theme getConfiguredTheme() {
|
|
|
|
Theme theme = getPreferences().getCurrentTheme();
|
|
|
|
|
2020-04-20 15:29:41 +02:00
|
|
|
if (theme == Theme.SYSTEM || theme == Theme.SYSTEM_AMOLED) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-11 18:42:44 +02:00
|
|
|
return theme;
|
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-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.
|
|
|
|
*/
|
2020-07-05 19:49:51 +02:00
|
|
|
protected boolean isOrphan() {
|
2020-07-01 19:44:58 +02:00
|
|
|
return !(this instanceof MainActivity) && !(this instanceof AuthActivity) && !(this instanceof IntroActivity) && _app.isVaultLocked();
|
2019-05-12 17:12:09 +02:00
|
|
|
}
|
2020-12-27 17:37:00 +01:00
|
|
|
|
|
|
|
private static boolean isDocsAction(@Nullable String action) {
|
|
|
|
return action != null && (action.equals(Intent.ACTION_GET_CONTENT)
|
|
|
|
|| action.equals(Intent.ACTION_CREATE_DOCUMENT)
|
|
|
|
|| action.equals(Intent.ACTION_OPEN_DOCUMENT)
|
|
|
|
|| action.equals(Intent.ACTION_OPEN_DOCUMENT_TREE));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isAutoLockBypassedForAction(@Nullable String action) {
|
|
|
|
return isDocsAction(action) || (action != null && (action.equals(Intent.ACTION_PICK)
|
|
|
|
|| action.equals(Intent.ACTION_SEND)
|
|
|
|
|| action.equals(Intent.ACTION_CHOOSER)));
|
|
|
|
}
|
2017-12-24 21:42:08 +01:00
|
|
|
}
|