From c235087c11139c969b18bf83ebf0ec783b32e553 Mon Sep 17 00:00:00 2001 From: ibaker Date: Mon, 7 Feb 2022 11:08:51 +0000 Subject: [PATCH] Remove deprecated ActionFile and ActionFileUpgradeUtil #minor-release PiperOrigin-RevId: 426868933 --- RELEASENOTES.md | 3 + .../android/exoplayer2/demo/DemoUtil.java | 31 -- docs/downloading-media.md | 5 - .../exoplayer2/offline/ActionFile.java | 188 ----------- .../offline/ActionFileUpgradeUtil.java | 120 ------- .../exoplayer2/offline/ActionFileTest.java | 133 -------- .../offline/ActionFileUpgradeUtilTest.java | 308 ------------------ .../offline/ProgressiveDownloaderTest.java | 3 +- ...n_file_for_download_index_upgrade_dash.exi | Bin 146 -> 0 bytes ...on_file_for_download_index_upgrade_hls.exi | Bin 146 -> 0 bytes ...for_download_index_upgrade_progressive.exi | Bin 140 -> 0 bytes ...ion_file_for_download_index_upgrade_ss.exi | Bin 154 -> 0 bytes .../offline/action_file_incomplete_header.exi | Bin 4 -> 0 bytes .../media/offline/action_file_no_data.exi | 0 .../media/offline/action_file_one_action.exi | Bin 69 -> 0 bytes .../media/offline/action_file_two_actions.exi | Bin 146 -> 0 bytes .../action_file_unsupported_version.exi | Bin 69 -> 0 bytes .../offline/action_file_zero_actions.exi | Bin 8 -> 0 bytes 18 files changed, 4 insertions(+), 787 deletions(-) delete mode 100644 library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFile.java delete mode 100644 library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtil.java delete mode 100644 library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileTest.java delete mode 100644 library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtilTest.java delete mode 100644 testdata/src/test/assets/media/offline/action_file_for_download_index_upgrade_dash.exi delete mode 100644 testdata/src/test/assets/media/offline/action_file_for_download_index_upgrade_hls.exi delete mode 100644 testdata/src/test/assets/media/offline/action_file_for_download_index_upgrade_progressive.exi delete mode 100644 testdata/src/test/assets/media/offline/action_file_for_download_index_upgrade_ss.exi delete mode 100644 testdata/src/test/assets/media/offline/action_file_incomplete_header.exi delete mode 100644 testdata/src/test/assets/media/offline/action_file_no_data.exi delete mode 100644 testdata/src/test/assets/media/offline/action_file_one_action.exi delete mode 100644 testdata/src/test/assets/media/offline/action_file_two_actions.exi delete mode 100644 testdata/src/test/assets/media/offline/action_file_unsupported_version.exi delete mode 100644 testdata/src/test/assets/media/offline/action_file_zero_actions.exi diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 28689c76a9..169f5cc652 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -169,6 +169,9 @@ a fallback value. * Remove `(Simple)ExoPlayer.setThrowsWhenUsingWrongThread`. Opting out of the thread enforcement is no longer possible. + * Remove `ActionFile` and `ActionFileUpgradeUtil`. Use ExoPlayer 2.16.1 or + before to use `ActionFileUpgradeUtil` to merge legacy action files into + `DefaultDownloadIndex`. * Change the following `IntDefs` to `@Target(TYPE_USE)` only. This may break the compilation of usages in Kotlin, which can be fixed by moving the annotation to annotate the type (`Int`). diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoUtil.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoUtil.java index 62c3a5a005..e6a67da586 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoUtil.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoUtil.java @@ -22,8 +22,6 @@ import com.google.android.exoplayer2.database.DatabaseProvider; import com.google.android.exoplayer2.database.StandaloneDatabaseProvider; import com.google.android.exoplayer2.ext.cronet.CronetDataSource; import com.google.android.exoplayer2.ext.cronet.CronetUtil; -import com.google.android.exoplayer2.offline.ActionFileUpgradeUtil; -import com.google.android.exoplayer2.offline.DefaultDownloadIndex; import com.google.android.exoplayer2.offline.DownloadManager; import com.google.android.exoplayer2.ui.DownloadNotificationHelper; import com.google.android.exoplayer2.upstream.DataSource; @@ -34,9 +32,7 @@ import com.google.android.exoplayer2.upstream.cache.Cache; import com.google.android.exoplayer2.upstream.cache.CacheDataSource; import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor; import com.google.android.exoplayer2.upstream.cache.SimpleCache; -import com.google.android.exoplayer2.util.Log; import java.io.File; -import java.io.IOException; import java.net.CookieHandler; import java.net.CookieManager; import java.net.CookiePolicy; @@ -60,8 +56,6 @@ public final class DemoUtil { private static final boolean USE_CRONET_FOR_NETWORKING = true; private static final String TAG = "DemoUtil"; - private static final String DOWNLOAD_ACTION_FILE = "actions"; - private static final String DOWNLOAD_TRACKER_ACTION_FILE = "tracked_actions"; private static final String DOWNLOAD_CONTENT_DIRECTORY = "downloads"; private static DataSource.@MonotonicNonNull Factory dataSourceFactory; @@ -155,14 +149,6 @@ public final class DemoUtil { private static synchronized void ensureDownloadManagerInitialized(Context context) { if (downloadManager == null) { - DefaultDownloadIndex downloadIndex = new DefaultDownloadIndex(getDatabaseProvider(context)); - upgradeActionFile( - context, DOWNLOAD_ACTION_FILE, downloadIndex, /* addNewDownloadsAsCompleted= */ false); - upgradeActionFile( - context, - DOWNLOAD_TRACKER_ACTION_FILE, - downloadIndex, - /* addNewDownloadsAsCompleted= */ true); downloadManager = new DownloadManager( context, @@ -175,23 +161,6 @@ public final class DemoUtil { } } - private static synchronized void upgradeActionFile( - Context context, - String fileName, - DefaultDownloadIndex downloadIndex, - boolean addNewDownloadsAsCompleted) { - try { - ActionFileUpgradeUtil.upgradeAndDelete( - new File(getDownloadDirectory(context), fileName), - /* downloadIdProvider= */ null, - downloadIndex, - /* deleteOnFailure= */ true, - addNewDownloadsAsCompleted); - } catch (IOException e) { - Log.e(TAG, "Failed to upgrade action file: " + fileName, e); - } - } - private static synchronized DatabaseProvider getDatabaseProvider(Context context) { if (databaseProvider == null) { databaseProvider = new StandaloneDatabaseProvider(context); diff --git a/docs/downloading-media.md b/docs/downloading-media.md index a835644d85..3ef28bd685 100644 --- a/docs/downloading-media.md +++ b/docs/downloading-media.md @@ -96,11 +96,6 @@ downloadManager.setMaxParallelDownloads(3); See [`DemoUtil`][] in the demo app for a concrete example. -The example in the demo app also imports download state from legacy `ActionFile` -instances. This is only necessary if your app used `ActionFile` prior to -ExoPlayer 2.10.0. -{:.info} - ## Adding a download ## To add a download you need to create a `DownloadRequest` and send it to your diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFile.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFile.java deleted file mode 100644 index 2f7db22326..0000000000 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFile.java +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.android.exoplayer2.offline; - -import android.net.Uri; -import androidx.annotation.Nullable; -import com.google.android.exoplayer2.offline.DownloadRequest.UnsupportedRequestException; -import com.google.android.exoplayer2.util.AtomicFile; -import com.google.android.exoplayer2.util.MimeTypes; -import com.google.android.exoplayer2.util.Util; -import java.io.DataInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; - -/** - * Loads {@link DownloadRequest DownloadRequests} from legacy action files. - * - * @deprecated Legacy action files should be merged into download indices using {@link - * ActionFileUpgradeUtil}. - */ -@Deprecated -/* package */ final class ActionFile { - - private static final int VERSION = 0; - private static final String DOWNLOAD_TYPE_PROGRESSIVE = "progressive"; - private static final String DOWNLOAD_TYPE_DASH = "dash"; - private static final String DOWNLOAD_TYPE_HLS = "hls"; - private static final String DOWNLOAD_TYPE_SS = "ss"; - - private final AtomicFile atomicFile; - - /** - * @param actionFile The file from which {@link DownloadRequest DownloadRequests} will be loaded. - */ - public ActionFile(File actionFile) { - atomicFile = new AtomicFile(actionFile); - } - - /** Returns whether the file or its backup exists. */ - public boolean exists() { - return atomicFile.exists(); - } - - /** Deletes the action file and its backup. */ - public void delete() { - atomicFile.delete(); - } - - /** - * Loads {@link DownloadRequest DownloadRequests} from the file. - * - * @return The loaded {@link DownloadRequest DownloadRequests}, or an empty array if the file does - * not exist. - * @throws IOException If there is an error reading the file. - */ - public DownloadRequest[] load() throws IOException { - if (!exists()) { - return new DownloadRequest[0]; - } - @Nullable InputStream inputStream = null; - try { - inputStream = atomicFile.openRead(); - DataInputStream dataInputStream = new DataInputStream(inputStream); - int version = dataInputStream.readInt(); - if (version > VERSION) { - throw new IOException("Unsupported action file version: " + version); - } - int actionCount = dataInputStream.readInt(); - ArrayList actions = new ArrayList<>(); - for (int i = 0; i < actionCount; i++) { - try { - actions.add(readDownloadRequest(dataInputStream)); - } catch (UnsupportedRequestException e) { - // remove DownloadRequest is not supported. Ignore and continue loading rest. - } - } - return actions.toArray(new DownloadRequest[0]); - } finally { - Util.closeQuietly(inputStream); - } - } - - private static DownloadRequest readDownloadRequest(DataInputStream input) throws IOException { - String downloadType = input.readUTF(); - int version = input.readInt(); - - Uri uri = Uri.parse(input.readUTF()); - boolean isRemoveAction = input.readBoolean(); - - int dataLength = input.readInt(); - @Nullable byte[] data; - if (dataLength != 0) { - data = new byte[dataLength]; - input.readFully(data); - } else { - data = null; - } - - // Serialized version 0 progressive actions did not contain keys. - boolean isLegacyProgressive = version == 0 && DOWNLOAD_TYPE_PROGRESSIVE.equals(downloadType); - List keys = new ArrayList<>(); - if (!isLegacyProgressive) { - int keyCount = input.readInt(); - for (int i = 0; i < keyCount; i++) { - keys.add(readKey(downloadType, version, input)); - } - } - - // Serialized version 0 and 1 DASH/HLS/SS actions did not contain a custom cache key. - boolean isLegacySegmented = - version < 2 - && (DOWNLOAD_TYPE_DASH.equals(downloadType) - || DOWNLOAD_TYPE_HLS.equals(downloadType) - || DOWNLOAD_TYPE_SS.equals(downloadType)); - @Nullable String customCacheKey = null; - if (!isLegacySegmented) { - customCacheKey = input.readBoolean() ? input.readUTF() : null; - } - - // Serialized version 0, 1 and 2 did not contain an id. We need to generate one. - String id = version < 3 ? generateDownloadId(uri, customCacheKey) : input.readUTF(); - - if (isRemoveAction) { - // Remove actions are not supported anymore. - throw new UnsupportedRequestException(); - } - - return new DownloadRequest.Builder(id, uri) - .setMimeType(inferMimeType(downloadType)) - .setStreamKeys(keys) - .setCustomCacheKey(customCacheKey) - .setData(data) - .build(); - } - - private static StreamKey readKey(String type, int version, DataInputStream input) - throws IOException { - int periodIndex; - int groupIndex; - int trackIndex; - - // Serialized version 0 HLS/SS actions did not contain a period index. - if ((DOWNLOAD_TYPE_HLS.equals(type) || DOWNLOAD_TYPE_SS.equals(type)) && version == 0) { - periodIndex = 0; - groupIndex = input.readInt(); - trackIndex = input.readInt(); - } else { - periodIndex = input.readInt(); - groupIndex = input.readInt(); - trackIndex = input.readInt(); - } - return new StreamKey(periodIndex, groupIndex, trackIndex); - } - - private static String inferMimeType(String downloadType) { - switch (downloadType) { - case DOWNLOAD_TYPE_DASH: - return MimeTypes.APPLICATION_MPD; - case DOWNLOAD_TYPE_HLS: - return MimeTypes.APPLICATION_M3U8; - case DOWNLOAD_TYPE_SS: - return MimeTypes.APPLICATION_SS; - case DOWNLOAD_TYPE_PROGRESSIVE: - default: - return MimeTypes.VIDEO_UNKNOWN; - } - } - - private static String generateDownloadId(Uri uri, @Nullable String customCacheKey) { - return customCacheKey != null ? customCacheKey : uri.toString(); - } -} diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtil.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtil.java deleted file mode 100644 index 999059e852..0000000000 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtil.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.android.exoplayer2.offline; - -import static com.google.android.exoplayer2.offline.Download.STATE_QUEUED; - -import androidx.annotation.Nullable; -import androidx.annotation.WorkerThread; -import com.google.android.exoplayer2.C; -import java.io.File; -import java.io.IOException; - -/** Utility class for upgrading legacy action files into {@link DefaultDownloadIndex}. */ -public final class ActionFileUpgradeUtil { - - /** Provides download IDs during action file upgrade. */ - public interface DownloadIdProvider { - - /** - * Returns a download id for given request. - * - * @param downloadRequest The request for which an ID is required. - * @return A corresponding download ID. - */ - String getId(DownloadRequest downloadRequest); - } - - private ActionFileUpgradeUtil() {} - - /** - * Merges {@link DownloadRequest DownloadRequests} contained in a legacy action file into a {@link - * DefaultDownloadIndex}, deleting the action file if the merge is successful or if {@code - * deleteOnFailure} is {@code true}. - * - *

