Skip to content

Commit

Permalink
Implmented loadOfflineRegions()
Browse files Browse the repository at this point in the history
  • Loading branch information
tnightingale committed Mar 25, 2016
1 parent 2cd4bec commit 652d4bf
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 139 deletions.
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

<framework src="src/android/mapbox.gradle" custom="true" type="gradleReference"/>
<source-file src="src/android/Mapbox.java" target-dir="src/com/telerik/plugins/mapbox"/>
<source-file src="src/android/MapboxManager.java" target-dir="src/com/telerik/plugins/mapbox"/>
<source-file src="src/android/OfflineRegion.java" target-dir="src/com/telerik/plugins/mapbox"/>
<source-file src="src/android/Map.java" target-dir="src/com/telerik/plugins/mapbox"/>

Expand Down
92 changes: 56 additions & 36 deletions src/android/Mapbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class Mapbox extends CordovaPlugin {
private static final String ACTION_CREATE_MAP = "createMap";
private static final String ACTION_JUMP_TO = "jumpTo";
private static final String ACTION_SHOW_USER_LOCATION = "showUserLocation";
private static final String ACTION_LIST_OFFLINE_REGIONS = "listOfflineRegions";
private static final String ACTION_CREATE_OFFLINE_REGION = "createOfflineRegion";
private static final String ACTION_DOWNLOAD_OFFLINE_REGION = "downloadOfflineRegion";
private static final String ACTION_PAUSE_OFFLINE_REGION = "pauseOfflineRegion";
Expand All @@ -64,12 +65,15 @@ public class Mapbox extends CordovaPlugin {
private static float retinaFactor;
private String accessToken;

private MapboxManager mapboxManager;

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);

this.retinaFactor = this.getRetinaFactor();
this.accessToken = this.getAccessToken();
this.mapboxManager = new MapboxManager(accessToken, retinaFactor, webView);
}

@Override
Expand Down Expand Up @@ -196,6 +200,10 @@ public boolean onInfoWindowClick(Marker marker) {
);
}

else if (ACTION_LIST_OFFLINE_REGIONS.equals(action)) {
this.listOfflineRegions(callbackContext);
}

