diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 482dfc626a..0b0225d3ee 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -42,6 +42,9 @@ * Leanback extension: * Cast Extension: * Test Utilities: +* Demo app: + * Use `HttpEngineDataSource` as the `HttpDataSource` when supported by the + device. * Remove deprecated symbols: ## 1.4 diff --git a/demos/main/src/main/java/androidx/media3/demo/main/DemoUtil.java b/demos/main/src/main/java/androidx/media3/demo/main/DemoUtil.java index f64e78c8c7..6a74f1ec5f 100644 --- a/demos/main/src/main/java/androidx/media3/demo/main/DemoUtil.java +++ b/demos/main/src/main/java/androidx/media3/demo/main/DemoUtil.java @@ -16,12 +16,15 @@ package androidx.media3.demo.main; import android.content.Context; +import android.net.http.HttpEngine; +import android.os.Build; import androidx.annotation.OptIn; import androidx.media3.database.DatabaseProvider; import androidx.media3.database.StandaloneDatabaseProvider; import androidx.media3.datasource.DataSource; import androidx.media3.datasource.DefaultDataSource; import androidx.media3.datasource.DefaultHttpDataSource; +import androidx.media3.datasource.HttpEngineDataSource; import androidx.media3.datasource.cache.Cache; import androidx.media3.datasource.cache.CacheDataSource; import androidx.media3.datasource.cache.NoOpCacheEvictor; @@ -47,13 +50,14 @@ public final class DemoUtil { public static final String DOWNLOAD_NOTIFICATION_CHANNEL_ID = "download_channel"; /** - * Whether the demo application uses Cronet for networking. Note that Cronet does not provide - * automatic support for cookies (https://github.com/google/ExoPlayer/issues/5975). + * Whether the demo application uses Cronet for networking when {@link HttpEngine} is not + * supported. Note that Cronet does not provide automatic support for cookies + * (https://github.com/google/ExoPlayer/issues/5975). * - *
If set to false, the platform's default network stack is used with a {@link CookieManager} - * configured in {@link #getHttpDataSourceFactory}. + *
If set to false, the {@link DefaultHttpDataSource} is used with a {@link CookieManager} + * configured in {@link #getHttpDataSourceFactory} when {@link HttpEngine} is not supported. */ - private static final boolean USE_CRONET_FOR_NETWORKING = true; + private static final boolean ALLOW_CRONET_FOR_NETWORKING = true; private static final String TAG = "DemoUtil"; private static final String DOWNLOAD_CONTENT_DIRECTORY = "downloads"; @@ -97,26 +101,38 @@ public final class DemoUtil { } public static synchronized DataSource.Factory getHttpDataSourceFactory(Context context) { - if (httpDataSourceFactory == null) { - if (USE_CRONET_FOR_NETWORKING) { - context = context.getApplicationContext(); - @Nullable CronetEngine cronetEngine = CronetUtil.buildCronetEngine(context); - if (cronetEngine != null) { - httpDataSourceFactory = - new CronetDataSource.Factory(cronetEngine, Executors.newSingleThreadExecutor()); - } - } - if (httpDataSourceFactory == null) { - // We don't want to use Cronet, or we failed to instantiate a CronetEngine. - CookieManager cookieManager = new CookieManager(); - cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER); - CookieHandler.setDefault(cookieManager); - httpDataSourceFactory = new DefaultHttpDataSource.Factory(); + if (httpDataSourceFactory != null) { + return httpDataSourceFactory; + } + context = context.getApplicationContext(); + if (Build.VERSION.SDK_INT >= 34) { + setCookieHandler(); + HttpEngine httpEngine = new HttpEngine.Builder(context).build(); + httpDataSourceFactory = + new HttpEngineDataSource.Factory(httpEngine, Executors.newSingleThreadExecutor()); + return httpDataSourceFactory; + } + if (ALLOW_CRONET_FOR_NETWORKING) { + @Nullable CronetEngine cronetEngine = CronetUtil.buildCronetEngine(context); + if (cronetEngine != null) { + httpDataSourceFactory = + new CronetDataSource.Factory(cronetEngine, Executors.newSingleThreadExecutor()); + return httpDataSourceFactory; } } + // The device doesn't support HttpEngine or we don't want to allow Cronet, or we failed to + // instantiate a CronetEngine. + setCookieHandler(); + httpDataSourceFactory = new DefaultHttpDataSource.Factory(); return httpDataSourceFactory; } + private static void setCookieHandler() { + CookieManager cookieManager = new CookieManager(); + cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER); + CookieHandler.setDefault(cookieManager); + } + /** Returns a {@link DataSource.Factory}. */ public static synchronized DataSource.Factory getDataSourceFactory(Context context) { if (dataSourceFactory == null) {