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

Commit

Permalink
[android] #3891 - fix observer crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Zugaldia committed Feb 21, 2016
1 parent be52b65 commit 7ea2d25
Showing 1 changed file with 32 additions and 58 deletions.
90 changes: 32 additions & 58 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1832,7 +1832,7 @@ void JNICALL listOfflineRegions(JNIEnv *env, jobject obj, jlong defaultFileSourc
}

void JNICALL createOfflineRegion(JNIEnv *env, jobject obj, jlong defaultFileSourcePtr, jobject definition_, jobject metadata_, jobject createCallback) {
mbgl::Log::Error(mbgl::Event::JNI, "createOfflineRegion");
mbgl::Log::Debug(mbgl::Event::JNI, "createOfflineRegion");

// Checks
assert(defaultFileSourcePtr != 0);
Expand Down Expand Up @@ -1908,7 +1908,7 @@ void JNICALL createOfflineRegion(JNIEnv *env, jobject obj, jlong defaultFileSour
}

void JNICALL setOfflineRegionObserver(JNIEnv *env, jobject obj, jobject offlineRegion_, jobject observerCallback) {
mbgl::Log::Error(mbgl::Event::JNI, "setOfflineRegionObserver");
mbgl::Log::Debug(mbgl::Event::JNI, "setOfflineRegionObserver");

// Offline region
jlong offlineRegionPtr = env->GetLongField(offlineRegion_, offlineRegionPtrId);
Expand All @@ -1922,39 +1922,14 @@ void JNICALL setOfflineRegionObserver(JNIEnv *env, jobject obj, jobject offlineR
// Define the observer
class Observer : public mbgl::OfflineRegionObserver {
public:

Observer(jobject observerCallback_)
: observerCallback(observerCallback_),
start(mbgl::SystemClock::now()) {
mbgl::Log::Error(mbgl::Event::JNI, "Constructor.");
renderDetach = attach_jni_thread(theJVM, &env, "Offline Thread");
if (renderDetach) {
mbgl::Log::Error(mbgl::Event::JNI, "Attached.");
}
: observerCallback(observerCallback_) {
}

void statusChanged(mbgl::OfflineRegionStatus status) override {
// Debug info
std::string bytesPerSecond = "-";

std::chrono::seconds sec(1);
auto elapsedSeconds = (mbgl::SystemClock::now() - start) / sec;
if (elapsedSeconds != 0) {
bytesPerSecond = mbgl::util::toString(status.completedResourceSize / elapsedSeconds);
}

std::ostringstream oss;
oss << status.completedResourceCount << " / " << status.requiredResourceCount
<< " resources"
<< (status.requiredResourceCountIsIndeterminate ? " (indeterminate); " : "; ")
<< status.completedResourceSize << " bytes downloaded"
<< " (" << bytesPerSecond << " bytes/sec)";
std::string statusText = oss.str();
mbgl::Log::Error(mbgl::Event::JNI, statusText);

if (status.complete()) {
mbgl::Log::Error(mbgl::Event::JNI, "Finished");
}
// Env
JNIEnv* env2;
jboolean renderDetach = attach_jni_thread(theJVM, &env2, "Offline Thread");

// Conver to jint
jint downloadState = -1;
Expand All @@ -1967,36 +1942,35 @@ void JNICALL setOfflineRegionObserver(JNIEnv *env, jobject obj, jobject offlineR
return;
}

// Stats object (known issue)
// jobject jstatus = env->NewObject(offlineRegionStatusClass, offlineRegionStatusConstructorId);
// env->SetIntField(jstatus, offlineRegionStatusDownloadStateId, downloadState);
// env->SetLongField(jstatus, offlineRegionStatusCompletedResourceCountId, status.completedResourceCount);
// env->SetLongField(jstatus, offlineRegionStatusCompletedResourceSizeId, status.completedResourceSize);
// env->SetLongField(jstatus, offlineRegionStatusRequiredResourceCountId, status.requiredResourceCount);
// env->SetBooleanField(jstatus, offlineRegionStatusRequiredResourceCountIsIndeterminateId, status.requiredResourceCountIsIndeterminate);
// env->CallVoidMethod(observerCallback, offlineRegionObserveronStatusChangedId, jstatus);
// Stats object
jobject jstatus = env2->NewObject(offlineRegionStatusClass, offlineRegionStatusConstructorId);
env2->SetIntField(jstatus, offlineRegionStatusDownloadStateId, downloadState);
env2->SetLongField(jstatus, offlineRegionStatusCompletedResourceCountId, status.completedResourceCount);
env2->SetLongField(jstatus, offlineRegionStatusCompletedResourceSizeId, status.completedResourceSize);
env2->SetLongField(jstatus, offlineRegionStatusRequiredResourceCountId, status.requiredResourceCount);
env2->SetBooleanField(jstatus, offlineRegionStatusRequiredResourceCountIsIndeterminateId, status.requiredResourceCountIsIndeterminate);
env2->CallVoidMethod(observerCallback, offlineRegionObserveronStatusChangedId, jstatus);

// Detach when we're done
detach_jni_thread(theJVM, &env2, renderDetach);
}

void responseError(mbgl::Response::Error error) override {
mbgl::Log::Error(mbgl::Event::JNI, "Error.");
void responseError(mbgl::Response::Error error) override {
// Env
JNIEnv* env2;
jboolean renderDetach = attach_jni_thread(theJVM, &env2, "Offline Thread");

// Error object (know issue)
// jobject jerror = env->NewObject(offlineRegionErrorClass, offlineRegionErrorConstructorId);
// env->SetIntField(jerror, offlineRegionErrorReasonId, (uint8_t)error.reason);
// env->SetObjectField(jerror, offlineRegionErrorMessageId, std_string_to_jstring(env, error.message));
// env->CallVoidMethod(observerCallback, offlineRegionObserveronResponseErrorId, jerror);
}
// Error object
jobject jerror = env2->NewObject(offlineRegionErrorClass, offlineRegionErrorConstructorId);
env2->SetIntField(jerror, offlineRegionErrorReasonId, (uint8_t)error.reason);
env2->SetObjectField(jerror, offlineRegionErrorMessageId, std_string_to_jstring(env2, error.message));
env2->CallVoidMethod(observerCallback, offlineRegionObserveronResponseErrorId, jerror);

~Observer() {
// Detach when we're done
mbgl::Log::Error(mbgl::Event::JNI, "Dettached.");
detach_jni_thread(theJVM, &env, renderDetach);
detach_jni_thread(theJVM, &env2, renderDetach);
}

jboolean renderDetach;
JNIEnv* env;
jobject observerCallback;
mbgl::SystemTimePoint start;
};

// We need to add the corresponding DeleteGlobalRef
Expand All @@ -2007,7 +1981,7 @@ void JNICALL setOfflineRegionObserver(JNIEnv *env, jobject obj, jobject offlineR
}

void JNICALL setOfflineRegionDownloadState(JNIEnv *env, jobject obj, jobject offlineRegion_, jint offlineRegionDownloadState) {
mbgl::Log::Error(mbgl::Event::JNI, "setOfflineRegionDownloadState");
mbgl::Log::Debug(mbgl::Event::JNI, "setOfflineRegionDownloadState");

// State
mbgl::OfflineRegionDownloadState state;
Expand All @@ -2034,7 +2008,7 @@ void JNICALL setOfflineRegionDownloadState(JNIEnv *env, jobject obj, jobject off
}

void JNICALL getOfflineRegionStatus(JNIEnv *env, jobject obj, jobject offlineRegion_, jobject statusCallback) {
mbgl::Log::Error(mbgl::Event::JNI, "getOfflineRegionStatus");
mbgl::Log::Debug(mbgl::Event::JNI, "getOfflineRegionStatus");

// Offline region
jlong offlineRegionPtr = env->GetLongField(offlineRegion_, offlineRegionPtrId);
Expand All @@ -2050,13 +2024,13 @@ void JNICALL getOfflineRegionStatus(JNIEnv *env, jobject obj, jobject offlineReg
if (error) {
mbgl::Log::Error(mbgl::Event::JNI, "Get region status failed.");
} else {
mbgl::Log::Error(mbgl::Event::JNI, "Region status returned.");
mbgl::Log::Debug(mbgl::Event::JNI, "Region status returned.");
}
});
}

void JNICALL deleteOfflineRegion(JNIEnv *env, jobject obj, jobject offlineRegion_, jobject deleteCallback) {
mbgl::Log::Error(mbgl::Event::JNI, "deleteOfflineRegion");
mbgl::Log::Debug(mbgl::Event::JNI, "deleteOfflineRegion");

// Offline region
jlong offlineRegionPtr = env->GetLongField(offlineRegion_, offlineRegionPtrId);
Expand All @@ -2070,7 +2044,7 @@ void JNICALL deleteOfflineRegion(JNIEnv *env, jobject obj, jobject offlineRegion
// Set new state
defaultFileSource->deleteOfflineRegion(std::move(*offlineRegion), [](std::exception_ptr error) {
if (error) {
mbgl::Log::Error(mbgl::Event::JNI, "Delete region failed.");
mbgl::Log::Debug(mbgl::Event::JNI, "Delete region failed.");
} else {
mbgl::Log::Error(mbgl::Event::JNI, "Region deleted.");
}
Expand Down

0 comments on commit 7ea2d25

Please sign in to comment.