else if (ACTION_CREATE_OFFLINE_REGION.equals(action)) {
final JSONObject options = args.getJSONObject(0);
final CallbackContext onProgress = new CallbackContext(args.getString(1), this.webView);
Expand All @@ -205,14 +213,14 @@ else if (ACTION_CREATE_OFFLINE_REGION.equals(action)) {

else if (ACTION_DOWNLOAD_OFFLINE_REGION.equals(action)) {
final int offlineRegionId = args.getInt(0);
final OfflineRegion region = OfflineRegion.getOfflineRegion(offlineRegionId);
final OfflineRegion region = this.mapboxManager.getOfflineRegion(offlineRegionId);
region.download();
callbackContext.success();
}

else if (ACTION_PAUSE_OFFLINE_REGION.equals(action)) {
final int offlineRegionId = args.getInt(0);
final OfflineRegion region = OfflineRegion.getOfflineRegion(offlineRegionId);
final OfflineRegion region = this.mapboxManager.getOfflineRegion(offlineRegionId);
region.pause();
callbackContext.success();
}
Expand Down Expand Up @@ -391,46 +399,58 @@ private MapView createMapView(String accessToken, JSONObject options) {
return mapView;
}

public void createOfflineRegion(JSONObject options, final CallbackContext callback, final CallbackContext onProgress, final CallbackContext onComplete) throws JSONException {
OfflineManager offlineManager = OfflineManager.getInstance(this.webView.getContext());
offlineManager.setAccessToken(this.accessToken);
OfflineRegion.create(offlineManager, this.retinaFactor, options, new OfflineRegion.OfflineRegionCreatedCallback() {
public void listOfflineRegions(final CallbackContext callback) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void onCreate(OfflineRegion region) {
JSONObject resp = new JSONObject();
try {
resp.put("id", region.getId());
callback.success(resp);
return;
} catch (JSONException e) {
String error = "Failed to create offline region: " + e.getMessage();
Log.e(LOG_TAG, error);
callback.error(error);
return;
}
}
public void run() {
OfflineManager offlineManager = OfflineManager.getInstance(webView.getContext());
offlineManager.setAccessToken(accessToken);
mapboxManager.loadOfflineRegions(new MapboxManager.LoadOfflineRegionsCallback() {
@Override
public void onList(JSONArray offlineRegions) {
callback.success(offlineRegions);
}

@Override
public void onProgress(JSONObject progress) {
PluginResult result = new PluginResult(PluginResult.Status.OK, progress);
result.setKeepCallback(true);
onProgress.sendPluginResult(result);
@Override
public void onError(String error) {
String message = "Error loading offline regions: " + error;
callback.error(message);
}
});
}
});
}

public void createOfflineRegion(final JSONObject options, final CallbackContext callback, final CallbackContext onProgress, final CallbackContext onComplete) throws JSONException {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void onComplete(JSONObject progress) {
Log.d(LOG_TAG, "complete");
PluginResult result = new PluginResult(PluginResult.Status.OK, progress);
onComplete.sendPluginResult(result);
}
public void run() {
OfflineManager offlineManager = OfflineManager.getInstance(webView.getContext());
offlineManager.setAccessToken(accessToken);
mapboxManager.createOfflineRegion(options, callback, new MapboxManager.OfflineRegionStatusCallback() {
@Override
public void onProgress(JSONObject progress) {
PluginResult result = new PluginResult(PluginResult.Status.OK, progress);
result.setKeepCallback(true);
onProgress.sendPluginResult(result);
}

@Override
public void onError(String error) {
String message = "Failed to create offline region: " + error;
Log.e(LOG_TAG, message);
PluginResult result = new PluginResult(PluginResult.Status.ERROR, message);
result.setKeepCallback(true);
onComplete.error(message);
@Override
public void onComplete(JSONObject progress) {
Log.d(LOG_TAG, "complete");
PluginResult result = new PluginResult(PluginResult.Status.OK, progress);
onComplete.sendPluginResult(result);
}

@Override
public void onError(String error) {
String message = "Failed to create offline region: " + error;
Log.e(LOG_TAG, message);
PluginResult result = new PluginResult(PluginResult.Status.ERROR, message);
result.setKeepCallback(true);
onComplete.error(message);
}
});
}
});
}
Expand Down
154 changes: 154 additions & 0 deletions src/android/MapboxManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package com.telerik.plugins.mapbox;

import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.offline.OfflineManager;
import com.mapbox.mapboxsdk.offline.OfflineRegionDefinition;
import com.mapbox.mapboxsdk.offline.OfflineTilePyramidRegionDefinition;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

class MapboxManager {

// JSON encoding/decoding
public static final String JSON_CHARSET = "UTF-8";
public static final String JSON_FIELD_REGION_NAME = "FIELD_REGION_NAME";

private String mapboxAccessToken;

private Float density;

private CordovaWebView cordovaWebView;

private OfflineManager offlineManager;

private HashMap<Integer, OfflineRegion> regions = new HashMap<Integer, OfflineRegion>();

private HashMap<Long, com.mapbox.mapboxsdk.offline.OfflineRegion> mapboxRegions = new HashMap<Long, com.mapbox.mapboxsdk.offline.OfflineRegion>();

private int ids = 0;

public interface OfflineRegionStatusCallback {
void onComplete(JSONObject progress);
void onProgress(JSONObject progress);
void onError(String error);
}

public interface LoadOfflineRegionsCallback {
void onList(JSONArray regions);
void onError(String error);
}

public MapboxManager(String accessToken, Float screenDensity, CordovaWebView webView) {
this.mapboxAccessToken = accessToken;
this.density = screenDensity;
this.cordovaWebView = webView;
this.offlineManager = OfflineManager.getInstance(webView.getContext());
}

public void loadOfflineRegions(final LoadOfflineRegionsCallback callback) {
this.offlineManager.listOfflineRegions(new OfflineManager.ListOfflineRegionsCallback() {
@Override
public void onList(com.mapbox.mapboxsdk.offline.OfflineRegion[] offlineRegions) {
try {
JSONArray regions = new JSONArray();
JSONObject response;
for (com.mapbox.mapboxsdk.offline.OfflineRegion offlineRegion : offlineRegions) {
OfflineRegion region = createOfflineRegion(offlineRegion);
response = new JSONObject();
response.put("id", region.getId());
regions.put(response);
}
callback.onList(regions);
} catch (JSONException e) {
String error = "Error loading OfflineRegions: " + e.getMessage();
callback.onError(error);
}
}

@Override
public void onError(String error) {

}
});
}

public void createOfflineRegion(final JSONObject options, final CallbackContext callback, final OfflineRegionStatusCallback offlineRegionStatusCallback) {
try {
final String regionName = options.getString("name");

JSONObject metadata = new JSONObject();
metadata.put(JSON_FIELD_REGION_NAME, regionName);
byte[] encodedMetadata = metadata.toString().getBytes(JSON_CHARSET);
OfflineRegionDefinition definition = this.createOfflineRegionDefinition(density, options);

offlineManager.createOfflineRegion(definition, encodedMetadata, new OfflineManager.CreateOfflineRegionCallback() {
@Override
public void onCreate(com.mapbox.mapboxsdk.offline.OfflineRegion offlineRegion) {
try {
OfflineRegion region = createOfflineRegion(offlineRegion);
region.setObserver(offlineRegionStatusCallback);
JSONObject resp = new JSONObject();
resp.put("id", region.getId());
callback.success(resp);
} catch (JSONException e) {
this.onError(e.getMessage());
}
}

@Override
public void onError(String error) {
String message = "Failed to create offline region: " + error;
callback.error(message);
}
});
} catch (JSONException e) {
callback.error(e.getMessage());
} catch (UnsupportedEncodingException e) {
callback.error(e.getMessage());
}
}

private OfflineRegion createOfflineRegion(com.mapbox.mapboxsdk.offline.OfflineRegion offlineRegion) throws JSONException {
int id = this.ids++;
OfflineRegion region = new OfflineRegion(id, offlineRegion);
byte[] encodedMetadata = offlineRegion.getMetadata();
JSONObject metadata = new JSONObject(encodedMetadata.toString());
region.setRegionName(metadata.getString(JSON_FIELD_REGION_NAME));
regions.put(id, region);
return region;
}

public OfflineRegion getOfflineRegion(int id) {
return regions.get(id);
}

public void removeOfflineRegion(int id) {
regions.remove(id);
}

private OfflineRegionDefinition createOfflineRegionDefinition(float retinaFactor, JSONObject options) throws JSONException {
String styleURL = Mapbox.getStyle(options.getString("style"));
double minZoom = options.getDouble("minZoom");
double maxZoom = options.getDouble("maxZoom");
JSONObject boundsOptions = options.getJSONObject("bounds");
double north = boundsOptions.getDouble("north");
double east = boundsOptions.getDouble("east");
double south = boundsOptions.getDouble("south");
double west = boundsOptions.getDouble("west");

LatLngBounds bounds = new LatLngBounds.Builder()
.include(new LatLng(north, west))
.include(new LatLng(south, east))
.build();

return new OfflineTilePyramidRegionDefinition(styleURL, bounds, minZoom, maxZoom, retinaFactor);
}
}
Loading

0 comments on commit 652d4bf

Please sign in to comment.