Skip to content

Commit

Permalink
Merge pull request #1429 from r3dh3ck/fix/preferences_result_location
Browse files Browse the repository at this point in the history
Remove preferences result
  • Loading branch information
alexbakker authored Jul 23, 2024
2 parents a10693e + f796e45 commit 29eccaf
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 122 deletions.
42 changes: 6 additions & 36 deletions app/src/main/java/com/beemdevelopment/aegis/ui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,9 @@
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.beemdevelopment.aegis.AccountNamePosition;
import com.beemdevelopment.aegis.CopyBehavior;
import com.beemdevelopment.aegis.Preferences;
import com.beemdevelopment.aegis.R;
import com.beemdevelopment.aegis.SortCategory;
import com.beemdevelopment.aegis.ViewMode;
import com.beemdevelopment.aegis.helpers.FabScrollHelper;
import com.beemdevelopment.aegis.helpers.PermissionHelper;
import com.beemdevelopment.aegis.otp.GoogleAuthInfo;
Expand Down Expand Up @@ -136,12 +133,7 @@ public class MainActivity extends AegisActivity implements EntryListView.Listene
});

private final ActivityResultLauncher<Intent> preferenceResultLauncher =
registerForActivityResult(new StartActivityForResult(), activityResult -> {
if (activityResult.getResultCode() != RESULT_OK || activityResult.getData() == null) {
return;
}
onPreferencesResult(activityResult.getData());
});
registerForActivityResult(new StartActivityForResult(), activityResult -> onPreferencesResult());

private final ActivityResultLauncher<Intent> editEntryResultLauncher =
registerForActivityResult(new StartActivityForResult(), activityResult -> {
Expand Down Expand Up @@ -299,34 +291,10 @@ public void onEntryListTouch() {
}
}

private void onPreferencesResult(Intent data) {
private void onPreferencesResult() {
// refresh the entire entry list if needed
if (_loaded) {
if (data.getBooleanExtra("needsRecreate", false)) {
recreate();
} else if (data.getBooleanExtra("needsRefresh", false)) {
AccountNamePosition accountNamePosition = _prefs.getAccountNamePosition();
boolean showIcons = _prefs.isIconVisible();
boolean onlyShowNecessaryAccountNames = _prefs.onlyShowNecessaryAccountNames();
Preferences.CodeGrouping codeGroupSize = _prefs.getCodeGroupSize();
boolean highlightEntry = _prefs.isEntryHighlightEnabled();
boolean pauseFocused = _prefs.isPauseFocusedEnabled();
boolean tapToReveal = _prefs.isTapToRevealEnabled();
int tapToRevealTime = _prefs.getTapToRevealTime();
ViewMode viewMode = _prefs.getCurrentViewMode();
CopyBehavior copyBehavior = _prefs.getCopyBehavior();
_entryListView.setAccountNamePosition(accountNamePosition);
_entryListView.setOnlyShowNecessaryAccountNames(onlyShowNecessaryAccountNames);
_entryListView.setShowIcon(showIcons);
_entryListView.setCodeGroupSize(codeGroupSize);
_entryListView.setHighlightEntry(highlightEntry);
_entryListView.setPauseFocused(pauseFocused);
_entryListView.setTapToReveal(tapToReveal);
_entryListView.setTapToRevealTime(tapToRevealTime);
_entryListView.setViewMode(viewMode);
_entryListView.setCopyBehavior(copyBehavior);
_entryListView.refresh(true);
}
recreate();
}
}

Expand Down Expand Up @@ -871,7 +839,9 @@ private void loadEntries() {
_entryListView.setUsageCounts(_prefs.getUsageCounts());
_entryListView.setLastUsedTimestamps(_prefs.getLastUsedTimestamps());
_entryListView.addEntries(_vaultManager.getVault().getEntries());
_entryListView.runEntriesAnimation();
if (!_isRecreated) {
_entryListView.runEntriesAnimation();
}
_loaded = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,8 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

@Override
protected void onRestoreInstanceState(@NonNull final Bundle inState) {
if (_fragment instanceof PreferencesFragment) {
// pass the stored result intent back to the fragment
if (inState.containsKey("result")) {
((PreferencesFragment) _fragment).setResult(inState.getParcelable("result"));
}
}
super.onRestoreInstanceState(inState);
}

@Override
protected void onSaveInstanceState(@NonNull final Bundle outState) {
if (_fragment instanceof PreferencesFragment) {
// save the result intent of the fragment
// this is done so we don't lose anything if the fragment calls recreate on this activity
outState.putParcelable("result", ((PreferencesFragment) _fragment).getResult());
}
outState.putCharSequence("prefTitle", _prefTitle);
super.onSaveInstanceState(outState);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class AppearancePreferencesFragment extends PreferencesFragment {

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
super.onCreatePreferences(savedInstanceState, rootKey);
addPreferencesFromResource(R.xml.preferences_appearance);

_groupsPreference = requirePreference("pref_groups");
Expand Down Expand Up @@ -62,7 +61,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

dialog.dismiss();

getResult().putExtra("needsRecreate", true);
requireActivity().recreate();
})
.setNegativeButton(android.R.string.cancel, null)
Expand All @@ -74,7 +72,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
Preference dynamicColorsPreference = requirePreference("pref_dynamic_colors");
dynamicColorsPreference.setEnabled(DynamicColors.isDynamicColorAvailable());
dynamicColorsPreference.setOnPreferenceChangeListener((preference, newValue) -> {
getResult().putExtra("needsRecreate", true);
requireActivity().recreate();
return true;
});
Expand All @@ -96,7 +93,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

dialog.dismiss();

getResult().putExtra("needsRecreate", true);
requireActivity().recreate();
})
.setNegativeButton(android.R.string.cancel, null)
Expand All @@ -120,7 +116,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
int i = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
_prefs.setCurrentViewMode(ViewMode.fromInteger(i));
viewModePreference.setSummary(String.format("%s: %s", getString(R.string.selected), getResources().getStringArray(R.array.view_mode_titles)[i]));
getResult().putExtra("needsRefresh", true);
overrideAccountNamePosition(ViewMode.fromInteger(i) == ViewMode.TILES);
dialog.dismiss();
})
Expand All @@ -143,19 +138,12 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
_prefs.setCodeGroupSize(Preferences.CodeGrouping.valueOf(codeGroupings[newCodeGroupingIndex]));

