/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
* limitations under the License.
 */
package com.example.exoplayer;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.util.Util;

/**
 * A fullscreen activity to play audio or video streams.
 */
public class PlayerActivity extends AppCompatActivity {

  private SimpleExoPlayer player;
  private PlayerView playerView;

  private long playbackPosition;
  private int currentWindow;
  private boolean playWhenReady = true;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player);

    playerView = findViewById(R.id.video_view);
  }

  @Override
  public void onStart() {
    super.onStart();
    if (Util.SDK_INT > 23) {
      initializePlayer();
    }
  }

  @Override
  public void onResume() {
    super.onResume();
    hideSystemUi();
    if ((Util.SDK_INT <= 23 || player == null)) {
      initializePlayer();
    }
  }

  @Override
  public void onPause() {
    super.onPause();
    if (Util.SDK_INT <= 23) {
      releasePlayer();
    }
  }

  @Override
  public void onStop() {
    super.onStop();
    if (Util.SDK_INT > 23) {
      releasePlayer();
    }
  }

  private void initializePlayer() {
    if (player == null) {
      player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this),
          new DefaultTrackSelector(), new DefaultLoadControl());
      playerView.setPlayer(player);
      player.setPlayWhenReady(playWhenReady);
      player.seekTo(currentWindow, playbackPosition);
    }
    MediaSource mediaSource = buildMediaSource(Uri.parse(getString(R.string.media_url_mp4)));
    player.prepare(mediaSource, true, false);
  }

  private void releasePlayer() {
    if (player != null) {
      playbackPosition = player.getCurrentPosition();
      currentWindow = player.getCurrentWindowIndex();
      playWhenReady = player.getPlayWhenReady();
      player.release();
      player = null;
    }
  }

  private MediaSource buildMediaSource(Uri uri) {
    return new ExtractorMediaSource.Factory(new DefaultHttpDataSourceFactory("exoplayer-codelab"))
        .createMediaSource(uri);
  }

  @SuppressLint("InlinedApi")
  private void hideSystemUi() {
    playerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
        | View.SYSTEM_UI_FLAG_FULLSCREEN
        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
  }

}

@@ -575,16 +575,17 @@
 ons and%0A
+ 
 * limita
@@ -645,16 +645,66 @@
 layer;%0A%0A
+import androidx.appcompat.app.AppCompatActivity;%0A%0A
 import a
@@ -804,187 +804,19 @@
 oid.
-support.v7.app.AppCompatActivity;%0Aimport android.view.View;%0A%0Aimport com.google.android.exoplayer2.DefaultLoadControl;%0Aimport com.google.android.exoplayer2.DefaultRenderersFactory;
+view.View;%0A
 %0Aimp
@@ -921,16 +921,86 @@
 Player;%0A
+import com.google.android.exoplayer2.source.ConcatenatingMediaSource;%0A
 import c
@@ -1155,43 +1155,37 @@
 er2.
-trackselection.DefaultTrackSelector
+source.ProgressiveMediaSource
 ;%0Aim
@@ -1234,16 +1234,74 @@
 erView;%0A
+import com.google.android.exoplayer2.upstream.DataSource;%0A
 import c
@@ -1553,30 +1553,29 @@
 ate 
-SimpleExo
 Player
+View
  player
+View
 ;%0A  
@@ -1582,39 +1582,39 @@
 private 
+SimpleExo
 Player
-View
  player
-View;%0A
+;
 %0A  priva
@@ -1620,29 +1620,36 @@
 ate 
-long playbackPosition
+boolean playWhenReady = true
 ;%0A  
@@ -1673,16 +1673,20 @@
 ntWindow
+ = 0
 ;%0A  priv
@@ -1693,36 +1693,33 @@
 ate 
-boolean playWhenReady = true
+long playbackPosition = 0
 ;%0A%0A 
@@ -2500,36 +2500,8 @@
 ) %7B%0A
-    if (player == null) %7B%0A  
     
@@ -2548,150 +2548,168 @@
 nce(
-new DefaultRenderersFactory(this),%0A          new DefaultTrackSelector(), new DefaultLoadControl());%0A      playerView.setPlayer(player
+this);%0A    playerView.setPlayer(player);%0A%0A    Uri uri = Uri.parse(getString(R.string.media_url_mp4));%0A    MediaSource mediaSource = buildMediaSource(uri
 );%0A
+%0A
     
-  
 play
@@ -2744,18 +2744,16 @@
 Ready);%0A
-  
     play
@@ -2800,108 +2800,8 @@
 n);%0A
-    %7D%0A    MediaSource mediaSource = buildMediaSource(Uri.parse(getString(R.string.media_url_mp4)));%0A
     
@@ -2828,19 +2828,20 @@
 Source, 
-tru
+fals
 e, false
@@ -3178,48 +3178,47 @@
     
-return new ExtractorMedi
+DataSource.Factory dat
 aSource
-.
 Factory
-(
+ = 
 new 
@@ -3266,18 +3266,87 @@
 odelab%22)
-)%0A
+;%0A    return new ProgressiveMediaSource.Factory(dataSourceFactory)%0A    
         
@@ -3743,11 +3743,10 @@
 N);%0A  %7D%0A
-%0A
 %7D%0A

Save state of codelab-01