This method must not be called while the {@link DefaultDownloadIndex} is being used by a - * {@link DownloadManager}. - * - *

This method may be slow and shouldn't normally be called on the main thread. - * - * @param actionFilePath The action file path. - * @param downloadIdProvider A download ID provider, or {@code null}. If {@code null} then ID of - * each download will be its custom cache key if one is specified, or else its URL. - * @param downloadIndex The index into which the requests will be merged. - * @param deleteOnFailure Whether to delete the action file if the merge fails. - * @param addNewDownloadsAsCompleted Whether to add new downloads as completed. - * @throws IOException If an error occurs loading or merging the requests. - */ - @WorkerThread - @SuppressWarnings("deprecation") - public static void upgradeAndDelete( - File actionFilePath, - @Nullable DownloadIdProvider downloadIdProvider, - DefaultDownloadIndex downloadIndex, - boolean deleteOnFailure, - boolean addNewDownloadsAsCompleted) - throws IOException { - ActionFile actionFile = new ActionFile(actionFilePath); - if (actionFile.exists()) { - boolean success = false; - try { - long nowMs = System.currentTimeMillis(); - for (DownloadRequest request : actionFile.load()) { - if (downloadIdProvider != null) { - request = request.copyWithId(downloadIdProvider.getId(request)); - } - mergeRequest(request, downloadIndex, addNewDownloadsAsCompleted, nowMs); - } - success = true; - } finally { - if (success || deleteOnFailure) { - actionFile.delete(); - } - } - } - } - - /** - * Merges a {@link DownloadRequest} into a {@link DefaultDownloadIndex}. - * - * @param request The request to be merged. - * @param downloadIndex The index into which the request will be merged. - * @param addNewDownloadAsCompleted Whether to add new downloads as completed. - * @throws IOException If an error occurs merging the request. - */ - /* package */ static void mergeRequest( - DownloadRequest request, - DefaultDownloadIndex downloadIndex, - boolean addNewDownloadAsCompleted, - long nowMs) - throws IOException { - @Nullable Download download = downloadIndex.getDownload(request.id); - if (download != null) { - download = DownloadManager.mergeRequest(download, request, download.stopReason, nowMs); - } else { - download = - new Download( - request, - addNewDownloadAsCompleted ? Download.STATE_COMPLETED : STATE_QUEUED, - /* startTimeMs= */ nowMs, - /* updateTimeMs= */ nowMs, - /* contentLength= */ C.LENGTH_UNSET, - Download.STOP_REASON_NONE, - Download.FAILURE_REASON_NONE); - } - downloadIndex.putDownload(download); - } -} diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileTest.java deleted file mode 100644 index 02ff4bba5e..0000000000 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileTest.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.android.exoplayer2.offline; - -import static com.google.common.truth.Truth.assertThat; - -import android.net.Uri; -import androidx.test.core.app.ApplicationProvider; -import androidx.test.ext.junit.runners.AndroidJUnit4; -import com.google.android.exoplayer2.testutil.TestUtil; -import com.google.android.exoplayer2.util.MimeTypes; -import com.google.android.exoplayer2.util.Util; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - -/** Unit tests for {@link ActionFile}. */ -@SuppressWarnings("deprecation") -@RunWith(AndroidJUnit4.class) -public class ActionFileTest { - - private File tempFile; - private DownloadRequest expectedAction1; - private DownloadRequest expectedAction2; - - @Before - public void setUp() throws Exception { - tempFile = Util.createTempFile(ApplicationProvider.getApplicationContext(), "ExoPlayerTest"); - expectedAction1 = - buildExpectedRequest(Uri.parse("http://test1.uri"), TestUtil.buildTestData(16)); - expectedAction2 = - buildExpectedRequest(Uri.parse("http://test2.uri"), TestUtil.buildTestData(32)); - } - - @After - public void tearDown() throws Exception { - tempFile.delete(); - } - - @Test - public void loadNoDataThrowsIOException() throws Exception { - ActionFile actionFile = getActionFile("media/offline/action_file_no_data.exi"); - try { - actionFile.load(); - Assert.fail(); - } catch (IOException e) { - // Expected exception. - } - } - - @Test - public void loadIncompleteHeaderThrowsIOException() throws Exception { - ActionFile actionFile = getActionFile("media/offline/action_file_incomplete_header.exi"); - try { - actionFile.load(); - Assert.fail(); - } catch (IOException e) { - // Expected exception. - } - } - - @Test - public void loadZeroActions() throws Exception { - ActionFile actionFile = getActionFile("media/offline/action_file_zero_actions.exi"); - DownloadRequest[] actions = actionFile.load(); - assertThat(actions).isNotNull(); - assertThat(actions).hasLength(0); - } - - @Test - public void loadOneAction() throws Exception { - ActionFile actionFile = getActionFile("media/offline/action_file_one_action.exi"); - DownloadRequest[] actions = actionFile.load(); - assertThat(actions).hasLength(1); - assertThat(actions[0]).isEqualTo(expectedAction1); - } - - @Test - public void loadTwoActions() throws Exception { - ActionFile actionFile = getActionFile("media/offline/action_file_two_actions.exi"); - DownloadRequest[] actions = actionFile.load(); - assertThat(actions).hasLength(2); - assertThat(actions[0]).isEqualTo(expectedAction1); - assertThat(actions[1]).isEqualTo(expectedAction2); - } - - @Test - public void loadUnsupportedVersion() throws Exception { - ActionFile actionFile = getActionFile("media/offline/action_file_unsupported_version.exi"); - try { - actionFile.load(); - Assert.fail(); - } catch (IOException e) { - // Expected exception. - } - } - - private ActionFile getActionFile(String fileName) throws IOException { - // Copy the test data from the asset to where the ActionFile expects it to be. - byte[] actionFileBytes = - TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), fileName); - try (FileOutputStream output = new FileOutputStream(tempFile)) { - output.write(actionFileBytes); - } - // Load the action file. - return new ActionFile(tempFile); - } - - private static DownloadRequest buildExpectedRequest(Uri uri, byte[] data) { - return new DownloadRequest.Builder(/* id= */ uri.toString(), uri) - .setMimeType(MimeTypes.VIDEO_UNKNOWN) - .setData(data) - .build(); - } -} diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtilTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtilTest.java deleted file mode 100644 index 239762c57f..0000000000 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtilTest.java +++ /dev/null @@ -1,308 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.android.exoplayer2.offline; - -import static com.google.common.truth.Truth.assertThat; - -import android.net.Uri; -import androidx.test.core.app.ApplicationProvider; -import androidx.test.ext.junit.runners.AndroidJUnit4; -import com.google.android.exoplayer2.database.StandaloneDatabaseProvider; -import com.google.android.exoplayer2.testutil.TestUtil; -import com.google.android.exoplayer2.util.MimeTypes; -import com.google.android.exoplayer2.util.Util; -import com.google.common.collect.ImmutableList; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - -/** Unit tests for {@link ActionFileUpgradeUtil}. */ -@RunWith(AndroidJUnit4.class) -public class ActionFileUpgradeUtilTest { - - private static final long NOW_MS = 1234; - - private File tempFile; - private StandaloneDatabaseProvider databaseProvider; - private DefaultDownloadIndex downloadIndex; - - @Before - public void setUp() throws Exception { - tempFile = Util.createTempFile(ApplicationProvider.getApplicationContext(), "ExoPlayerTest"); - databaseProvider = new StandaloneDatabaseProvider(ApplicationProvider.getApplicationContext()); - downloadIndex = new DefaultDownloadIndex(databaseProvider); - } - - @After - public void tearDown() { - databaseProvider.close(); - tempFile.delete(); - } - - @Test - public void upgradeAndDelete_progressiveActionFile_createsDownloads() throws IOException { - byte[] actionFileBytes = - TestUtil.getByteArray( - ApplicationProvider.getApplicationContext(), - "media/offline/action_file_for_download_index_upgrade_progressive.exi"); - try (FileOutputStream output = new FileOutputStream(tempFile)) { - output.write(actionFileBytes); - } - DownloadRequest expectedRequest1 = - new DownloadRequest.Builder( - /* id= */ "http://www.test.com/1/video.mp4", - Uri.parse("http://www.test.com/1/video.mp4")) - .setMimeType(MimeTypes.VIDEO_UNKNOWN) - .build(); - DownloadRequest expectedRequest2 = - new DownloadRequest.Builder( - /* id= */ "customCacheKey", Uri.parse("http://www.test.com/2/video.mp4")) - .setMimeType(MimeTypes.VIDEO_UNKNOWN) - .setCustomCacheKey("customCacheKey") - .setData(new byte[] {0, 1, 2, 3}) - .build(); - - ActionFileUpgradeUtil.upgradeAndDelete( - tempFile, - /* downloadIdProvider= */ null, - downloadIndex, - /* deleteOnFailure= */ true, - /* addNewDownloadsAsCompleted= */ false); - - assertThat(tempFile.exists()).isFalse(); - assertDownloadIndexContainsRequest(expectedRequest1, Download.STATE_QUEUED); - assertDownloadIndexContainsRequest(expectedRequest2, Download.STATE_QUEUED); - } - - @Test - public void upgradeAndDelete_dashActionFile_createsDownloads() throws IOException { - byte[] actionFileBytes = - TestUtil.getByteArray( - ApplicationProvider.getApplicationContext(), - "media/offline/action_file_for_download_index_upgrade_dash.exi"); - try (FileOutputStream output = new FileOutputStream(tempFile)) { - output.write(actionFileBytes); - } - DownloadRequest expectedRequest1 = - new DownloadRequest.Builder( - /* id= */ "http://www.test.com/1/manifest.mpd", - Uri.parse("http://www.test.com/1/manifest.mpd")) - .setMimeType(MimeTypes.APPLICATION_MPD) - .build(); - DownloadRequest expectedRequest2 = - new DownloadRequest.Builder( - /* id= */ "http://www.test.com/2/manifest.mpd", - Uri.parse("http://www.test.com/2/manifest.mpd")) - .setMimeType(MimeTypes.APPLICATION_MPD) - .setStreamKeys( - ImmutableList.of( - new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0), - new StreamKey(/* groupIndex= */ 1, /* trackIndex= */ 1))) - .setData(new byte[] {0, 1, 2, 3}) - .build(); - - ActionFileUpgradeUtil.upgradeAndDelete( - tempFile, - /* downloadIdProvider= */ null, - downloadIndex, - /* deleteOnFailure= */ true, - /* addNewDownloadsAsCompleted= */ false); - - assertThat(tempFile.exists()).isFalse(); - assertDownloadIndexContainsRequest(expectedRequest1, Download.STATE_QUEUED); - assertDownloadIndexContainsRequest(expectedRequest2, Download.STATE_QUEUED); - } - - @Test - public void upgradeAndDelete_hlsActionFile_createsDownloads() throws IOException { - byte[] actionFileBytes = - TestUtil.getByteArray( - ApplicationProvider.getApplicationContext(), - "media/offline/action_file_for_download_index_upgrade_hls.exi"); - try (FileOutputStream output = new FileOutputStream(tempFile)) { - output.write(actionFileBytes); - } - DownloadRequest expectedRequest1 = - new DownloadRequest.Builder( - /* id= */ "http://www.test.com/1/manifest.m3u8", - Uri.parse("http://www.test.com/1/manifest.m3u8")) - .setMimeType(MimeTypes.APPLICATION_M3U8) - .build(); - DownloadRequest expectedRequest2 = - new DownloadRequest.Builder( - /* id= */ "http://www.test.com/2/manifest.m3u8", - Uri.parse("http://www.test.com/2/manifest.m3u8")) - .setMimeType(MimeTypes.APPLICATION_M3U8) - .setStreamKeys( - ImmutableList.of( - new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0), - new StreamKey(/* groupIndex= */ 1, /* trackIndex= */ 1))) - .setData(new byte[] {0, 1, 2, 3}) - .build(); - - ActionFileUpgradeUtil.upgradeAndDelete( - tempFile, - /* downloadIdProvider= */ null, - downloadIndex, - /* deleteOnFailure= */ true, - /* addNewDownloadsAsCompleted= */ false); - - assertThat(tempFile.exists()).isFalse(); - assertDownloadIndexContainsRequest(expectedRequest1, Download.STATE_QUEUED); - assertDownloadIndexContainsRequest(expectedRequest2, Download.STATE_QUEUED); - } - - @Test - public void upgradeAndDelete_smoothStreamingActionFile_createsDownloads() throws IOException { - byte[] actionFileBytes = - TestUtil.getByteArray( - ApplicationProvider.getApplicationContext(), - "media/offline/action_file_for_download_index_upgrade_ss.exi"); - try (FileOutputStream output = new FileOutputStream(tempFile)) { - output.write(actionFileBytes); - } - DownloadRequest expectedRequest1 = - new DownloadRequest.Builder( - /* id= */ "http://www.test.com/1/video.ism/manifest", - Uri.parse("http://www.test.com/1/video.ism/manifest")) - .setMimeType(MimeTypes.APPLICATION_SS) - .build(); - DownloadRequest expectedRequest2 = - new DownloadRequest.Builder( - /* id= */ "http://www.test.com/2/video.ism/manifest", - Uri.parse("http://www.test.com/2/video.ism/manifest")) - .setMimeType(MimeTypes.APPLICATION_SS) - .setStreamKeys( - ImmutableList.of( - new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0), - new StreamKey(/* groupIndex= */ 1, /* trackIndex= */ 1))) - .setData(new byte[] {0, 1, 2, 3}) - .build(); - - ActionFileUpgradeUtil.upgradeAndDelete( - tempFile, - /* downloadIdProvider= */ null, - downloadIndex, - /* deleteOnFailure= */ true, - /* addNewDownloadsAsCompleted= */ false); - - assertThat(tempFile.exists()).isFalse(); - assertDownloadIndexContainsRequest(expectedRequest1, Download.STATE_QUEUED); - assertDownloadIndexContainsRequest(expectedRequest2, Download.STATE_QUEUED); - } - - @Test - public void mergeRequest_nonExistingDownload_createsNewDownload() throws IOException { - DownloadRequest request = - new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download")) - .setStreamKeys( - ImmutableList.of( - new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2), - new StreamKey(/* periodIndex= */ 3, /* groupIndex= */ 4, /* trackIndex= */ 5))) - .setKeySetId(new byte[] {1, 2, 3, 4}) - .setCustomCacheKey("key123") - .setData(new byte[] {1, 2, 3, 4}) - .build(); - - ActionFileUpgradeUtil.mergeRequest( - request, downloadIndex, /* addNewDownloadAsCompleted= */ false, NOW_MS); - - assertDownloadIndexContainsRequest(request, Download.STATE_QUEUED); - } - - @Test - public void mergeRequest_existingDownload_createsMergedDownload() throws IOException { - StreamKey streamKey1 = - new StreamKey(/* periodIndex= */ 3, /* groupIndex= */ 4, /* trackIndex= */ 5); - StreamKey streamKey2 = - new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2); - DownloadRequest request1 = - new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download1")) - .setStreamKeys(ImmutableList.of(streamKey1)) - .setKeySetId(new byte[] {1, 2, 3, 4}) - .setCustomCacheKey("key123") - .setData(new byte[] {1, 2, 3, 4}) - .build(); - DownloadRequest request2 = - new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download2")) - .setMimeType(MimeTypes.APPLICATION_MP4) - .setStreamKeys(ImmutableList.of(streamKey2)) - .setKeySetId(new byte[] {5, 4, 3, 2, 1}) - .setCustomCacheKey("key345") - .setData(new byte[] {5, 4, 3, 2, 1}) - .build(); - - ActionFileUpgradeUtil.mergeRequest( - request1, downloadIndex, /* addNewDownloadAsCompleted= */ false, NOW_MS); - ActionFileUpgradeUtil.mergeRequest( - request2, downloadIndex, /* addNewDownloadAsCompleted= */ false, NOW_MS); - Download download = downloadIndex.getDownload(request2.id); - - assertThat(download).isNotNull(); - assertThat(download.request.mimeType).isEqualTo(MimeTypes.APPLICATION_MP4); - assertThat(download.request.customCacheKey).isEqualTo(request2.customCacheKey); - assertThat(download.request.data).isEqualTo(request2.data); - assertThat(download.request.uri).isEqualTo(request2.uri); - assertThat(download.request.streamKeys).containsExactly(streamKey1, streamKey2); - assertThat(download.request.keySetId).isEqualTo(request2.keySetId); - assertThat(download.state).isEqualTo(Download.STATE_QUEUED); - } - - @Test - public void mergeRequest_addNewDownloadAsCompleted() throws IOException { - StreamKey streamKey1 = - new StreamKey(/* periodIndex= */ 3, /* groupIndex= */ 4, /* trackIndex= */ 5); - StreamKey streamKey2 = - new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2); - DownloadRequest request1 = - new DownloadRequest.Builder(/* id= */ "id1", Uri.parse("https://www.test.com/download1")) - .setStreamKeys(ImmutableList.of(streamKey1)) - .setKeySetId(new byte[] {1, 2, 3, 4}) - .setCustomCacheKey("key123") - .setData(new byte[] {1, 2, 3, 4}) - .build(); - DownloadRequest request2 = - new DownloadRequest.Builder(/* id= */ "id2", Uri.parse("https://www.test.com/download2")) - .setStreamKeys(ImmutableList.of(streamKey2)) - .setKeySetId(new byte[] {5, 4, 3, 2, 1}) - .setCustomCacheKey("key456") - .setData(new byte[] {5, 4, 3, 2, 1}) - .build(); - ActionFileUpgradeUtil.mergeRequest( - request1, downloadIndex, /* addNewDownloadAsCompleted= */ false, NOW_MS); - - // Merging existing download, keeps it queued. - ActionFileUpgradeUtil.mergeRequest( - request1, downloadIndex, /* addNewDownloadAsCompleted= */ true, NOW_MS); - assertThat(downloadIndex.getDownload(request1.id).state).isEqualTo(Download.STATE_QUEUED); - - // New download is merged as completed. - ActionFileUpgradeUtil.mergeRequest( - request2, downloadIndex, /* addNewDownloadAsCompleted= */ true, NOW_MS); - assertThat(downloadIndex.getDownload(request2.id).state).isEqualTo(Download.STATE_COMPLETED); - } - - private void assertDownloadIndexContainsRequest(DownloadRequest request, int state) - throws IOException { - Download download = downloadIndex.getDownload(request.id); - assertThat(download.request).isEqualTo(request); - assertThat(download.state).isEqualTo(state); - } -} diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/ProgressiveDownloaderTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/ProgressiveDownloaderTest.java index b80483a4e6..0dfe89eea3 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/ProgressiveDownloaderTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/offline/ProgressiveDownloaderTest.java @@ -41,8 +41,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -/** Unit tests for {@link ActionFile}. */ -@SuppressWarnings("deprecation") +/** Unit tests for {@link ProgressiveDownloader}. */ @RunWith(AndroidJUnit4.class) public class ProgressiveDownloaderTest { diff --git a/testdata/src/test/assets/media/offline/action_file_for_download_index_upgrade_dash.exi b/testdata/src/test/assets/media/offline/action_file_for_download_index_upgrade_dash.exi deleted file mode 100644 index 9c249a377eb4efe157bc08906206ff9dcb081842..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 146 zcmZQz00SllmXyTe3@}?Mqokz3N?*Ucyj-s&wYWqtIX_q5P(L>@FEb6q%`HfQXabQ0 Yv>9P&V_{%qVg_ntLYM((F#=hP0B~O%cmMzZ diff --git a/testdata/src/test/assets/media/offline/action_file_for_download_index_upgrade_hls.exi b/testdata/src/test/assets/media/offline/action_file_for_download_index_upgrade_hls.exi deleted file mode 100644 index a6315a80ef4d4bfd0177ba698ca53e1caf07676e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 146 zcmZQz00Sll=8T+TAd`_nIisYcz)D}gyu4hmB(=CiFF8L~-%vj{F)uR>#LYD>wSZ^> ak@&S4VQOPxU}RzjYGgu~0cSA+S&RTibQ|CR diff --git a/testdata/src/test/assets/media/offline/action_file_for_download_index_upgrade_progressive.exi b/testdata/src/test/assets/media/offline/action_file_for_download_index_upgrade_progressive.exi deleted file mode 100644 index 477bd0815a5c80d19c275ee2440eb9de4d667185..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 140 zcmZQz00Sll?t-HH^rF<_;>@yCu#kL4NlAf~zJ7Umxn4 kfeAz-k@}2K^|3H8GBGnU@FkZPm*nR@yCpb!&-Kt@SPft9{~NosM4p~9J|`1= diff --git a/testdata/src/test/assets/media/offline/action_file_two_actions.exi b/testdata/src/test/assets/media/offline/action_file_two_actions.exi deleted file mode 100644 index 35c9b35e1ee54b5ef04a1f11ed94e9c16ed7017c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 146 zcmZQz00Sll?t-HH^rF<_;>@yCpb!&-Kt@SPft9{~NosM4p@q)ks6a`pW|dL*DcgNoe2-b#&8HMhi=683@Sp^2 F7y#d!EZqPA diff --git a/testdata/src/test/assets/media/offline/action_file_unsupported_version.exi b/testdata/src/test/assets/media/offline/action_file_unsupported_version.exi deleted file mode 100644 index 25e0dee842c29f0a3715408b7bfd8a91f7e4bc33..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 69 zcmZQzU|?hbVeW#W{Pd#K;^NG*RG9