Search in sources :

Example 1 with DownloadRequest

use of com.google.android.exoplayer2.offline.DownloadRequest 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 DownloadRequest

use of com.google.android.exoplayer2.offline.DownloadRequest in project ExoPlayer by google.

the class PlayerActivity method createMediaItems.

private static List<MediaItem> createMediaItems(Intent intent, DownloadTracker downloadTracker) {
    List<MediaItem> mediaItems = new ArrayList<>();
    for (MediaItem item : IntentUtil.createMediaItemsFromIntent(intent)) {
        @Nullable DownloadRequest downloadRequest = downloadTracker.getDownloadRequest(item.localConfiguration.uri);
        if (downloadRequest != null) {
            MediaItem.Builder builder = item.buildUpon();
            builder.setMediaId(downloadRequest.id).setUri(downloadRequest.uri).setCustomCacheKey(downloadRequest.customCacheKey).setMimeType(downloadRequest.mimeType).setStreamKeys(downloadRequest.streamKeys);
            @Nullable MediaItem.DrmConfiguration drmConfiguration = item.localConfiguration.drmConfiguration;
            if (drmConfiguration != null) {
                builder.setDrmConfiguration(drmConfiguration.buildUpon().setKeySetId(downloadRequest.keySetId).build());
            }
            mediaItems.add(builder.build());
        } else {
            mediaItems.add(item);
        }
    }
    return mediaItems;
}
Also used : MediaItem(com.google.android.exoplayer2.MediaItem) ArrayList(java.util.ArrayList) DownloadRequest(com.google.android.exoplayer2.offline.DownloadRequest) Nullable(androidx.annotation.Nullable)

Example 3 with DownloadRequest

use of com.google.android.exoplayer2.offline.DownloadRequest in project ExoPlayer by google.

the class DownloadManagerTest method mergeRequest_completedWithStopReason_becomesStopped.

@Test
public void mergeRequest_completedWithStopReason_becomesStopped() {
    DownloadRequest downloadRequest = createDownloadRequest(ID1);
    DownloadBuilder downloadBuilder = new DownloadBuilder(downloadRequest).setState(Download.STATE_COMPLETED).setStopReason(/* stopReason= */
    1);
    Download download = downloadBuilder.build();
    Download mergedDownload = DownloadManager.mergeRequest(download, downloadRequest, download.stopReason, NOW_MS);
    Download expectedDownload = downloadBuilder.setStartTimeMs(NOW_MS).setState(Download.STATE_STOPPED).build();
    assertEqualIgnoringUpdateTime(mergedDownload, expectedDownload);
}
Also used : DownloadBuilder(com.google.android.exoplayer2.testutil.DownloadBuilder) Test(org.junit.Test)

Example 4 with DownloadRequest

use of com.google.android.exoplayer2.offline.DownloadRequest in project ExoPlayer by google.

the class DownloadManagerTest method mergeRequest_failed_becomesQueued.

@Test
public void mergeRequest_failed_becomesQueued() {
    DownloadRequest downloadRequest = createDownloadRequest(ID1);
    DownloadBuilder downloadBuilder = new DownloadBuilder(downloadRequest).setState(Download.STATE_FAILED).setFailureReason(Download.FAILURE_REASON_UNKNOWN);
    Download download = downloadBuilder.build();
    Download mergedDownload = DownloadManager.mergeRequest(download, downloadRequest, download.stopReason, NOW_MS);
    Download expectedDownload = downloadBuilder.setStartTimeMs(NOW_MS).setState(Download.STATE_QUEUED).setFailureReason(Download.FAILURE_REASON_NONE).build();
    assertEqualIgnoringUpdateTime(mergedDownload, expectedDownload);
}
Also used : DownloadBuilder(com.google.android.exoplayer2.testutil.DownloadBuilder) Test(org.junit.Test)

Example 5 with DownloadRequest

use of com.google.android.exoplayer2.offline.DownloadRequest in project ExoPlayer by google.

the class DownloadServiceDashTest method downloadKeys.

private void downloadKeys(StreamKey... keys) {
    ArrayList<StreamKey> keysList = new ArrayList<>();
    Collections.addAll(keysList, keys);
    DownloadRequest action = new DownloadRequest.Builder(TEST_ID, TEST_MPD_URI).setMimeType(MimeTypes.APPLICATION_MPD).setStreamKeys(keysList).build();
    testThread.runOnMainThread(() -> {
        Intent startIntent = DownloadService.buildAddDownloadIntent(context, DownloadService.class, action, /* foreground= */
        false);
        dashDownloadService.onStartCommand(startIntent, 0, 0);
    });
}
Also used : ArrayList(java.util.ArrayList) DownloadRequest(com.google.android.exoplayer2.offline.DownloadRequest) Intent(android.content.Intent) StreamKey(com.google.android.exoplayer2.offline.StreamKey)

Aggregations

Test (org.junit.Test)5 DownloadRequest (com.google.android.exoplayer2.offline.DownloadRequest)4 DownloadBuilder (com.google.android.exoplayer2.testutil.DownloadBuilder)4 Nullable (androidx.annotation.Nullable)3 MediaItem (com.google.android.exoplayer2.MediaItem)2 ArrayList (java.util.ArrayList)2 Intent (android.content.Intent)1 StreamKey (com.google.android.exoplayer2.offline.StreamKey)1 Requirements (com.google.android.exoplayer2.scheduler.Requirements)1 Ignore (org.junit.Ignore)1