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

Commit

Permalink
[android] #3891 - implementing metada and download callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Zugaldia committed Feb 21, 2016
1 parent 8446964 commit d379120
Show file tree
Hide file tree
Showing 9 changed files with 555 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.mapbox.mapboxsdk.offline;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;

import java.util.List;
import java.io.File;

/**
* Created by antonio on 2/17/16.
Expand All @@ -11,9 +13,6 @@ public class OfflineManager {

private final static String LOG_TAG = "OfflineManager";

// TODO: Move to JNI for consistency
private final static String DB_NAME = "/mbgl-cache.db";

// Holds the pointer to JNI DefaultFileSource
private long mDefaultFileSourcePtr = 0;

Expand All @@ -32,17 +31,28 @@ public interface CreateOfflineRegionCallback {
}

/*
* Constructor
* Constructors
*/

private OfflineManager() { }
private OfflineManager() {
// For JNI use only
}

public OfflineManager(Context context) {
String cachePath = context.getCacheDir().getAbsolutePath();
String assetRoot = context.getFilesDir().getAbsolutePath();

// Debug
Log.d(LOG_TAG, "Reading files in assetRoot: " + assetRoot);
File fileRoot = new File(assetRoot);
File files[] = fileRoot.listFiles();
Log.d(LOG_TAG, "Size: " + files.length);
for (int i = 0; i < files.length; i++) {
Log.d(LOG_TAG, "Filename: " + files[i].getName());
}

// Get pointer to DefaultFileSource instance
mDefaultFileSourcePtr = createDefaultFileSource(cachePath + DB_NAME, assetRoot + DB_NAME);
mDefaultFileSourcePtr = createDefaultFileSource(cachePath, assetRoot);
}

