<commit_before>package de.nicidienase.chaosflix.touch.playback;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.LoadControl;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.dash.DashMediaSource;
import com.google.android.exoplayer2.source.dash.DefaultDashChunkSource;
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
import com.google.android.exoplayer2.source.smoothstreaming.DefaultSsChunkSource;
import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.util.Util;

import de.nicidienase.chaosflix.R;
import de.nicidienase.chaosflix.databinding.FragmentExoPlayerBinding;
import de.nicidienase.chaosflix.touch.ViewModelFactory;

public class ExoPlayerFragment extends Fragment implements PlayerEventListener.PlayerStateChangeListener {
	private static final String TAG = ExoPlayerFragment.class.getSimpleName();
	private static final String PLAYBACK_STATE = "playback_state";
	private static final String ARG_item = "item";

	private OnMediaPlayerInteractionListener listener;
	private final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();

	private String userAgent;
	private Handler mainHandler = new Handler();
	private boolean playbackState = true;
	private SimpleExoPlayer exoPlayer;
	private PlayerViewModel viewModel;
	private PlaybackItem item;

	FragmentExoPlayerBinding binding;

	public ExoPlayerFragment() {
	}

	public static ExoPlayerFragment newInstance(PlaybackItem item) {
		ExoPlayerFragment fragment = new ExoPlayerFragment();
		Bundle args = new Bundle();
		args.putParcelable(ARG_item, item);
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {
			item = getArguments().getParcelable(ARG_item);
		}
		if (savedInstanceState != null) {
			playbackState = savedInstanceState.getBoolean(PLAYBACK_STATE, true);
		}
		viewModel = ViewModelProviders.of(this, ViewModelFactory.INSTANCE).get(PlayerViewModel.class);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		binding = DataBindingUtil.inflate(inflater, R.layout.fragment_exo_player, container, false);

		Toolbar toolbar = binding.getRoot().findViewById(R.id.toolbar);
		toolbar.setTitle(item.getTitle());
		toolbar.setSubtitle(item.getSubtitle());
		((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
		((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		return binding.getRoot();
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		if (exoPlayer == null) {
			exoPlayer = setupPlayer();
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if (exoPlayer != null) {
			exoPlayer.setPlayWhenReady(playbackState);
			viewModel.getPlaybackProgress(item.getEventId()).observe(this, playbackProgress -> {
				if (playbackProgress != null) {
					exoPlayer.seekTo(playbackProgress.getProgress());
				}
			});
			binding.videoView.setPlayer(exoPlayer);
		}
	}

	@Override
	public void onStop() {
		super.onStop();
		getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	@Override
	public void onPause() {
		super.onPause();
		if (exoPlayer != null) {
			viewModel.setPlaybackProgress(item.getEventId(), exoPlayer.getCurrentPosition());
			exoPlayer.setPlayWhenReady(false);
		}
	}

	@Override
	public void onStart() {
		super.onStart();
		getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		if (exoPlayer != null) {
			outState.putBoolean(PLAYBACK_STATE, exoPlayer.getPlayWhenReady());
		}
	}

	private SimpleExoPlayer setupPlayer() {
		Log.d(TAG, "Setting up Player.");
		binding.videoView.setKeepScreenOn(true);

		userAgent = Util.getUserAgent(getContext(), getResources().getString(R.string.app_name));

		AdaptiveTrackSelection.Factory trackSelectorFactory = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
		DefaultTrackSelector trackSelector = new DefaultTrackSelector(trackSelectorFactory);<commit_msg>fix pause of playback in split-screen mode<commit_after>package de.nicidienase.chaosflix.touch.playback;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.LoadControl;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.dash.DashMediaSource;
import com.google.android.exoplayer2.source.dash.DefaultDashChunkSource;
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
import com.google.android.exoplayer2.source.smoothstreaming.DefaultSsChunkSource;
import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.util.Util;

import de.nicidienase.chaosflix.R;
import de.nicidienase.chaosflix.databinding.FragmentExoPlayerBinding;
import de.nicidienase.chaosflix.touch.ViewModelFactory;

public class ExoPlayerFragment extends Fragment implements PlayerEventListener.PlayerStateChangeListener {
	private static final String TAG = ExoPlayerFragment.class.getSimpleName();
	private static final String PLAYBACK_STATE = "playback_state";
	private static final String ARG_item = "item";

	private OnMediaPlayerInteractionListener listener;
	private final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();

	private String userAgent;
	private Handler mainHandler = new Handler();
	private boolean playbackState = true;
	private SimpleExoPlayer exoPlayer;
	private PlayerViewModel viewModel;
	private PlaybackItem item;

	FragmentExoPlayerBinding binding;

	public ExoPlayerFragment() {
	}

	public static ExoPlayerFragment newInstance(PlaybackItem item) {
		ExoPlayerFragment fragment = new ExoPlayerFragment();
		Bundle args = new Bundle();
		args.putParcelable(ARG_item, item);
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {
			item = getArguments().getParcelable(ARG_item);
		}
		if (savedInstanceState != null) {
			playbackState = savedInstanceState.getBoolean(PLAYBACK_STATE, true);
		}
		viewModel = ViewModelProviders.of(this, ViewModelFactory.INSTANCE).get(PlayerViewModel.class);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		binding = DataBindingUtil.inflate(inflater, R.layout.fragment_exo_player, container, false);

		Toolbar toolbar = binding.getRoot().findViewById(R.id.toolbar);
		toolbar.setTitle(item.getTitle());
		toolbar.setSubtitle(item.getSubtitle());
		((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
		((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		return binding.getRoot();
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		if (exoPlayer == null) {
			exoPlayer = setupPlayer();
		}
	}

	@Override
	public void onStop() {
		super.onStop();
		if (exoPlayer != null) {
			viewModel.setPlaybackProgress(item.getEventId(), exoPlayer.getCurrentPosition());
			exoPlayer.setPlayWhenReady(false);
		}
		getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}


	@Override
	public void onStart() {
		super.onStart();
		getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		if (exoPlayer != null) {
			exoPlayer.setPlayWhenReady(playbackState);
			viewModel.getPlaybackProgress(item.getEventId()).observe(this, playbackProgress -> {
				if (playbackProgress != null) {
					exoPlayer.seekTo(playbackProgress.getProgress());
				}
			});
			binding.videoView.setPlayer(exoPlayer);
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		if (exoPlayer != null) {
			outState.putBoolean(PLAYBACK_STATE, exoPlayer.getPlayWhenReady());
		}
	}

	private SimpleExoPlayer setupPlayer() {
		Log.d(TAG, "Setting up Player.");
		binding.videoView.setKeepScreenOn(true);

		userAgent = Util.getUserAgent(getContext(), getResources().getString(R.string.app_name));

		AdaptiveTrackSelection.Factory trackSelectorFactory = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
		DefaultTrackSelector trackSelector = new DefaultTrackSelector(trackSelectorFactory);
{'fraction_non_alphanumeric': 0.1013691850874299, 'fraction_numerical': 0.004289013526888816, 'mean_word_length': 6.7377153797064455, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1894552', 'n_tokens_mistral': 3823, 'n_tokens_neox': 3598, 'n_words': 587}