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

Commit

Permalink
[android] - add filesource activation/deactivation to OfflineRegion s…
Browse files Browse the repository at this point in the history
…tatus and deletion
  • Loading branch information
tobrun committed Jan 11, 2018
1 parent a500125 commit d164ed0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 65 deletions.
6 changes: 3 additions & 3 deletions platform/android/MapboxGLAndroidSDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ android {
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

lintOptions {
Expand All @@ -120,7 +120,7 @@ android {
}

testOptions {
unitTests{
unitTests {
returnDefaultValues = true
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.mapbox.mapboxsdk.offline;

import android.os.Handler;
import android.os.Looper;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.mapbox.mapboxsdk.LibraryLoader;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.storage.FileSource;

import java.lang.annotation.Retention;
Expand Down Expand Up @@ -51,7 +51,7 @@ public class OfflineRegion {
private byte[] metadata;

// Makes sure callbacks come back to the main thread
private Handler handler;
private final Handler handler = new Handler();

/**
* A region can have a single observer, which gets notified whenever a change
Expand Down Expand Up @@ -239,14 +239,6 @@ public byte[] getMetadata() {
return metadata;
}

private Handler getHandler() {
if (handler == null) {
handler = new Handler(Looper.getMainLooper());
}

return handler;
}

/**
* Register an observer to be notified when the state of the region changes.
*
Expand All @@ -257,12 +249,9 @@ public void setObserver(@Nullable final OfflineRegionObserver observer) {
@Override
public void onStatusChanged(final OfflineRegionStatus status) {
if (deliverMessages()) {
getHandler().post(new Runnable() {
@Override
public void run() {
if (observer != null) {
observer.onStatusChanged(status);
}
handler.post(() -> {
if (observer != null) {
observer.onStatusChanged(status);
}
});
}
Expand All @@ -271,12 +260,9 @@ public void run() {
@Override
public void onError(final OfflineRegionError error) {
if (deliverMessages()) {
getHandler().post(new Runnable() {
@Override
public void run() {
if (observer != null) {
observer.onError(error);
}
handler.post(() -> {
if (observer != null) {
observer.onError(error);
}
});
}
Expand All @@ -285,12 +271,9 @@ public void run() {
@Override
public void mapboxTileCountLimitExceeded(final long limit) {
if (deliverMessages()) {
getHandler().post(new Runnable() {
@Override
public void run() {
if (observer != null) {
observer.mapboxTileCountLimitExceeded(limit);
}
handler.post(() -> {
if (observer != null) {
observer.mapboxTileCountLimitExceeded(limit);
}
});
}
Expand Down Expand Up @@ -325,24 +308,21 @@ public void setDownloadState(@DownloadState int state) {
* @param callback the callback to invoked.
*/
public void getStatus(@NonNull final OfflineRegionStatusCallback callback) {
FileSource.getInstance(Mapbox.getApplicationContext()).activate();
getOfflineRegionStatus(new OfflineRegionStatusCallback() {
@Override
public void onStatus(final OfflineRegionStatus status) {
getHandler().post(new Runnable() {
@Override
public void run() {
callback.onStatus(status);
}
handler.post(() -> {
callback.onStatus(status);
FileSource.getInstance(Mapbox.getApplicationContext()).deactivate();
});
}

@Override
public void onError(final String error) {
getHandler().post(new Runnable() {
@Override
public void run() {
callback.onError(error);
}
handler.post(() -> {
callback.onError(error);
FileSource.getInstance(Mapbox.getApplicationContext()).deactivate();
});
}
});
Expand All @@ -368,26 +348,23 @@ public void run() {
public void delete(@NonNull final OfflineRegionDeleteCallback callback) {
if (!isDeleted) {
isDeleted = true;
FileSource.getInstance(Mapbox.getApplicationContext()).activate();
deleteOfflineRegion(new OfflineRegionDeleteCallback() {
@Override
public void onDelete() {
getHandler().post(new Runnable() {
@Override
public void run() {
callback.onDelete();
OfflineRegion.this.finalize();
}
handler.post((Runnable) () -> {
callback.onDelete();
FileSource.getInstance(Mapbox.getApplicationContext()).deactivate();
OfflineRegion.this.finalize();
});
}

@Override
public void onError(final String error) {
getHandler().post(new Runnable() {
@Override
public void run() {
isDeleted = false;
callback.onError(error);
}
handler.post(() -> {
isDeleted = false;
FileSource.getInstance(Mapbox.getApplicationContext()).deactivate();
callback.onError(error);
});
}
});
Expand All @@ -408,23 +385,15 @@ public void updateMetadata(@NonNull final byte[] bytes, @NonNull final OfflineRe
updateOfflineRegionMetadata(bytes, new OfflineRegionUpdateMetadataCallback() {
@Override
public void onUpdate(final byte[] metadata) {
getHandler().post(new Runnable() {
@Override
public void run() {
OfflineRegion.this.metadata = metadata;
callback.onUpdate(metadata);
}
handler.post(() -> {
OfflineRegion.this.metadata = metadata;
callback.onUpdate(metadata);
});
}

@Override
public void onError(final String error) {
getHandler().post(new Runnable() {
@Override
public void run() {
callback.onError(error);
}
});
handler.post(() -> callback.onError(error));
}
});
}
Expand Down

0 comments on commit d164ed0

Please sign in to comment.