From c72127e1ce53caf3e28e7bda0155ac84beaf8a31 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Tue, 26 Sep 2023 17:05:40 +0200 Subject: [PATCH] keep screen on while playing voice message --- .../securesms/audio/AudioSlidePlayer.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java b/src/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java index 4e745773d..0330c81ca 100644 --- a/src/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java +++ b/src/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java @@ -1,12 +1,15 @@ package org.thoughtcrime.securesms.audio; +import android.app.Activity; import android.content.Context; +import android.content.ContextWrapper; import android.content.Intent; import android.net.Uri; import android.os.Handler; import android.os.Message; import android.util.Log; import android.util.Pair; +import android.view.WindowManager; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -129,6 +132,7 @@ public class AudioSlidePlayer { @Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { Log.d(TAG, "onPlayerStateChanged(" + playWhenReady + ", " + playbackState + ")"); + Activity activity; switch (playbackState) { case Player.STATE_READY: @@ -151,6 +155,11 @@ public class AudioSlidePlayer { setPlaying(AudioSlidePlayer.this); } + activity = getActivity(context); + if (activity != null) { + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } + notifyOnStart(); progressEventHandler.sendEmptyMessage(0); break; @@ -163,6 +172,11 @@ public class AudioSlidePlayer { mediaPlayer = null; } + activity = getActivity(context); + if (activity != null) { + activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } + notifyOnStop(); progressEventHandler.removeMessages(0); } @@ -285,6 +299,13 @@ public class AudioSlidePlayer { }; } + private Activity getActivity(Context context) { + if (context == null) return null; + if (context instanceof Activity) return (Activity) context; + if (context instanceof ContextWrapper) return getActivity(((ContextWrapper)context).getBaseContext()); + return null; + } + private synchronized static void setPlaying(@NonNull AudioSlidePlayer player) { if (playing.isPresent() && playing.get() != player) { playing.get().notifyOnStop();