/*
Expand All @@ -63,10 +73,8 @@ public String getAccessToken() {
* callback, which will be executed on the database thread; it is the responsibility
* of the SDK bindings to re-execute a user-provided callback on the main thread.
*/
public void listOfflineRegions(ListOfflineRegionsCallback callback) {
if (callback != null) {
listOfflineRegions(mDefaultFileSourcePtr, callback);
}
public void listOfflineRegions(@NonNull ListOfflineRegionsCallback callback) {
listOfflineRegions(mDefaultFileSourcePtr, callback);
}

/**
Expand All @@ -81,12 +89,11 @@ public void listOfflineRegions(ListOfflineRegionsCallback callback) {
* optionally registering an `OfflineRegionObserver` beforehand.
*/
public void createOfflineRegion(
OfflineRegionDefinition definition,
OfflineRegionMetadata metadata,
CreateOfflineRegionCallback callback) {
if (callback != null) {
createOfflineRegion(mDefaultFileSourcePtr, definition, metadata, callback);
}
@NonNull OfflineRegionDefinition definition,
@NonNull OfflineRegionMetadata metadata,
@NonNull CreateOfflineRegionCallback callback) {

createOfflineRegion(mDefaultFileSourcePtr, definition, metadata, callback);
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapbox.mapboxsdk.offline;

import android.support.annotation.IntDef;
import android.support.annotation.NonNull;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -11,6 +12,8 @@
*/
public class OfflineRegion {

private final static String LOG_TAG = "OfflineRegion";

// Parent OfflineManager
private OfflineManager offlineManager;

Expand Down Expand Up @@ -48,7 +51,7 @@ public interface OfflineRegionObserver {
* responsibility of the SDK bindings to wrap this object in an interface that
* re-executes the user-provided implementation on the main thread.
*/
void onResponseError(Exception error);
void onResponseError(OfflineRegionError error);
}

/*
Expand Down Expand Up @@ -84,7 +87,9 @@ public interface OfflineRegionDeleteCallback {
* To create a new offline region, use OfflineManager.createOfflineRegion() instead.
*/

private OfflineRegion() { }
private OfflineRegion() {
// For JNI use only
}

/*
* Getters
Expand All @@ -105,10 +110,8 @@ public OfflineRegionMetadata getMetadata() {
/**
* Register an observer to be notified when the state of the region changes.
*/
public void setObserver(OfflineRegionObserver observer) {
if (observer != null) {
setOfflineRegionObserver(this, observer);
}
public void setObserver(@NonNull OfflineRegionObserver observer) {
setOfflineRegionObserver(this, observer);
}

/**
Expand All @@ -124,10 +127,8 @@ public void setDownloadState(@DownloadState int state) {
* executed on the database thread; it is the responsibility of the SDK bindings
* to re-execute a user-provided callback on the main thread.
*/
public void getStatus(OfflineRegionStatusCallback callback) {
if (callback != null) {
getOfflineRegionStatus(this, callback);
}
public void getStatus(@NonNull OfflineRegionStatusCallback callback) {
getOfflineRegionStatus(this, callback);
}

/**
Expand All @@ -145,10 +146,8 @@ public void getStatus(OfflineRegionStatusCallback callback) {
* executed on the database thread; it is the responsibility of the SDK bindings
* to re-execute a user-provided callback on the main thread.
*/
public void delete(OfflineRegionDeleteCallback callback) {
if (callback != null) {
deleteOfflineRegion(this, callback);
}
public void delete(@NonNull OfflineRegionDeleteCallback callback) {
deleteOfflineRegion(this, callback);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public class OfflineRegionDefinition {
private double maxZoom;
private float pixelRatio;

/*
* Constructors
*/

private OfflineRegionDefinition() {
// For JNI use only
}

public OfflineRegionDefinition(
String styleURL, LatLngBounds bounds, double minZoom, double maxZoom, float pixelRatio) {
this.bounds = bounds;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.mapbox.mapboxsdk.offline;

/**
* Created by antonio on 2/21/16.
*/
public class OfflineRegionError {

/**
* Success = 1
* NotFound = 2
* Server = 3
* Connection = 4
* Other = 6
*/
private int reason;

/**
/* An error message from the request handler, e.g. a server message or a system message
/* informing the user about the reason for the failure.
*/
private String message;

/*
* Constructors
*/

private OfflineRegionError() {
// For JNI use only
}

public OfflineRegionError(int reason, String message) {
this.reason = reason;
this.message = message;
}

/*
* Getters
*/

public int getReason() {
return reason;
}

public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ public void setMetadata(byte[] metadata) {
this.metadata = metadata;
}

/*
* Overrides
*/

@Override
public String toString() {
return "OfflineRegionMetadata{metadata=" + metadata + "}";
}

/*
* byte[] utils
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ public class OfflineRegionStatus {
*/
private boolean requiredResourceCountIsIndeterminate = true;

/*
* Use setObserver(OfflineRegionObserver observer) to obtain a OfflineRegionStatus object.
*/

private OfflineRegionStatus() {
// For JNI use only
}

/*
* Is the region complete?
*/

public boolean complete() {
return (completedResourceCount == requiredResourceCount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.mapbox.mapboxsdk.offline.OfflineManager;
import com.mapbox.mapboxsdk.offline.OfflineRegion;
import com.mapbox.mapboxsdk.offline.OfflineRegionDefinition;
import com.mapbox.mapboxsdk.offline.OfflineRegionError;
import com.mapbox.mapboxsdk.offline.OfflineRegionMetadata;
import com.mapbox.mapboxsdk.offline.OfflineRegionStatus;
import com.mapbox.mapboxsdk.testapp.offline.DownloadRegionDialog;
Expand Down Expand Up @@ -197,14 +198,14 @@ public void onList(OfflineRegion[] offlineRegions) {

// Check result
if (offlineRegions == null || offlineRegions.length == 0) {
showMessage("You have no regions yet.");
Log.d(LOG_TAG, "You have no regions yet.");
return;
}

// Get regions info
ArrayList<String> offlineRegionsNames = new ArrayList<>();
for (OfflineRegion offlineRegion : offlineRegions) {
offlineRegionsNames.add("Region " + offlineRegion.getID());
offlineRegionsNames.add(getRegionName(offlineRegion));
}

// Create args
Expand All @@ -221,6 +222,23 @@ public void onList(OfflineRegion[] offlineRegions) {
public void onError(String error) {
Log.e(LOG_TAG, "Error: " + error);
}

private String getRegionName(OfflineRegion offlineRegion) {
String regionName;

// try {
// byte[] metadata = offlineRegion.getMetadata().getMetadata();
// HashMap<String, String> data = (HashMap<String, String>) OfflineRegionMetadata.deserialize(metadata);
// regionName = data.get("name");
// } catch (Exception e) {
// Log.d(LOG_TAG, "Failed to decode metadata: " + e.getMessage());
// regionName = "Region " + offlineRegion.getID();
// }

regionName = "Region " + offlineRegion.getID();

return regionName;
}
});
}

Expand All @@ -229,7 +247,7 @@ public void onError(String error) {
*/

@Override
public void onDownloadRegionDialogPositiveClick(String regionName) {
public void onDownloadRegionDialogPositiveClick(final String regionName) {
Log.d(LOG_TAG, "Download started: " + regionName);

// Start progress bar
Expand All @@ -244,8 +262,7 @@ public void onDownloadRegionDialogPositiveClick(String regionName) {
OfflineRegionDefinition definition = new OfflineRegionDefinition(
styleURL, bounds, minZoom, maxZoom, pixelRatio);

// Metadata
// TODO: Make this a custom class to showcase a HashMap as metadata
// Sample way of encoding metadata
OfflineRegionMetadata metadata = null;
try {
HashMap<String, String> data = new HashMap<>();
Expand All @@ -260,7 +277,7 @@ public void onDownloadRegionDialogPositiveClick(String regionName) {
mOfflineManager.createOfflineRegion(definition, metadata, new OfflineManager.CreateOfflineRegionCallback() {
@Override
public void onCreate(OfflineRegion offlineRegion) {
Log.d(LOG_TAG, "Offline region created: " + offlineRegion.getID());
Log.d(LOG_TAG, "Offline region created: " + regionName);
mOfflineRegion = offlineRegion;
launchDownload();
}
Expand All @@ -286,8 +303,9 @@ public void onStatusChanged(OfflineRegionStatus status) {
}

@Override
public void onResponseError(Exception error) {
Log.d(LOG_TAG, "onResponseError: " + error.getMessage());
public void onResponseError(OfflineRegionError error) {
Log.e(LOG_TAG, "onResponseError reason: " + error.getReason());
Log.e(LOG_TAG, "onResponseError message: " + error.getMessage());
}
});

Expand Down Expand Up @@ -323,11 +341,7 @@ private void endProgress(String message) {
// mProgressBar.setVisibility(View.GONE);

// Show a toast
// showMessage(message);
}

private void showMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
// Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}

}
Loading

0 comments on commit d379120

Please sign in to comment.