From bef4a20c317f187b951518fa89a827ac1e1320ce Mon Sep 17 00:00:00 2001 From: olly Date: Thu, 3 May 2018 15:07:55 -0700 Subject: [PATCH] Move DownloadManager initialization into the application ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=195317847 --- .../exoplayer2/demo/DemoApplication.java | 49 ++++++++++++++----- .../exoplayer2/demo/DemoDownloadService.java | 25 +--------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoApplication.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoApplication.java index bbe220e696..2788684de5 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoApplication.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoApplication.java @@ -16,6 +16,13 @@ package com.google.android.exoplayer2.demo; 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.DefaultDataSourceFactory; 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_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; + private File downloadDirectory; private Cache downloadCache; + private DownloadManager downloadManager; @Override public void onCreate() { @@ -52,7 +69,7 @@ public class DemoApplication extends Application { public DataSource.Factory buildDataSourceFactory(TransferListener listener) { DefaultDataSourceFactory upstreamFactory = new DefaultDataSourceFactory(this, listener, buildHttpDataSourceFactory(listener)); - return createReadOnlyCacheDataSource(upstreamFactory, getDownloadCache()); + return buildReadOnlyCacheDataSource(upstreamFactory, getDownloadCache()); } /** Returns a {@link HttpDataSource.Factory}. */ @@ -66,8 +83,23 @@ public class DemoApplication extends Application { return "withExtensions".equals(BuildConfig.FLAVOR); } - /** Returns the download {@link Cache}. */ - public synchronized Cache getDownloadCache() { + /** Returns the download manager used by the application. */ + 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) { File downloadContentDirectory = new File(getDownloadDirectory(), DOWNLOAD_CONTENT_DIRECTORY); downloadCache = new SimpleCache(downloadContentDirectory, new NoOpCacheEvictor()); @@ -75,11 +107,6 @@ public class DemoApplication extends Application { 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() { if (downloadDirectory == null) { downloadDirectory = getExternalFilesDir(null); @@ -90,14 +117,14 @@ public class DemoApplication extends Application { return downloadDirectory; } - private static CacheDataSourceFactory createReadOnlyCacheDataSource( + private static CacheDataSourceFactory buildReadOnlyCacheDataSource( DefaultDataSourceFactory upstreamFactory, Cache cache) { return new CacheDataSourceFactory( cache, upstreamFactory, new FileDataSourceFactory(), - /*cacheWriteDataSinkFactory=*/ null, + /* cacheWriteDataSinkFactory= */ null, CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, - /*eventListener=*/ null); + /* eventListener= */ null); } } diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoDownloadService.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoDownloadService.java index 357233d283..3e28c13d1c 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoDownloadService.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoDownloadService.java @@ -19,12 +19,7 @@ import android.app.Notification; import com.google.android.exoplayer2.offline.DownloadManager; import com.google.android.exoplayer2.offline.DownloadManager.TaskState; 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.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.util.NotificationUtil; 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 FOREGROUND_NOTIFICATION_ID = 1; - private static DownloadManager downloadManager; - public DemoDownloadService() { super( FOREGROUND_NOTIFICATION_ID, @@ -48,23 +41,7 @@ public class DemoDownloadService extends DownloadService { @Override protected DownloadManager getDownloadManager() { - if (downloadManager == null) { - 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; + return ((DemoApplication) getApplication()).getDownloadManager(); } @Override