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

Commit

Permalink
[android] #3891 - remove debug code and add access token methods
Browse files Browse the repository at this point in the history
  • Loading branch information
zugaldia committed Feb 18, 2016
1 parent 797d501 commit e2e0ed5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
import android.content.Context;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;

/**
* Created by antonio on 2/17/16.
Expand All @@ -18,6 +12,9 @@ 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 @@ -39,63 +36,21 @@ public interface CreateOfflineRegionCallback {

public OfflineManager(Context context) {
String cachePath = context.getCacheDir().getAbsolutePath();
Log.d(LOG_TAG, "cachePath: " + cachePath);

String assetRoot = context.getFilesDir().getAbsolutePath();
Log.d(LOG_TAG, "assetRoot: " + assetRoot);

// Debug only
try {
testWritable(cachePath);
testWritable(assetRoot);
} catch (IOException e) {
Log.e(LOG_TAG, "Failed: " + e.getMessage());
e.printStackTrace();
}

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

/*
* For debug only.
*
* We're getting an `unable to open database file` exception in core, this code tests that the
* `cachePath` and `assetRoot` are in fact writable:
* https://gist.github.com/zugaldia/ade660864976f8fd4123
*/

private void testWritable(String path) throws IOException {
Log.d(LOG_TAG, "Testing path: " + path);

// Create file
String filename = UUID.randomUUID().toString() + ".txt";
Log.d(LOG_TAG, "Writing file: " + filename);
File f = new File(path, filename);
OutputStream out = new BufferedOutputStream(new FileOutputStream(f));
out.write("This is a test.".getBytes());
out.close();

// List files
File folder = new File(path);
File listFiles[] = folder.listFiles();
Log.d(LOG_TAG, "Size: "+ listFiles.length);
for (int i = 0; i < listFiles.length; i++)
{
Log.d(LOG_TAG, "Name: " + listFiles[i].getName());
}
mDefaultFileSourcePtr = createDefaultFileSource(cachePath + DB_NAME, assetRoot + DB_NAME);
}

/*
* Access token getter/setter
*/

public void setAccessToken(String accessToken) {

setAccessToken(mDefaultFileSourcePtr, accessToken);
}

public String getAccessToken() {
return null;
return getAccessToken(mDefaultFileSourcePtr);
}

/**
Expand Down Expand Up @@ -132,5 +87,6 @@ public void createOfflineRegion(
*/

private native long createDefaultFileSource(String cachePath, String assetRoot);

private native void setAccessToken(long defaultFileSourcePtr, String accessToken);
private native String getAccessToken(long defaultFileSourcePtr);
}
18 changes: 17 additions & 1 deletion platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,20 @@ jlong JNICALL createDefaultFileSource(JNIEnv *env, jobject obj, jstring cachePat
return defaultFileSourcePtr;
}

void JNICALL setAccessToken(JNIEnv *env, jobject obj, jlong defaultFileSourcePtr, jstring accessToken_) {
mbgl::Log::Debug(mbgl::Event::JNI, "setAccessToken");
std::string accessToken = std_string_from_jstring(env, accessToken_);
mbgl::DefaultFileSource *defaultFileSource = reinterpret_cast<mbgl::DefaultFileSource *>(defaultFileSourcePtr);
defaultFileSource->setAccessToken(accessToken);
}

jstring JNICALL getAccessToken(JNIEnv *env, jobject obj, jlong defaultFileSourcePtr) {
mbgl::Log::Debug(mbgl::Event::JNI, "getAccessToken");
mbgl::DefaultFileSource *defaultFileSource = reinterpret_cast<mbgl::DefaultFileSource *>(defaultFileSourcePtr);
std::string accessToken = defaultFileSource->getAccessToken();
return std_string_to_jstring(env, accessToken);
}

// Offline calls end

}
Expand Down Expand Up @@ -2170,7 +2184,9 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
// Offline register begin

const std::vector<JNINativeMethod> offlineMethods = {
{"createDefaultFileSource", "(Ljava/lang/String;Ljava/lang/String;)J", reinterpret_cast<void *>(&createDefaultFileSource)}
{"createDefaultFileSource", "(Ljava/lang/String;Ljava/lang/String;)J", reinterpret_cast<void *>(&createDefaultFileSource)},
{"setAccessToken", "(JLjava/lang/String;)V", reinterpret_cast<void *>(&setAccessToken)},
{"getAccessToken", "(J)Ljava/lang/String;", reinterpret_cast<void *>(&getAccessToken)}
};

if (env->RegisterNatives(customOfflineManagerClass, offlineMethods.data(), offlineMethods.size()) < 0) {
Expand Down
10 changes: 1 addition & 9 deletions platform/default/mbgl/storage/offline_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,7 @@ void OfflineDatabase::ensureSchema() {
db->setBusyTimeout(Milliseconds::max());
} catch (mapbox::sqlite::Exception& ex) {
if (ex.code == SQLITE_CANTOPEN) {
try {
db = std::make_unique<Database>(path.c_str(), ReadWrite | Create);
} catch (...) {
// This throws an exception on Android: `unable to open database file`
Log::Error(Event::Database, "----- start -----");
Log::Error(Event::Database, util::toString(std::current_exception()));
Log::Error(Event::Database, "----- end -----");
throw;
}
db = std::make_unique<Database>(path.c_str(), ReadWrite | Create);
db->setBusyTimeout(Milliseconds::max());
} else if (ex.code == SQLITE_NOTADB) {
removeExisting();
Expand Down

0 comments on commit e2e0ed5

Please sign in to comment.