Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Android Platform bootstrap. #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 12 additions & 24 deletions src/main/java/org/bridj/AndroidSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,20 @@
*/
public class AndroidSupport extends PlatformSupport {

private volatile Application app;

AndroidSupport() {
}

synchronized void setApp(Application application) {
if (this.app != null && application != null && this.app != application) {
throw new IllegalArgumentException("Android Application has already been set to a different value : " + this.app);
}

this.app = application;
}

public static void setApplication(Application application) {
((AndroidSupport) PlatformSupport.getInstance()).setApp(application);
Application app()
{
return AndroidWorkaround.getApplication();
}

String adviseToSetApp() {
return app == null ? "" : "Please use AndroidSupport.setApplication(Application). ";
return app() == null ? "" : "Please use AndroidSupport.setApplication(Application). ";
}
volatile AndroidClassDefiner classDefiner;

synchronized File getCacheDir() throws FileNotFoundException {
File cacheDir = null;
if (app != null) {
cacheDir = app.getCacheDir();
if (app() != null) {
cacheDir = app().getCacheDir();
}

if (cacheDir == null || !cacheDir.isDirectory() || !cacheDir.canWrite()) {
Expand Down Expand Up @@ -103,11 +91,11 @@ String getLibFileName(String libName) {
synchronized File getNativeLibraryDir(String someBundledNativeLibraryName) throws FileNotFoundException {
//String someKnownResource =
File f = null;
if (app != null) {
if (app() != null) {
try {
// ApplicationInfo.nativeLibraryDir is only available from API level 9 and later
// http://developer.android.com/reference/android/content/pm/ApplicationInfo.html#nativeLibraryDir
f = (File) ApplicationInfo.class.getField("nativeLibraryDir").get(app.getApplicationInfo());
f = (File) ApplicationInfo.class.getField("nativeLibraryDir").get(app().getApplicationInfo());
} catch (Throwable th) {
}
}
Expand All @@ -124,8 +112,8 @@ synchronized File getNativeLibraryDir(String someBundledNativeLibraryName) throw
}

synchronized File getApplicationDataDir(String someKnownResource) throws FileNotFoundException {
if (app != null) {
return new File(app.getApplicationInfo().dataDir);
if (app() != null) {
return new File(app().getApplicationInfo().dataDir);
} else {
return new File(new File(Environment.getDataDirectory(), "data"), getPackageName(someKnownResource));
}
Expand All @@ -142,8 +130,8 @@ public synchronized NativeLibrary loadNativeLibrary(String name) throws IOExcept
}

synchronized String getPackageName(String someKnownResource) throws FileNotFoundException {
if (app != null) {
return app.getPackageName();
if (app() != null) {
return app().getPackageName();
} else {
URL resource = Platform.getResource(someKnownResource);
if (resource == null) {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/bridj/AndroidWorkaround.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.bridj;

import android.app.Application;

/**
* Created by atsushi on 15/06/28.
*/
public class AndroidWorkaround {
private static Application app;

public static void setApplication(Application application) {
app = application;
}

public static Application getApplication()
{
return app;
}
}
6 changes: 5 additions & 1 deletion src/main/java/org/bridj/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,11 @@ static File extractEmbeddedLibraryResource(String name) throws IOException {
static File createTempDir(String prefix) throws IOException {
File dir;
for (int i = 0; i < maxTempFileAttempts; i++) {
dir = File.createTempFile(prefix, "");
if (Platform.isAndroid()) {
dir = AndroidWorkaround.getApplication().getDir(prefix, 0);
} else {
dir = File.createTempFile(prefix, "");
}
if (dir.delete() && dir.mkdirs()) {
return dir;
}
Expand Down