Add DownloadService constructor for notification channel

Also move NotificationUtil from the UI module to the core module.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=193941401
This commit is contained in:
andrewlewis 2018-04-23 10:06:07 -07:00 committed by Oliver Woodman
parent 79c105be59
commit ddd603a1ee
5 changed files with 44 additions and 15 deletions

View File

@ -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.hls.offline.HlsDownloadAction;
import com.google.android.exoplayer2.source.smoothstreaming.offline.SsDownloadAction; 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.ui.NotificationUtil;
import com.google.android.exoplayer2.util.ErrorMessageProvider; import com.google.android.exoplayer2.util.ErrorMessageProvider;
import com.google.android.exoplayer2.util.NotificationUtil;
/** A service for downloading media. */ /** A service for downloading media. */
public class DemoDownloadService extends DownloadService { public class DemoDownloadService extends DownloadService {
@ -41,17 +41,11 @@ public class DemoDownloadService extends DownloadService {
private static DownloadManager downloadManager; private static DownloadManager downloadManager;
public DemoDownloadService() { public DemoDownloadService() {
super(FOREGROUND_NOTIFICATION_ID); super(
} FOREGROUND_NOTIFICATION_ID,
DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL,
@Override
public void onCreate() {
NotificationUtil.createNotificationChannel(
this,
CHANNEL_ID, CHANNEL_ID,
R.string.exo_download_notification_channel_name, R.string.exo_download_notification_channel_name);
NotificationUtil.IMPORTANCE_LOW);
super.onCreate();
} }
@Override @Override

View File

@ -107,7 +107,7 @@ public final class DownloadManager {
public DownloadManager( public DownloadManager(
Cache cache, Cache cache,
DataSource.Factory upstreamDataSourceFactory, DataSource.Factory upstreamDataSourceFactory,
String actionSaveFile, File actionSaveFile,
Deserializer... deserializers) { Deserializer... deserializers) {
this( this(
new DownloaderConstructorHelper(cache, upstreamDataSourceFactory), new DownloaderConstructorHelper(cache, upstreamDataSourceFactory),

View File

@ -23,11 +23,13 @@ import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.Looper; import android.os.Looper;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.util.Log; import android.util.Log;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState; import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
import com.google.android.exoplayer2.scheduler.Requirements; import com.google.android.exoplayer2.scheduler.Requirements;
import com.google.android.exoplayer2.scheduler.RequirementsWatcher; import com.google.android.exoplayer2.scheduler.RequirementsWatcher;
import com.google.android.exoplayer2.scheduler.Scheduler; import com.google.android.exoplayer2.scheduler.Scheduler;
import com.google.android.exoplayer2.util.NotificationUtil;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.io.IOException; import java.io.IOException;
@ -69,6 +71,8 @@ public abstract class DownloadService extends Service {
private static Scheduler scheduler; private static Scheduler scheduler;
private final ForegroundNotificationUpdater foregroundNotificationUpdater; private final ForegroundNotificationUpdater foregroundNotificationUpdater;
private final @Nullable String channelId;
private final @StringRes int channelName;
private DownloadManager downloadManager; private DownloadManager downloadManager;
private DownloadListener downloadListener; private DownloadListener downloadListener;
@ -94,9 +98,37 @@ public abstract class DownloadService extends Service {
*/ */
protected DownloadService( protected DownloadService(
int foregroundNotificationId, long foregroundNotificationUpdateInterval) { 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 = foregroundNotificationUpdater =
new ForegroundNotificationUpdater( new ForegroundNotificationUpdater(
foregroundNotificationId, foregroundNotificationUpdateInterval); foregroundNotificationId, foregroundNotificationUpdateInterval);
this.channelId = channelId;
this.channelName = channelName;
} }
/** /**
@ -132,10 +164,13 @@ public abstract class DownloadService extends Service {
@Override @Override
public void onCreate() { public void onCreate() {
logd("onCreate"); logd("onCreate");
if (channelId != null) {
NotificationUtil.createNotificationChannel(
this, channelId, channelName, NotificationUtil.IMPORTANCE_LOW);
}
downloadManager = getDownloadManager(); downloadManager = getDownloadManager();
downloadListener = new DownloadListener(); downloadListener = new DownloadListener();
downloadManager.addListener(downloadListener); downloadManager.addListener(downloadListener);
if (requirementsWatcher == null) { if (requirementsWatcher == null) {
Requirements requirements = getRequirements(); Requirements requirements = getRequirements();
if (requirements != null) { if (requirements != null) {

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.google.android.exoplayer2.ui; package com.google.android.exoplayer2.util;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Notification; import android.app.Notification;
@ -24,7 +24,6 @@ import android.content.Intent;
import android.support.annotation.IntDef; import android.support.annotation.IntDef;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import com.google.android.exoplayer2.util.Util;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;

View File

@ -42,6 +42,7 @@ import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.NotificationUtil;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.util.ArrayList; import java.util.ArrayList;