diff options
author | Tobrun <tobrun.van.nuland@gmail.com> | 2019-06-12 16:40:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-12 16:40:59 +0200 |
commit | c2fb3cc76f238908b8c469f47ca547565bcd0242 (patch) | |
tree | e2ef680bafa8de3c2240d8aa2341f4c4899993a1 | |
parent | 844299f1c3b22eed4bf07c37052b3c2ea35e2564 (diff) | |
download | qtlocation-mapboxgl-c2fb3cc76f238908b8c469f47ca547565bcd0242.tar.gz |
[android] - add resource usage to HttpRequest, add offline query parameter flag to http request (#14837)
7 files changed, 114 insertions, 19 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HttpRequest.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HttpRequest.java index fdbb589fef..1fd4f43cd4 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HttpRequest.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HttpRequest.java @@ -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(); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HttpRequestUrl.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HttpRequestUrl.java index 6a1b641d67..706f57ce9c 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HttpRequestUrl.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HttpRequestUrl.java @@ -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; } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/NativeHttpRequest.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/NativeHttpRequest.java index 156a91d810..e59b07dc1f 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/NativeHttpRequest.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/NativeHttpRequest.java @@ -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() { diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/http/HttpRequestImpl.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/http/HttpRequestImpl.java index 441db7e945..14b76e4fb7 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/http/HttpRequestImpl.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/http/HttpRequestImpl.java @@ -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) diff --git a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxInjector.java b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxInjector.java index 46adc2007b..966c756318 100644 --- a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxInjector.java +++ b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxInjector.java @@ -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) { diff --git a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/module/http/HttpRequestUrlTest.kt b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/module/http/HttpRequestUrlTest.kt new file mode 100644 index 0000000000..994a41938b --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/module/http/HttpRequestUrlTest.kt @@ -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() + } +}
\ No newline at end of file diff --git a/platform/android/src/http_file_source.cpp b/platform/android/src/http_file_source.cpp index e7826e38a9..7066918fef 100644 --- a/platform/android/src/http_file_source.cpp +++ b/platform/android/src/http_file_source.cpp @@ -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() { |