Skip to content

Commit

Permalink
[#605]修复Android 13锁屏动画处理导致的崩溃问题 (#606)
Browse files Browse the repository at this point in the history
Signed-off-by: chaihua1 <[email protected]>
  • Loading branch information
charles-chai authored Dec 18, 2023
1 parent 6c29ae9 commit 047977d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
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 @@ -31,6 +31,7 @@
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;
Expand Down Expand Up @@ -235,6 +236,10 @@ public void setRepeatCount(int repeatCount) {
}
}

public int getRepeatCount() {
return mRepeatCount;
}

public void start() {
if (mComponent == null) {
return;
Expand Down Expand Up @@ -271,6 +276,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

0 comments on commit 047977d

Please sign in to comment.