Skip to content

Commit

Permalink
Don't keep screen on when there is nothing to play
Browse files Browse the repository at this point in the history
Fixes #359
  • Loading branch information
samfundev committed Jul 18, 2022
1 parent caa6bb9 commit 82a3be4
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions app/src/main/java/com/perflyst/twire/fragments/StreamFragment.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.perflyst.twire.fragments;

import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_STATE_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_PLAY_WHEN_READY_CHANGED;
import static com.google.android.exoplayer2.Player.STATE_BUFFERING;
import static com.google.android.exoplayer2.Player.STATE_READY;

import android.app.Activity;
import android.app.PendingIntent;
import android.app.PictureInPictureParams;
Expand Down Expand Up @@ -34,7 +39,6 @@
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
Expand Down Expand Up @@ -354,8 +358,6 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
mRootView.findViewById(R.id.exo_position).setOnClickListener(v -> showSeekDialog());
}

keepScreenOn();

if (autoPlay || vodId != null) {
startStreamWithQuality(settings.getPrefStreamQuality());
}
Expand Down Expand Up @@ -464,6 +466,14 @@ private void releasePlayer() {
}

/* Player.Listener implementation */
@Override
public void onEvents(@NonNull Player player, Player.Events events) {
if (!events.containsAny(EVENT_PLAY_WHEN_READY_CHANGED, EVENT_PLAYBACK_STATE_CHANGED)) return;

int playbackState = player.getPlaybackState();
requireView().setKeepScreenOn(player.getPlayWhenReady() && (playbackState == STATE_READY || playbackState == STATE_BUFFERING));
}

@Override
public void onPlayerError(@NonNull PlaybackException exception) {
Log.e(LOG_TAG, "Something went wrong playing the stream for " + mUserInfo.getDisplayName() + " - Exception: " + exception);
Expand All @@ -475,14 +485,12 @@ public void onPlayerError(@NonNull PlaybackException exception) {
public void onPlayWhenReadyChanged(boolean isPlaying, int _ignored) {
if (isPlaying) {
showPauseIcon();
keepScreenOn();

if (!isAudioOnlyModeEnabled() && vodId == null) {
player.seekToDefaultPosition(); // Go forward to live
}
} else {
showPlayIcon();
releaseScreenOn();
}
}

Expand Down Expand Up @@ -1256,20 +1264,6 @@ private void playWithExternalPlayer() {
private void registerAudioOnlyDelegate() {
}

/**
* Notifies the system that the screen though not timeout and fade to black.
*/
private void keepScreenOn() {
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

/**
* Notifies the system that the screen can now time out.
*/
private void releaseScreenOn() {
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

private void updateSelectedQuality(String quality) {
//TODO: Bad design
if (quality == null) {
Expand Down

0 comments on commit 82a3be4

Please sign in to comment.