From ba1ebe7844269d7f6df5570dc48ae719c3bba58b Mon Sep 17 00:00:00 2001 From: olly Date: Sat, 9 Jan 2021 14:28:57 +0000 Subject: [PATCH] Clarify how cookies are handled (or not) in the demo app 1. Remove cookie manager logic from PlayerActivity, since it has no effect when Cronet is used (which is now the default) 2. Add toggle in DemoUtil to use Cronet or the default network stack. Configure the cookie manager only when using the default network stack PiperOrigin-RevId: 350922671 --- .../android/exoplayer2/demo/DemoUtil.java | 30 +++++++++++++++---- .../exoplayer2/demo/PlayerActivity.java | 13 -------- 2 files changed, 25 insertions(+), 18 deletions(-) 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 704d819db4..080387db7e 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 @@ -30,6 +30,7 @@ import com.google.android.exoplayer2.offline.DownloadManager; import com.google.android.exoplayer2.ui.DownloadNotificationHelper; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; +import com.google.android.exoplayer2.upstream.DefaultHttpDataSource; import com.google.android.exoplayer2.upstream.HttpDataSource; import com.google.android.exoplayer2.upstream.cache.Cache; import com.google.android.exoplayer2.upstream.cache.CacheDataSource; @@ -38,6 +39,9 @@ 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; import java.util.concurrent.Executors; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; @@ -46,6 +50,15 @@ 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). + * + *

If set to false, the platform's default network stack is used with a {@link CookieManager} + * configured in {@link #getHttpDataSourceFactory}. + */ + private static final boolean USE_CRONET_FOR_NETWORKING = true; + private static final String USER_AGENT = "ExoPlayerDemo/" + ExoPlayerLibraryInfo.VERSION @@ -87,11 +100,18 @@ public final class DemoUtil { public static synchronized HttpDataSource.Factory getHttpDataSourceFactory(Context context) { if (httpDataSourceFactory == null) { - context = context.getApplicationContext(); - CronetEngineWrapper cronetEngineWrapper = - new CronetEngineWrapper(context, USER_AGENT, /* preferGMSCoreCronet= */ false); - httpDataSourceFactory = - new CronetDataSource.Factory(cronetEngineWrapper, Executors.newSingleThreadExecutor()); + if (USE_CRONET_FOR_NETWORKING) { + context = context.getApplicationContext(); + CronetEngineWrapper cronetEngineWrapper = + new CronetEngineWrapper(context, USER_AGENT, /* preferGMSCoreCronet= */ false); + httpDataSourceFactory = + new CronetDataSource.Factory(cronetEngineWrapper, Executors.newSingleThreadExecutor()); + } else { + CookieManager cookieManager = new CookieManager(); + cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER); + CookieHandler.setDefault(cookieManager); + httpDataSourceFactory = new DefaultHttpDataSource.Factory().setUserAgent(USER_AGENT); + } } return httpDataSourceFactory; } diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java index bb2d50ec5b..5fb342be43 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java @@ -58,9 +58,6 @@ import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.util.ErrorMessageProvider; import com.google.android.exoplayer2.util.EventLogger; import com.google.android.exoplayer2.util.Util; -import java.net.CookieHandler; -import java.net.CookieManager; -import java.net.CookiePolicy; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -77,13 +74,6 @@ public class PlayerActivity extends AppCompatActivity private static final String KEY_POSITION = "position"; private static final String KEY_AUTO_PLAY = "auto_play"; - private static final CookieManager DEFAULT_COOKIE_MANAGER; - - static { - DEFAULT_COOKIE_MANAGER = new CookieManager(); - DEFAULT_COOKIE_MANAGER.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER); - } - protected StyledPlayerView playerView; protected LinearLayout debugRootView; protected TextView debugTextView; @@ -111,9 +101,6 @@ public class PlayerActivity extends AppCompatActivity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); dataSourceFactory = DemoUtil.getDataSourceFactory(/* context= */ this); - if (CookieHandler.getDefault() != DEFAULT_COOKIE_MANAGER) { - CookieHandler.setDefault(DEFAULT_COOKIE_MANAGER); - } setContentView(); debugRootView = findViewById(R.id.controls_root);