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
This commit is contained in:
olly 2021-01-09 14:28:57 +00:00 committed by Ian Baker
parent 2a5f6d8f62
commit ba1ebe7844
2 changed files with 25 additions and 18 deletions

View File

@ -30,6 +30,7 @@ import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.ui.DownloadNotificationHelper; import com.google.android.exoplayer2.ui.DownloadNotificationHelper;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; 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.HttpDataSource;
import com.google.android.exoplayer2.upstream.cache.Cache; import com.google.android.exoplayer2.upstream.cache.Cache;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource; 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 com.google.android.exoplayer2.util.Log;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull; 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"; 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 = private static final String USER_AGENT =
"ExoPlayerDemo/" "ExoPlayerDemo/"
+ ExoPlayerLibraryInfo.VERSION + ExoPlayerLibraryInfo.VERSION
@ -87,11 +100,18 @@ public final class DemoUtil {
public static synchronized HttpDataSource.Factory getHttpDataSourceFactory(Context context) { public static synchronized HttpDataSource.Factory getHttpDataSourceFactory(Context context) {
if (httpDataSourceFactory == null) { if (httpDataSourceFactory == null) {
context = context.getApplicationContext(); if (USE_CRONET_FOR_NETWORKING) {
CronetEngineWrapper cronetEngineWrapper = context = context.getApplicationContext();
new CronetEngineWrapper(context, USER_AGENT, /* preferGMSCoreCronet= */ false); CronetEngineWrapper cronetEngineWrapper =
httpDataSourceFactory = new CronetEngineWrapper(context, USER_AGENT, /* preferGMSCoreCronet= */ false);
new CronetDataSource.Factory(cronetEngineWrapper, Executors.newSingleThreadExecutor()); 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; return httpDataSourceFactory;
} }

View File

@ -58,9 +58,6 @@ import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.util.ErrorMessageProvider; import com.google.android.exoplayer2.util.ErrorMessageProvider;
import com.google.android.exoplayer2.util.EventLogger; import com.google.android.exoplayer2.util.EventLogger;
import com.google.android.exoplayer2.util.Util; 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.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; 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_POSITION = "position";
private static final String KEY_AUTO_PLAY = "auto_play"; 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 StyledPlayerView playerView;
protected LinearLayout debugRootView; protected LinearLayout debugRootView;
protected TextView debugTextView; protected TextView debugTextView;
@ -111,9 +101,6 @@ public class PlayerActivity extends AppCompatActivity
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
dataSourceFactory = DemoUtil.getDataSourceFactory(/* context= */ this); dataSourceFactory = DemoUtil.getDataSourceFactory(/* context= */ this);
if (CookieHandler.getDefault() != DEFAULT_COOKIE_MANAGER) {
CookieHandler.setDefault(DEFAULT_COOKIE_MANAGER);
}
setContentView(); setContentView();
debugRootView = findViewById(R.id.controls_root); debugRootView = findViewById(R.id.controls_root);