/*
 * Copyright (C) 2016 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.demo;

import android.app.Application;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.util.Util;

/**
 * Placeholder application to facilitate overriding Application methods for debugging and testing.
 */
public class DemoApplication extends Application {

  protected String userAgent;

  @Override
  public void onCreate() {
    super.onCreate();
    userAgent = Util.getUserAgent(this, "ExoPlayerDemo");
  }

  /** Returns a {@link DataSource.Factory}. */
  public DataSource.Factory buildDataSourceFactory(TransferListener<? super DataSource> listener) {
    return new DefaultDataSourceFactory(this, listener, buildHttpDataSourceFactory(listener));
  }

  /** Returns a {@link HttpDataSource.Factory}. */
  public HttpDataSource.Factory buildHttpDataSourceFactory(
      TransferListener<? super DataSource> listener) {
    return new DefaultHttpDataSourceFactory(userAgent, listener);
  }

  public boolean useExtensionRenderers() {
    return "withExtensions".equals(BuildConfig.FLAVOR);
  }

}

@@ -888,32 +888,101 @@
 aSourceFactory;%0A
+import com.google.android.exoplayer2.upstream.FileDataSourceFactory;%0A
 import com.googl
@@ -1091,16 +1091,355 @@
 stener;%0A
+import com.google.android.exoplayer2.upstream.cache.Cache;%0Aimport com.google.android.exoplayer2.upstream.cache.CacheDataSource;%0Aimport com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory;%0Aimport com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor;%0Aimport com.google.android.exoplayer2.upstream.cache.SimpleCache;%0A
 import c
@@ -1477,16 +1477,37 @@
 il.Util;
+%0Aimport java.io.File;
 %0A%0A/**%0A *
@@ -1667,32 +1667,131 @@
   pr
-otected String userAgent
+ivate static final String DOWNLOAD_CACHE_FOLDER = %22downloads%22;%0A%0A  protected String userAgent;%0A  private Cache downloadCache
 ;%0A%0A 
@@ -2056,38 +2056,82 @@
 listener) %7B%0A    
-return
+DefaultDataSourceFactory upstreamFactory =%0A       
  new DefaultData
@@ -2199,16 +2199,95 @@
 ener));%0A
+    return createReadOnlyCacheDataSource(upstreamFactory, getDownloadCache());%0A
   %7D%0A%0A  /
@@ -2526,108 +2526,905 @@
 %0A%0A  
-public boolean useExtensionRenderers() %7B%0A    return %22withExtensions%22.equals(BuildConfig.FLAVOR);%0A  %7D%0A
+/** Returns the download %7B@link Cache%7D. */%0A  public Cache getDownloadCache() %7B%0A    if (downloadCache == null) %7B%0A      File dir = getExternalFilesDir(null);%0A      if (dir == null) %7B%0A        dir = getFilesDir();%0A      %7D%0A      File downloadCacheFolder = new File(dir, DOWNLOAD_CACHE_FOLDER);%0A      downloadCache = new SimpleCache(downloadCacheFolder, new NoOpCacheEvictor());%0A    %7D%0A    return downloadCache;%0A  %7D%0A%0A  public boolean useExtensionRenderers() %7B%0A    return %22withExtensions%22.equals(BuildConfig.FLAVOR);%0A  %7D%0A%0A  private static CacheDataSourceFactory createReadOnlyCacheDataSource(%0A      DefaultDataSourceFactory upstreamFactory, Cache cache) %7B%0A    return new CacheDataSourceFactory(%0A        cache,%0A        upstreamFactory,%0A        new FileDataSourceFactory(),%0A        /*cacheWriteDataSinkFactory=*/ null,%0A        CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR,%0A        /*eventListener=*/ null);%0A  %7D
 %0A%7D%0A

Make demo app play content from download cache