From 1ecd0f81c1677f90da4ad8794128cc6e66938d37 Mon Sep 17 00:00:00 2001 From: Jakob Nixdorf Date: Mon, 3 Jul 2017 15:45:32 +0200 Subject: [PATCH] Fix the Animator duration scale bug --- .../shadowice/flocke/andotp/MainActivity.java | 43 ++++++++++++++++++- app/src/main/res/values/strings.xml | 13 ++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/shadowice/flocke/andotp/MainActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/MainActivity.java index 9ee4550e..e682ac11 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/MainActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/MainActivity.java @@ -29,11 +29,14 @@ import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.os.Handler; +import android.preference.PreferenceManager; +import android.provider.Settings; import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; @@ -60,6 +63,7 @@ import com.google.zxing.integration.android.IntentIntegrator; import org.shadowice.flocke.andotp.ItemTouchHelper.SimpleItemTouchHelperCallback; +import java.lang.reflect.Method; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { @@ -243,6 +247,40 @@ public class MainActivity extends AppCompatActivity { .show(); } + // Try to fix the animation scale + private float fixAnimationScale() { + float durationScale = Settings.Global.getFloat(this.getContentResolver(), Settings.Global.ANIMATOR_DURATION_SCALE, 0); + + if (durationScale != 1) { + try { + Class c = Class.forName("android.animation.ValueAnimator"); + Method m = c.getMethod("setDurationScale", new Class[]{float.class}); + m.invoke(null, new Object[]{ 1f }); + durationScale = 1f; + } catch (Throwable t) { + t.printStackTrace(); + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + if (!prefs.getBoolean(getString(R.string.pref_animator_warning_displayed), false)) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.title_animator_duration) + .setMessage(R.string.msg_animator_duration_scale) + .setIcon(android.R.drawable.ic_dialog_alert) + .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) {} + }) + .create() + .show(); + + prefs.edit().putBoolean(getString(R.string.pref_animator_warning_displayed), true).apply(); + } + } + } + + return durationScale; + } + // Initialize the main application @Override protected void onCreate(Bundle savedInstanceState) { @@ -278,6 +316,9 @@ public class MainActivity extends AppCompatActivity { ItemTouchHelper touchHelper = new ItemTouchHelper(callback); touchHelper.attachToRecyclerView(recList); + final float durationScale = fixAnimationScale(); + final long animatorDuration = (long) (1000 / durationScale); + adapter.setMoveEventCallback(new EntriesCardAdapter.ViewHolderEventCallback() { @Override public void onMoveEventStart() { @@ -304,7 +345,7 @@ public class MainActivity extends AppCompatActivity { progressBar.setProgress(progress*100); ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", (progress-1)*100); - animation.setDuration(1000); + animation.setDuration(animatorDuration); animation.setInterpolator(new LinearInterpolator()); animation.start(); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 008e594c..6f13a677 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -14,6 +14,15 @@ Import (JSON) Import / Export + Your devices Animator duration scale seems to + be set to something other than the default (1.0x), which will result in a not very + smooth animation of the countdown bar. + \n\nandOTP tried to apply an automatic fix to itself, but something went wrong with that. + Now it will try to compensate manually during the animation. In the case that does not work + you can either live with the jumpy countdown bar or change your devices Animator duration + scale in the Developer options. + \n\nThis message will not be shown again! + Camera permission not granted Are you sure you want do remove this account? Export to external storage successful @@ -29,6 +38,10 @@ External storage currently not accessible Storage permissions not granted + Animator duration scale + + animator_warning_displayed + An open-source two-factor authentication App for Android 4.4+.