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

Commit

Permalink
[android] resource transform callback example
Browse files Browse the repository at this point in the history
  • Loading branch information
ivovandongen committed Mar 30, 2017
1 parent 2bfbe90 commit 61e2695
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
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

0 comments on commit 61e2695

Please sign in to comment.