Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] [android] - public api configurable base endpoint (#6309)
Browse files Browse the repository at this point in the history
add runloop to test
  • Loading branch information
tobrun authored Sep 16, 2016
1 parent 217c43d commit 9ef6544
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 54 deletions.
3 changes: 3 additions & 0 deletions include/mbgl/storage/default_file_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class DefaultFileSource : public FileSource {
bool supportsOptionalRequests() const override {
return true;
}

void setAPIBaseURL(const std::string&);
std::string getAPIBaseURL() const;

void setAccessToken(const std::string&);
std::string getAccessToken() const;
Expand Down
5 changes: 5 additions & 0 deletions include/mbgl/storage/online_file_source.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <mbgl/storage/file_source.hpp>
#include <mbgl/util/constants.hpp>

namespace mbgl {

Expand All @@ -9,6 +10,9 @@ class OnlineFileSource : public FileSource {
OnlineFileSource();
~OnlineFileSource() override;

void setAPIBaseURL(const std::string& t) { apiBaseURL = t; }
std::string getAPIBaseURL() const { return apiBaseURL; }

void setAccessToken(const std::string& t) { accessToken = t; }
std::string getAccessToken() const { return accessToken; }

Expand All @@ -20,6 +24,7 @@ class OnlineFileSource : public FileSource {
class Impl;
const std::unique_ptr<Impl> impl;
std::string accessToken;
std::string apiBaseURL = mbgl::util::API_BASE_URL;
};

} // namespace mbgl
2 changes: 2 additions & 0 deletions include/mbgl/util/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ constexpr UnitBezier DEFAULT_TRANSITION_EASE = { 0, 0, 0.25, 1 };

constexpr int DEFAULT_RATE_LIMIT_TIMEOUT = 5;

constexpr const char* API_BASE_URL = "https://api.mapbox.com";

} // namespace util

namespace debug {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private HTTPRequest(long nativePtr, String resourceUrl, String userAgent, String

HttpUrl httpUrl = HttpUrl.parse(resourceUrl);
final String host = httpUrl.host().toLowerCase(MapboxConstants.MAPBOX_LOCALE);
if (host.equals("mapbox.com") || host.endsWith(".mapbox.com")) {
if (host.equals("mapbox.com") || host.endsWith(".mapbox.com") || host.equals("mapbox.cn") || host.endsWith(".mapbox.cn")) {
if (httpUrl.querySize() == 0) {
resourceUrl = resourceUrl + "?";
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ private void setInitialState(MapboxMapOptions options) {
mMyLocationView.setTilt(position.tilt);
}

// api base url
String apiBaseUrl = options.getApiBaseUrl();
if (!TextUtils.isEmpty(apiBaseUrl)) {
setApiBaseUrl(apiBaseUrl);
}

// access token
String accessToken = options.getAccessToken();
if (!TextUtils.isEmpty(accessToken)) {
Expand Down Expand Up @@ -877,6 +883,15 @@ public String getStyleUrl() {
return mStyleUrl;
}

//
// API Base URL
//

@UiThread
void setApiBaseUrl(@NonNull String baseUrl) {
mNativeMapView.setApiBaseUrl(baseUrl);
}

//
// Access token
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.VectorDrawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.ColorInt;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Gravity;
Expand Down Expand Up @@ -79,6 +76,7 @@ public class MapboxMapOptions implements Parcelable {
private int myLocationAccuracyTintColor;
private int myLocationAccuracyAlpha;

private String apiBaseUrl;
private String style;
@Deprecated
private String accessToken;
Expand Down Expand Up @@ -123,12 +121,12 @@ private MapboxMapOptions(Parcel in) {
}

Bitmap foregroundBearingBitmap = in.readParcelable(getClass().getClassLoader());
if(foregroundBearingBitmap!=null) {
if (foregroundBearingBitmap != null) {
myLocationForegroundBearingDrawable = new BitmapDrawable(foregroundBearingBitmap);
}

Bitmap backgroundBitmap = in.readParcelable(getClass().getClassLoader());
if(backgroundBitmap!=null){
if (backgroundBitmap != null) {
myLocationBackgroundDrawable = new BitmapDrawable(backgroundBitmap);
}

Expand All @@ -140,6 +138,7 @@ private MapboxMapOptions(Parcel in) {

style = in.readString();
accessToken = in.readString();
apiBaseUrl = in.readString();
}

public static Bitmap getBitmapFromDrawable(Drawable drawable) {
Expand Down Expand Up @@ -172,6 +171,7 @@ public static MapboxMapOptions createFromAttributes(@NonNull Context context, @N

mapboxMapOptions.accessToken(typedArray.getString(R.styleable.MapView_access_token));
mapboxMapOptions.styleUrl(typedArray.getString(R.styleable.MapView_style_url));
mapboxMapOptions.apiBaseUrl(typedArray.getString(R.styleable.MapView_api_base_url));

mapboxMapOptions.zoomGesturesEnabled(typedArray.getBoolean(R.styleable.MapView_zoom_enabled, true));
mapboxMapOptions.scrollGesturesEnabled(typedArray.getBoolean(R.styleable.MapView_scroll_enabled, true));
Expand Down Expand Up @@ -237,6 +237,17 @@ public static MapboxMapOptions createFromAttributes(@NonNull Context context, @N
return mapboxMapOptions;
}

/**
* Specifies the URL used for API endpoint.
*
* @param apiBaseUrl The base of our API endpoint
* @return This
*/
public MapboxMapOptions apiBaseUrl(String apiBaseUrl) {
this.apiBaseUrl = apiBaseUrl;
return this;
}

/**
* Specifies a the initial camera position for the map view.
*
Expand Down Expand Up @@ -583,6 +594,15 @@ public MapboxMapOptions myLocationAccuracyAlpha(@IntRange(from = 0, to = 255) in
return this;
}

/**
* Get the current configured API endpoint base URL.
*
* @return Base URL to be used API endpoint.
*/
public String getApiBaseUrl() {
return apiBaseUrl;
}

/**
* Get the current configured initial camera position for a map view.
*
Expand Down Expand Up @@ -914,6 +934,7 @@ public void writeToParcel(Parcel dest, int flags) {

dest.writeString(style);
dest.writeString(accessToken);
dest.writeString(apiBaseUrl);
}

@Override
Expand Down Expand Up @@ -957,6 +978,8 @@ public boolean equals(Object o) {
if (!Arrays.equals(myLocationBackgroundPadding, options.myLocationBackgroundPadding))
return false;
if (style != null ? !style.equals(options.style) : options.style != null) return false;
if (apiBaseUrl != null ? !apiBaseUrl.equals(options.apiBaseUrl) : options.apiBaseUrl != null)
return false;
return accessToken != null ? accessToken.equals(options.accessToken) : options.accessToken == null;

}
Expand Down Expand Up @@ -993,6 +1016,7 @@ public int hashCode() {
result = 31 * result + myLocationAccuracyAlpha;
result = 31 * result + (style != null ? style.hashCode() : 0);
result = 31 * result + (accessToken != null ? accessToken.hashCode() : 0);
result = 31 * result + (apiBaseUrl != null ? apiBaseUrl.hashCode() : 0);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,10 @@ public void scheduleTakeSnapshot() {
nativeScheduleTakeSnapshot(mNativeMapViewPtr);
}

public void setApiBaseUrl(String baseUrl) {
nativeSetAPIBaseURL(mNativeMapViewPtr, baseUrl);
}

//
// Callbacks
//
Expand Down Expand Up @@ -721,4 +725,6 @@ private native void nativeSetVisibleCoordinateBounds(long mNativeMapViewPtr, Lat
private native Feature[] nativeQueryRenderedFeaturesForPoint(long nativeMapViewPtr, float x, float y, String[] layerIds);

private native Feature[] nativeQueryRenderedFeaturesForBox(long mNativeMapViewPtr, float left, float top, float right, float bottom, String[] layerIds);

private native void nativeSetAPIBaseURL(long nativeMapViewPtr, String baseUrl);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<!--Configuration-->
<attr name="access_token" format="string" />
<attr name="style_url" format="string" />
<attr name="api_base_url" format="string" />

<!--Camera-->
<attr name="center_longitude" format="float" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
Expand Down
10 changes: 9 additions & 1 deletion platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,13 @@ jni::jobject* nativeGetClasses(JNIEnv *env, jni::jobject* obj, jlong nativeMapVi
return std_vector_string_to_jobject(env, nativeMapView->getMap().getClasses());
}

void nativeSetAPIBaseURL(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* url) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetAPIBaseURL");
assert(nativeMapViewPtr != 0);
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
nativeMapView->getFileSource().setAPIBaseURL(std_string_from_jstring(env, url));
}

void nativeSetStyleUrl(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* url) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetStyleURL");
assert(nativeMapViewPtr != 0);
Expand Down Expand Up @@ -1836,7 +1843,8 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
MAKE_NATIVE_METHOD(nativeSetContentPadding, "(JDDDD)V"),
MAKE_NATIVE_METHOD(nativeScheduleTakeSnapshot, "(J)V"),
MAKE_NATIVE_METHOD(nativeQueryRenderedFeaturesForPoint, "(JFF[Ljava/lang/String;)[Lcom/mapbox/services/commons/geojson/Feature;"),
MAKE_NATIVE_METHOD(nativeQueryRenderedFeaturesForBox, "(JFFFF[Ljava/lang/String;)[Lcom/mapbox/services/commons/geojson/Feature;")
MAKE_NATIVE_METHOD(nativeQueryRenderedFeaturesForBox, "(JFFFF[Ljava/lang/String;)[Lcom/mapbox/services/commons/geojson/Feature;"),
MAKE_NATIVE_METHOD(nativeSetAPIBaseURL, "(JLjava/lang/String;)V")
);

// Offline begin
Expand Down
16 changes: 16 additions & 0 deletions platform/default/default_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ class DefaultFileSource::Impl {
Impl(const std::string& cachePath, uint64_t maximumCacheSize)
: offlineDatabase(cachePath, maximumCacheSize) {
}

void setAPIBaseURL(const std::string& url) {
onlineFileSource.setAPIBaseURL(url);
}

std::string getAPIBaseURL() const{
return onlineFileSource.getAPIBaseURL();
}

void setAccessToken(const std::string& accessToken) {
onlineFileSource.setAccessToken(accessToken);
Expand Down Expand Up @@ -151,6 +159,14 @@ DefaultFileSource::DefaultFileSource(const std::string& cachePath,

DefaultFileSource::~DefaultFileSource() = default;

void DefaultFileSource::setAPIBaseURL(const std::string& baseURL) {
thread->invokeSync(&Impl::setAPIBaseURL, baseURL);
}

std::string DefaultFileSource::getAPIBaseURL() const {
return thread->invokeSync(&Impl::getAPIBaseURL);
}

void DefaultFileSource::setAccessToken(const std::string& accessToken) {
thread->invokeSync(&Impl::setAccessToken, accessToken);
}
Expand Down
10 changes: 5 additions & 5 deletions platform/default/online_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,24 +160,24 @@ std::unique_ptr<AsyncRequest> OnlineFileSource::request(const Resource& resource
break;

case Resource::Kind::Style:
res.url = mbgl::util::mapbox::normalizeStyleURL(resource.url, accessToken);
res.url = mbgl::util::mapbox::normalizeStyleURL(apiBaseURL, resource.url, accessToken);
break;

case Resource::Kind::Source:
res.url = util::mapbox::normalizeSourceURL(resource.url, accessToken);
res.url = util::mapbox::normalizeSourceURL(apiBaseURL, resource.url, accessToken);
break;

case Resource::Kind::Glyphs:
res.url = util::mapbox::normalizeGlyphsURL(resource.url, accessToken);
res.url = util::mapbox::normalizeGlyphsURL(apiBaseURL, resource.url, accessToken);
break;

case Resource::Kind::SpriteImage:
case Resource::Kind::SpriteJSON:
res.url = util::mapbox::normalizeSpriteURL(resource.url, accessToken);
res.url = util::mapbox::normalizeSpriteURL(apiBaseURL, resource.url, accessToken);
break;

case Resource::Kind::Tile:
res.url = util::mapbox::normalizeTileURL(resource.url, accessToken);
res.url = util::mapbox::normalizeTileURL(apiBaseURL, resource.url, accessToken);
break;
}

Expand Down
Loading

0 comments on commit 9ef6544

Please sign in to comment.