Keep screen on when viewing a video

Fixes #6642
Closes #6837

// FREEBIE
pull/1/head
jdaminato94 7 years ago committed by Moxie Marlinspike
parent 66062f200a
commit 6945ecf81d

@ -176,6 +176,7 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im
} else if (mediaType != null && mediaType.startsWith("video/")) { } else if (mediaType != null && mediaType.startsWith("video/")) {
image.setVisibility(View.GONE); image.setVisibility(View.GONE);
video.setVisibility(View.VISIBLE); video.setVisibility(View.VISIBLE);
video.setWindow(getWindow());
video.setVideoSource(masterSecret, new VideoSlide(this, mediaUri, size)); video.setVideoSource(masterSecret, new VideoSlide(this, mediaUri, size));
} }
} catch (IOException e) { } catch (IOException e) {

@ -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 <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms.video; package org.thoughtcrime.securesms.video;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.MediaController; import android.widget.MediaController;
import android.widget.Toast; import android.widget.Toast;
import android.widget.VideoView; import android.widget.VideoView;
import com.google.android.exoplayer2.DefaultLoadControl; 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.ExoPlayerFactory;
import com.google.android.exoplayer2.LoadControl; import com.google.android.exoplayer2.LoadControl;
import com.google.android.exoplayer2.SimpleExoPlayer; 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.DefaultExtractorsFactory;
import com.google.android.exoplayer2.extractor.ExtractorsFactory; import com.google.android.exoplayer2.extractor.ExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource; import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource; 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.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection; 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.trackselection.TrackSelector;
import com.google.android.exoplayer2.ui.SimpleExoPlayerView; import com.google.android.exoplayer2.ui.SimpleExoPlayerView;
import com.google.android.exoplayer2.upstream.BandwidthMeter; import com.google.android.exoplayer2.upstream.BandwidthMeter;
@ -48,6 +70,7 @@ public class VideoPlayer extends FrameLayout {
@Nullable private SimpleExoPlayer exoPlayer; @Nullable private SimpleExoPlayer exoPlayer;
@Nullable private AttachmentServer attachmentServer; @Nullable private AttachmentServer attachmentServer;
@Nullable private Window window;
public VideoPlayer(Context context) { public VideoPlayer(Context context) {
this(context, null); 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) private void setExoViewSource(@NonNull MasterSecret masterSecret, @NonNull VideoSlide videoSource)
throws IOException throws IOException
{ {
@ -98,6 +125,7 @@ public class VideoPlayer extends FrameLayout {
LoadControl loadControl = new DefaultLoadControl(); LoadControl loadControl = new DefaultLoadControl();
exoPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl); exoPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl);
exoPlayer.addListener(new ExoPlayerListener(window));
exoView.setPlayer(exoPlayer); exoView.setPlayer(exoPlayer);
DefaultDataSourceFactory defaultDataSourceFactory = new DefaultDataSourceFactory(getContext(), "GenericUserAgent", null); DefaultDataSourceFactory defaultDataSourceFactory = new DefaultDataSourceFactory(getContext(), "GenericUserAgent", null);
@ -141,4 +169,47 @@ public class VideoPlayer extends FrameLayout {
videoView.setMediaController(mediaController); 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() { }
}
} }

Loading…
Cancel
Save