From b1b8b2e6f965709c84edd124437261834d7a023a Mon Sep 17 00:00:00 2001 From: ibaker Date: Wed, 6 Mar 2024 02:22:54 -0800 Subject: [PATCH] Remove some deprecated `DataSpec` constructors PiperOrigin-RevId: 613137809 --- RELEASENOTES.md | 1 + .../androidx/media3/datasource/DataSpec.java | 236 ++---------------- .../media3/datasource/DataSpecTest.java | 128 +--------- 3 files changed, 30 insertions(+), 335 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 024fcdc796..0e212c2ea8 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -89,6 +89,7 @@ * Remove deprecated symbols: * Remove `CronetDataSourceFactory`. Use `CronetDataSource.Factory` instead. + * Remove some `DataSpec` constructors. Use `DataSpec.Builder` instead. ## 1.3 diff --git a/libraries/datasource/src/main/java/androidx/media3/datasource/DataSpec.java b/libraries/datasource/src/main/java/androidx/media3/datasource/DataSpec.java index 2b858ded0f..2da5d6a095 100644 --- a/libraries/datasource/src/main/java/androidx/media3/datasource/DataSpec.java +++ b/libraries/datasource/src/main/java/androidx/media3/datasource/DataSpec.java @@ -427,232 +427,36 @@ public final class DataSpec { * @param position {@link #position}. * @param length {@link #length}. */ + @SuppressWarnings("deprecation") public DataSpec(Uri uri, long position, long length) { + this(uri, position, length, /* key= */ null); + } + + /** + * Constructs an instance. + * + * @deprecated Use {@link Builder}. + * @param uri {@link #uri}. + * @param position {@link #position}. + * @param length {@link #length}. + * @param key {@link #key}. + */ + @Deprecated + public DataSpec(Uri uri, long position, long length, @Nullable String key) { this( uri, /* uriPositionOffset= */ 0, HTTP_METHOD_GET, - /* httpBody= */ null, - /* httpRequestHeaders= */ Collections.emptyMap(), + null, + Collections.emptyMap(), position, length, - /* key= */ null, - /* flags= */ 0, + key, + 0, /* customData= */ null); } - /** - * Constructs an instance. - * - * @deprecated Use {@link Builder}. - * @param uri {@link #uri}. - * @param flags {@link #flags}. - */ - @SuppressWarnings("deprecation") - @Deprecated - public DataSpec(Uri uri, @Flags int flags) { - this(uri, /* position= */ 0, C.LENGTH_UNSET, /* key= */ null, flags); - } - - /** - * Constructs an instance. - * - * @deprecated Use {@link Builder}. - * @param uri {@link #uri}. - * @param position {@link #position}. - * @param length {@link #length}. - * @param key {@link #key}. - */ - @SuppressWarnings("deprecation") - @Deprecated - public DataSpec(Uri uri, long position, long length, @Nullable String key) { - this(uri, position, position, length, key, /* flags= */ 0); - } - - /** - * Constructs an instance. - * - * @deprecated Use {@link Builder}. - * @param uri {@link #uri}. - * @param position {@link #position}. - * @param length {@link #length}. - * @param key {@link #key}. - * @param flags {@link #flags}. - */ - @SuppressWarnings("deprecation") - @Deprecated - public DataSpec(Uri uri, long position, long length, @Nullable String key, @Flags int flags) { - this(uri, position, position, length, key, flags); - } - - /** - * Constructs an instance. - * - * @deprecated Use {@link Builder}. - * @param uri {@link #uri}. - * @param position {@link #position}, equal to {@link #position}. - * @param length {@link #length}. - * @param key {@link #key}. - * @param flags {@link #flags}. - * @param httpRequestHeaders {@link #httpRequestHeaders} - */ - @SuppressWarnings("deprecation") - @Deprecated - public DataSpec( - Uri uri, - long position, - long length, - @Nullable String key, - @Flags int flags, - Map httpRequestHeaders) { - this( - uri, - HTTP_METHOD_GET, - /* httpBody= */ null, - position, - position, - length, - key, - flags, - httpRequestHeaders); - } - - /** - * Constructs an instance where {@link #uriPositionOffset} may be non-zero. - * - * @deprecated Use {@link Builder}. - * @param uri {@link #uri}. - * @param absoluteStreamPosition The sum of {@link #uriPositionOffset} and {@link #position}. - * @param position {@link #position}. - * @param length {@link #length}. - * @param key {@link #key}. - * @param flags {@link #flags}. - */ - @SuppressWarnings("deprecation") - @Deprecated - public DataSpec( - Uri uri, - long absoluteStreamPosition, - long position, - long length, - @Nullable String key, - @Flags int flags) { - this(uri, /* postBody= */ null, absoluteStreamPosition, position, length, key, flags); - } - - /** - * Construct a instance where {@link #uriPositionOffset} may be non-zero. The {@link #httpMethod} - * is inferred from {@code postBody}. If {@code postBody} is non-null then {@link #httpMethod} is - * set to {@link #HTTP_METHOD_POST}. If {@code postBody} is null then {@link #httpMethod} is set - * to {@link #HTTP_METHOD_GET}. - * - * @deprecated Use {@link Builder}. Note that the httpMethod must be set explicitly for the - * Builder. - * @param uri {@link #uri}. - * @param postBody {@link #httpBody} The body of the HTTP request, which is also used to infer the - * {@link #httpMethod}. - * @param absoluteStreamPosition The sum of {@link #uriPositionOffset} and {@link #position}. - * @param position {@link #position}. - * @param length {@link #length}. - * @param key {@link #key}. - * @param flags {@link #flags}. - */ - @SuppressWarnings("deprecation") - @Deprecated - public DataSpec( - Uri uri, - @Nullable byte[] postBody, - long absoluteStreamPosition, - long position, - long length, - @Nullable String key, - @Flags int flags) { - this( - uri, - /* httpMethod= */ postBody != null ? HTTP_METHOD_POST : HTTP_METHOD_GET, - /* httpBody= */ postBody, - absoluteStreamPosition, - position, - length, - key, - flags); - } - - /** - * Construct a instance where {@link #uriPositionOffset} may be non-zero. - * - * @deprecated Use {@link Builder}. - * @param uri {@link #uri}. - * @param httpMethod {@link #httpMethod}. - * @param httpBody {@link #httpBody}. - * @param absoluteStreamPosition The sum of {@link #uriPositionOffset} and {@link #position}. - * @param position {@link #position}. - * @param length {@link #length}. - * @param key {@link #key}. - * @param flags {@link #flags}. - */ - @SuppressWarnings("deprecation") - @Deprecated - public DataSpec( - Uri uri, - @HttpMethod int httpMethod, - @Nullable byte[] httpBody, - long absoluteStreamPosition, - long position, - long length, - @Nullable String key, - @Flags int flags) { - this( - uri, - httpMethod, - httpBody, - absoluteStreamPosition, - position, - length, - key, - flags, - /* httpRequestHeaders= */ Collections.emptyMap()); - } - - /** - * Construct a instance where {@link #uriPositionOffset} may be non-zero. - * - * @deprecated Use {@link Builder}. - * @param uri {@link #uri}. - * @param httpMethod {@link #httpMethod}. - * @param httpBody {@link #httpBody}. - * @param absoluteStreamPosition The sum of {@link #uriPositionOffset} and {@link #position}. - * @param position {@link #position}. - * @param length {@link #length}. - * @param key {@link #key}. - * @param flags {@link #flags}. - * @param httpRequestHeaders {@link #httpRequestHeaders}. - */ - @Deprecated - public DataSpec( - Uri uri, - @HttpMethod int httpMethod, - @Nullable byte[] httpBody, - long absoluteStreamPosition, - long position, - long length, - @Nullable String key, - @Flags int flags, - Map httpRequestHeaders) { - this( - uri, - /* uriPositionOffset= */ absoluteStreamPosition - position, - httpMethod, - httpBody, - httpRequestHeaders, - position, - length, - key, - flags, - /* customData= */ null); - } - - @SuppressWarnings("deprecation") + @SuppressWarnings("deprecation") // Setting deprecated absoluteStreamPosition field. private DataSpec( Uri uri, long uriPositionOffset, diff --git a/libraries/datasource/src/test/java/androidx/media3/datasource/DataSpecTest.java b/libraries/datasource/src/test/java/androidx/media3/datasource/DataSpecTest.java index efb2ed8b56..b2fff51914 100644 --- a/libraries/datasource/src/test/java/androidx/media3/datasource/DataSpecTest.java +++ b/libraries/datasource/src/test/java/androidx/media3/datasource/DataSpecTest.java @@ -20,7 +20,6 @@ import static com.google.common.truth.Truth.assertThat; import static junit.framework.TestCase.fail; import android.net.Uri; -import androidx.annotation.Nullable; import androidx.media3.common.C; import androidx.test.ext.junit.runners.AndroidJUnit4; import java.util.HashMap; @@ -40,60 +39,8 @@ public class DataSpecTest { DataSpec dataSpec = new DataSpec(uri); assertDefaultDataSpec(dataSpec, uri); - dataSpec = new DataSpec(uri, /* flags= */ 0); - assertDefaultDataSpec(dataSpec, uri); - dataSpec = new DataSpec(uri, /* position= */ 0, C.LENGTH_UNSET, /* key= */ null); assertDefaultDataSpec(dataSpec, uri); - - dataSpec = - new DataSpec(uri, /* position= */ 0, C.LENGTH_UNSET, /* key= */ null, /* flags= */ 0); - assertDefaultDataSpec(dataSpec, uri); - - dataSpec = - new DataSpec( - uri, - /* position= */ 0, - /* length= */ C.LENGTH_UNSET, - /* key= */ null, - /* flags= */ 0, - new HashMap<>()); - assertDefaultDataSpec(dataSpec, uri); - - dataSpec = - new DataSpec( - uri, - /* absoluteStreamPosition= */ 0, - /* position= */ 0, - /* length= */ C.LENGTH_UNSET, - null, - /* flags= */ 0); - assertDefaultDataSpec(dataSpec, uri); - - dataSpec = - new DataSpec( - uri, - DataSpec.HTTP_METHOD_GET, - /* httpBody= */ null, - /* absoluteStreamPosition= */ 0, - /* position= */ 0, - /* length= */ C.LENGTH_UNSET, - /* key= */ null, - /* flags= */ 0); - assertDefaultDataSpec(dataSpec, uri); - - dataSpec = - new DataSpec( - uri, - DataSpec.HTTP_METHOD_GET, - /* httpBody= */ null, - /* absoluteStreamPosition= */ 0, - /* position= */ 0, - /* length= */ C.LENGTH_UNSET, - /* key= */ null, - /* flags= */ 0, - new HashMap<>()); - assertDefaultDataSpec(dataSpec, uri); } @Test @@ -106,34 +53,22 @@ public class DataSpecTest { @SuppressWarnings("deprecation") @Test - public void createDataSpec_setsValues() { + public void createDataSpec_deprecatedConstructor_setsSomeValues() { Uri uri = Uri.parse("www.google.com"); - Map httpRequestHeaders = createHttpRequestHeaders(3); - byte[] httpBody = new byte[] {0, 1, 2, 3}; - DataSpec dataSpec = - new DataSpec( - uri, - DataSpec.HTTP_METHOD_POST, - httpBody, - /* absoluteStreamPosition= */ 200, - /* position= */ 150, - /* length= */ 5, - /* key= */ "key", - /* flags= */ DataSpec.FLAG_ALLOW_GZIP, - httpRequestHeaders); + DataSpec dataSpec = new DataSpec(uri, /* position= */ 150, /* length= */ 5, /* key= */ "key"); assertThat(dataSpec.uri).isEqualTo(uri); - // uriPositionOffset = absoluteStreamPosition - position - assertThat(dataSpec.uriPositionOffset).isEqualTo(50); - assertThat(dataSpec.httpMethod).isEqualTo(DataSpec.HTTP_METHOD_POST); - assertThat(dataSpec.httpBody).isEqualTo(httpBody); - assertThat(dataSpec.httpRequestHeaders).isEqualTo(httpRequestHeaders); - assertThat(dataSpec.absoluteStreamPosition).isEqualTo(200); + assertThat(dataSpec.httpMethod).isEqualTo(DataSpec.HTTP_METHOD_GET); + assertThat(dataSpec.httpBody).isNull(); + assertThat(dataSpec.httpRequestHeaders).isEmpty(); + assertThat(dataSpec.absoluteStreamPosition).isEqualTo(150); assertThat(dataSpec.position).isEqualTo(150); + // uriPositionOffset = absoluteStreamPosition - position + assertThat(dataSpec.uriPositionOffset).isEqualTo(0); assertThat(dataSpec.length).isEqualTo(5); assertThat(dataSpec.key).isEqualTo("key"); - assertThat(dataSpec.flags).isEqualTo(DataSpec.FLAG_ALLOW_GZIP); + assertThat(dataSpec.flags).isEqualTo(0); assertHttpRequestHeadersReadOnly(dataSpec); } @@ -214,51 +149,6 @@ public class DataSpecTest { assertHttpRequestHeadersReadOnly(dataSpec); } - @SuppressWarnings("deprecation") - @Test - public void createDataSpec_setsHttpMethodAndPostBody() { - Uri uri = Uri.parse("www.google.com"); - - @Nullable byte[] postBody = new byte[] {0, 1, 2, 3}; - DataSpec dataSpec = - new DataSpec( - uri, - postBody, - /* absoluteStreamPosition= */ 0, - /* position= */ 0, - /* length= */ C.LENGTH_UNSET, - /* key= */ null, - /* flags= */ 0); - assertThat(dataSpec.httpMethod).isEqualTo(DataSpec.HTTP_METHOD_POST); - assertThat(dataSpec.httpBody).isEqualTo(postBody); - - postBody = new byte[0]; - dataSpec = - new DataSpec( - uri, - postBody, - /* absoluteStreamPosition= */ 0, - /* position= */ 0, - /* length= */ C.LENGTH_UNSET, - /* key= */ null, - /* flags= */ 0); - assertThat(dataSpec.httpMethod).isEqualTo(DataSpec.HTTP_METHOD_POST); - assertThat(dataSpec.httpBody).isNull(); - - postBody = null; - dataSpec = - new DataSpec( - uri, - postBody, - /* absoluteStreamPosition= */ 0, - /* position= */ 0, - /* length= */ C.LENGTH_UNSET, - /* key= */ null, - /* flags= */ 0); - assertThat(dataSpec.httpMethod).isEqualTo(DataSpec.HTTP_METHOD_GET); - assertThat(dataSpec.httpBody).isNull(); - } - @Test public void withUri_copiesHttpRequestHeaders() { Map httpRequestHeaders = createHttpRequestHeaders(5);