Search in sources :

Example 1 with Requirements

use of com.google.android.exoplayer2.scheduler.Requirements in project ExoPlayer by google.

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(com.google.android.exoplayer2.scheduler.Requirements)

Example 2 with Requirements

use of com.google.android.exoplayer2.scheduler.Requirements in project ExoPlayer by google.

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(com.google.android.exoplayer2.scheduler.Requirements) IOException(java.io.IOException) TestDownloadManagerListener(com.google.android.exoplayer2.robolectric.TestDownloadManagerListener)

Example 3 with Requirements

use of com.google.android.exoplayer2.scheduler.Requirements in project ExoPlayer by google.

the class DownloadNotificationHelper method buildProgressNotification.

/**
 * Returns a progress notification for the given downloads.
 *
 * @param context A context.
 * @param smallIcon A small icon for the notification.
 * @param contentIntent An optional content intent to send when the notification is clicked.
 * @param message An optional message to display on the notification.
 * @param downloads The downloads.
 * @param notMetRequirements Any requirements for downloads that are not currently met.
 * @return The notification.
 */
public Notification buildProgressNotification(Context context, @DrawableRes int smallIcon, @Nullable PendingIntent contentIntent, @Nullable String message, List<Download> downloads, @Requirements.RequirementFlags int notMetRequirements) {
    float totalPercentage = 0;
    int downloadTaskCount = 0;
    boolean allDownloadPercentagesUnknown = true;
    boolean haveDownloadedBytes = false;
    boolean haveDownloadingTasks = false;
    boolean haveQueuedTasks = false;
    boolean haveRemovingTasks = false;
    for (int i = 0; i < downloads.size(); i++) {
        Download download = downloads.get(i);
        switch(download.state) {
            case Download.STATE_REMOVING:
                haveRemovingTasks = true;
                break;
            case Download.STATE_QUEUED:
                haveQueuedTasks = true;
                break;
            case Download.STATE_RESTARTING:
            case Download.STATE_DOWNLOADING:
                haveDownloadingTasks = true;
                float downloadPercentage = download.getPercentDownloaded();
                if (downloadPercentage != C.PERCENTAGE_UNSET) {
                    allDownloadPercentagesUnknown = false;
                    totalPercentage += downloadPercentage;
                }
                haveDownloadedBytes |= download.getBytesDownloaded() > 0;
                downloadTaskCount++;
                break;
            // Terminal states aren't expected, but if we encounter them we do nothing.
            case Download.STATE_STOPPED:
            case Download.STATE_COMPLETED:
            case Download.STATE_FAILED:
            default:
                break;
        }
    }
    int titleStringId;
    boolean showProgress = true;
    if (haveDownloadingTasks) {
        titleStringId = R.string.exo_download_downloading;
    } else if (haveQueuedTasks && notMetRequirements != 0) {
        showProgress = false;
        if ((notMetRequirements & Requirements.NETWORK_UNMETERED) != 0) {
            // Note: This assumes that "unmetered" == "WiFi", since it provides a clearer message that's
            // correct in the majority of cases.
            titleStringId = R.string.exo_download_paused_for_wifi;
        } else if ((notMetRequirements & Requirements.NETWORK) != 0) {
            titleStringId = R.string.exo_download_paused_for_network;
        } else {
            titleStringId = R.string.exo_download_paused;
        }
    } else if (haveRemovingTasks) {
        titleStringId = R.string.exo_download_removing;
    } else {
        // There are either no downloads, or all downloads are in terminal states.
        titleStringId = NULL_STRING_ID;
    }
    int maxProgress = 0;
    int currentProgress = 0;
    boolean indeterminateProgress = false;
    if (showProgress) {
        maxProgress = 100;
        if (haveDownloadingTasks) {
            currentProgress = (int) (totalPercentage / downloadTaskCount);
            indeterminateProgress = allDownloadPercentagesUnknown && haveDownloadedBytes;
        } else {
            indeterminateProgress = true;
        }
    }
    return buildNotification(context, smallIcon, contentIntent, message, titleStringId, maxProgress, currentProgress, indeterminateProgress, /* ongoing= */
    true, /* showWhen= */
    false);
}
Also used : Download(com.google.android.exoplayer2.offline.Download)

Example 4 with Requirements

use of com.google.android.exoplayer2.scheduler.Requirements in project ExoPlayer by google.

the class DownloadManagerDashTest method createDownloadManager.

private void createDownloadManager() {
    runOnMainThread(() -> {
        Factory fakeDataSourceFactory = new FakeDataSource.Factory().setFakeDataSet(fakeDataSet);
        DefaultDownloaderFactory downloaderFactory = new DefaultDownloaderFactory(new CacheDataSource.Factory().setCache(cache).setUpstreamDataSourceFactory(fakeDataSourceFactory), /* executor= */
        Runnable::run);
        downloadManager = new DownloadManager(ApplicationProvider.getApplicationContext(), downloadIndex, downloaderFactory);
        downloadManager.setRequirements(new Requirements(0));
        downloadManagerListener = new TestDownloadManagerListener(downloadManager);
        downloadManager.resumeDownloads();
    });
}
Also used : FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) CacheDataSource(com.google.android.exoplayer2.upstream.cache.CacheDataSource) DefaultDownloaderFactory(com.google.android.exoplayer2.offline.DefaultDownloaderFactory) TestRunnable(com.google.android.exoplayer2.testutil.DummyMainThread.TestRunnable) DefaultDownloaderFactory(com.google.android.exoplayer2.offline.DefaultDownloaderFactory) Factory(com.google.android.exoplayer2.upstream.DataSource.Factory) DownloadManager(com.google.android.exoplayer2.offline.DownloadManager) Requirements(com.google.android.exoplayer2.scheduler.Requirements) TestDownloadManagerListener(com.google.android.exoplayer2.robolectric.TestDownloadManagerListener)

Example 5 with Requirements

use of com.google.android.exoplayer2.scheduler.Requirements in project ExoPlayer by google.

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(com.google.android.exoplayer2.scheduler.Requirements)

Aggregations

Requirements (com.google.android.exoplayer2.scheduler.Requirements)5 TestDownloadManagerListener (com.google.android.exoplayer2.robolectric.TestDownloadManagerListener)2 Nullable (androidx.annotation.Nullable)1 Constraints (androidx.work.Constraints)1 DefaultDownloaderFactory (com.google.android.exoplayer2.offline.DefaultDownloaderFactory)1 Download (com.google.android.exoplayer2.offline.Download)1 DownloadManager (com.google.android.exoplayer2.offline.DownloadManager)1 RequirementsWatcher (com.google.android.exoplayer2.scheduler.RequirementsWatcher)1 TestRunnable (com.google.android.exoplayer2.testutil.DummyMainThread.TestRunnable)1 FakeDataSource (com.google.android.exoplayer2.testutil.FakeDataSource)1 Factory (com.google.android.exoplayer2.upstream.DataSource.Factory)1 CacheDataSource (com.google.android.exoplayer2.upstream.cache.CacheDataSource)1 IOException (java.io.IOException)1