Move DownloadManager initialization into the application

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=195317847
This commit is contained in:
olly 2018-05-03 15:07:55 -07:00 committed by Oliver Woodman
parent a0b3fb4e87
commit bef4a20c31
2 changed files with 39 additions and 35 deletions

View File

@ -16,6 +16,13 @@
package com.google.android.exoplayer2.demo; package com.google.android.exoplayer2.demo;
import android.app.Application; import android.app.Application;
import com.google.android.exoplayer2.offline.DownloadAction.Deserializer;
import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
import com.google.android.exoplayer2.offline.ProgressiveDownloadAction;
import com.google.android.exoplayer2.source.dash.offline.DashDownloadAction;
import com.google.android.exoplayer2.source.hls.offline.HlsDownloadAction;
import com.google.android.exoplayer2.source.smoothstreaming.offline.SsDownloadAction;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
@ -37,10 +44,20 @@ public class DemoApplication extends Application {
private static final String DOWNLOAD_ACTION_FILE = "actions"; private static final String DOWNLOAD_ACTION_FILE = "actions";
private static final String DOWNLOAD_CONTENT_DIRECTORY = "downloads"; private static final String DOWNLOAD_CONTENT_DIRECTORY = "downloads";
private static final int MAX_SIMULTANEOUS_DOWNLOADS = 2;
private static final Deserializer[] DOWNLOAD_DESERIALIZERS =
new Deserializer[] {
DashDownloadAction.DESERIALIZER,
HlsDownloadAction.DESERIALIZER,
SsDownloadAction.DESERIALIZER,
ProgressiveDownloadAction.DESERIALIZER
};
protected String userAgent; protected String userAgent;
private File downloadDirectory; private File downloadDirectory;
private Cache downloadCache; private Cache downloadCache;
private DownloadManager downloadManager;
@Override @Override
public void onCreate() { public void onCreate() {
@ -52,7 +69,7 @@ public class DemoApplication extends Application {
public DataSource.Factory buildDataSourceFactory(TransferListener<? super DataSource> listener) { public DataSource.Factory buildDataSourceFactory(TransferListener<? super DataSource> listener) {
DefaultDataSourceFactory upstreamFactory = DefaultDataSourceFactory upstreamFactory =
new DefaultDataSourceFactory(this, listener, buildHttpDataSourceFactory(listener)); new DefaultDataSourceFactory(this, listener, buildHttpDataSourceFactory(listener));
return createReadOnlyCacheDataSource(upstreamFactory, getDownloadCache()); return buildReadOnlyCacheDataSource(upstreamFactory, getDownloadCache());
} }
/** Returns a {@link HttpDataSource.Factory}. */ /** Returns a {@link HttpDataSource.Factory}. */
@ -66,8 +83,23 @@ public class DemoApplication extends Application {
return "withExtensions".equals(BuildConfig.FLAVOR); return "withExtensions".equals(BuildConfig.FLAVOR);
} }
/** Returns the download {@link Cache}. */ /** Returns the download manager used by the application. */
public synchronized Cache getDownloadCache() { public synchronized DownloadManager getDownloadManager() {
if (downloadManager == null) {
DownloaderConstructorHelper constructorHelper =
new DownloaderConstructorHelper(getDownloadCache(), buildHttpDataSourceFactory(null));
downloadManager =
new DownloadManager(
constructorHelper,
MAX_SIMULTANEOUS_DOWNLOADS,
DownloadManager.DEFAULT_MIN_RETRY_COUNT,
new File(getDownloadDirectory(), DOWNLOAD_ACTION_FILE),
DOWNLOAD_DESERIALIZERS);
}
return downloadManager;
}
private synchronized Cache getDownloadCache() {
if (downloadCache == null) { if (downloadCache == null) {
File downloadContentDirectory = new File(getDownloadDirectory(), DOWNLOAD_CONTENT_DIRECTORY); File downloadContentDirectory = new File(getDownloadDirectory(), DOWNLOAD_CONTENT_DIRECTORY);
downloadCache = new SimpleCache(downloadContentDirectory, new NoOpCacheEvictor()); downloadCache = new SimpleCache(downloadContentDirectory, new NoOpCacheEvictor());
@ -75,11 +107,6 @@ public class DemoApplication extends Application {
return downloadCache; return downloadCache;
} }
/** Returns the file in which active download actions should be saved. */
public synchronized File getDownloadActionFile() {
return new File(getDownloadDirectory(), DOWNLOAD_ACTION_FILE);
}
private File getDownloadDirectory() { private File getDownloadDirectory() {
if (downloadDirectory == null) { if (downloadDirectory == null) {
downloadDirectory = getExternalFilesDir(null); downloadDirectory = getExternalFilesDir(null);
@ -90,14 +117,14 @@ public class DemoApplication extends Application {
return downloadDirectory; return downloadDirectory;
} }
private static CacheDataSourceFactory createReadOnlyCacheDataSource( private static CacheDataSourceFactory buildReadOnlyCacheDataSource(
DefaultDataSourceFactory upstreamFactory, Cache cache) { DefaultDataSourceFactory upstreamFactory, Cache cache) {
return new CacheDataSourceFactory( return new CacheDataSourceFactory(
cache, cache,
upstreamFactory, upstreamFactory,
new FileDataSourceFactory(), new FileDataSourceFactory(),
/*cacheWriteDataSinkFactory=*/ null, /* cacheWriteDataSinkFactory= */ null,
CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR,
/*eventListener=*/ null); /* eventListener= */ null);
} }
} }

View File

@ -19,12 +19,7 @@ import android.app.Notification;
import com.google.android.exoplayer2.offline.DownloadManager; import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState; import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
import com.google.android.exoplayer2.offline.DownloadService; import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
import com.google.android.exoplayer2.offline.ProgressiveDownloadAction;
import com.google.android.exoplayer2.scheduler.PlatformScheduler; import com.google.android.exoplayer2.scheduler.PlatformScheduler;
import com.google.android.exoplayer2.source.dash.offline.DashDownloadAction;
import com.google.android.exoplayer2.source.hls.offline.HlsDownloadAction;
import com.google.android.exoplayer2.source.smoothstreaming.offline.SsDownloadAction;
import com.google.android.exoplayer2.ui.DownloadNotificationUtil; import com.google.android.exoplayer2.ui.DownloadNotificationUtil;
import com.google.android.exoplayer2.util.NotificationUtil; import com.google.android.exoplayer2.util.NotificationUtil;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
@ -36,8 +31,6 @@ public class DemoDownloadService extends DownloadService {
private static final int JOB_ID = 1; private static final int JOB_ID = 1;
private static final int FOREGROUND_NOTIFICATION_ID = 1; private static final int FOREGROUND_NOTIFICATION_ID = 1;
private static DownloadManager downloadManager;
public DemoDownloadService() { public DemoDownloadService() {
super( super(
FOREGROUND_NOTIFICATION_ID, FOREGROUND_NOTIFICATION_ID,
@ -48,23 +41,7 @@ public class DemoDownloadService extends DownloadService {
@Override @Override
protected DownloadManager getDownloadManager() { protected DownloadManager getDownloadManager() {
if (downloadManager == null) { return ((DemoApplication) getApplication()).getDownloadManager();
DemoApplication application = (DemoApplication) getApplication();
DownloaderConstructorHelper constructorHelper =
new DownloaderConstructorHelper(
application.getDownloadCache(), application.buildHttpDataSourceFactory(null));
downloadManager =
new DownloadManager(
constructorHelper,
/* maxSimultaneousDownloads= */ 2,
DownloadManager.DEFAULT_MIN_RETRY_COUNT,
application.getDownloadActionFile(),
DashDownloadAction.DESERIALIZER,
HlsDownloadAction.DESERIALIZER,
SsDownloadAction.DESERIALIZER,
ProgressiveDownloadAction.DESERIALIZER);
}
return downloadManager;
} }
@Override @Override