20
20
import android .view .View ;
21
21
import android .view .animation .LinearInterpolator ;
22
22
23
- import org .extra .tools .BroadcastUtil ;
24
- import org .extra .tools .ScreenBroadcastReceiver ;
23
+ import org .extra .tools .Lifecycle ;
24
+ import org .extra .tools .LifecycleListener ;
25
25
26
26
import java .util .ArrayList ;
27
27
import java .util .List ;
28
28
29
29
30
- public class PAGView extends TextureView implements TextureView .SurfaceTextureListener , ScreenBroadcastReceiver . ScreenStateListener {
30
+ public class PAGView extends TextureView implements TextureView .SurfaceTextureListener , LifecycleListener {
31
31
32
32
private final static String TAG = "PAGView" ;
33
33
private SurfaceTextureListener mListener ;
@@ -306,6 +306,7 @@ public void onAnimationRepeat(Animator animator) {
306
306
};
307
307
308
308
private void setupSurfaceTexture () {
309
+ Lifecycle .getInstance ().addListener (this );
309
310
setOpaque (false );
310
311
pagPlayer = new PAGPlayer ();
311
312
setSurfaceTextureListener (this );
@@ -406,7 +407,6 @@ protected void onAttachedToWindow() {
406
407
isAttachedToWindow = true ;
407
408
super .onAttachedToWindow ();
408
409
animator .addListener (mAnimatorListenerAdapter );
409
- BroadcastUtil .getInstance ().registerScreenBroadcast (this );
410
410
synchronized (g_HandlerLock ) {
411
411
StartHandlerThread ();
412
412
}
@@ -417,7 +417,6 @@ protected void onAttachedToWindow() {
417
417
protected void onDetachedFromWindow () {
418
418
isAttachedToWindow = false ;
419
419
super .onDetachedFromWindow ();
420
- BroadcastUtil .getInstance ().unregisterScreenBroadcast (this );
421
420
if (pagSurface != null ) {
422
421
// 延迟释放 pagSurface,否则Android 4.4 及之前版本会在 onDetachedFromWindow() 时 Crash。https://www.jianshu.com/p/675455c225bd
423
422
pagSurface .release ();
@@ -758,24 +757,6 @@ public void freeCache() {
758
757
}
759
758
}
760
759
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
-
779
760
@ Override
780
761
public void setBackgroundDrawable (Drawable background ) {
781
762
if (Build .VERSION .SDK_INT < Build .VERSION_CODES .N && background != null ) {
@@ -799,6 +780,16 @@ public void onVisibilityAggregated(boolean isVisible) {
799
780
}
800
781
}
801
782
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 (getVisibility () == View .VISIBLE ) {
788
+ setVisibility (View .INVISIBLE );
789
+ setVisibility (View .VISIBLE );
790
+ }
791
+ }
792
+
802
793
private void pauseAnimator () {
803
794
if (_isAnimatorPreRunning == null ) {
804
795
_isAnimatorPreRunning = animator .isRunning ();
@@ -817,8 +808,4 @@ private void resumeAnimator() {
817
808
_isAnimatorPreRunning = null ;
818
809
doPlay ();
819
810
}
820
-
821
- static {
822
- BroadcastUtil .getInstance ().registerScreenBroadcast ();
823
- }
824
811
}
0 commit comments