ruboto-core 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,126 @@
1
+ /**********************************************************************************************
2
+ *
3
+ * RubotoBroadcastReceiver.java is generated from RubotoClass.java.erb. Any changes needed in should be
4
+ * made within the erb template or they will be lost.
5
+ *
6
+ */
7
+
8
+ package org.ruboto;
9
+
10
+ import java.io.IOException;
11
+ import android.content.BroadcastReceiver;
12
+ import android.os.Handler;
13
+ import android.os.Bundle;
14
+
15
+ import org.jruby.Ruby;
16
+ import org.jruby.javasupport.util.RuntimeHelpers;
17
+ import org.jruby.runtime.builtin.IRubyObject;
18
+ import org.jruby.javasupport.JavaUtil;
19
+ import org.jruby.exceptions.RaiseException;
20
+
21
+ public abstract class RubotoBroadcastReceiver extends BroadcastReceiver
22
+
23
+ {
24
+ public static final int CB_CHECK_SYNCHRONOUS_HINT = 0;
25
+ public static final int CB_PEEK_SERVICE = 1;
26
+ public static final int CB_LAST = 2;
27
+
28
+ private boolean[] callbackOptions = new boolean [CB_LAST];
29
+
30
+ private String remoteVariable = "";
31
+
32
+ private final Handler loadingHandler = new Handler();
33
+ private IRubyObject __this__;
34
+ private Ruby __ruby__;
35
+ private String scriptName;
36
+ public Object[] args;
37
+
38
+ public RubotoBroadcastReceiver setRemoteVariable(String var) {
39
+ remoteVariable = ((var == null) ? "" : (var + "."));
40
+ return this;
41
+ }
42
+
43
+ /**********************************************************************************
44
+ *
45
+ * Callback management
46
+ */
47
+
48
+ public void requestCallback(int id) {
49
+ callbackOptions[id] = true;
50
+ }
51
+
52
+ public void removeCallback(int id) {
53
+ callbackOptions[id] = false;
54
+ }
55
+
56
+ /*
57
+ * Activity Lifecycle: onCreate
58
+ */
59
+
60
+ @Override
61
+ public void onReceive(android.content.Context arg0, android.content.Intent arg1) {
62
+
63
+ args = new Object[2];
64
+ args[0] = arg0;
65
+ args[1] = arg1;
66
+
67
+
68
+ if (Script.getRuby() == null){
69
+ Script.setUpJRuby(null);
70
+ }
71
+ Script.defineGlobalVariable("$broadcast_receiver", this);
72
+
73
+
74
+ __ruby__ = Script.getRuby();
75
+ __this__ = JavaUtil.convertJavaToRuby(__ruby__, RubotoBroadcastReceiver.this);
76
+
77
+ try {
78
+ new Script(scriptName).execute();
79
+ }
80
+ catch(IOException e){
81
+
82
+ }
83
+ }
84
+
85
+ public void setScriptName(String name){
86
+ scriptName = name;
87
+ }
88
+
89
+
90
+
91
+ /*********************************************************************************
92
+ *
93
+ * Ruby Generated Callback Methods
94
+ */
95
+
96
+ /*
97
+ * android.content.BroadcastReceiver
98
+ */
99
+
100
+ public void checkSynchronousHint() {
101
+ if (callbackOptions[CB_CHECK_SYNCHRONOUS_HINT]) {
102
+
103
+ try {
104
+ RuntimeHelpers.invoke(__ruby__.getCurrentContext(), __this__, "check_synchronous_hint");
105
+ } catch (RaiseException re) {
106
+ re.printStackTrace(__ruby__.getErrorStream());
107
+
108
+ }
109
+ }
110
+ }
111
+
112
+ public android.os.IBinder peekService(android.content.Context arg0, android.content.Intent arg1) {
113
+ if (callbackOptions[CB_PEEK_SERVICE]) {
114
+
115
+ try {
116
+ return (android.os.IBinder)RuntimeHelpers.invoke(__ruby__.getCurrentContext(), __this__, "peek_service", JavaUtil.convertJavaToRuby(__ruby__, arg0), JavaUtil.convertJavaToRuby(__ruby__, arg1)).toJava(android.os.IBinder.class);
117
+ } catch (RaiseException re) {
118
+ re.printStackTrace(__ruby__.getErrorStream());
119
+ return null;
120
+ }
121
+ } else {
122
+ return null;
123
+ }
124
+ }
125
+
126
+ }
@@ -0,0 +1,233 @@
1
+ /**********************************************************************************************
2
+ *
3
+ * RubotoService.java is generated from RubotoClass.java.erb. Any changes needed in should be
4
+ * made within the erb template or they will be lost.
5
+ *
6
+ */
7
+
8
+ package org.ruboto;
9
+
10
+ import java.io.IOException;
11
+ import android.app.Service;
12
+ import android.os.Handler;
13
+ import android.os.Bundle;
14
+
15
+ import org.jruby.Ruby;
16
+ import org.jruby.javasupport.util.RuntimeHelpers;
17
+ import org.jruby.runtime.builtin.IRubyObject;
18
+ import org.jruby.javasupport.JavaUtil;
19
+ import org.jruby.exceptions.RaiseException;
20
+
21
+ public abstract class RubotoService extends Service
22
+
23
+ {
24
+ public static final int CB_LOW_MEMORY = 0;
25
+ public static final int CB_DUMP = 1;
26
+ public static final int CB_UNBIND = 2;
27
+ public static final int CB_START_COMMAND = 3;
28
+ public static final int CB_FINALIZE = 4;
29
+ public static final int CB_START = 5;
30
+ public static final int CB_DESTROY = 6;
31
+ public static final int CB_REBIND = 7;
32
+ public static final int CB_CONFIGURATION_CHANGED = 8;
33
+ public static final int CB_BIND = 9;
34
+ public static final int CB_LAST = 10;
35
+
36
+ private boolean[] callbackOptions = new boolean [CB_LAST];
37
+
38
+ private String remoteVariable = "";
39
+
40
+ private final Handler loadingHandler = new Handler();
41
+ private IRubyObject __this__;
42
+ private Ruby __ruby__;
43
+ private String scriptName;
44
+ public Object[] args;
45
+
46
+ public RubotoService setRemoteVariable(String var) {
47
+ remoteVariable = ((var == null) ? "" : (var + "."));
48
+ return this;
49
+ }
50
+
51
+ /**********************************************************************************
52
+ *
53
+ * Callback management
54
+ */
55
+
56
+ public void requestCallback(int id) {
57
+ callbackOptions[id] = true;
58
+ }
59
+
60
+ public void removeCallback(int id) {
61
+ callbackOptions[id] = false;
62
+ }
63
+
64
+ /*
65
+ * Activity Lifecycle: onCreate
66
+ */
67
+
68
+ @Override
69
+ public void onCreate() {
70
+
71
+ args = new Object[0];
72
+ super.onCreate();
73
+
74
+
75
+ if (Script.getRuby() == null){
76
+ Script.setUpJRuby(null);
77
+ }
78
+ Script.defineGlobalVariable("$service", this);
79
+
80
+
81
+ __ruby__ = Script.getRuby();
82
+ __this__ = JavaUtil.convertJavaToRuby(__ruby__, RubotoService.this);
83
+
84
+ try {
85
+ new Script(scriptName).execute();
86
+ }
87
+ catch(IOException e){
88
+
89
+ }
90
+ }
91
+
92
+ public void setScriptName(String name){
93
+ scriptName = name;
94
+ }
95
+
96
+
97
+
98
+ /*********************************************************************************
99
+ *
100
+ * Ruby Generated Callback Methods
101
+ */
102
+
103
+ /*
104
+ * android.app.Service
105
+ */
106
+
107
+ public void onLowMemory() {
108
+ if (callbackOptions[CB_LOW_MEMORY]) {
109
+
110
+ try {
111
+ RuntimeHelpers.invoke(__ruby__.getCurrentContext(), __this__, "on_low_memory");
112
+ } catch (RaiseException re) {
113
+ re.printStackTrace(__ruby__.getErrorStream());
114
+
115
+ }
116
+ }
117
+ }
118
+
119
+ public void dump(java.io.FileDescriptor arg0, java.io.PrintWriter arg1, java.lang.String[] arg2) {
120
+ if (callbackOptions[CB_DUMP]) {
121
+
122
+ try {
123
+ RuntimeHelpers.invoke(__ruby__.getCurrentContext(), __this__, "dump", JavaUtil.convertJavaToRuby(__ruby__, arg0), JavaUtil.convertJavaToRuby(__ruby__, arg1), JavaUtil.convertJavaToRuby(__ruby__, arg2));
124
+ } catch (RaiseException re) {
125
+ re.printStackTrace(__ruby__.getErrorStream());
126
+
127
+ }
128
+ }
129
+ }
130
+
131
+ public boolean onUnbind(android.content.Intent arg0) {
132
+ if (callbackOptions[CB_UNBIND]) {
133
+
134
+ try {
135
+ return (Boolean)RuntimeHelpers.invoke(__ruby__.getCurrentContext(), __this__, "on_unbind", JavaUtil.convertJavaToRuby(__ruby__, arg0)).toJava(boolean.class);
136
+ } catch (RaiseException re) {
137
+ re.printStackTrace(__ruby__.getErrorStream());
138
+ return false;
139
+ }
140
+ } else {
141
+ return false;
142
+ }
143
+ }
144
+
145
+ public int onStartCommand(android.content.Intent arg0, int arg1, int arg2) {
146
+ if (callbackOptions[CB_START_COMMAND]) {
147
+
148
+ try {
149
+ return (Integer)RuntimeHelpers.invoke(__ruby__.getCurrentContext(), __this__, "on_start_command", JavaUtil.convertJavaToRuby(__ruby__, arg0), JavaUtil.convertJavaToRuby(__ruby__, arg1), JavaUtil.convertJavaToRuby(__ruby__, arg2)).toJava(int.class);
150
+ } catch (RaiseException re) {
151
+ re.printStackTrace(__ruby__.getErrorStream());
152
+ return 0;
153
+ }
154
+ } else {
155
+ return 0;
156
+ }
157
+ }
158
+
159
+ public void finalize() {
160
+ if (callbackOptions[CB_FINALIZE]) {
161
+
162
+ try {
163
+ RuntimeHelpers.invoke(__ruby__.getCurrentContext(), __this__, "finalize");
164
+ } catch (RaiseException re) {
165
+ re.printStackTrace(__ruby__.getErrorStream());
166
+
167
+ }
168
+ }
169
+ }
170
+
171
+ public void onStart(android.content.Intent arg0, int arg1) {
172
+ if (callbackOptions[CB_START]) {
173
+
174
+ try {
175
+ RuntimeHelpers.invoke(__ruby__.getCurrentContext(), __this__, "on_start", JavaUtil.convertJavaToRuby(__ruby__, arg0), JavaUtil.convertJavaToRuby(__ruby__, arg1));
176
+ } catch (RaiseException re) {
177
+ re.printStackTrace(__ruby__.getErrorStream());
178
+
179
+ }
180
+ }
181
+ }
182
+
183
+ public void onDestroy() {
184
+ if (callbackOptions[CB_DESTROY]) {
185
+
186
+ try {
187
+ RuntimeHelpers.invoke(__ruby__.getCurrentContext(), __this__, "on_destroy");
188
+ } catch (RaiseException re) {
189
+ re.printStackTrace(__ruby__.getErrorStream());
190
+
191
+ }
192
+ }
193
+ }
194
+
195
+ public void onRebind(android.content.Intent arg0) {
196
+ if (callbackOptions[CB_REBIND]) {
197
+
198
+ try {
199
+ RuntimeHelpers.invoke(__ruby__.getCurrentContext(), __this__, "on_rebind", JavaUtil.convertJavaToRuby(__ruby__, arg0));
200
+ } catch (RaiseException re) {
201
+ re.printStackTrace(__ruby__.getErrorStream());
202
+
203
+ }
204
+ }
205
+ }
206
+
207
+ public void onConfigurationChanged(android.content.res.Configuration arg0) {
208
+ if (callbackOptions[CB_CONFIGURATION_CHANGED]) {
209
+
210
+ try {
211
+ RuntimeHelpers.invoke(__ruby__.getCurrentContext(), __this__, "on_configuration_changed", JavaUtil.convertJavaToRuby(__ruby__, arg0));
212
+ } catch (RaiseException re) {
213
+ re.printStackTrace(__ruby__.getErrorStream());
214
+
215
+ }
216
+ }
217
+ }
218
+
219
+ public android.os.IBinder onBind(android.content.Intent arg0) {
220
+ if (callbackOptions[CB_BIND]) {
221
+
222
+ try {
223
+ return (android.os.IBinder)RuntimeHelpers.invoke(__ruby__.getCurrentContext(), __this__, "on_bind", JavaUtil.convertJavaToRuby(__ruby__, arg0)).toJava(android.os.IBinder.class);
224
+ } catch (RaiseException re) {
225
+ re.printStackTrace(__ruby__.getErrorStream());
226
+ return null;
227
+ }
228
+ } else {
229
+ return null;
230
+ }
231
+ }
232
+
233
+ }
@@ -0,0 +1,30 @@
1
+ package org.ruboto;
2
+
3
+ import android.content.Context;
4
+ import android.graphics.Canvas;
5
+ import android.util.AttributeSet;
6
+ import android.view.View;
7
+
8
+ public class RubotoView extends View {
9
+ public RubotoView(Context context) {
10
+ super(context);
11
+ }
12
+
13
+ public RubotoView(Context context, AttributeSet attrs) {
14
+ super(context, attrs);
15
+ }
16
+
17
+ public RubotoView(Context context, AttributeSet attrs, int defStyle) {
18
+ super(context, attrs, defStyle);
19
+ }
20
+
21
+ @Override
22
+ protected void onDraw(Canvas canvas) {
23
+ ((RubotoActivity) getContext()).onDraw(this, canvas);
24
+ }
25
+
26
+ @Override
27
+ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
28
+ ((RubotoActivity) getContext()).onSizeChanged(this, w, h, oldw, oldh);
29
+ }
30
+ }
@@ -0,0 +1,231 @@
1
+ package org.ruboto;
2
+
3
+ import java.io.BufferedReader;
4
+ import java.io.BufferedWriter;
5
+ import java.io.BufferedOutputStream;
6
+ import java.io.File;
7
+ import java.io.FileReader;
8
+ import java.io.FileWriter;
9
+ import java.io.FilenameFilter;
10
+ import java.io.FileOutputStream;
11
+ import java.io.IOException;
12
+ import java.io.PrintStream;
13
+ import java.io.InputStream;
14
+ import java.io.OutputStream;
15
+ import java.util.ArrayList;
16
+ import java.util.Arrays;
17
+ import java.util.List;
18
+
19
+ import org.jruby.Ruby;
20
+ import org.jruby.RubyInstanceConfig;
21
+ import org.jruby.exceptions.RaiseException;
22
+ import org.jruby.javasupport.JavaUtil;
23
+ import org.jruby.parser.EvalStaticScope;
24
+ import org.jruby.runtime.DynamicScope;
25
+ import org.jruby.runtime.ThreadContext;
26
+ import org.jruby.runtime.scope.ManyVarsDynamicScope;
27
+
28
+ import android.os.Environment;
29
+ import android.util.Log;
30
+ import android.content.res.AssetManager;
31
+ import org.apache.http.client.methods.HttpGet;
32
+ import org.apache.http.impl.client.BasicResponseHandler;
33
+ import org.apache.http.impl.client.DefaultHttpClient;
34
+
35
+ public class Script {
36
+ private static String scriptsDir = "scripts";
37
+ private static File scriptsDirFile = null;
38
+
39
+ private String name = null;
40
+ private static Ruby ruby;
41
+ private static DynamicScope scope;
42
+ private static boolean initialized = false;
43
+
44
+ private String contents = null;
45
+
46
+ public static final String TAG = "RUBOTO"; //for logging
47
+
48
+ /*************************************************************************************************
49
+ *
50
+ * Static Methods: JRuby Execution
51
+ */
52
+
53
+ public static final FilenameFilter RUBY_FILES = new FilenameFilter() {
54
+ public boolean accept(File dir, String fname) {
55
+ return fname.endsWith(".rb");
56
+ }
57
+ };
58
+
59
+ public static boolean initialized() {
60
+ return initialized;
61
+ }
62
+
63
+ public static synchronized Ruby setUpJRuby(PrintStream out) {
64
+ if (ruby == null) {
65
+ RubyInstanceConfig config = new RubyInstanceConfig();
66
+ config.setCompileMode(RubyInstanceConfig.CompileMode.OFF);
67
+
68
+ config.setLoader(Script.class.getClassLoader());
69
+ if (scriptsDir != null) config.setCurrentDirectory(scriptsDir);
70
+
71
+ if (out != null) {
72
+ config.setOutput(out);
73
+ config.setError(out);
74
+ }
75
+
76
+ /* Set up Ruby environment */
77
+ ruby = Ruby.newInstance(config);
78
+
79
+ ThreadContext context = ruby.getCurrentContext();
80
+ DynamicScope currentScope = context.getCurrentScope();
81
+ scope = new ManyVarsDynamicScope(new EvalStaticScope(currentScope.getStaticScope()), currentScope);
82
+
83
+ initialized = true;
84
+ }
85
+
86
+ return ruby;
87
+ }
88
+
89
+ public static String execute(String code) {
90
+ if (!initialized) return null;
91
+ try {
92
+ return ruby.evalScriptlet(code, scope).inspect().asJavaString();
93
+ } catch (RaiseException re) {
94
+ re.printStackTrace(ruby.getErrorStream());
95
+ return null;
96
+ }
97
+ }
98
+
99
+ public static void defineGlobalConstant(String name, Object object) {
100
+ ruby.defineGlobalConstant(name, JavaUtil.convertJavaToRuby(ruby, object));
101
+ }
102
+
103
+ public static void defineGlobalVariable(String name, Object object) {
104
+ ruby.getGlobalVariables().set(name, JavaUtil.convertJavaToRuby(ruby, object));
105
+ }
106
+
107
+ public static Ruby getRuby() {
108
+ return ruby;
109
+ }
110
+
111
+ /*************************************************************************************************
112
+ *
113
+ * Static Methods: Scripts Directory
114
+ */
115
+
116
+ public static void setDir(String dir) {
117
+ scriptsDir = dir;
118
+ scriptsDirFile = new File(dir);
119
+ if (ruby != null) ruby.setCurrentDirectory(scriptsDir);
120
+ }
121
+
122
+ public static String getDir() {
123
+ return scriptsDir;
124
+ }
125
+
126
+ public static File getDirFile() {
127
+ return scriptsDirFile;
128
+ }
129
+
130
+ public static Boolean configDir(String noSdcard) {
131
+ setDir(noSdcard);
132
+
133
+ /* Create directory if it doesn't exist */
134
+ if (!scriptsDirFile.exists()) {
135
+ // TODO check return code
136
+ scriptsDirFile.mkdir();
137
+ return true;
138
+ }
139
+
140
+ return false;
141
+ }
142
+
143
+ private static void copyScripts(String from, File to, AssetManager assets) {
144
+ try {
145
+ byte[] buffer = new byte[8192];
146
+ for (String f : assets.list(from)) {
147
+ File dest = new File(to, f);
148
+
149
+ if (dest.exists())
150
+ continue;
151
+
152
+ Log.d(TAG, "copying file " + f);
153
+
154
+ InputStream is = assets.open(from+ "/" +f);
155
+ OutputStream fos = new BufferedOutputStream(new FileOutputStream(dest));
156
+
157
+ int n;
158
+ while ((n = is.read(buffer, 0, buffer.length)) != -1)
159
+ fos.write(buffer, 0, n);
160
+
161
+ is.close();
162
+ fos.close();
163
+ }
164
+ } catch (IOException iox) {
165
+ Log.e(TAG, "error copying demo scripts", iox);
166
+ }
167
+ }
168
+
169
+ public static void copyScriptsIfNeeded(String to, AssetManager assets) {
170
+ /* the if makes sure we only do this the first time */
171
+ if (configDir(to))
172
+ copyScripts("scripts", scriptsDirFile, assets);
173
+ }
174
+
175
+
176
+ /*************************************************************************************************
177
+ *
178
+ * Constructors
179
+ */
180
+
181
+ public Script(String name) {
182
+ this(name, null);
183
+ }
184
+
185
+ public Script(String name, String contents) {
186
+ this.name = name;
187
+ this.contents = contents;
188
+ File file = getFile();
189
+ }
190
+
191
+ /*************************************************************************************************
192
+ *
193
+ * Attribute Access
194
+ */
195
+
196
+ public String getName() {
197
+ return name;
198
+ }
199
+
200
+ public File getFile() {
201
+ return new File(getDir(), name);
202
+ }
203
+
204
+ public Script setName(String name) {
205
+ this.name = name;
206
+ // TODO: Other states possible
207
+ return this;
208
+ }
209
+
210
+ public String getContents() throws IOException {
211
+ BufferedReader buffer = new BufferedReader(new FileReader(getFile()));
212
+ StringBuilder source = new StringBuilder();
213
+ while (true) {
214
+ String line = buffer.readLine();
215
+ if (line == null) break;
216
+ source.append(line).append("\n");
217
+ }
218
+ buffer.close();
219
+ contents = source.toString();
220
+ return contents;
221
+ }
222
+
223
+ /*************************************************************************************************
224
+ *
225
+ * Script Actions
226
+ */
227
+
228
+ public String execute() throws IOException {
229
+ return Script.execute(getContents());
230
+ }
231
+ }