Skip to content

Commit

Permalink
Remove listening for screen lock and unlock events and add listening …
Browse files Browse the repository at this point in the history
…for onResume.

On some Android devices, the screen lock and unlock events are not paired, we can use the activity's onResume callback.
  • Loading branch information
lvpengwei committed Feb 17, 2022
1 parent 153abc9 commit e925946
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 212 deletions.
117 changes: 0 additions & 117 deletions android/libpag/src/main/java/org/extra/tools/BroadcastUtil.java

This file was deleted.

68 changes: 68 additions & 0 deletions android/libpag/src/main/java/org/extra/tools/Lifecycle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.extra.tools;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import org.libpag.PAGView;

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

public class Lifecycle implements Handler.Callback {
private static final String FRAGMENT_TAG = "io.pag.manager";
private static final String TAG = "Lifecycle";

private static final int ID_REMOVE_FRAGMENT_MANAGER = 1;
private static final Lifecycle lifecycle = new Lifecycle();
private final Handler handler;
private final Map<FragmentManager, LifecycleFragment> pendingRequestManagerFragments =
new HashMap<>();

private Lifecycle() {
handler = new Handler(Looper.getMainLooper(), this);
}

public static Lifecycle getInstance() {
return lifecycle;
}

public void addListener(final PAGView pagView) {
if (pagView.getContext() instanceof Activity) {
Activity activity = (Activity) pagView.getContext();
FragmentManager fm = activity.getFragmentManager();
LifecycleFragment current = pendingRequestManagerFragments.get(fm);
if (current == null) {
current = (LifecycleFragment) fm.findFragmentByTag(FRAGMENT_TAG);
if (current == null) {
current = new LifecycleFragment();
pendingRequestManagerFragments.put(fm, current);
fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss();
handler.obtainMessage(ID_REMOVE_FRAGMENT_MANAGER, fm).sendToTarget();
}
}
current.addListener(pagView);
}
}

@Override
public boolean handleMessage(Message message) {
boolean handled = true;
if (message.what == ID_REMOVE_FRAGMENT_MANAGER) {
FragmentManager fm = (FragmentManager) message.obj;
LifecycleFragment current = (LifecycleFragment) fm.findFragmentByTag(FRAGMENT_TAG);
if (fm.isDestroyed()) {
Log.w(TAG, "Parent was destroyed before our Fragment could be added.");
} else if (current != pendingRequestManagerFragments.get(fm)) {
Log.w(TAG, "adding Fragment failed.");
}
pendingRequestManagerFragments.remove(fm);
} else {
handled = false;
}
return handled;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.extra.tools;

import android.app.Fragment;

import java.util.Collections;
import java.util.Set;
import java.util.WeakHashMap;

public class LifecycleFragment extends Fragment {
private final Set<LifecycleListener> lifecycleListeners =
Collections.newSetFromMap(new WeakHashMap<LifecycleListener, Boolean>());

public LifecycleFragment() {
}

public void addListener(LifecycleListener listener) {
lifecycleListeners.add(listener);
}

@Override
public void onResume() {
super.onResume();
for (LifecycleListener lifecycleListener : lifecycleListeners) {
if (lifecycleListener != null) {
lifecycleListener.onResume();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.extra.tools;

public interface LifecycleListener {
void onResume();
}

This file was deleted.

41 changes: 14 additions & 27 deletions android/libpag/src/main/java/org/libpag/PAGView.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import android.view.View;
import android.view.animation.LinearInterpolator;

import org.extra.tools.BroadcastUtil;
import org.extra.tools.ScreenBroadcastReceiver;
import org.extra.tools.Lifecycle;
import org.extra.tools.LifecycleListener;

import java.util.ArrayList;
import java.util.List;


public class PAGView extends TextureView implements TextureView.SurfaceTextureListener, ScreenBroadcastReceiver.ScreenStateListener {
public class PAGView extends TextureView implements TextureView.SurfaceTextureListener, LifecycleListener {

private final static String TAG = "PAGView";
private SurfaceTextureListener mListener;
Expand Down Expand Up @@ -306,6 +306,7 @@ public void onAnimationRepeat(Animator animator) {
};

private void setupSurfaceTexture() {
Lifecycle.getInstance().addListener(this);
setOpaque(false);
pagPlayer = new PAGPlayer();
setSurfaceTextureListener(this);
Expand Down Expand Up @@ -406,7 +407,6 @@ protected void onAttachedToWindow() {
isAttachedToWindow = true;
super.onAttachedToWindow();
animator.addListener(mAnimatorListenerAdapter);
BroadcastUtil.getInstance().registerScreenBroadcast(this);
synchronized (g_HandlerLock) {
StartHandlerThread();
}
Expand All @@ -417,7 +417,6 @@ protected void onAttachedToWindow() {
protected void onDetachedFromWindow() {
isAttachedToWindow = false;
super.onDetachedFromWindow();
BroadcastUtil.getInstance().unregisterScreenBroadcast(this);
if (pagSurface != null) {
// 延迟释放 pagSurface,否则Android 4.4 及之前版本会在 onDetachedFromWindow() 时 Crash。https://www.jianshu.com/p/675455c225bd
pagSurface.release();
Expand Down Expand Up @@ -758,24 +757,6 @@ public void freeCache() {
}
}

@Override
public void onScreenOff() {
if (this.getVisibility() == View.VISIBLE) {
this.mSaveVisibleState = true;
// workaround 在有些手机上,如果不置成不可见,解锁以后画面会不可见
// 在VIVO IQOO Pro表现为必现的不可见,在一加6t上表现为偶现不可见
setVisibility(View.INVISIBLE);
}
}

@Override
public void onScreenOn() {
if (this.mSaveVisibleState) {
this.setVisibility(View.VISIBLE);
}
this.mSaveVisibleState = false;
}

@Override
public void setBackgroundDrawable(Drawable background) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N && background != null) {
Expand All @@ -799,6 +780,16 @@ public void onVisibilityAggregated(boolean isVisible) {
}
}

@Override
public void onResume() {
// When the device is locked and then unlocked, the PAGView's content may disappear,
// use the following way to make the content appear.
if (isAttachedToWindow && getVisibility() == View.VISIBLE) {
setVisibility(View.INVISIBLE);
setVisibility(View.VISIBLE);
}
}

private void pauseAnimator() {
if (_isAnimatorPreRunning == null) {
_isAnimatorPreRunning = animator.isRunning();
Expand All @@ -817,8 +808,4 @@ private void resumeAnimator() {
_isAnimatorPreRunning = null;
doPlay();
}

static {
BroadcastUtil.getInstance().registerScreenBroadcast();
}
}

0 comments on commit e925946

Please sign in to comment.