sinkuri256 / android-scripting

Automatically exported from code.google.com/p/android-scripting
0 stars 0 forks source link

Allow to embedding more than 1 script as raw resource #470

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
# HG changeset patch
# User Naranjo Manuel Francisco <manuel@aircable.net>
# Date 1286731322 10800
# Node ID ff8831a5f99f04eb06a5a5348d1497986b143d1f
# Parent  d43a27fc9cf7e223872978062fce850a7aab8036
Allow to embedded more than 1 file as raw resource

This patch allows to include more than 1 resource in res/raw, copying the 
content
of res/raw in the same place where the script is going to be executed.

This allowed me to include more than 1 Python script, been able from the main
script to import the others, and include html resources for a webview, allowing
to create a true end user application in just one APK the right way!

I know the patch isn't perfect, but I wanted to send it before moving forward, 
to
get comments about it.

diff -r d43a27fc9cf7 -r ff8831a5f99f 
android/ScriptForAndroidTemplate/src/com/dummy/fooforandroid/ScriptService.java
--- 
a/android/ScriptForAndroidTemplate/src/com/dummy/fooforandroid/ScriptService.jav
a   Thu Sep 16 18:02:26 2010 +0200
+++ 
b/android/ScriptForAndroidTemplate/src/com/dummy/fooforandroid/ScriptService.jav
a   Sun Oct 10 14:22:02 2010 -0300
@@ -18,6 +18,8 @@

 import android.app.Service;
 import android.content.Intent;
+import android.content.res.Resources;
+import android.content.res.Resources.NotFoundException;
 import android.os.Binder;
 import android.os.IBinder;

@@ -36,6 +38,8 @@
 import com.googlecode.android_scripting.jsonrpc.RpcReceiverManager;

 import java.io.File;
+import java.io.InputStream;
+import java.text.DateFormat.Field;
 import java.util.concurrent.CountDownLatch;

 /**
@@ -65,6 +69,33 @@
    mInterpreterConfiguration = ((BaseApplication) getApplication()).getInterpreterConfiguration();
  }

+  private void copyResourcesToLocal(){
+    R.raw a = new R.raw();
+    java.lang.reflect.Field[] t = R.raw.class.getFields();
+    Resources resources = getResources();
+    for (int i = 0; i < t.length; i++){
+        String name;
+               try {
+                       name = resources.getText(t[i].getInt(a)).toString();
+                       String sFileName = name.substring(name.lastIndexOf('/') 
+ 1, name.length());
+                       InputStream content = 
getResources().openRawResource(t[i].getInt(a));
+                   // Copies script to internal memory.
+                   sFileName = 
InterpreterUtils.getInterpreterRoot(this).getAbsolutePath() + "/" + sFileName;
+                   FileUtils.copyFromStream(sFileName, content );
+               } catch (NotFoundException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               } catch (IllegalArgumentException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               } catch (IllegalAccessException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
+    }
+
+  }
+
  @Override
  public void onStart(Intent intent, final int startId) {
    super.onStart(intent, startId);
@@ -83,7 +114,7 @@
      stopSelf(startId);
      return;
    }
-
+
    // Copies script to internal memory.
    fileName = InterpreterUtils.getInterpreterRoot(this).getAbsolutePath() + "/" + fileName;
    File script = new File(fileName);
@@ -91,6 +122,8 @@
    if (!script.exists()) {
      script = FileUtils.copyFromStream(fileName, getResources().openRawResource(Script.ID));
    }
+
+    copyResourcesToLocal(); // Copy all resources

    if (Script.getFileExtension(this).equals(HtmlInterpreter.HTML_EXTENSION)) {
      HtmlActivityTask htmlTask =

Original issue reported on code.google.com by damonkoh...@gmail.com on 6 Nov 2010 at 5:49

GoogleCodeExporter commented 9 years ago

Original comment by damonkoh...@gmail.com on 6 Nov 2010 at 9:44

GoogleCodeExporter commented 9 years ago
Was this patch ever included in main stream? I made a clone from the main 
repository and it's not available there.

Original comment by naranjo....@gmail.com on 24 Nov 2010 at 5:42

GoogleCodeExporter commented 9 years ago
I made a new version of the patch, now it checks if there's need to modify the 
local resource, it does a simple text compare, using something like md5 would 
be faster and possibly safer but I'm lazy and this is good enough ;)

This patch was made against r3 I can't get the latest trunk to work correctly, 
something has changed and I don't have the time to check what did.

I made my own source clone available @ 
http://code.google.com/r/naranjomanuel-sl4a/source/browse

Original comment by naranjo....@gmail.com on 25 Nov 2010 at 4:11

Attachments:

GoogleCodeExporter commented 9 years ago
Patch has been included.

Original comment by rjmatthews62 on 5 Mar 2011 at 2:23