Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#605]修复Android 13锁屏动画处理导致的崩溃问题 #606

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.HashMap;
import java.util.Map;

import org.hapjs.component.Component;

public class Animation {
Expand Down Expand Up @@ -101,6 +102,12 @@ public void pause() {
}
}

public void resume() {
if (mAnimatorSet != null) {
mAnimatorSet.resume();
}
}

public void finish() {
if (mAnimatorSet != null) {
mAnimatorSet.finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
import android.view.View;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;

import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.Objects;

import org.hapjs.common.utils.ColorUtil;
import org.hapjs.component.Component;
import org.hapjs.component.bridge.SimpleActivityStateListener;
Expand Down Expand Up @@ -228,6 +230,10 @@ public void setRepeatCount(int repeatCount) {
}
}

public int getRepeatCount() {
return mRepeatCount;
}

public void start() {
if (mComponent == null) {
return;
Expand Down Expand Up @@ -264,6 +270,10 @@ public void pause() {
mWrapped.pause();
}

public void resume() {
mWrapped.resume();
}

public void reverse() {
for (Animator animator : mWrapped.getChildAnimations()) {
animator.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@

import android.app.Activity;
import android.util.Log;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.hapjs.bridge.CallbackContext;
import org.hapjs.bridge.CallbackContextHolder;
import org.hapjs.bridge.CallbackHybridFeature;
import org.hapjs.bridge.FeatureExtension;
import org.hapjs.bridge.HybridManager;
import org.hapjs.bridge.LifecycleListener;
import org.hapjs.bridge.Request;
import org.hapjs.bridge.Response;
import org.hapjs.bridge.annotation.ActionAnnotation;
Expand Down Expand Up @@ -82,7 +87,42 @@ public class AnimationFeature extends CallbackHybridFeature {
protected static final String EVENT_ON_ANIMATION_FINISH = "onfinish";
private static final String TAG = "AnimationFeature";
private static final String CONNECTOR = "-";
private Map<String, Animation> mAnimations = new ConcurrentHashMap<>();
private final Map<String, Animation> mAnimations = new ConcurrentHashMap<>();
private final Map<String, Animation> mPausingAnimations = new ConcurrentHashMap<>();
private final Object mLock = new Object();
private HybridManager mHybridManager;
private final LifecycleListener mLifecycleListener = new LifecycleListener() {
@Override
public void onResume() {
super.onResume();
synchronized (mLock) {
Set<Map.Entry<String, Animation>> entries = mPausingAnimations.entrySet();
for (Map.Entry<String, Animation> entry : entries) {
entry.getValue().resume();
}
mPausingAnimations.clear();
}
}

@Override
public void onPause() {
super.onPause();
synchronized (mLock) {
Set<Map.Entry<String, Animation>> entries = mAnimations.entrySet();
for (Map.Entry<String, Animation> entry : entries) {
Animation animation = entry.getValue();
//页面 onPause 时,要暂停所有 infinity 的动画,否则会在 Android 13 的设备上崩溃
if (animation.getAnimatorSet() != null
&& Integer.MAX_VALUE == animation.getAnimatorSet().getRepeatCount()) {
if (animation.getPlayState().equals("running")) {
mPausingAnimations.put(entry.getKey(), entry.getValue());
animation.pause();
}
}
}
}
}
};

@Override
public String getName() {
Expand All @@ -98,6 +138,10 @@ protected Response invokeInner(final Request request) throws Exception {
Activity activity = request.getNativeInterface().getActivity();
switch (request.getAction()) {
case ACTION_ENABLE:
if (mHybridManager == null) {
mHybridManager = request.getView().getHybridManager();
setLifecycleListener();
}
enable(request, compId, animId);
break;
case ACTION_PLAY:
Expand All @@ -114,6 +158,9 @@ public void run() {
new Runnable() {
@Override
public void run() {
synchronized (mLock) {
mPausingAnimations.remove(key);
}
pause(key);
}
});
Expand Down Expand Up @@ -177,6 +224,12 @@ public void run() {
return Response.SUCCESS;
}

private void setLifecycleListener() {
if (mHybridManager != null) {
mHybridManager.addLifecycleListener(mLifecycleListener);
}
}

private void enable(Request request, String compId, String animId) {
int ref = Integer.parseInt(compId);
RootView rootView = request.getNativeInterface().getRootView();
Expand Down Expand Up @@ -405,6 +458,14 @@ public boolean isBuiltInExtension() {
return true;
}

@Override
public void dispose(boolean force) {
super.dispose(force);
if (force && mHybridManager != null) {
mHybridManager.removeLifecycleListener(mLifecycleListener);
}
}

private class AnimationCallbackContext extends CallbackContext
implements Animation.OnFinishListener, Animation.OnCancelListener {

Expand Down
Loading