/*
 * 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.ext.ima;

import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.ViewGroup;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.source.CompositeMediaSource;
import com.google.android.exoplayer2.source.MediaPeriod;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ads.AdsMediaSource;
import com.google.android.exoplayer2.upstream.Allocator;
import com.google.android.exoplayer2.upstream.DataSource;

/**
 * A {@link MediaSource} that inserts ads linearly with a provided content media source.
 *
 * @deprecated Use com.google.android.exoplayer2.source.ads.AdsMediaSource with ImaAdsLoader.
 */
@Deprecated
public final class ImaAdsMediaSource extends CompositeMediaSource<Void> {

  private final AdsMediaSource adsMediaSource;
  private Listener listener;

  /**
   * Constructs a new source that inserts ads linearly with the content specified by
   * {@code contentMediaSource}.
   *
   * @param contentMediaSource The {@link MediaSource} providing the content to play.
   * @param dataSourceFactory Factory for data sources used to load ad media.
   * @param imaAdsLoader The loader for ads.
   * @param adUiViewGroup A {@link ViewGroup} on top of the player that will show any ad UI.
   */
  public ImaAdsMediaSource(MediaSource contentMediaSource, DataSource.Factory dataSourceFactory,
      ImaAdsLoader imaAdsLoader, ViewGroup adUiViewGroup) {
    this(contentMediaSource, dataSourceFactory, imaAdsLoader, adUiViewGroup, null, null);
  }

  /**
   * Constructs a new source that inserts ads linearly with the content specified by {@code
   * contentMediaSource}.
   *
   * @param contentMediaSource The {@link MediaSource} providing the content to play.
   * @param dataSourceFactory Factory for data sources used to load ad media.
   * @param imaAdsLoader The loader for ads.
   * @param adUiViewGroup A {@link ViewGroup} on top of the player that will show any ad UI.
   * @param eventHandler A handler for events. May be null if delivery of events is not required.
   * @param eventListener A listener of events. May be null if delivery of events is not required.
   */
  public ImaAdsMediaSource(
      MediaSource contentMediaSource,
      DataSource.Factory dataSourceFactory,
      ImaAdsLoader imaAdsLoader,
      ViewGroup adUiViewGroup,
      @Nullable Handler eventHandler,
      @Nullable AdsMediaSource.EventListener eventListener) {
    adsMediaSource = new AdsMediaSource(contentMediaSource, dataSourceFactory, imaAdsLoader,
        adUiViewGroup, eventHandler, eventListener);
  }

  @Override
  public void prepareSource(ExoPlayer player, boolean isTopLevelSource, Listener listener) {
    super.prepareSource(player, isTopLevelSource, listener);
    this.listener = listener;
    prepareChildSource(/* id= */ null, adsMediaSource);
  }

  @Override
  public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) {
    return adsMediaSource.createPeriod(id, allocator);
  }

  @Override
  public void releasePeriod(MediaPeriod mediaPeriod) {
    adsMediaSource.releasePeriod(mediaPeriod);
  }

  @Override
  protected void onChildSourceInfoRefreshed(
      Void id, MediaSource mediaSource, Timeline timeline, @Nullable Object manifest) {
    listener.onSourceInfoRefreshed(this, timeline, manifest);
  }
}

@@ -862,74 +862,8 @@
 ne;%0A
-import com.google.android.exoplayer2.source.CompositeMediaSource;%0A
 impo
@@ -1150,16 +1150,44 @@
 aSource;
+%0Aimport java.io.IOException;
 %0A%0A/**%0A *
@@ -1427,25 +1427,19 @@
 rce 
-extend
+implement
 s 
-Composite
 Medi
@@ -1449,14 +1449,8 @@
 urce
-%3CVoid%3E
  %7B%0A%0A
@@ -1499,37 +1499,8 @@
 rce;
-%0A  private Listener listener;
 %0A%0A  
@@ -3285,16 +3285,29 @@
 eSource(
+%0A      final 
 ExoPlaye
@@ -3341,16 +3341,22 @@
 lSource,
+ final
  Listene
@@ -3377,13 +3377,22 @@
     
-super
+adsMediaSource
 .pre
@@ -3406,128 +3406,474 @@
 rce(
-player, isTopLevelSource, listener);%0A    this.listener = listener;%0A    prepareChildSource(/* id= */ null, adsMediaSource
+%0A        player,%0A        isTopLevelSource,%0A        new Listener() %7B%0A          @Override%0A          public void onSourceInfoRefreshed(%0A              MediaSource source, Timeline timeline, @Nullable Object manifest) %7B%0A            listener.onSourceInfoRefreshed(ImaAdsMediaSource.this, timeline, manifest);%0A          %7D%0A        %7D);%0A  %7D%0A%0A  @Override%0A  public void maybeThrowSourceInfoRefreshError() throws IOException %7B%0A    adsMediaSource.maybeThrowSourceInfoRefreshError(
 );%0A 
@@ -4161,197 +4161,70 @@
 %0A  p
-rotected void onChildSourceInfoRefreshed(%0A      Void id, MediaSource mediaSource, Timeline timeline, @Nullable Object manifest) %7B%0A    listener.onSourceInfoRefreshed(this, timeline, manifest
+ublic void releaseSource() %7B%0A    adsMediaSource.releaseSource(
 );%0A 

Fix ImaAdsMediaSource isTopLevelSource