Commit ba1ebe78 by olly Committed by Ian Baker

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
parent 2a5f6d8f
......@@ -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).
*
* <p>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;
}
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment