From 6e5a80a0006e59a134d17a0a0648903d0b78154c Mon Sep 17 00:00:00 2001 From: Alexander Bakker Date: Wed, 23 Dec 2020 11:38:57 +0100 Subject: [PATCH] Fix an issue where the app would lock when showing DocumentsUI --- .../beemdevelopment/aegis/AegisApplication.java | 14 +++++++++++++- .../beemdevelopment/aegis/ui/AegisActivity.java | 6 ++++++ .../beemdevelopment/aegis/ui/MainActivity.java | 2 ++ .../aegis/ui/PreferencesFragment.java | 17 ++++++++++++----- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/beemdevelopment/aegis/AegisApplication.java b/app/src/main/java/com/beemdevelopment/aegis/AegisApplication.java index 1ef86ffc..c5057932 100644 --- a/app/src/main/java/com/beemdevelopment/aegis/AegisApplication.java +++ b/app/src/main/java/com/beemdevelopment/aegis/AegisApplication.java @@ -39,6 +39,7 @@ public class AegisApplication extends Application { private VaultManager _manager; private Preferences _prefs; private List _lockListeners; + private boolean _blockAutoLock; private static final String CODE_LOCK_STATUS_ID = "lock_status_channel"; private static final String CODE_LOCK_VAULT_ACTION = "lock_vault"; @@ -135,6 +136,15 @@ public class AegisApplication extends Application { _lockListeners.remove(listener); } + /** + * Sets whether to block automatic lock on minimization. This should only be called + * by activities before invoking an intent that shows a DocumentsUI, because that + * action leads AppLifecycleObserver to believe that the app has been minimized. + */ + public void setBlockAutoLock(boolean block) { + _blockAutoLock = block; + } + /** * Locks the vault and the app. * @param userInitiated whether or not the user initiated the lock in MainActivity. @@ -189,7 +199,9 @@ public class AegisApplication extends Application { private class AppLifecycleObserver implements LifecycleEventObserver { @Override public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) { - if (event == Lifecycle.Event.ON_STOP && isAutoLockEnabled(Preferences.AUTO_LOCK_ON_MINIMIZE)) { + if (event == Lifecycle.Event.ON_STOP + && isAutoLockEnabled(Preferences.AUTO_LOCK_ON_MINIMIZE) + && !_blockAutoLock) { lock(false); } } diff --git a/app/src/main/java/com/beemdevelopment/aegis/ui/AegisActivity.java b/app/src/main/java/com/beemdevelopment/aegis/ui/AegisActivity.java index 0e500819..1fad3570 100644 --- a/app/src/main/java/com/beemdevelopment/aegis/ui/AegisActivity.java +++ b/app/src/main/java/com/beemdevelopment/aegis/ui/AegisActivity.java @@ -55,6 +55,12 @@ public abstract class AegisActivity extends AppCompatActivity implements AegisAp super.onDestroy(); } + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + _app.setBlockAutoLock(false); + } + @Override public void onLocked(boolean userInitiated) { setResult(RESULT_CANCELED, null); diff --git a/app/src/main/java/com/beemdevelopment/aegis/ui/MainActivity.java b/app/src/main/java/com/beemdevelopment/aegis/ui/MainActivity.java index 8f949740..33820408 100644 --- a/app/src/main/java/com/beemdevelopment/aegis/ui/MainActivity.java +++ b/app/src/main/java/com/beemdevelopment/aegis/ui/MainActivity.java @@ -407,6 +407,8 @@ public class MainActivity extends AegisActivity implements EntryListView.Listene } private void startScanImageActivity() { + _app.setBlockAutoLock(true); + Intent galleryIntent = new Intent(Intent.ACTION_PICK); galleryIntent.setDataAndType(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*"); diff --git a/app/src/main/java/com/beemdevelopment/aegis/ui/PreferencesFragment.java b/app/src/main/java/com/beemdevelopment/aegis/ui/PreferencesFragment.java index be2779b7..58704da2 100644 --- a/app/src/main/java/com/beemdevelopment/aegis/ui/PreferencesFragment.java +++ b/app/src/main/java/com/beemdevelopment/aegis/ui/PreferencesFragment.java @@ -87,6 +87,7 @@ public class PreferencesFragment extends PreferenceFragmentCompat { private static final int CODE_BACKUPS = 8; private Intent _result; + private AegisApplication _app; private Preferences _prefs; private VaultManager _vault; @@ -112,9 +113,9 @@ public class PreferencesFragment extends PreferenceFragmentCompat { public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { addPreferencesFromResource(R.xml.preferences); - AegisApplication app = (AegisApplication) getActivity().getApplication(); - _prefs = app.getPreferences(); - _vault = app.getVaultManager(); + _app = (AegisApplication) getActivity().getApplication(); + _prefs = _app.getPreferences(); + _vault = _app.getVaultManager(); // set the result intent in advance setResult(new Intent()); @@ -190,6 +191,8 @@ public class PreferencesFragment extends PreferenceFragmentCompat { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); + + _app.setBlockAutoLock(true); startActivityForResult(intent, CODE_IMPORT); }); return true; @@ -282,14 +285,14 @@ public class PreferencesFragment extends PreferenceFragmentCompat { }); Preference tapToRevealTimePreference = findPreference("pref_tap_to_reveal_time"); - tapToRevealTimePreference.setSummary(app.getPreferences().getTapToRevealTime() + " seconds"); + tapToRevealTimePreference.setSummary(_app.getPreferences().getTapToRevealTime() + " seconds"); tapToRevealTimePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { Dialogs.showNumberPickerDialog(getActivity(), new Dialogs.NumberInputListener() { @Override public void onNumberInputResult(int number) { - app.getPreferences().setTapToRevealTime(number); + _app.getPreferences().setTapToRevealTime(number); tapToRevealTimePreference.setSummary(number + " seconds"); _result.putExtra("needsRefresh", true); } @@ -735,6 +738,7 @@ public class PreferencesFragment extends PreferenceFragmentCompat { .setType(getExportMimeType(requestCode)) .putExtra(Intent.EXTRA_TITLE, fileInfo.toString()); + _app.setBlockAutoLock(true); startActivityForResult(intent, requestCode); }); @@ -771,6 +775,8 @@ public class PreferencesFragment extends PreferenceFragmentCompat { .setType(getExportMimeType(requestCode)) .putExtra(Intent.EXTRA_STREAM, uri); Intent chooser = Intent.createChooser(intent, getString(R.string.pref_export_summary)); + + _app.setBlockAutoLock(true); startActivity(chooser); }); }); @@ -1014,6 +1020,7 @@ public class PreferencesFragment extends PreferenceFragmentCompat { | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION); + _app.setBlockAutoLock(true); startActivityForResult(intent, CODE_BACKUPS); }