Skip to content

Commit

Permalink
Fix for AnimatedVectorDrawables in Pico devices
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo authored and MortimerGoro committed Apr 9, 2020
1 parent 99a8fc6 commit e3943e9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import android.content.Context;
import android.graphics.drawable.AnimatedVectorDrawable;
import android.view.LayoutInflater;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

import androidx.databinding.DataBindingUtil;

import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.VRBrowserActivity;
import org.mozilla.vrbrowser.databinding.WebxrInterstitialBinding;
import org.mozilla.vrbrowser.utils.DeviceType;
import org.mozilla.vrbrowser.utils.ViewUtils;

import java.util.ArrayList;

Expand Down Expand Up @@ -43,12 +42,13 @@ protected void initializeWidgetPlacement(WidgetPlacement aPlacement) {
}

private void initialize() {
// AnimatedVectorDrawable doesn't work with a Hardware Accelerated canvas, we disable it for this view.
setIsHardwareAccelerationEnabled(false);
LayoutInflater inflater = LayoutInflater.from(getContext());
mBinding = DataBindingUtil.inflate(inflater, R.layout.webxr_interstitial, this, true);
mBinding.setLifecycleOwner((VRBrowserActivity)getContext());
mSpinnerAnimation = (AnimatedVectorDrawable) mBinding.webxrSpinner.getDrawable();
if (DeviceType.isPicoVR()) {
ViewUtils.forceAnimationOnUI(mSpinnerAnimation);
}
mWidgetManager.addWebXRListener(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import org.mozilla.vrbrowser.databinding.VoiceSearchDialogBinding;
import org.mozilla.vrbrowser.ui.widgets.WidgetManagerDelegate;
import org.mozilla.vrbrowser.ui.widgets.WidgetPlacement;
import org.mozilla.vrbrowser.utils.DeviceType;
import org.mozilla.vrbrowser.utils.LocaleUtils;
import org.mozilla.vrbrowser.utils.ViewUtils;

public class VoiceSearchWidget extends UIDialog implements WidgetManagerDelegate.PermissionListener,
Application.ActivityLifecycleCallbacks {
Expand Down Expand Up @@ -82,9 +84,6 @@ public VoiceSearchWidget(Context aContext, AttributeSet aAttrs, int aDefStyle) {
}

private void initialize(Context aContext) {
// AnimatedVectorDrawable doesn't work with a Hardware Accelerated canvas, we disable it for this view.
setIsHardwareAccelerationEnabled(false);

updateUI();

mWidgetManager.addPermissionListener(this);
Expand All @@ -94,6 +93,9 @@ private void initialize(Context aContext) {
mMozillaSpeechService.setProductTag(getContext().getString(R.string.voice_app_id));

mSearchingAnimation = (AnimatedVectorDrawable) mBinding.voiceSearchAnimationSearching.getDrawable();
if (DeviceType.isPicoVR()) {
ViewUtils.forceAnimationOnUI(mSearchingAnimation);
}

mMozillaSpeechService.addListener(mVoiceSearchListener);
((Application)aContext.getApplicationContext()).registerActivityLifecycleCallbacks(this);
Expand Down
16 changes: 16 additions & 0 deletions app/src/common/shared/org/mozilla/vrbrowser/utils/ViewUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.AnimatedVectorDrawable;
import android.os.Build;
import android.text.Html;
import android.text.Layout;
Expand All @@ -15,6 +16,7 @@
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -27,8 +29,12 @@

import org.mozilla.vrbrowser.ui.widgets.UIWidget;

import java.lang.reflect.Method;

public class ViewUtils {

private static final String LOGTAG = SystemUtils.createLogtag(ViewUtils.class);

public interface LinkClickableSpan {
void onClick(@NonNull View widget, @NonNull String url);
}
Expand Down Expand Up @@ -238,4 +244,14 @@ public static Bitmap getRoundedCroppedBitmap(@NonNull Bitmap bitmap) {

return output;
}

public static void forceAnimationOnUI(@NonNull AnimatedVectorDrawable drawable) {
try {
Method forceAnimationOnUI = AnimatedVectorDrawable.class.getMethod("forceAnimationOnUI");
forceAnimationOnUI.invoke(drawable);

} catch (Exception e) {
Log.e(LOGTAG, "Failed to call AnimatedVectorDrawable::forceAnimationOnUI: " + e);
}
}
}

0 comments on commit e3943e9

Please sign in to comment.