use of androidx.media3.exoplayer.offline.DownloadManager in project media by androidx.
the class DemoDownloadService method getDownloadManager.
@Override
protected DownloadManager getDownloadManager() {
// This will only happen once, because getDownloadManager is guaranteed to be called only once
// in the life cycle of the process.
DownloadManager downloadManager = DemoUtil.getDownloadManager(/* context= */
this);
DownloadNotificationHelper downloadNotificationHelper = DemoUtil.getDownloadNotificationHelper(/* context= */
this);
downloadManager.addListener(new TerminalStateNotificationHelper(this, downloadNotificationHelper, FOREGROUND_NOTIFICATION_ID + 1));
return downloadManager;
}
use of androidx.media3.exoplayer.offline.DownloadManager in project media by androidx.
the class DemoUtil method ensureDownloadManagerInitialized.
private static synchronized void ensureDownloadManagerInitialized(Context context) {
if (downloadManager == null) {
downloadManager = new DownloadManager(context, getDatabaseProvider(context), getDownloadCache(context), getHttpDataSourceFactory(context), Executors.newFixedThreadPool(/* nThreads= */
6));
downloadTracker = new DownloadTracker(context, getHttpDataSourceFactory(context), downloadManager);
}
}
use of androidx.media3.exoplayer.offline.DownloadManager in project media by androidx.
the class DownloadService method onStartCommand.
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
lastStartId = startId;
taskRemoved = false;
@Nullable String intentAction = null;
@Nullable String contentId = null;
if (intent != null) {
intentAction = intent.getAction();
contentId = intent.getStringExtra(KEY_CONTENT_ID);
startedInForeground |= intent.getBooleanExtra(KEY_FOREGROUND, false) || ACTION_RESTART.equals(intentAction);
}
// intentAction is null if the service is restarted or no action is specified.
if (intentAction == null) {
intentAction = ACTION_INIT;
}
DownloadManager downloadManager = Assertions.checkNotNull(downloadManagerHelper).downloadManager;
switch(intentAction) {
case ACTION_INIT:
case ACTION_RESTART:
// Do nothing.
break;
case ACTION_ADD_DOWNLOAD:
@Nullable DownloadRequest downloadRequest = Assertions.checkNotNull(intent).getParcelableExtra(KEY_DOWNLOAD_REQUEST);
if (downloadRequest == null) {
Log.e(TAG, "Ignored ADD_DOWNLOAD: Missing " + KEY_DOWNLOAD_REQUEST + " extra");
} else {
int stopReason = intent.getIntExtra(KEY_STOP_REASON, Download.STOP_REASON_NONE);
downloadManager.addDownload(downloadRequest, stopReason);
}
break;
case ACTION_REMOVE_DOWNLOAD:
if (contentId == null) {
Log.e(TAG, "Ignored REMOVE_DOWNLOAD: Missing " + KEY_CONTENT_ID + " extra");
} else {
downloadManager.removeDownload(contentId);
}
break;
case ACTION_REMOVE_ALL_DOWNLOADS:
downloadManager.removeAllDownloads();
break;
case ACTION_RESUME_DOWNLOADS:
downloadManager.resumeDownloads();
break;
case ACTION_PAUSE_DOWNLOADS:
downloadManager.pauseDownloads();
break;
case ACTION_SET_STOP_REASON:
if (!Assertions.checkNotNull(intent).hasExtra(KEY_STOP_REASON)) {
Log.e(TAG, "Ignored SET_STOP_REASON: Missing " + KEY_STOP_REASON + " extra");
} else {
int stopReason = intent.getIntExtra(KEY_STOP_REASON, /* defaultValue= */
0);
downloadManager.setStopReason(contentId, stopReason);
}
break;
case ACTION_SET_REQUIREMENTS:
@Nullable Requirements requirements = Assertions.checkNotNull(intent).getParcelableExtra(KEY_REQUIREMENTS);
if (requirements == null) {
Log.e(TAG, "Ignored SET_REQUIREMENTS: Missing " + KEY_REQUIREMENTS + " extra");
} else {
downloadManager.setRequirements(requirements);
}
break;
default:
Log.e(TAG, "Ignored unrecognized action: " + intentAction);
break;
}
if (Util.SDK_INT >= 26 && startedInForeground && foregroundNotificationUpdater != null) {
// From API level 26, services started in the foreground are required to show a notification.
foregroundNotificationUpdater.showNotificationIfNotAlready();
}
isStopped = false;
if (downloadManager.isIdle()) {
onIdle();
}
return START_STICKY;
}
use of androidx.media3.exoplayer.offline.DownloadManager in project media by androidx.
the class DownloadManagerTest method setupDownloadManager.
private void setupDownloadManager(int maxParallelDownloads) throws Exception {
if (downloadManager != null) {
releaseDownloadManager();
}
try {
runOnMainThread(() -> {
downloadManager = new DownloadManager(ApplicationProvider.getApplicationContext(), new DefaultDownloadIndex(TestUtil.getInMemoryDatabaseProvider()), new FakeDownloaderFactory());
downloadManager.setMaxParallelDownloads(maxParallelDownloads);
downloadManager.setMinRetryCount(MIN_RETRY_COUNT);
downloadManager.setRequirements(new Requirements(0));
downloadManager.resumeDownloads();
downloadManagerListener = new TestDownloadManagerListener(downloadManager);
});
downloadManagerListener.blockUntilInitialized();
} catch (Throwable throwable) {
throw new Exception(throwable);
}
}
use of androidx.media3.exoplayer.offline.DownloadManager in project media by androidx.
the class DownloadService method onCreate.
@Override
public void onCreate() {
if (channelId != null) {
NotificationUtil.createNotificationChannel(this, channelId, channelNameResourceId, channelDescriptionResourceId, NotificationUtil.IMPORTANCE_LOW);
}
Class<? extends DownloadService> clazz = getClass();
@Nullable DownloadManagerHelper downloadManagerHelper = downloadManagerHelpers.get(clazz);
if (downloadManagerHelper == null) {
boolean foregroundAllowed = foregroundNotificationUpdater != null;
// See https://developer.android.com/about/versions/12/foreground-services.
boolean canStartForegroundServiceFromBackground = Util.SDK_INT < 31;
@Nullable Scheduler scheduler = foregroundAllowed && canStartForegroundServiceFromBackground ? getScheduler() : null;
DownloadManager downloadManager = getDownloadManager();
downloadManager.resumeDownloads();
downloadManagerHelper = new DownloadManagerHelper(getApplicationContext(), downloadManager, foregroundAllowed, scheduler, clazz);
downloadManagerHelpers.put(clazz, downloadManagerHelper);
}
this.downloadManagerHelper = downloadManagerHelper;
downloadManagerHelper.attachService(this);
}
Aggregations