Skip to content

Commit

Permalink
implemented region status
Browse files Browse the repository at this point in the history
  • Loading branch information
tnightingale committed Mar 25, 2016
1 parent 652d4bf commit 866f381
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 170 deletions.
120 changes: 67 additions & 53 deletions src/android/Mapbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class Mapbox extends CordovaPlugin {
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";
private static final String ACTION_OFFLINE_REGION_STATUS = "offlineRegionStatus";
private static final String ACTION_ADD_MARKERS = "addMarkers";
private static final String ACTION_ADD_MARKER_CALLBACK = "addMarkerCallback";
private static final String ACTION_ADD_POLYGON = "addPolygon";
Expand Down Expand Up @@ -225,6 +226,23 @@ else if (ACTION_PAUSE_OFFLINE_REGION.equals(action)) {
callbackContext.success();
}

else if (ACTION_OFFLINE_REGION_STATUS.equals(action)) {
final int offlineRegionId = args.getInt(0);
final OfflineRegion region = this.mapboxManager.getOfflineRegion(offlineRegionId);

region.getStatus(new MapboxManager.OfflineRegionStatusCallback() {
@Override
public void onStatus(JSONObject status) {
callbackContext.success(status);
}

@Override
public void onError(String error) {
callbackContext.error(error);
}
});
}

else if (ACTION_GET_TILT.equals(action)) {
// if (mapView != null) {
// cordova.getActivity().runOnUiThread(new Runnable() {
Expand Down Expand Up @@ -333,69 +351,65 @@ public void run() {
}

private void createMap(final JSONObject options, final CallbackContext callback) {
if (accessToken == null) {
callback.error(MAPBOX_ACCESSTOKEN_RESOURCE_KEY + " not set in strings.xml");
return;
}

cordova.getActivity().runOnUiThread(new Runnable() {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
MapView mapView = createMapView(accessToken, options);
Map.create(mapView, options, new Map.MapCreatedCallback() {
@Override
public void onCreate(final Map map) {
JSONObject resp = new JSONObject();
try {
resp.put("id", map.getId());
callback.success(resp);
return;
} catch (JSONException e) {
String error = "Failed to create map: " + e.getMessage();
Log.e(LOG_TAG, error);
callback.error(error);
return;
try {
MapView mapView = createMapView(accessToken, options);
Map.create(mapView, options, new Map.MapCreatedCallback() {
@Override
public void onCreate(final Map map) {
JSONObject resp = new JSONObject();
try {
resp.put("id", map.getId());
callback.success(resp);
return;
} catch (JSONException e) {
String error = "Failed to create map: " + e.getMessage();
Log.e(LOG_TAG, error);
callback.error(error);
return;
}
}
}

@Override
public void onError(String error) {
String message = "Failed to create map: " + error;
Log.e(LOG_TAG, message);
callback.error(message);
}
});
@Override
public void onError(String error) {
String message = "Failed to create map: " + error;
Log.e(LOG_TAG, message);
callback.error(message);
}
});
} catch (JSONException e) {
callback.error(e.getMessage());
}
}
});
}

private MapView createMapView(String accessToken, JSONObject options) {
private MapView createMapView(String accessToken, JSONObject options) throws JSONException {
MapView mapView = new MapView(this.webView.getContext());
mapView.setAccessToken(accessToken);

try {
final JSONObject margins = options.isNull("margins") ? null : options.getJSONObject("margins");
final int left = (int) (retinaFactor * (margins == null || margins.isNull("left") ? 0 : margins.getInt("left")));
final int right = (int) (retinaFactor * (margins == null || margins.isNull("right") ? 0 : margins.getInt("right")));
final int top = (int) (retinaFactor * (margins == null || margins.isNull("top") ? 0 : margins.getInt("top")));
final int bottom = (int) (retinaFactor * (margins == null || margins.isNull("bottom") ? 0 : margins.getInt("bottom")));

// need to do this to register a receiver which onPause later needs
mapView.onResume();
mapView.onCreate(null);

// position the mapView overlay
int webViewWidth = webView.getView().getWidth();
int webViewHeight = webView.getView().getHeight();
final FrameLayout layout = (FrameLayout) webView.getView().getParent();
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(webViewWidth - left - right, webViewHeight - top - bottom);
params.setMargins(left, top, right, bottom);
mapView.setLayoutParams(params);

layout.addView(mapView);
} catch (JSONException e) {
e.printStackTrace();
}
final JSONObject margins = options.isNull("margins") ? null : options.getJSONObject("margins");
final int left = (int) (retinaFactor * (margins == null || margins.isNull("left") ? 0 : margins.getInt("left")));
final int right = (int) (retinaFactor * (margins == null || margins.isNull("right") ? 0 : margins.getInt("right")));
final int top = (int) (retinaFactor * (margins == null || margins.isNull("top") ? 0 : margins.getInt("top")));
final int bottom = (int) (retinaFactor * (margins == null || margins.isNull("bottom") ? 0 : margins.getInt("bottom")));

// need to do this to register a receiver which onPause later needs
mapView.onResume();
mapView.onCreate(null);

// position the mapView overlay
int webViewWidth = webView.getView().getWidth();
int webViewHeight = webView.getView().getHeight();
final FrameLayout layout = (FrameLayout) webView.getView().getParent();
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(webViewWidth - left - right, webViewHeight - top - bottom);
params.setMargins(left, top, right, bottom);
mapView.setLayoutParams(params);

layout.addView(mapView);

return mapView;
}

Expand Down Expand Up @@ -427,7 +441,7 @@ public void createOfflineRegion(final JSONObject options, final CallbackContext
public void run() {
OfflineManager offlineManager = OfflineManager.getInstance(webView.getContext());
offlineManager.setAccessToken(accessToken);
mapboxManager.createOfflineRegion(options, callback, new MapboxManager.OfflineRegionStatusCallback() {
mapboxManager.createOfflineRegion(options, callback, new MapboxManager.OfflineRegionProgressCallback() {
@Override
public void onProgress(JSONObject progress) {
PluginResult result = new PluginResult(PluginResult.Status.OK, progress);
Expand Down
69 changes: 35 additions & 34 deletions src/android/MapboxManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,21 @@ 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;
public static final String JSON_FIELD_ID = "id";
public static final String JSON_FIELD_REGION_NAME = "name";

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;
private HashMap<Long, OfflineRegion> regions = new HashMap<Long, OfflineRegion>();

public interface OfflineRegionStatusCallback {
void onStatus(JSONObject status);
void onError(String error);
}

public interface OfflineRegionProgressCallback {
void onComplete(JSONObject progress);
void onProgress(JSONObject progress);
void onError(String error);
Expand All @@ -47,40 +45,45 @@ public interface LoadOfflineRegionsCallback {
}

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

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();
JSONArray responses = new JSONArray();
JSONObject response;
OfflineRegion region;
for (com.mapbox.mapboxsdk.offline.OfflineRegion offlineRegion : offlineRegions) {
OfflineRegion region = createOfflineRegion(offlineRegion);
response = new JSONObject();
response.put("id", region.getId());
regions.put(response);
if (regions.containsKey(offlineRegion.getID())) {
region = regions.get(offlineRegion.getID());
} else {
region = createOfflineRegion(offlineRegion);
}
response = region.getMetadata();
response.put(JSON_FIELD_ID, region.getId());
responses.put(response);
}
callback.onList(regions);
callback.onList(responses);
} catch (JSONException e) {
String error = "Error loading OfflineRegions: " + e.getMessage();
callback.onError(error);
this.onError(e.getMessage());
} catch (UnsupportedEncodingException e) {
this.onError(e.getMessage());
}
}

@Override
public void onError(String error) {

callback.onError(error);
}
});
}

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

Expand All @@ -95,11 +98,13 @@ 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);
JSONObject response = region.getMetadata();
response.put(JSON_FIELD_ID, region.getId());
callback.success(response);
} catch (JSONException e) {
this.onError(e.getMessage());
} catch (UnsupportedEncodingException e) {
this.onError(e.getMessage());
}
}

Expand All @@ -116,21 +121,17 @@ public void onError(String error) {
}
}

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);
private OfflineRegion createOfflineRegion(com.mapbox.mapboxsdk.offline.OfflineRegion offlineRegion) throws JSONException, UnsupportedEncodingException {
OfflineRegion region = new OfflineRegion(offlineRegion);
regions.put(offlineRegion.getID(), region);
return region;
}

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

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

Expand Down
Loading

0 comments on commit 866f381

Please sign in to comment.