From cc8f14b5628b8842b7915d82f57ceba1f9ebbb86 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Tue, 16 Mar 2021 21:29:12 +0900 Subject: [PATCH] Fix Animatable drawable class cast issue on sdk lower than 24. Fixes https://github.com/austinhuang0131/barinsta/issues/794 --- .../DirectMessageThreadFragment.java | 5 ++--- .../main/java/awais/instagrabber/utils/Utils.java | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java index a4e88f19..5353ddfa 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java @@ -30,7 +30,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBar; import androidx.constraintlayout.widget.ConstraintLayout; -import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import androidx.lifecycle.LiveData; import androidx.lifecycle.MediatorLiveData; @@ -1204,7 +1203,7 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact private void setSendToMicIcon() { final Context context = getContext(); if (context == null) return; - final Drawable sendToMicDrawable = ContextCompat.getDrawable(context, R.drawable.avd_send_to_mic_anim); + final Drawable sendToMicDrawable = Utils.getAnimatableDrawable(context, R.drawable.avd_send_to_mic_anim); if (sendToMicDrawable instanceof Animatable) { AnimatedVectorDrawableCompat.registerAnimationCallback(sendToMicDrawable, sendToMicAnimationCallback); } @@ -1214,7 +1213,7 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact private void setMicToSendIcon() { final Context context = getContext(); if (context == null) return; - final Drawable micToSendDrawable = ContextCompat.getDrawable(context, R.drawable.avd_mic_to_send_anim); + final Drawable micToSendDrawable = Utils.getAnimatableDrawable(context, R.drawable.avd_mic_to_send_anim); if (micToSendDrawable instanceof Animatable) { AnimatedVectorDrawableCompat.registerAnimationCallback(micToSendDrawable, micToSendAnimationCallback); } diff --git a/app/src/main/java/awais/instagrabber/utils/Utils.java b/app/src/main/java/awais/instagrabber/utils/Utils.java index 3e45a21c..b69c01d1 100644 --- a/app/src/main/java/awais/instagrabber/utils/Utils.java +++ b/app/src/main/java/awais/instagrabber/utils/Utils.java @@ -10,6 +10,7 @@ import android.content.Intent; import android.content.res.Resources; import android.graphics.Color; import android.graphics.Rect; +import android.graphics.drawable.Drawable; import android.media.MediaScannerConnection; import android.media.MediaScannerConnection.OnScanCompletedListener; import android.net.Uri; @@ -28,10 +29,13 @@ import android.view.inputmethod.InputMethodManager; import android.webkit.MimeTypeMap; import android.widget.Toast; +import androidx.annotation.DrawableRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; +import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat; import com.google.android.exoplayer2.database.ExoDatabaseProvider; import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor; @@ -347,4 +351,15 @@ public final class Utils { Log.e(TAG, "hideKeyboard: ", e); } } + + public static Drawable getAnimatableDrawable(@NonNull final Context context, + @DrawableRes final int drawableResId) { + final Drawable drawable; + if (Build.VERSION.SDK_INT >= 24) { + drawable = ContextCompat.getDrawable(context, drawableResId); + } else { + drawable = AnimatedVectorDrawableCompat.create(context, drawableResId); + } + return drawable; + } }