From 9ec59148a926a5c8e5fecfbb64b6418d1dbb852d Mon Sep 17 00:00:00 2001 From: cyberta Date: Fri, 2 Dec 2022 15:37:59 +0100 Subject: implement transition animation between connected and disconnected state for RiseupVPn --- .../state_transition_connceted_disconnected.xml | 4 + app/src/custom/res/drawable/state_connected.xml | 30 ++++--- app/src/custom/res/drawable/state_disconnected.xml | 22 +++-- .../state_transition_connected_disconnected.xml | 89 +++++++++++++++++++++ .../bitmaskclient/base/fragments/EipFragment.java | 31 ++++++- app/src/main/res/layout/v_main_btn.xml | 55 ------------- .../state_transition_connected_disconnected.png | Bin 0 -> 40172 bytes 7 files changed, 158 insertions(+), 73 deletions(-) create mode 100644 app/src/custom/res/animator/state_transition_connceted_disconnected.xml create mode 100644 app/src/custom/res/drawable/state_transition_connected_disconnected.xml delete mode 100644 app/src/main/res/layout/v_main_btn.xml create mode 100644 app/src/normal/res/drawable/state_transition_connected_disconnected.png diff --git a/app/src/custom/res/animator/state_transition_connceted_disconnected.xml b/app/src/custom/res/animator/state_transition_connceted_disconnected.xml new file mode 100644 index 00000000..751d2da5 --- /dev/null +++ b/app/src/custom/res/animator/state_transition_connceted_disconnected.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/app/src/custom/res/drawable/state_connected.xml b/app/src/custom/res/drawable/state_connected.xml index c9854d69..e0a9005f 100644 --- a/app/src/custom/res/drawable/state_connected.xml +++ b/app/src/custom/res/drawable/state_connected.xml @@ -3,13 +3,25 @@ android:viewportHeight="220" android:width="166dp" android:height="220dp"> - - - + + + + + + + + \ No newline at end of file diff --git a/app/src/custom/res/drawable/state_disconnected.xml b/app/src/custom/res/drawable/state_disconnected.xml index 3d3660b8..7c49a537 100644 --- a/app/src/custom/res/drawable/state_disconnected.xml +++ b/app/src/custom/res/drawable/state_disconnected.xml @@ -1,9 +1,17 @@ - + android:viewportWidth="166" + android:viewportHeight="220" + android:width="166dp" + android:height="220dp"> + + + + \ No newline at end of file diff --git a/app/src/custom/res/drawable/state_transition_connected_disconnected.xml b/app/src/custom/res/drawable/state_transition_connected_disconnected.xml new file mode 100644 index 00000000..0496e729 --- /dev/null +++ b/app/src/custom/res/drawable/state_transition_connected_disconnected.xml @@ -0,0 +1,89 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/se/leap/bitmaskclient/base/fragments/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/base/fragments/EipFragment.java index 81a9f9eb..470470ac 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/fragments/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/fragments/EipFragment.java @@ -42,6 +42,8 @@ import android.content.Intent; import android.content.ServiceConnection; import android.content.SharedPreferences; import android.graphics.drawable.Animatable; +import android.graphics.drawable.Animatable2; +import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.IBinder; import android.text.Spannable; @@ -59,9 +61,13 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.AppCompatImageView; import androidx.appcompat.widget.AppCompatTextView; +import androidx.core.content.ContextCompat; import androidx.fragment.app.DialogFragment; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; +import androidx.vectordrawable.graphics.drawable.Animatable2Compat; +import androidx.vectordrawable.graphics.drawable.Animatable2Compat.AnimationCallback; +import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat; import java.util.Observable; import java.util.Observer; @@ -111,6 +117,7 @@ public class EipFragment extends Fragment implements Observer { private ProviderObservable providerObservable; private TorStatusObservable torStatusObservable; + private @DrawableRes int pendingAnimationState; private GatewaysManager gatewaysManager; //---saved Instance ------- @@ -530,7 +537,7 @@ public class EipFragment extends Fragment implements Observer { mainButton.updateState(false, false, false); mainDescription.setText(R.string.eip_status_unsecured); background.setImageResource(R.drawable.bg_disconnected); - animateState(R.drawable.state_disconnected); + animateState(R.drawable.state_transition_connected_disconnected); setActivityBarColor(R.color.bg_disconnected_top, R.color.bg_disconnected_top_light_transparent); } else if (eipStatus.isBlocking()) { setMainButtonEnabled(true); @@ -556,25 +563,45 @@ public class EipFragment extends Fragment implements Observer { mainDescription.setText(R.string.eip_status_unsecured); subDescription.setText(null); background.setImageResource(R.drawable.bg_disconnected); + animateState(R.drawable.state_disconnected); setActivityBarColor(R.color.bg_disconnected_top, R.color.bg_disconnected_top_light_transparent); } } private void animateState(@DrawableRes int drawableRes) { + @DrawableRes int lastDrawableId; try { - int lastDrawableId = (int) stateView.getTag(); + lastDrawableId = (int) stateView.getTag(); if (lastDrawableId == drawableRes) { return; } + + Drawable lastDrawable = ContextCompat.getDrawable(getContext(), lastDrawableId); + if (lastDrawable instanceof Animatable && ((Animatable) lastDrawable).isRunning()) { + pendingAnimationState = drawableRes; + } + } catch (NullPointerException | ClassCastException e) { // eat me } + stateView.setImageResource(drawableRes); stateView.setTag(drawableRes); if (stateView.getDrawable() instanceof Animatable) { Animatable animatedDrawable = (Animatable) stateView.getDrawable(); + AnimatedVectorDrawableCompat.registerAnimationCallback(stateView.getDrawable(), new AnimationCallback() { + @Override + public void onAnimationEnd(Drawable drawable) { + super.onAnimationEnd(drawable); + if (pendingAnimationState != 0) { + int newAnimationRes = pendingAnimationState; + pendingAnimationState = 0; + animateState(newAnimationRes); + } + } + }); animatedDrawable.start(); } } diff --git a/app/src/main/res/layout/v_main_btn.xml b/app/src/main/res/layout/v_main_btn.xml deleted file mode 100644 index 3ab62e5a..00000000 --- a/app/src/main/res/layout/v_main_btn.xml +++ /dev/null @@ -1,55 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/normal/res/drawable/state_transition_connected_disconnected.png b/app/src/normal/res/drawable/state_transition_connected_disconnected.png new file mode 100644 index 00000000..a4bd4a78 Binary files /dev/null and b/app/src/normal/res/drawable/state_transition_connected_disconnected.png differ -- cgit v1.2.3