From 6945ecf81d85545fc4216b6061c0f7c167bd0c5f Mon Sep 17 00:00:00 2001 From: jdaminato94 Date: Mon, 31 Jul 2017 20:40:46 -0500 Subject: [PATCH] Keep screen on when viewing a video Fixes #6642 Closes #6837 // FREEBIE --- .../securesms/MediaPreviewActivity.java | 1 + .../securesms/video/VideoPlayer.java | 73 ++++++++++++++++++- 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/org/thoughtcrime/securesms/MediaPreviewActivity.java b/src/org/thoughtcrime/securesms/MediaPreviewActivity.java index 08246658db..9535ad27a5 100644 --- a/src/org/thoughtcrime/securesms/MediaPreviewActivity.java +++ b/src/org/thoughtcrime/securesms/MediaPreviewActivity.java @@ -176,6 +176,7 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im } else if (mediaType != null && mediaType.startsWith("video/")) { image.setVisibility(View.GONE); video.setVisibility(View.VISIBLE); + video.setWindow(getWindow()); video.setVideoSource(masterSecret, new VideoSlide(this, mediaUri, size)); } } catch (IOException e) { diff --git a/src/org/thoughtcrime/securesms/video/VideoPlayer.java b/src/org/thoughtcrime/securesms/video/VideoPlayer.java index 6a61d7c4a8..e34c16b4ef 100644 --- a/src/org/thoughtcrime/securesms/video/VideoPlayer.java +++ b/src/org/thoughtcrime/securesms/video/VideoPlayer.java @@ -1,28 +1,50 @@ +/** + * Copyright (C) 2017 Whisper Systems + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.thoughtcrime.securesms.video; - import android.content.Context; import android.os.Build; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.util.Log; +import android.view.Window; +import android.view.WindowManager; import android.widget.FrameLayout; import android.widget.MediaController; import android.widget.Toast; import android.widget.VideoView; import com.google.android.exoplayer2.DefaultLoadControl; +import com.google.android.exoplayer2.ExoPlaybackException; +import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.ExoPlayerFactory; import com.google.android.exoplayer2.LoadControl; import com.google.android.exoplayer2.SimpleExoPlayer; +import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory; import com.google.android.exoplayer2.extractor.ExtractorsFactory; import com.google.android.exoplayer2.source.ExtractorMediaSource; import com.google.android.exoplayer2.source.MediaSource; +import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; import com.google.android.exoplayer2.trackselection.TrackSelection; +import com.google.android.exoplayer2.trackselection.TrackSelectionArray; import com.google.android.exoplayer2.trackselection.TrackSelector; import com.google.android.exoplayer2.ui.SimpleExoPlayerView; import com.google.android.exoplayer2.upstream.BandwidthMeter; @@ -48,6 +70,7 @@ public class VideoPlayer extends FrameLayout { @Nullable private SimpleExoPlayer exoPlayer; @Nullable private AttachmentServer attachmentServer; + @Nullable private Window window; public VideoPlayer(Context context) { this(context, null); @@ -89,6 +112,10 @@ public class VideoPlayer extends FrameLayout { } } + public void setWindow(Window window) { + this.window = window; + } + private void setExoViewSource(@NonNull MasterSecret masterSecret, @NonNull VideoSlide videoSource) throws IOException { @@ -98,6 +125,7 @@ public class VideoPlayer extends FrameLayout { LoadControl loadControl = new DefaultLoadControl(); exoPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl); + exoPlayer.addListener(new ExoPlayerListener(window)); exoView.setPlayer(exoPlayer); DefaultDataSourceFactory defaultDataSourceFactory = new DefaultDataSourceFactory(getContext(), "GenericUserAgent", null); @@ -141,4 +169,47 @@ public class VideoPlayer extends FrameLayout { videoView.setMediaController(mediaController); } + + private class ExoPlayerListener implements ExoPlayer.EventListener { + private final Window window; + + ExoPlayerListener(Window window) { + this.window = window; + } + + @Override + public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { + switch(playbackState) { + case ExoPlayer.STATE_IDLE: + case ExoPlayer.STATE_BUFFERING: + case ExoPlayer.STATE_ENDED: + window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + break; + case ExoPlayer.STATE_READY: + if (playWhenReady) { + window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } else { + window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } + break; + default: + break; + } + } + + @Override + public void onTimelineChanged(Timeline timeline, Object manifest) { } + + @Override + public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) { } + + @Override + public void onLoadingChanged(boolean isLoading) { } + + @Override + public void onPlayerError(ExoPlaybackException error) { } + + @Override + public void onPositionDiscontinuity() { } + } }