/*
 * 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 androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ConcatenatingMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;

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

  private PlayerView playerView;
  private SimpleExoPlayer player;
  private boolean playWhenReady = true;
  private int currentWindow = 0;
  private long playbackPosition = 0;

  @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() {
    player = new SimpleExoPlayer.Builder(this).build();
    playerView.setPlayer(player);

    Uri uri = Uri.parse(getString(R.string.media_url_mp4));
    MediaSource mediaSource = buildMediaSource(uri);

    player.setPlayWhenReady(playWhenReady);
    player.seekTo(currentWindow, playbackPosition);
    player.prepare(mediaSource, false, 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) {
    // These factories are used to construct two media sources below
    DataSource.Factory dataSourceFactory =
            new DefaultDataSourceFactory(this, "exoplayer-codelab");
    ProgressiveMediaSource.Factory mediaSourceFactory =
            new ProgressiveMediaSource.Factory(dataSourceFactory);

    // Create a media source using the supplied URI
    MediaSource mediaSource1 = mediaSourceFactory.createMediaSource(uri);

    // Additionally create a media source using an MP3
    Uri audioUri = Uri.parse(getString(R.string.media_url_mp3));
    MediaSource mediaSource2 = mediaSourceFactory.createMediaSource(audioUri);

    return new ConcatenatingMediaSource(mediaSource1, mediaSource2);
  }

  @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);
  }
}

@@ -739,32 +739,8 @@
 nt;%0A
-import android.net.Uri;%0A
 impo
@@ -829,273 +829,71 @@
 er2.
-ExoPlayerFactory;%0Aimport com.google.android.exoplayer2.SimpleExoPlayer;%0Aimport com.google.android.exoplayer2.source.ConcatenatingMediaSource;%0Aimport com.google.android.exoplayer2.source.MediaSource;%0Aimport com.google.android.exop
+MediaItem;%0Aimport com.google.android.exoplayer2.SimpleExoP
 layer
-2.source.ProgressiveMediaSource
 ;%0Aim
@@ -946,138 +946,8 @@
 ew;%0A
-import com.google.android.exoplayer2.upstream.DataSource;%0Aimport com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;%0A
 impo
@@ -2169,27 +2169,47 @@
     
-Uri uri = Uri.parse
+MediaItem mediaItem = MediaItem.fromUri
 (get
@@ -2249,54 +2249,166 @@
     
-MediaSource mediaSource = buildMediaSource(uri
+player.setMediaItem(mediaItem);%0A%0A    MediaItem secondMediaItem = MediaItem.fromUri(getString(R.string.media_url_mp3));%0A    player.addMediaItem(secondMediaItem
 );%0A%0A
@@ -2526,33 +2526,8 @@
 are(
-mediaSource, false, false
 );%0A 
@@ -2806,764 +2806,8 @@
  %7D%0A%0A
-  private MediaSource buildMediaSource(Uri uri) %7B%0A    // These factories are used to construct two media sources below%0A    DataSource.Factory dataSourceFactory =%0A            new DefaultDataSourceFactory(this, %22exoplayer-codelab%22);%0A    ProgressiveMediaSource.Factory mediaSourceFactory =%0A            new ProgressiveMediaSource.Factory(dataSourceFactory);%0A%0A    // Create a media source using the supplied URI%0A    MediaSource mediaSource1 = mediaSourceFactory.createMediaSource(uri);%0A%0A    // Additionally create a media source using an MP3%0A    Uri audioUri = Uri.parse(getString(R.string.media_url_mp3));%0A    MediaSource mediaSource2 = mediaSourceFactory.createMediaSource(audioUri);%0A%0A    return new ConcatenatingMediaSource(mediaSource1, mediaSource2);%0A  %7D%0A%0A
   @S

Update stage 02 to use ExoPlayer 2.12 APIs