dialog.dismiss();
getResult().putExtra("needsRefresh", true);
})
.setNegativeButton(android.R.string.cancel, null)
.create());
return true;
});

Preference onlyShowNecessaryAccountNames = requirePreference("pref_shared_issuer_account_name");
onlyShowNecessaryAccountNames.setOnPreferenceChangeListener((preference, newValue) -> {
getResult().putExtra("needsRefresh", true);
return true;
});

int currentAccountNamePosition = _prefs.getAccountNamePosition().ordinal();
_currentAccountNamePositionPreference = requirePreference("pref_account_name_position");
_currentAccountNamePositionPreference.setSummary(String.format("%s: %s", getString(R.string.selected), getResources().getStringArray(R.array.account_name_position_titles)[currentAccountNamePosition]));
Expand All @@ -168,7 +156,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
int i = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
_prefs.setAccountNamePosition(AccountNamePosition.fromInteger(i));
_currentAccountNamePositionPreference.setSummary(String.format("%s: %s", getString(R.string.selected), getResources().getStringArray(R.array.account_name_position_titles)[i]));
getResult().putExtra("needsRefresh", true);
dialog.dismiss();
})
.setNegativeButton(android.R.string.cancel, null)
Expand All @@ -177,12 +164,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
return true;
});

Preference showIconsPreference = requirePreference("pref_show_icons");
showIconsPreference.setOnPreferenceChangeListener((preference, newValue) -> {
getResult().putExtra("needsRefresh", true);
return true;
});

overrideAccountNamePosition(_prefs.getCurrentViewMode() == ViewMode.TILES);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public void onResume() {

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
super.onCreatePreferences(savedInstanceState, rootKey);
addPreferencesFromResource(R.xml.preferences_backups);

_backupsPasswordWarningPreference = requirePreference("pref_backups_warning_password");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

public class BehaviorPreferencesFragment extends PreferencesFragment {
private Preference _entryPausePreference;

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
super.onCreatePreferences(savedInstanceState, rootKey);
addPreferencesFromResource(R.xml.preferences_behavior);

int currentCopyBehavior = _prefs.getCopyBehavior().ordinal();
Expand All @@ -30,7 +28,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
int i = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
_prefs.setCopyBehavior(CopyBehavior.fromInteger(i));
copyBehaviorPreference.setSummary(String.format("%s: %s", getString(R.string.selected), getResources().getStringArray(R.array.copy_behavior_titles)[i]));
getResult().putExtra("needsRefresh", true);
dialog.dismiss();
})
.setNegativeButton(android.R.string.cancel, null)
Expand All @@ -39,18 +36,13 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
return true;
});

Preference entryPausePreference = requirePreference("pref_pause_entry");
entryPausePreference.setEnabled(_prefs.isTapToRevealEnabled() || _prefs.isEntryHighlightEnabled());

Preference entryHighlightPreference = requirePreference("pref_highlight_entry");
entryHighlightPreference.setOnPreferenceChangeListener((preference, newValue) -> {
getResult().putExtra("needsRefresh", true);
_entryPausePreference.setEnabled(_prefs.isTapToRevealEnabled() || (boolean) newValue);
return true;
});

