Make OkHttpDataSource userAgent parameter optional

This commit is contained in:
Alex Birkett 2017-06-23 18:56:40 +02:00 committed by Oliver Woodman
parent fd9b162d0f
commit d4e598f417
2 changed files with 20 additions and 12 deletions

View File

@ -16,6 +16,8 @@
package com.google.android.exoplayer2.ext.okhttp; package com.google.android.exoplayer2.ext.okhttp;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.DataSourceException; import com.google.android.exoplayer2.upstream.DataSourceException;
import com.google.android.exoplayer2.upstream.DataSpec; import com.google.android.exoplayer2.upstream.DataSpec;
@ -67,11 +69,11 @@ public class OkHttpDataSource implements HttpDataSource {
/** /**
* @param callFactory A {@link Call.Factory} (typically an {@link okhttp3.OkHttpClient}) for use * @param callFactory A {@link Call.Factory} (typically an {@link okhttp3.OkHttpClient}) for use
* by the source. * by the source.
* @param userAgent The User-Agent string that should be used. * @param userAgent An optional User-Agent string that should be used.
* @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the * @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the
* predicate then a InvalidContentTypeException} is thrown from {@link #open(DataSpec)}. * predicate then a InvalidContentTypeException} is thrown from {@link #open(DataSpec)}.
*/ */
public OkHttpDataSource(Call.Factory callFactory, String userAgent, public OkHttpDataSource(Call.Factory callFactory, @Nullable String userAgent,
Predicate<String> contentTypePredicate) { Predicate<String> contentTypePredicate) {
this(callFactory, userAgent, contentTypePredicate, null); this(callFactory, userAgent, contentTypePredicate, null);
} }
@ -79,13 +81,13 @@ public class OkHttpDataSource implements HttpDataSource {
/** /**
* @param callFactory A {@link Call.Factory} (typically an {@link okhttp3.OkHttpClient}) for use * @param callFactory A {@link Call.Factory} (typically an {@link okhttp3.OkHttpClient}) for use
* by the source. * by the source.
* @param userAgent The User-Agent string that should be used. * @param userAgent An optional User-Agent string that should be used.
* @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the * @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the
* predicate then a {@link InvalidContentTypeException} is thrown from * predicate then a {@link InvalidContentTypeException} is thrown from
* {@link #open(DataSpec)}. * {@link #open(DataSpec)}.
* @param listener An optional listener. * @param listener An optional listener.
*/ */
public OkHttpDataSource(Call.Factory callFactory, String userAgent, public OkHttpDataSource(Call.Factory callFactory, @Nullable String userAgent,
Predicate<String> contentTypePredicate, TransferListener<? super OkHttpDataSource> listener) { Predicate<String> contentTypePredicate, TransferListener<? super OkHttpDataSource> listener) {
this(callFactory, userAgent, contentTypePredicate, listener, null, null); this(callFactory, userAgent, contentTypePredicate, listener, null, null);
} }
@ -93,7 +95,7 @@ public class OkHttpDataSource implements HttpDataSource {
/** /**
* @param callFactory A {@link Call.Factory} (typically an {@link okhttp3.OkHttpClient}) for use * @param callFactory A {@link Call.Factory} (typically an {@link okhttp3.OkHttpClient}) for use
* by the source. * by the source.
* @param userAgent The User-Agent string that should be used. * @param userAgent An optional User-Agent string that should be used.
* @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the * @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the
* predicate then a {@link InvalidContentTypeException} is thrown from * predicate then a {@link InvalidContentTypeException} is thrown from
* {@link #open(DataSpec)}. * {@link #open(DataSpec)}.
@ -102,11 +104,11 @@ public class OkHttpDataSource implements HttpDataSource {
* @param defaultRequestProperties The optional default {@link RequestProperties} to be sent to * @param defaultRequestProperties The optional default {@link RequestProperties} to be sent to
* the server as HTTP headers on every request. * the server as HTTP headers on every request.
*/ */
public OkHttpDataSource(Call.Factory callFactory, String userAgent, public OkHttpDataSource(Call.Factory callFactory, @Nullable String userAgent,
Predicate<String> contentTypePredicate, TransferListener<? super OkHttpDataSource> listener, Predicate<String> contentTypePredicate, TransferListener<? super OkHttpDataSource> listener,
CacheControl cacheControl, RequestProperties defaultRequestProperties) { CacheControl cacheControl, RequestProperties defaultRequestProperties) {
this.callFactory = Assertions.checkNotNull(callFactory); this.callFactory = Assertions.checkNotNull(callFactory);
this.userAgent = Assertions.checkNotEmpty(userAgent); this.userAgent = userAgent;
this.contentTypePredicate = contentTypePredicate; this.contentTypePredicate = contentTypePredicate;
this.listener = listener; this.listener = listener;
this.cacheControl = cacheControl; this.cacheControl = cacheControl;
@ -280,7 +282,10 @@ public class OkHttpDataSource implements HttpDataSource {
} }
builder.addHeader("Range", rangeRequest); builder.addHeader("Range", rangeRequest);
} }
builder.addHeader("User-Agent", userAgent); if (userAgent != null) {
builder.addHeader("User-Agent", userAgent);
}
if (!allowGzip) { if (!allowGzip) {
builder.addHeader("Accept-Encoding", "identity"); builder.addHeader("Accept-Encoding", "identity");
} }

View File

@ -15,6 +15,8 @@
*/ */
package com.google.android.exoplayer2.ext.okhttp; package com.google.android.exoplayer2.ext.okhttp;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.HttpDataSource; import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.upstream.HttpDataSource.BaseFactory; import com.google.android.exoplayer2.upstream.HttpDataSource.BaseFactory;
@ -36,10 +38,10 @@ public final class OkHttpDataSourceFactory extends BaseFactory {
/** /**
* @param callFactory A {@link Call.Factory} (typically an {@link okhttp3.OkHttpClient}) for use * @param callFactory A {@link Call.Factory} (typically an {@link okhttp3.OkHttpClient}) for use
* by the sources created by the factory. * by the sources created by the factory.
* @param userAgent The User-Agent string that should be used. * @param userAgent An optional User-Agent string that should be used.
* @param listener An optional listener. * @param listener An optional listener.
*/ */
public OkHttpDataSourceFactory(Call.Factory callFactory, String userAgent, public OkHttpDataSourceFactory(Call.Factory callFactory, @Nullable String userAgent,
TransferListener<? super DataSource> listener) { TransferListener<? super DataSource> listener) {
this(callFactory, userAgent, listener, null); this(callFactory, userAgent, listener, null);
} }
@ -47,11 +49,12 @@ public final class OkHttpDataSourceFactory extends BaseFactory {
/** /**
* @param callFactory A {@link Call.Factory} (typically an {@link okhttp3.OkHttpClient}) for use * @param callFactory A {@link Call.Factory} (typically an {@link okhttp3.OkHttpClient}) for use
* by the sources created by the factory. * by the sources created by the factory.
* @param userAgent The User-Agent string that should be used. * @param userAgent An optional User-Agent string that should be used.
* @param listener An optional listener. * @param listener An optional listener.
* @param cacheControl An optional {@link CacheControl} for setting the Cache-Control header. * @param cacheControl An optional {@link CacheControl} for setting the Cache-Control header.
*/ */
public OkHttpDataSourceFactory(Call.Factory callFactory, String userAgent, public OkHttpDataSourceFactory(Call.Factory callFactory,
@Nullable String userAgent,
TransferListener<? super DataSource> listener, CacheControl cacheControl) { TransferListener<? super DataSource> listener, CacheControl cacheControl) {
this.callFactory = callFactory; this.callFactory = callFactory;
this.userAgent = userAgent; this.userAgent = userAgent;