Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #4 from brianleroux/master

added task for jar, added droidzine evalAssetFile logic
  • Loading branch information...
commit fc38e274a612319b47be54cc574a96e45c00e783 2 parents 08d983f + bce5436
@divineprog authored
View
3  .gitignore
@@ -0,0 +1,3 @@
+bin/
+gen/
+local.properties
View
25 build.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project name="androidjs" default="help">
+
+ <property file="local.properties" />
+ <property file="build.properties" />
+ <property file="default.properties" />
+
+ <path id="android.antlibs">
+ <pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
+ <pathelement path="${sdk.dir}/tools/lib/sdklib.jar" />
+ <pathelement path="${sdk.dir}/tools/lib/androidprefs.jar" />
+ </path>
+
+ <taskdef name="setup"
+ classname="com.android.ant.SetupTask"
+ classpathref="android.antlibs" />
+
+ <setup />
+
+ <target name="jar" depends="compile">
+ <jar jarfile="bin/droidscript.jar" basedir="bin/classes" excludes="**/R*.class" />
+ </target>
+
+ <!-- fuck I love xml -->
+</project>
View
12 default.properties
@@ -0,0 +1,12 @@
+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must be checked in Version Control Systems.
+#
+# To customize properties used by the Ant build system use,
+# "build.properties", and override values to adapt the script to your
+# project structure.
+
+android.library=true
+# Project target.
+target=android-8
View
BIN  libs/js.jar
Binary file not shown
View
BIN  libs/rhino1_7R2.jar
Binary file not shown
View
36 src/comikit/droidscript/DroidScriptActivity.java
@@ -24,6 +24,10 @@
import android.view.MenuItem;
import android.view.View;
+import org.mozilla.javascript.commonjs.module.*;
+import org.mozilla.javascript.commonjs.module.Require;
+
+
/**
* Activity that has a JavaScript interpreter.
*
@@ -54,11 +58,18 @@ public void onCreate(Bundle savedInstanceState)
{
String filenameOrUrl = intent.getStringExtra("ScriptName");
String script = intent.getStringExtra("Script");
+ String scriptAssetFilName = intent.getStringExtra("ScriptAsset");
+
if (null != filenameOrUrl)
{
setScriptFileName(filenameOrUrl);
evalFileOrUrl(filenameOrUrl);
}
+ else
+ if (null != scriptAssetFilName)
+ {
+ evalAssetFile(scriptAssetFilName);
+ }
else
if (null != script)
{
@@ -187,6 +198,24 @@ public Object evalApplicationFile(final String fileName)
return null;
}
}
+
+ /**
+ * Run a script stored as an asset.
+ */
+ public Object evalAssetFile(final String scriptName)
+ {
+ try
+ {
+ return eval(
+ DroidScriptIO.create().readStringFromAssetFile(this, scriptName),
+ scriptName);
+ }
+ catch (Throwable e)
+ {
+ reportError(e);
+ return null;
+ }
+ }
/**
* Run a script on the SD card or at an url.
@@ -472,12 +501,14 @@ public static String convertMultiLineStringToJavaScript(String s)
return result.toString();
}
+
public static class Interpreter
{
Context context;
Scriptable scope;
-
+ Require require;
+
public Interpreter()
{
// Creates and enters a Context. The Context stores information
@@ -493,6 +524,9 @@ public Interpreter()
public Interpreter setActivity(Activity activity)
{
+ DroidScriptAssetProvider provider = new DroidScriptAssetProvider(activity);
+ Require require = new Require(context, scope, provider, null, null, true);
+ require.install(scope);
// Set the global JavaScript variable Activity.
ScriptableObject.putProperty(scope, "Activity", Context.javaToJS(activity, scope));
return this;
View
42 src/comikit/droidscript/DroidScriptAssetProvider.java
@@ -0,0 +1,42 @@
+package comikit.droidscript;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.BufferedReader;
+
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.commonjs.module.provider.*;
+import org.mozilla.javascript.ScriptRuntime;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.commonjs.module.*;
+
+import android.app.Activity;
+
+
+public class DroidScriptAssetProvider implements ModuleScriptProvider, Serializable
+{
+ private Activity activity;
+ private Context context;
+
+ DroidScriptAssetProvider(Activity a)
+ {
+ activity = a;
+ }
+
+ public ModuleScript getModuleScript(Context context, String moduleId, Scriptable paths) throws Exception
+ {
+ // activity.getAssets()
+ try {
+ InputStream stream = DroidScriptIO.create().openAssetFileInputStream(activity, moduleId + ".js");
+ BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
+ //ModuleSource source = new ModuleSource(reader, null, moduleId, null);
+ return new ModuleScript( context.compileReader(reader, moduleId, 1, null), moduleId);
+ }
+ catch(Exception e) {
+ throw e;
+ }
+ }
+}
View
202 src/comikit/droidscript/DroidScriptIO.java
@@ -0,0 +1,202 @@
+package comikit.droidscript;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.net.URL;
+
+import android.app.Activity;
+import android.content.res.AssetManager;
+import android.os.Environment;
+
+/**
+ * Helper class for IO.
+ * @author Mikael Kindborg
+ * Email: mikael.kindborg@gmail.com
+ * Blog: divineprogrammer@blogspot.com
+ * Twitter: @divineprog
+ * Copyright (c) Mikael Kindborg 2010
+ * Source code license: MIT
+ */
+public class DroidScriptIO
+{
+ private DroidScriptIO()
+ {
+ }
+
+ public static DroidScriptIO create()
+ {
+ return new DroidScriptIO();
+ }
+
+ public static WebIO web()
+ {
+ return new WebIO();
+ }
+
+ public static AssetIO asset()
+ {
+ return new AssetIO();
+ }
+
+ public interface InputHandler
+ {
+ void handle(String resultCode, Object data);
+ }
+
+ public static class WebIO
+ {
+ public WebIO readStringFromUrl(final String url, final InputHandler handler)
+ {
+ new Thread()
+ {
+ public void run()
+ {
+ DroidScriptIO io = DroidScriptIO.create();
+ try
+ {
+ String data = io.readString(io.openUrl(url));
+ handler.handle("OK", data);
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ handler.handle("ERROR", e.toString());
+ }
+ }
+ }
+ .start();
+ return this;
+ }
+ }
+
+ public static class AssetIO
+ {
+ public AssetIO readString(String fileName, InputHandler handler)
+ {
+ handler.handle("OK", "");
+ return this;
+ }
+ }
+
+ public String readStringFromFileOrUrl(String fileOrUrl) throws Exception
+ {
+ if (fileOrUrl.startsWith("http://"))
+ {
+ return readString(openUrl(fileOrUrl));
+ }
+ else
+ {
+ return readString(openExternalStorageFileInputStream(fileOrUrl));
+ }
+ }
+
+ public void writeStringToFile(String file, String data) throws Exception
+ {
+ writeString(openExternalStorageFileOutputStream(file), data);
+ }
+
+ public String readStringFromApplicationFile(Activity activity, String filename) throws Exception
+ {
+ return readString(openApplicationFileInputStream(activity, filename));
+ }
+
+ public InputStream openApplicationFileInputStream(Activity activity, String filename) throws Exception
+ {
+ return activity.openFileInput(filename);
+ }
+
+ public String readStringFromAssetFile(Activity activity, String filename) throws Exception
+ {
+ return readString(openAssetFileInputStream(activity, filename));
+ }
+
+ public InputStream openAssetFileInputStream(Activity activity, String filename) throws Exception
+ {
+ return activity.getAssets().open(filename, AssetManager.ACCESS_BUFFER);
+ }
+
+ public InputStream openExternalStorageFileInputStream(String filename) throws Exception
+ {
+ // Might be useful: content://com.android.htmlfileprovider/sdcard/example/file.html
+ return new FileInputStream(
+ new File(Environment.getExternalStorageDirectory() + "/" + filename));
+ }
+
+ public OutputStream openExternalStorageFileOutputStream(String filename) throws Exception
+ {
+ return new FileOutputStream(
+ new File(Environment.getExternalStorageDirectory() + "/" + filename));
+ }
+
+ public boolean externalStorageFileExists(String filename) throws Exception
+ {
+ return new File(Environment.getExternalStorageDirectory() + "/" + filename).exists();
+ }
+
+ public boolean externalStorageCreateDirectory(String path) throws Exception
+ {
+ return new File(Environment.getExternalStorageDirectory() + "/" + path).mkdirs();
+ }
+
+ public void installFile(String file, String url) throws Exception
+ {
+ writeStringToFile(file, readStringFromFileOrUrl(url));
+ }
+
+ public InputStream openUrl(String url) throws Exception
+ {
+ return new URL(url).openStream();
+ }
+
+ public String readString(InputStream stream) throws Exception
+ {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
+ StringBuffer dataBuf = new StringBuffer();
+ while (true)
+ {
+ String data = reader.readLine();
+ if (null == data) { break; }
+ dataBuf.append(data + "\n");
+ }
+ reader.close();
+ return dataBuf.toString();
+ }
+
+ public void writeString(OutputStream stream, String data) throws Exception
+ {
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stream));
+ writer.write(data);
+ writer.close();
+ }
+
+ public String readStringRaw(InputStream stream) throws Exception
+ {
+ BufferedInputStream bufIn = new BufferedInputStream(stream);
+ StringBuffer dataBuf = new StringBuffer();
+ while (true)
+ {
+ int data = bufIn.read();
+ if (data == -1)
+ {
+ break;
+ }
+ else
+ {
+ dataBuf.append((char) data);
+ }
+ }
+
+ bufIn.close();
+ stream.close();
+
+ return dataBuf.toString();
+ }
+}
View
2  src/comikit/droidscript/DroidScriptServer.java
@@ -330,7 +330,7 @@ public void run()
Socket socket = null;
DefaultHttpServerConnection connection = null;
- // TODO: Kolla hur detta är gjort i webbservern i kodexemplet:
+ // TODO: Kolla hur detta aar gjort i webbservern i kodexemplet:
// http://svn.apache.org/repos/asf/httpcomponents/httpcore/branches/4.0.x/httpcore/src/examples/org/apache/http/examples/ElementalHttpServer.java
try
Please sign in to comment.
Something went wrong with that request. Please try again.