[android] - add resource usage to HttpRequest, add offline query parameter flag to http request (#14837)
This commit is contained in:
@@ -15,17 +15,18 @@ public interface HttpRequest {
|
||||
/**
|
||||
* Executes the request.
|
||||
*
|
||||
* @param httpRequest callback to be invoked when we receive a response
|
||||
* @param nativePtr the pointer associated to the request
|
||||
* @param resourceUrl the resource url to download
|
||||
* @param etag http header, identifier for a specific version of a resource
|
||||
* @param modified http header, used to determine if a resource hasn't been modified since
|
||||
* @param httpRequest callback to be invoked when we receive a response
|
||||
* @param nativePtr the pointer associated to the request
|
||||
* @param resourceUrl the resource url to download
|
||||
* @param etag http header, identifier for a specific version of a resource
|
||||
* @param modified http header, used to determine if a resource hasn't been modified since
|
||||
* @param offlineUsage flag to indicate a resource will be used for offline, appends offline=true as a query parameter
|
||||
*/
|
||||
void executeRequest(HttpResponder httpRequest, long nativePtr, String resourceUrl,
|
||||
String etag, String modified);
|
||||
String etag, String modified, boolean offlineUsage);
|
||||
|
||||
/**
|
||||
* Cancels the request.
|
||||
*/
|
||||
*/
|
||||
void cancelRequest();
|
||||
}
|
||||
|
||||
@@ -15,9 +15,10 @@ public class HttpRequestUrl {
|
||||
* @param host the host used as endpoint
|
||||
* @param resourceUrl the resource to download
|
||||
* @param querySize the query size of the resource request
|
||||
* @param offline the type of resource, either offline or online
|
||||
* @return the adapted resource url
|
||||
*/
|
||||
public static String buildResourceUrl(@NonNull String host, String resourceUrl, int querySize) {
|
||||
public static String buildResourceUrl(@NonNull String host, String resourceUrl, int querySize, boolean offline) {
|
||||
if (isValidMapboxEndpoint(host)) {
|
||||
if (querySize == 0) {
|
||||
resourceUrl = resourceUrl + "?";
|
||||
@@ -25,6 +26,10 @@ public class HttpRequestUrl {
|
||||
resourceUrl = resourceUrl + "&";
|
||||
}
|
||||
resourceUrl = resourceUrl + "sku=" + Mapbox.getSkuToken();
|
||||
|
||||
if (offline) {
|
||||
resourceUrl = resourceUrl + "&offline=true";
|
||||
}
|
||||
}
|
||||
return resourceUrl;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ public class NativeHttpRequest implements HttpResponder {
|
||||
private long nativePtr;
|
||||
|
||||
@Keep
|
||||
private NativeHttpRequest(long nativePtr, String resourceUrl, String etag, String modified) {
|
||||
private NativeHttpRequest(long nativePtr, String resourceUrl, String etag, String modified, boolean offlineUsage) {
|
||||
this.nativePtr = nativePtr;
|
||||
|
||||
if (resourceUrl.startsWith("local://")) {
|
||||
@@ -26,7 +26,7 @@ public class NativeHttpRequest implements HttpResponder {
|
||||
executeLocalRequest(resourceUrl);
|
||||
return;
|
||||
}
|
||||
httpRequest.executeRequest(this, nativePtr, resourceUrl, etag, modified);
|
||||
httpRequest.executeRequest(this, nativePtr, resourceUrl, etag, modified, offlineUsage);
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
|
||||
@@ -53,7 +53,7 @@ public class HttpRequestImpl implements HttpRequest {
|
||||
|
||||
@Override
|
||||
public void executeRequest(HttpResponder httpRequest, long nativePtr, @NonNull String resourceUrl,
|
||||
@NonNull String etag, @NonNull String modified) {
|
||||
@NonNull String etag, @NonNull String modified, boolean offlineUsage) {
|
||||
OkHttpCallback callback = new OkHttpCallback(httpRequest);
|
||||
try {
|
||||
HttpUrl httpUrl = HttpUrl.parse(resourceUrl);
|
||||
@@ -63,7 +63,7 @@ public class HttpRequestImpl implements HttpRequest {
|
||||
}
|
||||
|
||||
final String host = httpUrl.host().toLowerCase(MapboxConstants.MAPBOX_LOCALE);
|
||||
resourceUrl = HttpRequestUrl.buildResourceUrl(host, resourceUrl, httpUrl.querySize());
|
||||
resourceUrl = HttpRequestUrl.buildResourceUrl(host, resourceUrl, httpUrl.querySize(), offlineUsage);
|
||||
|
||||
final Request.Builder builder = new Request.Builder()
|
||||
.url(resourceUrl)
|
||||
|
||||
@@ -1,17 +1,36 @@
|
||||
package com.mapbox.mapboxsdk;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class MapboxInjector {
|
||||
|
||||
public static void inject(Context context, String accessToken) {
|
||||
private static final String FIELD_INSTANCE = "INSTANCE";
|
||||
private static final String FIELD_ACCOUNTS = "accounts";
|
||||
|
||||
public static void inject(@NonNull Context context, @NonNull String accessToken) {
|
||||
inject(context, accessToken, null);
|
||||
}
|
||||
|
||||
public static void inject(@NonNull Context context, @NonNull String accessToken, @Nullable String skuToken) {
|
||||
Mapbox mapbox = new Mapbox(context, accessToken);
|
||||
try {
|
||||
Field field = Mapbox.class.getDeclaredField("INSTANCE");
|
||||
field.setAccessible(true);
|
||||
field.set(mapbox, mapbox);
|
||||
Field instance = Mapbox.class.getDeclaredField(FIELD_INSTANCE);
|
||||
instance.setAccessible(true);
|
||||
instance.set(mapbox, mapbox);
|
||||
|
||||
Field accounts = Mapbox.class.getDeclaredField(FIELD_ACCOUNTS);
|
||||
accounts.setAccessible(true);
|
||||
|
||||
AccountsManager manager = mock(AccountsManager.class);
|
||||
when(manager.getSkuToken()).thenReturn(skuToken);
|
||||
accounts.set(mapbox, manager);
|
||||
} catch (Exception exception) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
@@ -19,7 +38,7 @@ public class MapboxInjector {
|
||||
|
||||
public static void clear() {
|
||||
try {
|
||||
Field field = Mapbox.class.getDeclaredField("INSTANCE");
|
||||
Field field = Mapbox.class.getDeclaredField(FIELD_INSTANCE);
|
||||
field.setAccessible(true);
|
||||
field.set(field, null);
|
||||
} catch (Exception exception) {
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.mapbox.mapboxsdk.module.http
|
||||
|
||||
import com.mapbox.mapboxsdk.MapboxInjector
|
||||
import com.mapbox.mapboxsdk.http.HttpRequestUrl
|
||||
import io.mockk.mockk
|
||||
import junit.framework.Assert.assertEquals
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class HttpRequestUrlTest {
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MapboxInjector.inject(mockk(relaxed = true), "pk.foobar", "foobar")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOfflineFlagMapboxCom() {
|
||||
val expected = "http://mapbox.com/path/of/no/return.pbf?sku=foobar&offline=true"
|
||||
val actual = HttpRequestUrl.buildResourceUrl("mapbox.com", "http://mapbox.com/path/of/no/return.pbf", 0, true)
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOfflineFlagMapboxCn() {
|
||||
val expected = "http://mapbox.cn/path/of/no/return.pbf?sku=foobar&offline=true"
|
||||
val actual = HttpRequestUrl.buildResourceUrl("mapbox.cn", "http://mapbox.cn/path/of/no/return.pbf", 0, true)
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOfflineFlagInvalidHost() {
|
||||
val expected = "http://foobar.com/path/of/no/return.pbf"
|
||||
val actual = HttpRequestUrl.buildResourceUrl("foobar.com", "http://foobar.com/path/of/no/return.pbf", 0, true)
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOnlineMapboxCom() {
|
||||
val expected = "http://mapbox.com/path/of/no/return.pbf?sku=foobar"
|
||||
val actual = HttpRequestUrl.buildResourceUrl("mapbox.com", "http://mapbox.com/path/of/no/return.pbf", 0, false)
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOnlineMapboxCn() {
|
||||
val expected = "http://mapbox.cn/path/of/no/return.pbf?sku=foobar"
|
||||
val actual = HttpRequestUrl.buildResourceUrl("mapbox.cn", "http://mapbox.cn/path/of/no/return.pbf", 0, false)
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOnlineInvalidHost() {
|
||||
val expected = "http://foobar.com/path/of/no/return.pbf"
|
||||
val actual = HttpRequestUrl.buildResourceUrl("foobar.com", "http://foobar.com/path/of/no/return.pbf", 0, false)
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
MapboxInjector.clear()
|
||||
}
|
||||
}
|
||||
@@ -81,14 +81,17 @@ HTTPRequest::HTTPRequest(jni::JNIEnv& env, const Resource& resource_, FileSource
|
||||
|
||||
static auto& javaClass = jni::Class<HTTPRequest>::Singleton(env);
|
||||
static auto constructor =
|
||||
javaClass.GetConstructor<jni::jlong, jni::String, jni::String, jni::String>(env);
|
||||
javaClass.GetConstructor<jni::jlong, jni::String, jni::String, jni::String, jni::jboolean>(env);
|
||||
|
||||
javaRequest = jni::NewGlobal(env,
|
||||
javaClass.New(env, constructor,
|
||||
reinterpret_cast<jlong>(this),
|
||||
jni::Make<jni::String>(env, resource.url),
|
||||
jni::Make<jni::String>(env, etagStr),
|
||||
jni::Make<jni::String>(env, modifiedStr)));
|
||||
jni::Make<jni::String>(env, modifiedStr),
|
||||
(jboolean) (resource_.usage == Resource::Usage::Offline)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
HTTPRequest::~HTTPRequest() {
|
||||
|
||||
Reference in New Issue
Block a user