diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java index 3f9010a609..2a8c4666e1 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java @@ -64,7 +64,7 @@ public final class CacheDataSource implements DataSource { public Factory() { cacheReadDataSourceFactory = new FileDataSource.Factory(); - cacheKeyFactory = CacheUtil.DEFAULT_CACHE_KEY_FACTORY; + cacheKeyFactory = CacheKeyFactory.DEFAULT; } /** @@ -123,7 +123,7 @@ public final class CacheDataSource implements DataSource { /** * Sets the {@link CacheKeyFactory}. * - *
The default is {@link CacheUtil#DEFAULT_CACHE_KEY_FACTORY}. + *
The default is {@link CacheKeyFactory#DEFAULT}.
*
* @param cacheKeyFactory The {@link CacheKeyFactory}.
* @return This factory.
@@ -508,8 +508,7 @@ public final class CacheDataSource implements DataSource {
@Nullable EventListener eventListener) {
this.cache = cache;
this.cacheReadDataSource = cacheReadDataSource;
- this.cacheKeyFactory =
- cacheKeyFactory != null ? cacheKeyFactory : CacheUtil.DEFAULT_CACHE_KEY_FACTORY;
+ this.cacheKeyFactory = cacheKeyFactory != null ? cacheKeyFactory : CacheKeyFactory.DEFAULT;
this.blockOnCache = (flags & FLAG_BLOCK_ON_CACHE) != 0;
this.ignoreCacheOnError = (flags & FLAG_IGNORE_CACHE_ON_ERROR) != 0;
this.ignoreCacheForUnsetLengthRequests =
diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheKeyFactory.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheKeyFactory.java
index 3401d6f575..2236b5f9cc 100644
--- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheKeyFactory.java
+++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheKeyFactory.java
@@ -20,6 +20,10 @@ import com.google.android.exoplayer2.upstream.DataSpec;
/** Factory for cache keys. */
public interface CacheKeyFactory {
+ /** Default {@link CacheKeyFactory}. */
+ CacheKeyFactory DEFAULT =
+ (dataSpec) -> dataSpec.key != null ? dataSpec.key : dataSpec.uri.toString();
+
/**
* Returns a cache key for the given {@link DataSpec}.
*
diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java
index f19b818e1a..be600a4a87 100644
--- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java
+++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java
@@ -15,7 +15,6 @@
*/
package com.google.android.exoplayer2.upstream.cache;
-import android.net.Uri;
import android.util.Pair;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
@@ -55,18 +54,9 @@ public final class CacheUtil {
/** Default buffer size to be used while caching. */
public static final int DEFAULT_BUFFER_SIZE_BYTES = 128 * 1024;
- /** Default {@link CacheKeyFactory}. */
- public static final CacheKeyFactory DEFAULT_CACHE_KEY_FACTORY =
- (dataSpec) -> dataSpec.key != null ? dataSpec.key : generateKey(dataSpec.uri);
-
- /**
- * Generates a cache key out of the given {@link Uri}.
- *
- * @param uri Uri of a content which the requested key is for.
- */
- public static String generateKey(Uri uri) {
- return uri.toString();
- }
+ /** @deprecated Use {@link CacheKeyFactory#DEFAULT}. */
+ @Deprecated
+ public static final CacheKeyFactory DEFAULT_CACHE_KEY_FACTORY = CacheKeyFactory.DEFAULT;
/**
* Queries the cache to obtain the request length and the number of bytes already cached for a
@@ -375,7 +365,7 @@ public final class CacheUtil {
private static String buildCacheKey(
DataSpec dataSpec, @Nullable CacheKeyFactory cacheKeyFactory) {
- return (cacheKeyFactory != null ? cacheKeyFactory : DEFAULT_CACHE_KEY_FACTORY)
+ return (cacheKeyFactory != null ? cacheKeyFactory : CacheKeyFactory.DEFAULT)
.buildCacheKey(dataSpec);
}
diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java
index 6562c17183..f6ba0a1e63 100644
--- a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java
+++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java
@@ -75,7 +75,7 @@ public final class CacheDataSourceTest {
boundedDataSpec = buildDataSpec(/* unbounded= */ false, /* key= */ null);
unboundedDataSpecWithKey = buildDataSpec(/* unbounded= */ true, DATASPEC_KEY);
boundedDataSpecWithKey = buildDataSpec(/* unbounded= */ false, DATASPEC_KEY);
- defaultCacheKey = CacheUtil.DEFAULT_CACHE_KEY_FACTORY.buildCacheKey(unboundedDataSpec);
+ defaultCacheKey = CacheKeyFactory.DEFAULT.buildCacheKey(unboundedDataSpec);
customCacheKey = "customKey." + defaultCacheKey;
cacheKeyFactory = dataSpec -> customCacheKey;
diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheKeyFactoryTest.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheKeyFactoryTest.java
new file mode 100644
index 0000000000..3c6542b90f
--- /dev/null
+++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheKeyFactoryTest.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2020 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.upstream.cache;
+
+import static com.google.android.exoplayer2.upstream.cache.CacheKeyFactory.DEFAULT;
+import static com.google.common.truth.Truth.assertThat;
+
+import android.net.Uri;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import com.google.android.exoplayer2.upstream.DataSpec;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/** Tests {@link CacheKeyFactoryTest}. */
+@RunWith(AndroidJUnit4.class)
+public class CacheKeyFactoryTest {
+
+ @Test
+ public void default_dataSpecWithKey_returnsKey() {
+ Uri testUri = Uri.parse("test");
+ String key = "key";
+ DataSpec dataSpec = new DataSpec.Builder().setUri(testUri).setKey(key).build();
+ assertThat(DEFAULT.buildCacheKey(dataSpec)).isEqualTo(key);
+ }
+
+ @Test
+ public void default_dataSpecWithoutKey_returnsUri() {
+ Uri testUri = Uri.parse("test");
+ DataSpec dataSpec = new DataSpec.Builder().setUri(testUri).build();
+ assertThat(DEFAULT.buildCacheKey(dataSpec)).isEqualTo(testUri.toString());
+ }
+}
diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheUtilTest.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheUtilTest.java
index d0a4da4f8c..9acc9b11c8 100644
--- a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheUtilTest.java
+++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheUtilTest.java
@@ -15,7 +15,6 @@
*/
package com.google.android.exoplayer2.upstream.cache;
-import static com.google.android.exoplayer2.C.LENGTH_UNSET;
import static com.google.android.exoplayer2.testutil.CacheAsserts.assertCacheEmpty;
import static com.google.android.exoplayer2.testutil.CacheAsserts.assertCachedData;
import static com.google.common.truth.Truth.assertThat;
@@ -104,37 +103,6 @@ public final class CacheUtilTest {
Util.recursiveDelete(tempFolder);
}
- @Test
- public void generateKey() {
- assertThat(CacheUtil.generateKey(Uri.EMPTY)).isNotNull();
-
- Uri testUri = Uri.parse("test");
- String key = CacheUtil.generateKey(testUri);
- assertThat(key).isNotNull();
-
- // Should generate the same key for the same input.
- assertThat(CacheUtil.generateKey(testUri)).isEqualTo(key);
-
- // Should generate different key for different input.
- assertThat(key.equals(CacheUtil.generateKey(Uri.parse("test2")))).isFalse();
- }
-
- @Test
- public void defaultCacheKeyFactory_buildCacheKey() {
- Uri testUri = Uri.parse("test");
- String key = "key";
- // If DataSpec.key is present, returns it.
- assertThat(
- CacheUtil.DEFAULT_CACHE_KEY_FACTORY.buildCacheKey(
- new DataSpec.Builder().setUri(testUri).setKey(key).build()))
- .isEqualTo(key);
- // If not generates a new one using DataSpec.uri.
- assertThat(
- CacheUtil.DEFAULT_CACHE_KEY_FACTORY.buildCacheKey(
- new DataSpec(testUri, /* position= */ 0, /* length= */ LENGTH_UNSET)))
- .isEqualTo(testUri.toString());
- }
-
@Test
public void getCachedNoData() {
Pair