[android] Simplify offline metadata API on Android

Fixes #4139
This commit is contained in:
Antonio Zugaldia
2016-02-29 13:44:44 -05:00
parent bef7fe76ff
commit 55cacfafd4
7 changed files with 41 additions and 122 deletions

View File

@@ -13,7 +13,6 @@ import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.gson.Gson;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.Style;
@@ -24,7 +23,6 @@ import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.offline.OfflineManager;
import com.mapbox.mapboxsdk.offline.OfflineRegion;
import com.mapbox.mapboxsdk.offline.OfflineRegionMetadata;
import com.mapbox.mapboxsdk.offline.OfflineTilePyramidRegionDefinition;
import com.mapbox.mapboxsdk.offline.OfflineRegionError;
import com.mapbox.mapboxsdk.offline.OfflineRegionStatus;
@@ -224,8 +222,7 @@ public class OfflineActivity extends AppCompatActivity
String regionName;
try {
String json = new String(offlineRegion.getMetadata().getMetadata(), CustomMetadata.CHARSET);
CustomMetadata customMetadata = new Gson().fromJson(json, CustomMetadata.class);
CustomMetadata customMetadata = CustomMetadata.decode(offlineRegion.getMetadata());
regionName = customMetadata.getRegionName();
} catch (UnsupportedEncodingException e) {
Log.e(LOG_TAG, "Failed to decode metadata: " + e.getMessage());
@@ -262,13 +259,10 @@ public class OfflineActivity extends AppCompatActivity
styleURL, bounds, minZoom, maxZoom, pixelRatio);
// Sample way of encoding metadata
OfflineRegionMetadata metadata;
byte[] metadata;
try {
CustomMetadata customMetadata = new CustomMetadata(regionName);
byte[] encoded = new Gson()
.toJson(customMetadata, CustomMetadata.class)
.getBytes(CustomMetadata.CHARSET);
metadata = new OfflineRegionMetadata(encoded);
metadata = customMetadata.encode();
} catch (UnsupportedEncodingException e) {
Log.e(LOG_TAG, "Failed to encode metadata: " + e.getMessage());
metadata = null;

View File

@@ -1,5 +1,9 @@
package com.mapbox.mapboxsdk.testapp.offline;
import com.google.gson.Gson;
import java.io.UnsupportedEncodingException;
/**
* A custom metadata class
*/
@@ -33,4 +37,20 @@ public class CustomMetadata {
this.regionName = regionName;
}
/*
* Helper methods to encode/decode metadata into/from byte[]
*/
public byte[] encode() throws UnsupportedEncodingException {
return new Gson()
.toJson(this, CustomMetadata.class)
.getBytes(CustomMetadata.CHARSET);
}
public static CustomMetadata decode(byte[] metadata) throws UnsupportedEncodingException {
String json = new String(metadata, CustomMetadata.CHARSET);
return new Gson()
.fromJson(json, CustomMetadata.class);
}
}