_entryPausePreference = requirePreference("pref_pause_entry");
_entryPausePreference.setOnPreferenceChangeListener((preference, newValue) -> {
getResult().putExtra("needsRefresh", true);
entryPausePreference.setEnabled(_prefs.isTapToRevealEnabled() || (boolean) newValue);
return true;
});
_entryPausePreference.setEnabled(_prefs.isTapToRevealEnabled() || _prefs.isEntryHighlightEnabled());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import com.beemdevelopment.aegis.BuildConfig;
import com.beemdevelopment.aegis.R;
import com.beemdevelopment.aegis.database.AuditLogRepository;
import com.beemdevelopment.aegis.helpers.DropdownHelper;
import com.beemdevelopment.aegis.importers.DatabaseImporter;
import com.beemdevelopment.aegis.otp.GoogleAuthInfo;
Expand Down Expand Up @@ -64,18 +63,12 @@
import java.util.stream.Collectors;

import javax.crypto.Cipher;
import javax.inject.Inject;

public class ImportExportPreferencesFragment extends PreferencesFragment {
// keep a reference to the type of database converter that was selected
private DatabaseImporter.Definition _importerDef;
private Vault.EntryFilter _exportFilter;

private final ActivityResultLauncher<Intent> importResultLauncher =
registerForActivityResult(new StartActivityForResult(), activityResult -> {
getResult().putExtra("needsRecreate", true);
});

private final ActivityResultLauncher<Intent> importSelectResultLauncher =
registerForActivityResult(new StartActivityForResult(), activityResult -> {
Intent data = activityResult.getData();
Expand All @@ -102,7 +95,6 @@ public class ImportExportPreferencesFragment extends PreferencesFragment {

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
super.onCreatePreferences(savedInstanceState, rootKey);
addPreferencesFromResource(R.xml.preferences_import_export);

if (savedInstanceState != null) {
Expand Down Expand Up @@ -169,7 +161,7 @@ private void startImportEntriesActivity(DatabaseImporter.Definition importerDef,
Intent intent = new Intent(requireContext(), ImportEntriesActivity.class);
intent.putExtra("importerDef", importerDef);
intent.putExtra("file", file);
importResultLauncher.launch(intent);
startActivity(intent);
}

private void startExport() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
public class MainPreferencesFragment extends PreferencesFragment {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
super.onCreatePreferences(savedInstanceState, rootKey);
addPreferencesFromResource(R.xml.preferences);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.beemdevelopment.aegis.ui.fragments.preferences;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.Animation;

import androidx.annotation.CallSuper;
Expand Down Expand Up @@ -31,8 +29,6 @@ public abstract class PreferencesFragment extends PreferenceFragmentCompat {
public static final int CODE_EXPORT_GOOGLE_URI = 7;
public static final int CODE_EXPORT_HTML = 8;

private Intent _result;

@Inject
Preferences _prefs;

Expand All @@ -42,12 +38,6 @@ public abstract class PreferencesFragment extends PreferenceFragmentCompat {
@Inject
protected AuditLogRepository _auditLogRepository;

@Override
@CallSuper
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setResult(new Intent());
}

@Override
@CallSuper
public void onResume() {
Expand All @@ -61,10 +51,6 @@ public void onResume() {
}
}

public Intent getResult() {
return _result;
}

@Override
@Nullable
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
Expand All @@ -75,11 +61,6 @@ public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
return super.onCreateAnimation(transit, enter, nextAnim);
}

public void setResult(Intent result) {
_result = result;
requireActivity().setResult(Activity.RESULT_OK, _result);
}

protected boolean saveAndBackupVault() {
try {
_vaultManager.saveAndBackup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,10 @@ public void onResume() {

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
super.onCreatePreferences(savedInstanceState, rootKey);
addPreferencesFromResource(R.xml.preferences_security);

Preference tapToRevealPreference = requirePreference("pref_tap_to_reveal");
tapToRevealPreference.setOnPreferenceChangeListener((preference, newValue) -> {
getResult().putExtra("needsRefresh", true);
return true;
});

Preference screenPreference = requirePreference("pref_secure_screen");
screenPreference.setOnPreferenceChangeListener((preference, newValue) -> {
getResult().putExtra("needsRecreate", true);
Window window = requireActivity().getWindow();
if ((boolean) newValue) {
window.addFlags(WindowManager.LayoutParams.FLAG_SECURE);
Expand All @@ -82,7 +74,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
Dialogs.showTapToRevealTimeoutPickerDialog(requireContext(), _prefs.getTapToRevealTime(), number -> {
_prefs.setTapToRevealTime(number);
tapToRevealTimePreference.setSummary(number + " seconds");
getResult().putExtra("needsRefresh", true);
});
return false;
});
Expand Down

0 comments on commit 29eccaf

Please sign in to comment.