Fix demo app download notification

PiperOrigin-RevId: 294503035
This commit is contained in:
olly 2020-02-11 21:09:05 +00:00 committed by Oliver Woodman
parent 147f70bf5b
commit 7a849e11f7
2 changed files with 24 additions and 14 deletions

View File

@ -25,6 +25,7 @@ import com.google.android.exoplayer2.offline.DefaultDownloadIndex;
import com.google.android.exoplayer2.offline.DefaultDownloaderFactory; import com.google.android.exoplayer2.offline.DefaultDownloaderFactory;
import com.google.android.exoplayer2.offline.DownloadManager; import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper; 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.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;
@ -45,6 +46,8 @@ import java.io.IOException;
*/ */
public class DemoApplication extends Application { 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 TAG = "DemoApplication";
private static final String DOWNLOAD_ACTION_FILE = "actions"; private static final String DOWNLOAD_ACTION_FILE = "actions";
private static final String DOWNLOAD_TRACKER_ACTION_FILE = "tracked_actions"; private static final String DOWNLOAD_TRACKER_ACTION_FILE = "tracked_actions";
@ -57,6 +60,7 @@ public class DemoApplication extends Application {
private Cache downloadCache; private Cache downloadCache;
private DownloadManager downloadManager; private DownloadManager downloadManager;
private DownloadTracker downloadTracker; private DownloadTracker downloadTracker;
private DownloadNotificationHelper downloadNotificationHelper;
@Override @Override
public void onCreate() { public void onCreate() {
@ -93,6 +97,14 @@ public class DemoApplication extends Application {
.setExtensionRendererMode(extensionRendererMode); .setExtensionRendererMode(extensionRendererMode);
} }
public DownloadNotificationHelper getDownloadNotificationHelper() {
if (downloadNotificationHelper == null) {
downloadNotificationHelper =
new DownloadNotificationHelper(this, DOWNLOAD_NOTIFICATION_CHANNEL_ID);
}
return downloadNotificationHelper;
}
public DownloadManager getDownloadManager() { public DownloadManager getDownloadManager() {
initDownloadManager(); initDownloadManager();
return downloadManager; return downloadManager;

View File

@ -15,6 +15,8 @@
*/ */
package com.google.android.exoplayer2.demo; package com.google.android.exoplayer2.demo;
import static com.google.android.exoplayer2.demo.DemoApplication.DOWNLOAD_NOTIFICATION_CHANNEL_ID;
import android.app.Notification; import android.app.Notification;
import android.content.Context; import android.content.Context;
import com.google.android.exoplayer2.offline.Download; import com.google.android.exoplayer2.offline.Download;
@ -29,35 +31,29 @@ import java.util.List;
/** A service for downloading media. */ /** A service for downloading media. */
public class DemoDownloadService extends DownloadService { public class DemoDownloadService extends DownloadService {
private static final String CHANNEL_ID = "download_channel";
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 DownloadNotificationHelper notificationHelper;
public DemoDownloadService() { public DemoDownloadService() {
super( super(
FOREGROUND_NOTIFICATION_ID, FOREGROUND_NOTIFICATION_ID,
DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL, DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL,
CHANNEL_ID, DOWNLOAD_NOTIFICATION_CHANNEL_ID,
R.string.exo_download_notification_channel_name, R.string.exo_download_notification_channel_name,
/* channelDescriptionResourceId= */ 0); /* channelDescriptionResourceId= */ 0);
} }
@Override
public void onCreate() {
super.onCreate();
notificationHelper = new DownloadNotificationHelper(this, CHANNEL_ID);
}
@Override @Override
protected DownloadManager getDownloadManager() { protected DownloadManager getDownloadManager() {
DownloadManager downloadManager = ((DemoApplication) getApplication()).getDownloadManager();
// This will only happen once, because getDownloadManager is guaranteed to be called only once // This will only happen once, because getDownloadManager is guaranteed to be called only once
// in the life cycle of the process. // in the life cycle of the process.
DemoApplication application = (DemoApplication) getApplication();
DownloadManager downloadManager = application.getDownloadManager();
DownloadNotificationHelper downloadNotificationHelper =
application.getDownloadNotificationHelper();
downloadManager.addListener( downloadManager.addListener(
new TerminalStateNotificationHelper( new TerminalStateNotificationHelper(
this, notificationHelper, FOREGROUND_NOTIFICATION_ID + 1)); this, downloadNotificationHelper, FOREGROUND_NOTIFICATION_ID + 1));
return downloadManager; return downloadManager;
} }
@ -68,7 +64,9 @@ public class DemoDownloadService extends DownloadService {
@Override @Override
protected Notification getForegroundNotification(List<Download> downloads) { protected Notification getForegroundNotification(List<Download> downloads) {
return notificationHelper.buildProgressNotification( return ((DemoApplication) getApplication())
.getDownloadNotificationHelper()
.buildProgressNotification(
R.drawable.ic_download, /* contentIntent= */ null, /* message= */ null, downloads); R.drawable.ic_download, /* contentIntent= */ null, /* message= */ null, downloads);
} }