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 d83d7076c5..bd74eb5c2c 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 @@ -25,6 +25,7 @@ import com.google.android.exoplayer2.offline.DefaultDownloadIndex; import com.google.android.exoplayer2.offline.DefaultDownloaderFactory; import com.google.android.exoplayer2.offline.DownloadManager; import com.google.android.exoplayer2.offline.DownloaderConstructorHelper; +import com.google.android.exoplayer2.ui.DownloadNotificationHelper; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; @@ -45,6 +46,8 @@ import java.io.IOException; */ public class DemoApplication extends Application { + public static final String DOWNLOAD_NOTIFICATION_CHANNEL_ID = "download_channel"; + private static final String TAG = "DemoApplication"; private static final String DOWNLOAD_ACTION_FILE = "actions"; private static final String DOWNLOAD_TRACKER_ACTION_FILE = "tracked_actions"; @@ -57,6 +60,7 @@ public class DemoApplication extends Application { private Cache downloadCache; private DownloadManager downloadManager; private DownloadTracker downloadTracker; + private DownloadNotificationHelper downloadNotificationHelper; @Override public void onCreate() { @@ -93,6 +97,14 @@ public class DemoApplication extends Application { .setExtensionRendererMode(extensionRendererMode); } + public DownloadNotificationHelper getDownloadNotificationHelper() { + if (downloadNotificationHelper == null) { + downloadNotificationHelper = + new DownloadNotificationHelper(this, DOWNLOAD_NOTIFICATION_CHANNEL_ID); + } + return downloadNotificationHelper; + } + public DownloadManager getDownloadManager() { initDownloadManager(); return downloadManager; 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 be2863d4eb..076e6d4016 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 @@ -15,6 +15,8 @@ */ package com.google.android.exoplayer2.demo; +import static com.google.android.exoplayer2.demo.DemoApplication.DOWNLOAD_NOTIFICATION_CHANNEL_ID; + import android.app.Notification; import android.content.Context; import com.google.android.exoplayer2.offline.Download; @@ -29,35 +31,29 @@ import java.util.List; /** A service for downloading media. */ public class DemoDownloadService extends DownloadService { - private static final String CHANNEL_ID = "download_channel"; private static final int JOB_ID = 1; private static final int FOREGROUND_NOTIFICATION_ID = 1; - private DownloadNotificationHelper notificationHelper; - public DemoDownloadService() { super( FOREGROUND_NOTIFICATION_ID, DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL, - CHANNEL_ID, + DOWNLOAD_NOTIFICATION_CHANNEL_ID, R.string.exo_download_notification_channel_name, /* channelDescriptionResourceId= */ 0); } - @Override - public void onCreate() { - super.onCreate(); - notificationHelper = new DownloadNotificationHelper(this, CHANNEL_ID); - } - @Override protected DownloadManager getDownloadManager() { - DownloadManager downloadManager = ((DemoApplication) getApplication()).getDownloadManager(); // This will only happen once, because getDownloadManager is guaranteed to be called only once // in the life cycle of the process. + DemoApplication application = (DemoApplication) getApplication(); + DownloadManager downloadManager = application.getDownloadManager(); + DownloadNotificationHelper downloadNotificationHelper = + application.getDownloadNotificationHelper(); downloadManager.addListener( new TerminalStateNotificationHelper( - this, notificationHelper, FOREGROUND_NOTIFICATION_ID + 1)); + this, downloadNotificationHelper, FOREGROUND_NOTIFICATION_ID + 1)); return downloadManager; } @@ -68,8 +64,10 @@ public class DemoDownloadService extends DownloadService { @Override protected Notification getForegroundNotification(List downloads) { - return notificationHelper.buildProgressNotification( - R.drawable.ic_download, /* contentIntent= */ null, /* message= */ null, downloads); + return ((DemoApplication) getApplication()) + .getDownloadNotificationHelper() + .buildProgressNotification( + R.drawable.ic_download, /* contentIntent= */ null, /* message= */ null, downloads); } /**