2019-02-07 22:39:33 +01:00
|
|
|
package com.beemdevelopment.aegis.ui;
|
2017-12-24 21:42:08 +01:00
|
|
|
|
2019-04-16 22:57:13 +02:00
|
|
|
import android.animation.ValueAnimator;
|
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;
|
2019-04-16 22:57:13 +02:00
|
|
|
import android.provider.Settings;
|
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
|
|
|
|
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;
|
2018-02-13 21:17:21 +01:00
|
|
|
|
2019-05-12 17:12:09 +02:00
|
|
|
import androidx.annotation.CallSuper;
|
2019-04-04 14:07:36 +02:00
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
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);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
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-04-16 22:57:13 +02:00
|
|
|
// apply a dirty hack to make progress bars work even if animations are disabled
|
|
|
|
setGlobalAnimationDurationScale();
|
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) {
|
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
|
|
|
}
|
|
|
|
|
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() {
|
2019-12-25 19:21:34 +01:00
|
|
|
return !(this instanceof MainActivity) && _app.getVaultManager().isLocked();
|
2019-05-12 17:12:09 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 22:57:13 +02:00
|
|
|
private void setGlobalAnimationDurationScale() {
|
|
|
|
float durationScale = Settings.Global.getFloat(getContentResolver(), Settings.Global.ANIMATOR_DURATION_SCALE, 0);
|
|
|
|
if (durationScale != 1) {
|
|
|
|
try {
|
|
|
|
ValueAnimator.class.getMethod("setDurationScale", float.class).invoke(null, 1f);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
Toast.makeText(this, R.string.progressbar_error, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-05 00:24:33 +02:00
|
|
|
|
|
|
|
protected Theme getCurrentTheme() {
|
|
|
|
return _currentTheme;
|
|
|
|
}
|
2017-12-24 21:42:08 +01:00
|
|
|
}
|