Skip to content

Commit e925946

Browse files
committed
Remove listening for screen lock and unlock events and add listening for onResume.
On some Android devices, the screen lock and unlock events are not paired, we can use the activity's onResume callback.
1 parent 153abc9 commit e925946

File tree

6 files changed

+116
-212
lines changed

6 files changed

+116
-212
lines changed

Diff for: android/libpag/src/main/java/org/extra/tools/BroadcastUtil.java

-117
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.extra.tools;
2+
3+
import android.app.Activity;
4+
import android.app.FragmentManager;
5+
import android.os.Handler;
6+
import android.os.Looper;
7+
import android.os.Message;
8+
import android.util.Log;
9+
10+
import org.libpag.PAGView;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
public class Lifecycle implements Handler.Callback {
16+
private static final String FRAGMENT_TAG = "io.pag.manager";
17+
private static final String TAG = "Lifecycle";
18+
19+
private static final int ID_REMOVE_FRAGMENT_MANAGER = 1;
20+
private static final Lifecycle lifecycle = new Lifecycle();
21+
private final Handler handler;
22+
private final Map<FragmentManager, LifecycleFragment> pendingRequestManagerFragments =
23+
new HashMap<>();
24+
25+
private Lifecycle() {
26+
handler = new Handler(Looper.getMainLooper(), this);
27+
}
28+
29+
public static Lifecycle getInstance() {
30+
return lifecycle;
31+
}
32+
33+
public void addListener(final PAGView pagView) {
34+
if (pagView.getContext() instanceof Activity) {
35+
Activity activity = (Activity) pagView.getContext();
36+
FragmentManager fm = activity.getFragmentManager();
37+
LifecycleFragment current = pendingRequestManagerFragments.get(fm);
38+
if (current == null) {
39+
current = (LifecycleFragment) fm.findFragmentByTag(FRAGMENT_TAG);
40+
if (current == null) {
41+
current = new LifecycleFragment();
42+
pendingRequestManagerFragments.put(fm, current);
43+
fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss();
44+
handler.obtainMessage(ID_REMOVE_FRAGMENT_MANAGER, fm).sendToTarget();
45+
}
46+
}
47+
current.addListener(pagView);
48+
}
49+
}
50+
51+
@Override
52+
public boolean handleMessage(Message message) {
53+
boolean handled = true;
54+
if (message.what == ID_REMOVE_FRAGMENT_MANAGER) {
55+
FragmentManager fm = (FragmentManager) message.obj;
56+
LifecycleFragment current = (LifecycleFragment) fm.findFragmentByTag(FRAGMENT_TAG);
57+
if (fm.isDestroyed()) {
58+
Log.w(TAG, "Parent was destroyed before our Fragment could be added.");
59+
} else if (current != pendingRequestManagerFragments.get(fm)) {
60+
Log.w(TAG, "adding Fragment failed.");
61+
}
62+
pendingRequestManagerFragments.remove(fm);
63+
} else {
64+
handled = false;
65+
}
66+
return handled;
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.extra.tools;
2+
3+
import android.app.Fragment;
4+
5+
import java.util.Collections;
6+
import java.util.Set;
7+
import java.util.WeakHashMap;
8+
9+
public class LifecycleFragment extends Fragment {
10+
private final Set<LifecycleListener> lifecycleListeners =
11+
Collections.newSetFromMap(new WeakHashMap<LifecycleListener, Boolean>());
12+
13+
public LifecycleFragment() {
14+
}
15+
16+
public void addListener(LifecycleListener listener) {
17+
lifecycleListeners.add(listener);
18+
}
19+
20+
@Override
21+
public void onResume() {
22+
super.onResume();
23+
for (LifecycleListener lifecycleListener : lifecycleListeners) {
24+
if (lifecycleListener != null) {
25+
lifecycleListener.onResume();
26+
}
27+
}
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.extra.tools;
2+
3+
public interface LifecycleListener {
4+
void onResume();
5+
}

Diff for: android/libpag/src/main/java/org/extra/tools/ScreenBroadcastReceiver.java

-68
This file was deleted.

Diff for: android/libpag/src/main/java/org/libpag/PAGView.java

+14-27
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
import android.view.View;
2121
import android.view.animation.LinearInterpolator;
2222

23-
import org.extra.tools.BroadcastUtil;
24-
import org.extra.tools.ScreenBroadcastReceiver;
23+
import org.extra.tools.Lifecycle;
24+
import org.extra.tools.LifecycleListener;
2525

2626
import java.util.ArrayList;
2727
import java.util.List;
2828

2929

30-
public class PAGView extends TextureView implements TextureView.SurfaceTextureListener, ScreenBroadcastReceiver.ScreenStateListener {
30+
public class PAGView extends TextureView implements TextureView.SurfaceTextureListener, LifecycleListener {
3131

3232
private final static String TAG = "PAGView";
3333
private SurfaceTextureListener mListener;
@@ -306,6 +306,7 @@ public void onAnimationRepeat(Animator animator) {
306306
};
307307

308308
private void setupSurfaceTexture() {
309+
Lifecycle.getInstance().addListener(this);
309310
setOpaque(false);
310311
pagPlayer = new PAGPlayer();
311312
setSurfaceTextureListener(this);
@@ -406,7 +407,6 @@ protected void onAttachedToWindow() {
406407
isAttachedToWindow = true;
407408
super.onAttachedToWindow();
408409
animator.addListener(mAnimatorListenerAdapter);
409-
BroadcastUtil.getInstance().registerScreenBroadcast(this);
410410
synchronized (g_HandlerLock) {
411411
StartHandlerThread();
412412
}
@@ -417,7 +417,6 @@ protected void onAttachedToWindow() {
417417
protected void onDetachedFromWindow() {
418418
isAttachedToWindow = false;
419419
super.onDetachedFromWindow();
420-
BroadcastUtil.getInstance().unregisterScreenBroadcast(this);
421420
if (pagSurface != null) {
422421
// 延迟释放 pagSurface,否则Android 4.4 及之前版本会在 onDetachedFromWindow() 时 Crash。https://www.jianshu.com/p/675455c225bd
423422
pagSurface.release();
@@ -758,24 +757,6 @@ public void freeCache() {
758757
}
759758
}
760759

761-
@Override
762-
public void onScreenOff() {
763-
if (this.getVisibility() == View.VISIBLE) {
764-
this.mSaveVisibleState = true;
765-
// workaround 在有些手机上,如果不置成不可见,解锁以后画面会不可见
766-
// 在VIVO IQOO Pro表现为必现的不可见,在一加6t上表现为偶现不可见
767-
setVisibility(View.INVISIBLE);
768-
}
769-
}
770-
771-
@Override
772-
public void onScreenOn() {
773-
if (this.mSaveVisibleState) {
774-
this.setVisibility(View.VISIBLE);
775-
}
776-
this.mSaveVisibleState = false;
777-
}
778-
779760
@Override
780761
public void setBackgroundDrawable(Drawable background) {
781762
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N && background != null) {
@@ -799,6 +780,16 @@ public void onVisibilityAggregated(boolean isVisible) {
799780
}
800781
}
801782

783+
@Override
784+
public void onResume() {
785+
// When the device is locked and then unlocked, the PAGView's content may disappear,
786+
// use the following way to make the content appear.
787+
if (isAttachedToWindow && getVisibility() == View.VISIBLE) {
788+
setVisibility(View.INVISIBLE);
789+
setVisibility(View.VISIBLE);
790+
}
791+
}
792+
802793
private void pauseAnimator() {
803794
if (_isAnimatorPreRunning == null) {
804795
_isAnimatorPreRunning = animator.isRunning();
@@ -817,8 +808,4 @@ private void resumeAnimator() {
817808
_isAnimatorPreRunning = null;
818809
doPlay();
819810
}
820-
821-
static {
822-
BroadcastUtil.getInstance().registerScreenBroadcast();
823-
}
824811
}

0 commit comments

Comments
 (0)