diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/photoviewer/PhotoViewer.java b/mastodon/src/main/java/org/joinmastodon/android/ui/photoviewer/PhotoViewer.java index 9ef99b59..50f6d4bc 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/photoviewer/PhotoViewer.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/photoviewer/PhotoViewer.java @@ -8,6 +8,7 @@ import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.media.MediaPlayer; import android.net.Uri; +import android.os.PowerManager; import android.util.Log; import android.view.Gravity; import android.view.KeyEvent; @@ -51,6 +52,7 @@ public class PhotoViewer implements ZoomPanView.Listener{ private ViewPager2 pager; private ColorDrawable background=new ColorDrawable(0xff000000); private ArrayList players=new ArrayList<>(); + private int screenOnRefCount=0; public PhotoViewer(Activity activity, List attachments, int index, Listener listener){ this.activity=activity; @@ -183,6 +185,26 @@ public class PhotoViewer implements ZoomPanView.Listener{ pager.setTranslationY(pager.getTranslationY()+y); } + private void incKeepScreenOn(){ + if(screenOnRefCount==0){ + WindowManager.LayoutParams wlp=(WindowManager.LayoutParams) windowView.getLayoutParams(); + wlp.flags|=WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; + wm.updateViewLayout(windowView, wlp); + } + screenOnRefCount++; + } + + private void decKeepScreenOn(){ + screenOnRefCount--; + if(screenOnRefCount<0) + throw new IllegalStateException(); + if(screenOnRefCount==0){ + WindowManager.LayoutParams wlp=(WindowManager.LayoutParams) windowView.getLayoutParams(); + wlp.flags&=~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; + wm.updateViewLayout(windowView, wlp); + } + } + public interface Listener{ void setPhotoViewVisibility(int index, boolean visible); @@ -319,6 +341,7 @@ public class PhotoViewer implements ZoomPanView.Listener{ public MediaPlayer player; private Surface surface; private boolean playerReady; + private boolean keepingScreenOn; public GifVViewHolder(){ textureView=new TextureView(activity); @@ -378,6 +401,12 @@ public class PhotoViewer implements ZoomPanView.Listener{ player.setSurface(surface); player.setLooping(true); player.start(); + if(item.type==Attachment.Type.VIDEO){ + incKeepScreenOn(); + keepingScreenOn=true; + }else{ + keepingScreenOn=false; + } } @Override @@ -405,6 +434,10 @@ public class PhotoViewer implements ZoomPanView.Listener{ player.release(); players.remove(player); player=null; + if(keepingScreenOn){ + decKeepScreenOn(); + keepingScreenOn=false; + } } } }