Search in sources :

Example 1 with Requirements

use of androidx.media3.exoplayer.scheduler.Requirements in project media by androidx.

the class DownloadManager method setRequirements.

/**
 * Sets the requirements that need to be met for downloads to progress.
 *
 * @param requirements A {@link Requirements}.
 */
public void setRequirements(Requirements requirements) {
    if (requirements.equals(requirementsWatcher.getRequirements())) {
        return;
    }
    requirementsWatcher.stop();
    requirementsWatcher = new RequirementsWatcher(context, requirementsListener, requirements);
    int notMetRequirements = requirementsWatcher.start();
    onRequirementsStateChanged(requirementsWatcher, notMetRequirements);
}
Also used : RequirementsWatcher(androidx.media3.exoplayer.scheduler.RequirementsWatcher)

Example 2 with Requirements

use of androidx.media3.exoplayer.scheduler.Requirements 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;
}
Also used : Nullable(androidx.annotation.Nullable) Requirements(androidx.media3.exoplayer.scheduler.Requirements)

Example 3 with Requirements

use of androidx.media3.exoplayer.scheduler.Requirements in project media by androidx.

the class WorkManagerScheduler method buildConstraints.

private static Constraints buildConstraints(Requirements requirements) {
    Requirements filteredRequirements = requirements.filterRequirements(SUPPORTED_REQUIREMENTS);
    if (!filteredRequirements.equals(requirements)) {
        Log.w(TAG, "Ignoring unsupported requirements: " + (filteredRequirements.getRequirements() ^ requirements.getRequirements()));
    }
    Constraints.Builder builder = new Constraints.Builder();
    if (requirements.isUnmeteredNetworkRequired()) {
        builder.setRequiredNetworkType(NetworkType.UNMETERED);
    } else if (requirements.isNetworkRequired()) {
        builder.setRequiredNetworkType(NetworkType.CONNECTED);
    } else {
        builder.setRequiredNetworkType(NetworkType.NOT_REQUIRED);
    }
    if (Util.SDK_INT >= 23 && requirements.isIdleRequired()) {
        setRequiresDeviceIdle(builder);
    }
    if (requirements.isChargingRequired()) {
        builder.setRequiresCharging(true);
    }
    if (requirements.isStorageNotLowRequired()) {
        builder.setRequiresStorageNotLow(true);
    }
    return builder.build();
}
Also used : Constraints(androidx.work.Constraints) Requirements(androidx.media3.exoplayer.scheduler.Requirements)

Example 4 with Requirements

use of androidx.media3.exoplayer.scheduler.Requirements 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);
    }
}
Also used : Requirements(androidx.media3.exoplayer.scheduler.Requirements) IOException(java.io.IOException) TestDownloadManagerListener(androidx.media3.test.utils.robolectric.TestDownloadManagerListener)

Example 5 with Requirements

use of androidx.media3.exoplayer.scheduler.Requirements in project media by androidx.

the class DownloadManager method onRequirementsStateChanged.

private void onRequirementsStateChanged(RequirementsWatcher requirementsWatcher, @Requirements.RequirementFlags int notMetRequirements) {
    Requirements requirements = requirementsWatcher.getRequirements();
    if (this.notMetRequirements != notMetRequirements) {
        this.notMetRequirements = notMetRequirements;
        pendingMessages++;
        internalHandler.obtainMessage(MSG_SET_NOT_MET_REQUIREMENTS, notMetRequirements, /* unused */
        0).sendToTarget();
    }
    boolean waitingForRequirementsChanged = updateWaitingForRequirements();
    for (Listener listener : listeners) {
        listener.onRequirementsStateChanged(this, requirements, notMetRequirements);
    }
    if (waitingForRequirementsChanged) {
        notifyWaitingForRequirementsChanged();
    }
}
Also used : Requirements(androidx.media3.exoplayer.scheduler.Requirements)

Aggregations

Requirements (androidx.media3.exoplayer.scheduler.Requirements)5 TestDownloadManagerListener (androidx.media3.test.utils.robolectric.TestDownloadManagerListener)2 Nullable (androidx.annotation.Nullable)1 Factory (androidx.media3.datasource.DataSource.Factory)1 CacheDataSource (androidx.media3.datasource.cache.CacheDataSource)1 DefaultDownloaderFactory (androidx.media3.exoplayer.offline.DefaultDownloaderFactory)1 DownloadManager (androidx.media3.exoplayer.offline.DownloadManager)1 RequirementsWatcher (androidx.media3.exoplayer.scheduler.RequirementsWatcher)1 TestRunnable (androidx.media3.test.utils.DummyMainThread.TestRunnable)1 FakeDataSource (androidx.media3.test.utils.FakeDataSource)1 Constraints (androidx.work.Constraints)1 IOException (java.io.IOException)1