/*
 * 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.google.android.exoplayer2.castdemo;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ext.cast.CastPlayer;
import com.google.android.exoplayer2.ui.PlaybackControlView;
import com.google.android.exoplayer2.ui.SimpleExoPlayerView;
import com.google.android.exoplayer2.util.Util;
import com.google.android.gms.cast.framework.CastButtonFactory;

/**
 * An activity that plays video using {@link SimpleExoPlayer} and {@link CastPlayer}.
 */
public class MainActivity extends AppCompatActivity {

  private SimpleExoPlayerView simpleExoPlayerView;
  private PlaybackControlView castControlView;
  private PlayerManager playerManager;

  // Activity lifecycle methods.

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_activity);

    simpleExoPlayerView = findViewById(R.id.player_view);
    simpleExoPlayerView.requestFocus();

    castControlView = findViewById(R.id.cast_control_view);

    ListView sampleList = findViewById(R.id.sample_list);
    sampleList.setAdapter(new SampleListAdapter());
    sampleList.setOnItemClickListener(new SampleClickListener());
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.menu, menu);
    CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), menu,
        R.id.media_route_menu_item);
    return true;
  }

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

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

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

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

  // Activity input.

  @Override
  public boolean dispatchKeyEvent(KeyEvent event) {
    // If the event was not handled then see if the player view can handle it.
    return super.dispatchKeyEvent(event) || playerManager.dispatchKeyEvent(event);
  }

  // Internal methods.

  private void setupPlayerManager() {
    playerManager = new PlayerManager(simpleExoPlayerView, castControlView,
        getApplicationContext());
  }

  private void releasePlayerManager() {
    playerManager.release();
    playerManager = null;
  }

  // User controls.

  private final class SampleListAdapter extends ArrayAdapter<CastDemoUtil.Sample> {

    public SampleListAdapter() {
      super(getApplicationContext(), android.R.layout.simple_list_item_1, CastDemoUtil.SAMPLES);
    }

    @Override
    @NonNull
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
      View view = super.getView(position, convertView, parent);
      view.setBackgroundColor(Color.WHITE);
      return view;
    }

  }

  private class SampleClickListener implements AdapterView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      if (parent.getSelectedItemPosition() != position) {
        CastDemoUtil.Sample currentSample = CastDemoUtil.SAMPLES.get(position);
        playerManager.setCurrentSample(currentSample, 0, true);
      }
    }

  }

}

@@ -1265,56 +1265,8 @@
 ew;%0A
-import com.google.android.exoplayer2.util.Util;%0A
 impo
@@ -2449,21 +2449,22 @@
  void on
-Start
+Resume
 () %7B%0A   
@@ -2476,21 +2476,22 @@
 r.on
-Start
+Resume
 ();%0A    
 if (
@@ -2490,434 +2490,185 @@
     
-if (Util.SDK_INT %3E 23) %7B%0A      setupPlayerManager();%0A    %7D%0A  %7D%0A%0A  @Override%0A  public void onResume() %7B%0A    super.onResume();%0A    if ((Util.SDK_INT %3C= 23)) %7B%0A      setupPlayerManager();%0A    %7D%0A  %7D%0A%0A  @Override%0A  public void onPause() %7B%0A    super.onPause();%0A    if (Util.SDK_INT %3C= 23) %7B%0A      releasePlayerManager();%0A    %7D%0A  %7D%0A%0A  @Override%0A  public void onStop() %7B%0A    super.onStop();%0A    if (Util.SDK_INT %3E 23) %7B%0A      releaseP
+playerManager = new PlayerManager(simpleExoPlayerView, castControlView, this);%0A  %7D%0A%0A  @Override%0A  public void onPause() %7B%0A    super.onPause();%0A    playerManager.release();%0A    p
 laye
@@ -2679,17 +2679,16 @@
 ager
-();%0A    %7D
+ = null;
 %0A  %7D
@@ -2946,285 +2946,8 @@
  %7D%0A%0A
-  // Internal methods.%0A%0A  private void setupPlayerManager() %7B%0A    playerManager = new PlayerManager(simpleExoPlayerView, castControlView,%0A        getApplicationContext());%0A  %7D%0A%0A  private void releasePlayerManager() %7B%0A    playerManager.release();%0A    playerManager = null;%0A  %7D%0A%0A
   //

Simplify non-main demo apps to not care about multi-window