From a80873d0b9c95f474dda0d835c2d43c46b1a82ea Mon Sep 17 00:00:00 2001 From: Campello Manuel <9112949+CampelloManuel@users.noreply.github.com> Date: Sun, 23 Jul 2023 14:49:05 +0200 Subject: [PATCH] show shake animation only if animations are enabled as a system setting --- .../helpers/PreferencesHelper.java | 25 +++++++++++++++++++ .../notepad/fragments/DialogPassword.java | 16 ++++++++---- .../notepad/fragments/DialogPasswordV11.java | 8 ++++-- .../notepad/prefs/PasswordPrefs.java | 8 ++++-- 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/nononsenseapps/helpers/PreferencesHelper.java b/app/src/main/java/com/nononsenseapps/helpers/PreferencesHelper.java index 497f02a8b..b21be8772 100644 --- a/app/src/main/java/com/nononsenseapps/helpers/PreferencesHelper.java +++ b/app/src/main/java/com/nononsenseapps/helpers/PreferencesHelper.java @@ -19,6 +19,7 @@ import android.content.Context; import android.content.SharedPreferences; +import android.provider.Settings; import androidx.annotation.NonNull; import androidx.preference.PreferenceManager; @@ -103,4 +104,28 @@ public static boolean isPasswordSet(@NonNull Context context) { return !Prefs(context).getString(PasswordPrefs.KEY_PASSWORD, "").isEmpty(); } + /** + * @return TRUE if animations are enabled in the system settings. Used to choose if animations + * should be displayed in the app + */ + public static boolean areAnimationsEnabled(@NonNull Context context) { + + // there are 3 redundant system settings that control animations + String[] sysSettingsToCheck = new String[] { + Settings.Global.ANIMATOR_DURATION_SCALE, + Settings.Global.TRANSITION_ANIMATION_SCALE, + Settings.Global.WINDOW_ANIMATION_SCALE + }; + + // if at least 1 of those is set to "0x", which means "disable animations", + // we assume that the user wants to disable all animations, also in this app + for (String option : sysSettingsToCheck) { + float f = Settings.Global.getFloat(context.getContentResolver(), option, 1.0f); + if (f == 0) return false; + } + + // if none of the 3 settings is "0x", we assume that animations are enabled + return true; + } + } diff --git a/app/src/main/java/com/nononsenseapps/notepad/fragments/DialogPassword.java b/app/src/main/java/com/nononsenseapps/notepad/fragments/DialogPassword.java index bc8d5cd7d..3d6298204 100644 --- a/app/src/main/java/com/nononsenseapps/notepad/fragments/DialogPassword.java +++ b/app/src/main/java/com/nononsenseapps/notepad/fragments/DialogPassword.java @@ -30,6 +30,7 @@ import androidx.fragment.app.DialogFragment; import androidx.preference.PreferenceManager; +import com.nononsenseapps.helpers.PreferencesHelper; import com.nononsenseapps.notepad.R; import com.nononsenseapps.notepad.databinding.FragmentDialogPasswordBinding; import com.nononsenseapps.notepad.prefs.PasswordPrefs; @@ -116,9 +117,11 @@ private void checkPassword(final String enteredPassword, final String currentPas } dismiss(); } else { - Animation shake = AnimationUtils.loadAnimation(getActivity(), - R.anim.shake); - mBinding.passwordField.startAnimation(shake); + if (PreferencesHelper.areAnimationsEnabled(this.getContext())) { + // shake the dialog to show that the password is wrong + Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake); + mBinding.passwordField.startAnimation(shake); + } Toast.makeText(getActivity(), getText(R.string.password_incorrect), Toast.LENGTH_SHORT).show(); } @@ -135,8 +138,11 @@ private void setPassword(final String pass1, final String pass2) { } dismiss(); } else { - Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake); - mBinding.passwordVerificationField.startAnimation(shake); + if (PreferencesHelper.areAnimationsEnabled(this.getContext())) { + // shake the dialog to show that the password is wrong + Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake); + mBinding.passwordVerificationField.startAnimation(shake); + } Toast.makeText(getActivity(), getText(R.string.passwords_dont_match), Toast.LENGTH_SHORT).show(); } diff --git a/app/src/main/java/com/nononsenseapps/notepad/fragments/DialogPasswordV11.java b/app/src/main/java/com/nononsenseapps/notepad/fragments/DialogPasswordV11.java index 7f26a30c2..a6df20202 100644 --- a/app/src/main/java/com/nononsenseapps/notepad/fragments/DialogPasswordV11.java +++ b/app/src/main/java/com/nononsenseapps/notepad/fragments/DialogPasswordV11.java @@ -30,6 +30,7 @@ import androidx.fragment.app.DialogFragment; import androidx.preference.PreferenceManager; +import com.nononsenseapps.helpers.PreferencesHelper; import com.nononsenseapps.notepad.R; import com.nononsenseapps.notepad.databinding.FragmentDialogPasswordBinding; import com.nononsenseapps.notepad.fragments.DialogPassword.PasswordConfirmedListener; @@ -98,8 +99,11 @@ private void checkPassword(String enteredPassword, String currentPassword) { } dismiss(); } else { - Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake); - mBinding.passwordField.startAnimation(shake); + if (PreferencesHelper.areAnimationsEnabled(this.getContext())) { + // shake the dialog to show that the password is wrong + Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake); + mBinding.passwordField.startAnimation(shake); + } Toast.makeText(getActivity(), getText(R.string.password_incorrect), Toast.LENGTH_SHORT).show(); } diff --git a/app/src/main/java/com/nononsenseapps/notepad/prefs/PasswordPrefs.java b/app/src/main/java/com/nononsenseapps/notepad/prefs/PasswordPrefs.java index e09712cc5..bcde0ca04 100644 --- a/app/src/main/java/com/nononsenseapps/notepad/prefs/PasswordPrefs.java +++ b/app/src/main/java/com/nononsenseapps/notepad/prefs/PasswordPrefs.java @@ -30,6 +30,7 @@ import androidx.fragment.app.Fragment; import androidx.preference.PreferenceManager; +import com.nononsenseapps.helpers.PreferencesHelper; import com.nononsenseapps.notepad.R; import com.nononsenseapps.notepad.databinding.AppPrefPasswordLayoutBinding; import com.nononsenseapps.notepad.fragments.DialogPasswordV11; @@ -83,9 +84,12 @@ private void applyPassword() { showPasswordDialog(passw1); } } else { + if (PreferencesHelper.areAnimationsEnabled(this.getContext())) { + // shake the dialog to show that the password is wrong + Animation shake = AnimationUtils.loadAnimation(this.getContext(), R.anim.shake); + mBinding.tempPassword2.startAnimation(shake); + } // Show a toast so the user knows he did something wrong - Animation shake = AnimationUtils.loadAnimation(this.getContext(), R.anim.shake); - mBinding.tempPassword2.startAnimation(shake); Toast.makeText(this.getContext(), getText(R.string.passwords_dont_match), Toast.LENGTH_SHORT).show(); }