Skip to content

Commit

Permalink
Allow context factory configuration. Reference #8
Browse files Browse the repository at this point in the history
  • Loading branch information
f43nd1r committed Jan 4, 2021
1 parent c43fddf commit 900cc56
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
* @author F43nd1r
* @since 11.01.2016
*/
@VisibleForTesting
public class AndroidContextFactory extends ContextFactory {

private final File cacheDirectory;
Expand All @@ -50,9 +49,8 @@ public AndroidContextFactory(File cacheDirectory) {
* @param parent the parent of the create classloader
* @return a new ClassLoader
*/
@VisibleForTesting
@Override
public BaseAndroidClassLoader createClassLoader(ClassLoader parent) {
protected BaseAndroidClassLoader createClassLoader(ClassLoader parent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return new InMemoryAndroidClassLoader(parent);
}
Expand Down
29 changes: 19 additions & 10 deletions src/main/java/com/faendir/rhino_android/RhinoAndroidHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.faendir.rhino_android;

import androidx.annotation.VisibleForTesting;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.SecurityController;
Expand All @@ -35,8 +34,10 @@ public class RhinoAndroidHelper {

/**
* Constructs a new helper using the default temporary directory.
* Note: It is recommended to use a custom directory, so no permission problems occur.
*
* @deprecated permission issues may occur.
*/
@Deprecated
public RhinoAndroidHelper() {
this(new File(System.getProperty("java.io.tmpdir", "."), "classes"));
}
Expand All @@ -59,6 +60,15 @@ public RhinoAndroidHelper(File cacheDirectory) {
this.cacheDirectory = cacheDirectory;
}

/**
* @return a context prepared for android
* @deprecated use {@link #enterContext()} instead
*/
@Deprecated
public static Context prepareContext() {
return new RhinoAndroidHelper().enterContext();
}

/**
* call this instead of {@link Context#enter()}
*
Expand All @@ -73,26 +83,25 @@ public Context enterContext() {
/**
* @return The Context factory which has to be used on android.
*/
@VisibleForTesting
public AndroidContextFactory getContextFactory() {
AndroidContextFactory factory;
if (!ContextFactory.hasExplicitGlobal()) {
factory = new AndroidContextFactory(cacheDirectory);
factory = createAndroidContextFactory(cacheDirectory);
ContextFactory.getGlobalSetter().setContextFactoryGlobal(factory);
} else if (!(ContextFactory.getGlobal() instanceof AndroidContextFactory)) {
throw new IllegalStateException("Cannot initialize factory for Android Rhino: There is already another factory");
} else {
factory = (AndroidContextFactory) ContextFactory.getGlobal();
factory = (AndroidContextFactory)ContextFactory.getGlobal();
}
return factory;
}

/**
* @return a context prepared for android
* @deprecated use {@link #enterContext()} instead
* Override this to modify the context factory.
*
* @return a new {@link AndroidContextFactory}
*/
@Deprecated
public static Context prepareContext() {
return new RhinoAndroidHelper().enterContext();
protected AndroidContextFactory createAndroidContextFactory(File cacheDirectory) {
return new AndroidContextFactory(cacheDirectory);
}
}

0 comments on commit 900cc56

Please sign in to comment.