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

Fix resource transform callback #8582

Merged
merged 3 commits into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ private FileSource(String cachePath, String apkPath) {
* The callback will be executed on the main thread once for every requested URL.
* </p>
*
* @param callback the callback to be invoked
* @param callback the callback to be invoked or null to reset
*/
public native void setResourceTransform(@NonNull final ResourceTransformCallback callback);
public native void setResourceTransform(final ResourceTransformCallback callback);

private native void initialize(String accessToken, String cachePath, String apkPath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,20 @@
android:value=".activity.FeatureOverviewActivity"/>
</activity>

<!-- Storage -->
<activity
android:name=".activity.storage.UrlTransformActivity"
android:description="@string/description_url_transform"
android:label="@string/activity_url_transform">
<meta-data
android:name="@string/category"
android:value="@string/category_storage"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>


<!-- For Instrumentation tests -->
<activity
android:name=".activity.style.RuntimeStyleTestActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.mapbox.mapboxsdk.testapp.activity.storage;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.storage.FileSource;
import com.mapbox.mapboxsdk.storage.Resource;
import com.mapbox.mapboxsdk.testapp.R;

import timber.log.Timber;

/**
* Test activity showcasing the url transform
*/
public class UrlTransformActivity extends AppCompatActivity {

private MapView mapView;

/**
* Be sure to use an isolated class so the activity is not leaked when
* the activity goes out of scope (static class in this case).
* <p>
* Alternatively, unregister the callback in {@link Activity#onDestroy()}
*/
private static final class Transform implements FileSource.ResourceTransformCallback {
@Override
public String onURL(@Resource.Kind int kind, String url) {
Timber.i("[%s] Could be rewriting %s", Thread.currentThread().getName(), url);
return url;
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_driven_style);

// Initialize map as normal
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);

// Get a handle to the file source and set the resource transform
FileSource.getInstance(UrlTransformActivity.this).setResourceTransform(new Transform());

mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap map) {
Timber.i("Map loaded");
}
});
}

@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}

@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}

@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}

@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

@Override
protected void onDestroy() {
super.onDestroy();

// Example of how to reset the transform callback
FileSource.getInstance(UrlTransformActivity.this).setResourceTransform(null);

mapView.onDestroy();
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<string name="activity_simple_map">Simple Map</string>
<string name="activity_map_in_dialog">Dialog with map</string>
<string name="activity_marker_view_rectangle">Marker views in rectangle</string>
<string name="activity_url_transform">Url transform</string>

<!--Description-->
<string name="description_user_location_tracking">Tracks the location of the user</string>
Expand Down Expand Up @@ -107,6 +108,7 @@
<string name="description_map_in_dialog">Display a map inside a dialog fragment</string>
<string name="description_marker_view_rectangle">Marker Views within a rectangle</string>
<string name="description_circle_layer">Show bus stops and route in Singapore</string>
<string name="description_url_transform">Transform urls on the fly</string>

<!--Categories-->
<string name="category">category</string>
Expand All @@ -122,6 +124,7 @@
<string name="category_userlocation">User Location</string>
<string name="category_style">Styling</string>
<string name="category_features">Features</string>
<string name="category_storage">Storage</string>

<!--Actions-->
<string name="action_remove_polylines">Remove polylines</string>
Expand Down
6 changes: 3 additions & 3 deletions platform/android/src/file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ void FileSource::setResourceTransform(jni::JNIEnv& env, jni::Object<FileSource::
// a subsequent call.
// Note: we're converting it to shared_ptr because this lambda is converted to a std::function,
// which requires copyability of its captured variables.
callback = std::shared_ptr<jni::jobject>(transformCallback.NewGlobalRef(env).release()->Get(), GenericGlobalRefDeleter()),
env
callback = std::shared_ptr<jni::jobject>(transformCallback.NewGlobalRef(env).release()->Get(), GenericGlobalRefDeleter())
](mbgl::Resource::Kind kind, std::string&& url_) {
return FileSource::ResourceTransformCallback::onURL(const_cast<jni::JNIEnv&>(env), jni::Object<FileSource::ResourceTransformCallback>(*callback), int(kind), url_);
android::UniqueEnv _env = android::AttachEnv();
return FileSource::ResourceTransformCallback::onURL(*_env, jni::Object<FileSource::ResourceTransformCallback>(*callback), int(kind), url_);
});
} else {
// Reset the callback
Expand Down