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 5d4532765d..fbcdd9e34f 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 @@ -28,8 +28,8 @@ 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.NotificationUtil; import com.google.android.exoplayer2.util.ErrorMessageProvider; +import com.google.android.exoplayer2.util.NotificationUtil; /** A service for downloading media. */ public class DemoDownloadService extends DownloadService { @@ -41,17 +41,11 @@ public class DemoDownloadService extends DownloadService { private static DownloadManager downloadManager; public DemoDownloadService() { - super(FOREGROUND_NOTIFICATION_ID); - } - - @Override - public void onCreate() { - NotificationUtil.createNotificationChannel( - this, + super( + FOREGROUND_NOTIFICATION_ID, + DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL, CHANNEL_ID, - R.string.exo_download_notification_channel_name, - NotificationUtil.IMPORTANCE_LOW); - super.onCreate(); + R.string.exo_download_notification_channel_name); } @Override diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java index fe4c031d63..0dbd5551e3 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java @@ -107,7 +107,7 @@ public final class DownloadManager { public DownloadManager( Cache cache, DataSource.Factory upstreamDataSourceFactory, - String actionSaveFile, + File actionSaveFile, Deserializer... deserializers) { this( new DownloaderConstructorHelper(cache, upstreamDataSourceFactory), diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java index 84e712c269..e3db5e3bc6 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadService.java @@ -23,11 +23,13 @@ import android.os.Handler; import android.os.IBinder; import android.os.Looper; import android.support.annotation.Nullable; +import android.support.annotation.StringRes; import android.util.Log; import com.google.android.exoplayer2.offline.DownloadManager.DownloadState; import com.google.android.exoplayer2.scheduler.Requirements; import com.google.android.exoplayer2.scheduler.RequirementsWatcher; import com.google.android.exoplayer2.scheduler.Scheduler; +import com.google.android.exoplayer2.util.NotificationUtil; import com.google.android.exoplayer2.util.Util; import java.io.IOException; @@ -69,6 +71,8 @@ public abstract class DownloadService extends Service { private static Scheduler scheduler; private final ForegroundNotificationUpdater foregroundNotificationUpdater; + private final @Nullable String channelId; + private final @StringRes int channelName; private DownloadManager downloadManager; private DownloadListener downloadListener; @@ -94,9 +98,37 @@ public abstract class DownloadService extends Service { */ protected DownloadService( int foregroundNotificationId, long foregroundNotificationUpdateInterval) { + this( + foregroundNotificationId, + foregroundNotificationUpdateInterval, + /* channelId= */ null, + /* channelName= */ 0); + } + + /** + * Creates a DownloadService. + * + * @param foregroundNotificationId The notification id for the foreground notification, must not + * be 0. + * @param foregroundNotificationUpdateInterval The maximum interval to update foreground + * notification, in milliseconds. + * @param channelId An id for a low priority notification channel to create, or {@code null} if + * the app will take care of creating a notification channel if needed. If specified, must be + * unique per package and the value may be truncated if it is too long. + * @param channelName A string resource identifier for the user visible name of the channel, if + * {@code channelId} is specified. The recommended maximum length is 40 characters; the value + * may be truncated if it is too long. + */ + protected DownloadService( + int foregroundNotificationId, + long foregroundNotificationUpdateInterval, + @Nullable String channelId, + @StringRes int channelName) { foregroundNotificationUpdater = new ForegroundNotificationUpdater( foregroundNotificationId, foregroundNotificationUpdateInterval); + this.channelId = channelId; + this.channelName = channelName; } /** @@ -132,10 +164,13 @@ public abstract class DownloadService extends Service { @Override public void onCreate() { logd("onCreate"); + if (channelId != null) { + NotificationUtil.createNotificationChannel( + this, channelId, channelName, NotificationUtil.IMPORTANCE_LOW); + } downloadManager = getDownloadManager(); downloadListener = new DownloadListener(); downloadManager.addListener(downloadListener); - if (requirementsWatcher == null) { Requirements requirements = getRequirements(); if (requirements != null) { diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/NotificationUtil.java b/library/core/src/main/java/com/google/android/exoplayer2/util/NotificationUtil.java similarity index 98% rename from library/ui/src/main/java/com/google/android/exoplayer2/ui/NotificationUtil.java rename to library/core/src/main/java/com/google/android/exoplayer2/util/NotificationUtil.java index 94700b1662..c93d7cd72e 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/NotificationUtil.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/util/NotificationUtil.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.google.android.exoplayer2.ui; +package com.google.android.exoplayer2.util; import android.annotation.SuppressLint; import android.app.Notification; @@ -24,7 +24,6 @@ import android.content.Intent; import android.support.annotation.IntDef; import android.support.annotation.Nullable; import android.support.annotation.StringRes; -import com.google.android.exoplayer2.util.Util; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java index 2953fb0fa7..2eafa388e3 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java @@ -42,6 +42,7 @@ import com.google.android.exoplayer2.PlaybackParameters; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.util.Assertions; +import com.google.android.exoplayer2.util.NotificationUtil; import com.google.android.exoplayer2.util.Util; import java.lang.annotation.Retention; import java.util.ArrayList;