Search in sources :

Example 1 with DownloadManager

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

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

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);
    }
}
Also used : DownloadManager(com.google.android.exoplayer2.offline.DownloadManager)

Example 3 with DownloadManager

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

the class DownloadServiceDashTest method setUp.

@Before
public void setUp() throws IOException {
    testThread = new DummyMainThread();
    context = ApplicationProvider.getApplicationContext();
    tempFolder = Util.createTempDirectory(context, "ExoPlayerTest");
    cache = new SimpleCache(tempFolder, new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider());
    Runnable pauseAction = () -> {
        if (pauseDownloadCondition != null) {
            try {
                pauseDownloadCondition.block();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    };
    fakeDataSet = new FakeDataSet().setData(TEST_MPD_URI, TEST_MPD).newData("audio_init_data").appendReadAction(pauseAction).appendReadData(TestUtil.buildTestData(10)).endData().setRandomData("audio_segment_1", 4).setRandomData("audio_segment_2", 5).setRandomData("audio_segment_3", 6).setRandomData("text_segment_1", 1).setRandomData("text_segment_2", 2).setRandomData("text_segment_3", 3);
    final DataSource.Factory fakeDataSourceFactory = new FakeDataSource.Factory().setFakeDataSet(fakeDataSet);
    fakeStreamKey1 = new StreamKey(0, 0, 0);
    fakeStreamKey2 = new StreamKey(0, 1, 0);
    testThread.runTestOnMainThread(() -> {
        DefaultDownloadIndex downloadIndex = new DefaultDownloadIndex(TestUtil.getInMemoryDatabaseProvider());
        DefaultDownloaderFactory downloaderFactory = new DefaultDownloaderFactory(new CacheDataSource.Factory().setCache(cache).setUpstreamDataSourceFactory(fakeDataSourceFactory), /* executor= */
        Runnable::run);
        final DownloadManager dashDownloadManager = new DownloadManager(ApplicationProvider.getApplicationContext(), downloadIndex, downloaderFactory);
        downloadManagerListener = new TestDownloadManagerListener(dashDownloadManager);
        dashDownloadManager.resumeDownloads();
        dashDownloadService = new DownloadService(DownloadService.FOREGROUND_NOTIFICATION_ID_NONE) {

            @Override
            protected DownloadManager getDownloadManager() {
                return dashDownloadManager;
            }

            @Override
            @Nullable
            protected Scheduler getScheduler() {
                return null;
            }

            @Override
            protected Notification getForegroundNotification(List<Download> downloads, @Requirements.RequirementFlags int notMetRequirements) {
                throw new UnsupportedOperationException();
            }
        };
        dashDownloadService.onCreate();
    });
}
Also used : Scheduler(com.google.android.exoplayer2.scheduler.Scheduler) DownloadManager(com.google.android.exoplayer2.offline.DownloadManager) DownloadService(com.google.android.exoplayer2.offline.DownloadService) Notification(android.app.Notification) SimpleCache(com.google.android.exoplayer2.upstream.cache.SimpleCache) FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) CacheDataSource(com.google.android.exoplayer2.upstream.cache.CacheDataSource) DefaultDownloaderFactory(com.google.android.exoplayer2.offline.DefaultDownloaderFactory) Download(com.google.android.exoplayer2.offline.Download) NoOpCacheEvictor(com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor) FakeDataSet(com.google.android.exoplayer2.testutil.FakeDataSet) CacheDataSource(com.google.android.exoplayer2.upstream.cache.CacheDataSource) DataSource(com.google.android.exoplayer2.upstream.DataSource) FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) DefaultDownloadIndex(com.google.android.exoplayer2.offline.DefaultDownloadIndex) DummyMainThread(com.google.android.exoplayer2.testutil.DummyMainThread) StreamKey(com.google.android.exoplayer2.offline.StreamKey) Nullable(androidx.annotation.Nullable) TestDownloadManagerListener(com.google.android.exoplayer2.robolectric.TestDownloadManagerListener) Before(org.junit.Before)

Example 4 with DownloadManager

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

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

Aggregations

DownloadManager (com.google.android.exoplayer2.offline.DownloadManager)4 Nullable (androidx.annotation.Nullable)3 TestDownloadManagerListener (com.google.android.exoplayer2.robolectric.TestDownloadManagerListener)3 Requirements (com.google.android.exoplayer2.scheduler.Requirements)3 DefaultDownloaderFactory (com.google.android.exoplayer2.offline.DefaultDownloaderFactory)2 Scheduler (com.google.android.exoplayer2.scheduler.Scheduler)2 FakeDataSource (com.google.android.exoplayer2.testutil.FakeDataSource)2 CacheDataSource (com.google.android.exoplayer2.upstream.cache.CacheDataSource)2 Notification (android.app.Notification)1 DefaultDownloadIndex (com.google.android.exoplayer2.offline.DefaultDownloadIndex)1 Download (com.google.android.exoplayer2.offline.Download)1 DownloadService (com.google.android.exoplayer2.offline.DownloadService)1 StreamKey (com.google.android.exoplayer2.offline.StreamKey)1 DummyMainThread (com.google.android.exoplayer2.testutil.DummyMainThread)1 TestRunnable (com.google.android.exoplayer2.testutil.DummyMainThread.TestRunnable)1 FakeDataSet (com.google.android.exoplayer2.testutil.FakeDataSet)1 DownloadNotificationHelper (com.google.android.exoplayer2.ui.DownloadNotificationHelper)1 DataSource (com.google.android.exoplayer2.upstream.DataSource)1 Factory (com.google.android.exoplayer2.upstream.DataSource.Factory)1 NoOpCacheEvictor (com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor)1