diff --git a/rakelib/ruboto.rake b/rakelib/ruboto.rake index 2abeb57..24ec377 100755 --- a/rakelib/ruboto.rake +++ b/rakelib/ruboto.rake @@ -207,7 +207,12 @@ file BUNDLE_JAR => [GEM_FILE, GEM_LOCK_FILE] do FileUtils.mkdir_p BUNDLE_PATH sh "bundle install --gemfile #{GEM_FILE} --path=#{BUNDLE_PATH}" - gem_path = Dir["#{BUNDLE_PATH}/*ruby/1.8/gems"][0] + + gem_paths = Dir["#{BUNDLE_PATH}/{{,j}ruby,rbx}/{1.8,1.9{,.1},shared}/gems"] + raise "Gem path not found" if gem_paths.empty? + raise "Found multiple gem paths: #{gem_paths}" if gem_paths.size > 1 + gem_path = gem_paths[0] + puts "Found gems in #{gem_path}" if package != 'org.ruboto.core' && JRUBY_JARS.none? { |f| File.exists? f } Dir.chdir gem_path do @@ -350,6 +355,17 @@ def package_installed? test = false return false end end + + sdcard_path = "/mnt/asec/#{package_name}#{i}/pkg.apk" + o = `adb shell ls -l #{sdcard_path}`.chomp + if o =~ /^-r-xr-xr-x system\s+root\s+(\d+) \d{4}-\d{2}-\d{2} \d{2}:\d{2} #{File.basename(sdcard_path)}$/ + apk_file = test ? TEST_APK_FILE : APK_FILE + if !File.exists?(apk_file) || $1.to_i == File.size(apk_file) + return true + else + return false + end + end end return nil end @@ -397,7 +413,10 @@ def install_apk when false puts "Package #{package} already installed, but of different size. Replacing package." output = `adb install -r #{APK_FILE} 2>&1` - return if $? == 0 && output !~ failure_pattern && output =~ success_pattern + if $? == 0 && output !~ failure_pattern && output =~ success_pattern + clear_update + return + end case $1 when 'INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES' puts "Found package signed with different certificate. Uninstalling it and retrying install." @@ -407,7 +426,9 @@ def install_apk end uninstall_apk end + puts "Installing package #{package}" output = `adb install #{APK_FILE} 2>&1` + puts output raise "Install failed (#{$?}) #{$1 ? "[#$1}]" : output}" if $? != 0 || output =~ failure_pattern || output !~ success_pattern clear_update end diff --git a/res/layout/get_ruboto_core.xml b/res/layout/get_ruboto_core.xml index 525b4b1..afa8151 100644 --- a/res/layout/get_ruboto_core.xml +++ b/res/layout/get_ruboto_core.xml @@ -6,8 +6,6 @@ android:orientation="vertical" android:gravity="center_horizontal|center_vertical" > - 0) { + Log.i("Showing splash"); requestWindowFeature(android.view.Window.FEATURE_NO_TITLE); setContentView(splash); } else { + Log.i("Showing progress"); loadingDialog = ProgressDialog.show(this, null, "Starting...", true, true); loadingDialog.setCanceledOnTouchOutside(false); loadingDialog.setOnCancelListener(new OnCancelListener() { diff --git a/src/org/ruboto/JRubyAdapter.java b/src/org/ruboto/JRubyAdapter.java index 899105b..b0213f7 100644 --- a/src/org/ruboto/JRubyAdapter.java +++ b/src/org/ruboto/JRubyAdapter.java @@ -19,48 +19,210 @@ public class JRubyAdapter { private static boolean isDebugBuild = false; private static PrintStream output = null; private static boolean initialized = false; - - private static String localContextScope = "SINGLETON"; + private static String localContextScope = "SINGLETON"; // FIXME(uwe): Why not CONCURRENT ? Help needed! private static String localVariableBehavior = "TRANSIENT"; - private static String RUBOTO_CORE_VERSION_NAME; - /************************************************************************************************* - * - * Static Methods: ScriptingContainer config + /** + * @deprecated As of Ruboto 0.7.1, replaced by {@link #runRubyMethod(Object receiver, String methodName, Object... args)} */ + @Deprecated public static void callMethod(Object receiver, String methodName, Object[] args) { + runRubyMethod(receiver, methodName, args); + } - public static void setLocalContextScope(String val) { - localContextScope = val; + /** + * @deprecated As of Ruboto 0.7.1, replaced by {@link #runRubyMethod(Object receiver, String methodName, Object... args)} + */ + @Deprecated public static void callMethod(Object object, String methodName, Object arg) { + runRubyMethod(object, methodName, arg); } - public static void setLocalVariableBehavior(String val) { - localVariableBehavior = val; + /** + * @deprecated As of Ruboto 0.7.1, replaced by {@link #runRubyMethod(Object receiver, String methodName, Object... args)} + */ + @Deprecated public static void callMethod(Object object, String methodName) { + runRubyMethod(object, methodName, new Object[] {}); + } + + /** + * @deprecated As of Ruboto 0.7.1, replaced by {@link #runRubyMethod(Class returnType, Object receiver, String methodName, Object... args)} + */ + @SuppressWarnings("unchecked") + @Deprecated public static T callMethod(Object receiver, String methodName, Object[] args, Class returnType) { + return runRubyMethod(returnType, receiver, methodName, args); + } + + /** + * @deprecated As of Ruboto 0.7.1, replaced by {@link #runRubyMethod(Class returnType, Object receiver, String methodName, Object... args)} + */ + @SuppressWarnings("unchecked") + @Deprecated public static T callMethod(Object receiver, String methodName, Object arg, Class returnType) { + return runRubyMethod(returnType, receiver, methodName, arg); + } + + /** + * @deprecated As of Ruboto 0.7.1, replaced by {@link #runRubyMethod(Class returnType, Object receiver, String methodName, Object... args)} + */ + @SuppressWarnings("unchecked") + @Deprecated public static T callMethod(Object receiver, String methodName, Class returnType) { + return runRubyMethod(returnType, receiver, methodName); } - /************************************************************************************************* - * - * Static Methods: JRuby Execution + /** + * @deprecated As of Ruboto 0.7.0, replaced by {@link #put(String name, Object object)} */ + @Deprecated public static void defineGlobalConstant(String name, Object object) { + put(name, object); + } + + /** + * @deprecated As of Ruboto 0.7.0, replaced by {@link #put(String name, Object object)} + */ + @Deprecated public static void defineGlobalVariable(String name, Object object) { + put(name, object); + } + + /** + * @deprecated As of Ruboto 0.7.0, replaced by {@link #runScriptlet(String code)} + */ + @Deprecated public static Object exec(String code) { + try { + Method runScriptletMethod = ruby.getClass().getMethod("runScriptlet", String.class); + return runScriptletMethod.invoke(ruby, code); + } catch (NoSuchMethodException nsme) { + throw new RuntimeException(nsme); + } catch (IllegalAccessException iae) { + throw new RuntimeException(iae); + } catch (java.lang.reflect.InvocationTargetException ite) { + if (isDebugBuild) { + throw ((RuntimeException) ite.getCause()); + } else { + return null; + } + } + } + + /** + * @deprecated As of Ruboto 0.7.0, replaced by {@link #runScriptlet(String code)} + */ + @Deprecated public static String execute(String code) { + return runRubyMethod(String.class, exec(code), "inspect"); + } + + public static Object get(String name) { + try { + Method getMethod = ruby.getClass().getMethod("get", String.class); + return getMethod.invoke(ruby, name); + } catch (NoSuchMethodException nsme) { + throw new RuntimeException(nsme); + } catch (IllegalAccessException iae) { + throw new RuntimeException(iae); + } catch (java.lang.reflect.InvocationTargetException ite) { + throw new RuntimeException(ite); + } + } + + public static String getPlatformVersionName() { + return RUBOTO_CORE_VERSION_NAME; + } + + public static String getScriptFilename() { + return callScriptingContainerMethod(String.class, "getScriptFilename"); + } + + public static Object runRubyMethod(Object receiver, String methodName, Object... args) { + try { + // FIXME(uwe): Simplify when we stop supporting JRuby < 1.7.0 + if (isJRubyPreOneSeven()) { + if (args.length == 0) { + Method m = ruby.getClass().getMethod("callMethod", Object.class, String.class, Class.class); + // System.out.println("Calling callMethod(" + receiver + ", " + methodName + ", " + Object.class + ")"); + return m.invoke(ruby, receiver, methodName, Object.class); + } else { + Method m = ruby.getClass().getMethod("callMethod", Object.class, String.class, Object[].class, Class.class); + // System.out.println("Calling callMethod(" + receiver + ", " + methodName + ", " + args + ", " + Object.class + ")"); + return m.invoke(ruby, receiver, methodName, args, Object.class); + } + } else { + Method m = ruby.getClass().getMethod("runRubyMethod", Class.class, Object.class, String.class, Object[].class); + return m.invoke(ruby, Object.class, receiver, methodName, args); + } + } catch (NoSuchMethodException nsme) { + throw new RuntimeException(nsme); + } catch (IllegalAccessException iae) { + throw new RuntimeException(iae); + } catch (java.lang.reflect.InvocationTargetException ite) { + printStackTrace(ite); + if (isDebugBuild) { + throw new RuntimeException(ite); + } + } + return null; + } - public static final FilenameFilter RUBY_FILES = new FilenameFilter() { - public boolean accept(File dir, String fname) { - return fname.endsWith(".rb"); + @SuppressWarnings("unchecked") + public static T runRubyMethod(Class returnType, Object receiver, String methodName, Object... args) { + try { + if (isJRubyPreOneSeven()) { + if (args.length == 0) { + Method m = ruby.getClass().getMethod("callMethod", Object.class, String.class, Class.class); + return (T) m.invoke(ruby, receiver, methodName, returnType); + } else { + Method m = ruby.getClass().getMethod("callMethod", Object.class, String.class, Object[].class, Class.class); + return (T) m.invoke(ruby, receiver, methodName, args, returnType); + } + } else { + Method m = ruby.getClass().getMethod("runRubyMethod", Class.class, Object.class, String.class, Object[].class); + return (T) m.invoke(ruby, returnType, receiver, methodName, args); + } + } catch (NoSuchMethodException nsme) { + throw new RuntimeException(nsme); + } catch (IllegalAccessException iae) { + throw new RuntimeException(iae); + } catch (java.lang.reflect.InvocationTargetException ite) { + printStackTrace(ite); } - }; - - public static synchronized boolean isInitialized() { - return initialized; - } - - public static boolean usesPlatformApk() { - return RUBOTO_CORE_VERSION_NAME != null; - } - - public static String getPlatformVersionName() { - return RUBOTO_CORE_VERSION_NAME; - } - + return null; + } + + public static boolean isDebugBuild() { + return isDebugBuild; + } + + public static boolean isInitialized() { + return initialized; + } + + public static void put(String name, Object object) { + try { + Method putMethod = ruby.getClass().getMethod("put", String.class, Object.class); + putMethod.invoke(ruby, name, object); + } catch (NoSuchMethodException nsme) { + throw new RuntimeException(nsme); + } catch (IllegalAccessException iae) { + throw new RuntimeException(iae); + } catch (java.lang.reflect.InvocationTargetException ite) { + throw new RuntimeException(ite); + } + } + + public static Object runScriptlet(String code) { + try { + Method runScriptletMethod = ruby.getClass().getMethod("runScriptlet", String.class); + return runScriptletMethod.invoke(ruby, code); + } catch (NoSuchMethodException nsme) { + throw new RuntimeException(nsme); + } catch (IllegalAccessException iae) { + throw new RuntimeException(iae); + } catch (java.lang.reflect.InvocationTargetException ite) { + if (isDebugBuild) { + throw ((RuntimeException) ite.getCause()); + } else { + return null; + } + } + } + public static synchronized boolean setUpJRuby(Context appContext) { return setUpJRuby(appContext, output == null ? System.out : output); } @@ -70,7 +232,7 @@ public static synchronized boolean setUpJRuby(Context appContext, PrintStream ou if (!initialized) { // BEGIN Ruboto HeapAlloc @SuppressWarnings("unused") - byte[] arrayForHeapAllocation = new byte[13 * 1024 * 1024]; + byte[] arrayForHeapAllocation = new byte[13 * 1024 * 1024]; arrayForHeapAllocation = null; // END Ruboto HeapAlloc setDebugBuild(appContext); @@ -102,14 +264,14 @@ public static synchronized boolean setUpJRuby(Context appContext, PrintStream ou } catch (ClassNotFoundException e1) { String packageName = "org.ruboto.core"; try { - PackageInfo pkgInfo = appContext.getPackageManager().getPackageInfo(packageName, 0); + PackageInfo pkgInfo = appContext.getPackageManager().getPackageInfo(packageName, 0); apkName = pkgInfo.applicationInfo.sourceDir; - RUBOTO_CORE_VERSION_NAME = pkgInfo.versionName; + RUBOTO_CORE_VERSION_NAME = pkgInfo.versionName; } catch (PackageManager.NameNotFoundException e2) { - out.println("JRuby not found in local APK:"); - e1.printStackTrace(out); - out.println("JRuby not found in platform APK:"); - e2.printStackTrace(out); + System.out.println("JRuby not found in local APK:"); + e1.printStackTrace(); + System.out.println("JRuby not found in platform APK:"); + e2.printStackTrace(); return false; } @@ -138,16 +300,16 @@ public static synchronized boolean setUpJRuby(Context appContext, PrintStream ou callScriptingContainerMethod(Void.class, "setCompileMode", Enum.valueOf(compileModeClass, "OFF")); // Class traceTypeClass = Class.forName("org.jruby.runtime.backtrace.TraceType", true, classLoader); - // Method traceTypeForMethod = traceTypeClass.getMethod("traceTypeFor", String.class); - // Object traceTypeRaw = traceTypeForMethod.invoke(null, "raw"); + // Method traceTypeForMethod = traceTypeClass.getMethod("traceTypeFor", String.class); + // Object traceTypeRaw = traceTypeForMethod.invoke(null, "raw"); // callScriptingContainerMethod(Void.class, "setTraceType", traceTypeRaw); // FIXME(uwe): Write tutorial on profiling. // container.getProvider().getRubyInstanceConfig().setProfilingMode(mode); // callScriptingContainerMethod(Void.class, "setClassLoader", classLoader); - Method setClassLoaderMethod = ruby.getClass().getMethod("setClassLoader", ClassLoader.class); - setClassLoaderMethod.invoke(ruby, classLoader); + Method setClassLoaderMethod = ruby.getClass().getMethod("setClassLoader", ClassLoader.class); + setClassLoaderMethod.invoke(ruby, classLoader); Thread.currentThread().setContextClassLoader(classLoader); @@ -166,11 +328,8 @@ public static synchronized boolean setUpJRuby(Context appContext, PrintStream ou Log.i("Setting JRUBY_HOME: " + jrubyHome); System.setProperty("jruby.home", jrubyHome); - String extraScriptsDir = scriptsDirName(appContext); - Log.i("Checking scripts in " + extraScriptsDir); - if (configDir(extraScriptsDir)) { - Log.i("Added extra scripts path: " + extraScriptsDir); - } + addLoadPath(scriptsDirName(appContext)); + initialized = true; } catch (ClassNotFoundException e) { handleInitException(e); @@ -191,47 +350,23 @@ public static synchronized boolean setUpJRuby(Context appContext, PrintStream ou return initialized; } - private static String scriptsDirName(Context context) { - File storageDir = null; - if (JRubyAdapter.isDebugBuild()) { - - // FIXME(uwe): Simplify this as soon as we drop support for android-7 - if (android.os.Build.VERSION.SDK_INT >= 8) { - try { - Method method = context.getClass().getMethod("getExternalFilesDir", String.class); - storageDir = (File) method.invoke(context, (Object) null); - } catch (SecurityException e) { - printStackTrace(e); - } catch (NoSuchMethodException e) { - printStackTrace(e); - } catch (IllegalArgumentException e) { - printStackTrace(e); - } catch (IllegalAccessException e) { - printStackTrace(e); - } catch (InvocationTargetException e) { - printStackTrace(e); - } - } else { - storageDir = new File(Environment.getExternalStorageDirectory(), "Android/data/" + context.getPackageName() + "/files"); - Log.e("Calculated path to sdcard the old way: " + storageDir); - } - // FIXME end + public static void setScriptFilename(String name) { + callScriptingContainerMethod(Void.class, "setScriptFilename", name); + } - if (storageDir == null || (!storageDir.exists() && !storageDir.mkdirs())) { - Log.e("Development mode active, but sdcard is not available. Make sure you have added\n\nto your AndroidManifest.xml file."); - storageDir = context.getFilesDir(); - } - } else { - storageDir = context.getFilesDir(); - } - return storageDir.getAbsolutePath() + "/scripts"; + public static boolean usesPlatformApk() { + return RUBOTO_CORE_VERSION_NAME != null; } - public static Boolean configDir(String scriptsDir) { + // Private methods + + private static Boolean addLoadPath(String scriptsDir) { if (new File(scriptsDir).exists()) { - Log.i("Found extra scripts dir: " + scriptsDir); - Script.setDir(scriptsDir); - JRubyAdapter.exec("$:.unshift '" + scriptsDir + "' ; $:.uniq!"); + Log.i("Added directory to load path: " + scriptsDir); + Script.addDir(scriptsDir); + runScriptlet("$:.unshift '" + scriptsDir + "' ; $:.uniq!"); + Log.d("Changing JRuby current directory to " + scriptsDir); + callScriptingContainerMethod(Void.class, "setCurrentDirectory", scriptsDir); return true; } else { Log.i("Extra scripts dir not present: " + scriptsDir); @@ -239,36 +374,6 @@ public static Boolean configDir(String scriptsDir) { } } - public static void setOutputStream(PrintStream out) { - if (ruby == null) { - output = out; - } else { - try { - Method setOutputMethod = ruby.getClass().getMethod("setOutput", PrintStream.class); - setOutputMethod.invoke(ruby, out); - Method setErrorMethod = ruby.getClass().getMethod("setError", PrintStream.class); - setErrorMethod.invoke(ruby, out); - } catch (IllegalArgumentException e) { - handleInitException(e); - } catch (SecurityException e) { - handleInitException(e); - } catch (IllegalAccessException e) { - handleInitException(e); - } catch (InvocationTargetException e) { - handleInitException(e); - } catch (NoSuchMethodException e) { - handleInitException(e); - } - } - } - - private static void handleInitException(Exception e) { - Log.e("Exception starting JRuby"); - Log.e(e.getMessage() != null ? e.getMessage() : e.getClass().getName()); - e.printStackTrace(); - ruby = null; - } - @SuppressWarnings("unchecked") public static T callScriptingContainerMethod(Class returnType, String methodName, Object... args) { Class[] argClasses = new Class[args.length]; @@ -276,10 +381,8 @@ public static T callScriptingContainerMethod(Class returnType, String met argClasses[i] = args[i].getClass(); } try { - Method method = ruby.getClass().getMethod(methodName, argClasses); - System.out.println("callScriptingContainerMethod: method: " + method); - T result = (T) method.invoke(ruby, args); - System.out.println("callScriptingContainerMethod: result: " + result); + Method method = ruby.getClass().getMethod(methodName, argClasses); + T result = (T) method.invoke(ruby, args); return result; } catch (RuntimeException re) { re.printStackTrace(); @@ -293,68 +396,80 @@ public static T callScriptingContainerMethod(Class returnType, String met return null; } - public static String execute(String code) { - Object result = exec(code); - return result != null ? result.toString() : "nil"; -// TODO: Why is callMethod returning "main"? -// return result != null ? callMethod(result, "inspect", String.class) : "null"; + private static void handleInitException(Exception e) { + Log.e("Exception starting JRuby"); + Log.e(e.getMessage() != null ? e.getMessage() : e.getClass().getName()); + e.printStackTrace(); + ruby = null; } - public static Object exec(String code) { - // return callScriptingContainerMethod(Object.class, "runScriptlet", code); - try { - Method runScriptletMethod = ruby.getClass().getMethod("runScriptlet", String.class); - return runScriptletMethod.invoke(ruby, code); - } catch (NoSuchMethodException nsme) { - throw new RuntimeException(nsme); - } catch (IllegalAccessException iae) { - throw new RuntimeException(iae); - } catch (java.lang.reflect.InvocationTargetException ite) { - if (isDebugBuild) { - throw ((RuntimeException) ite.getCause()); - } else { - return null; - } - } - } + // FIXME(uwe): Remove when we stop supporting JRuby < 1.7.0 + @Deprecated public static boolean isJRubyPreOneSeven() { + return ((String)get("JRUBY_VERSION")).equals("1.7.0.dev") || ((String)get("JRUBY_VERSION")).startsWith("1.6."); + } - public static void defineGlobalConstant(String name, Object object) { - put(name, object); + // FIXME(uwe): Remove when we stop supporting JRuby < 1.7.0 + @Deprecated public static boolean isJRubyOneSeven() { + return !isJRubyPreOneSeven() && ((String)get("JRUBY_VERSION")).startsWith("1.7."); } - public static void put(String name, Object object) { - try { - Method putMethod = ruby.getClass().getMethod("put", String.class, Object.class); - putMethod.invoke(ruby, name, object); - } catch (NoSuchMethodException nsme) { - throw new RuntimeException(nsme); - } catch (IllegalAccessException iae) { - throw new RuntimeException(iae); - } catch (java.lang.reflect.InvocationTargetException ite) { - throw new RuntimeException(ite); - } + // FIXME(uwe): Remove when we stop supporting Ruby 1.8 + @Deprecated public static boolean isRubyOneEight() { + return ((String)get("RUBY_VERSION")).startsWith("1.8."); } - - public static Object get(String name) { + + // FIXME(uwe): Remove when we stop supporting Ruby 1.8 + @Deprecated public static boolean isRubyOneNine() { + return ((String)get("RUBY_VERSION")).startsWith("1.9."); + } + + static void printStackTrace(Throwable t) { + // TODO(uwe): Simplify this when Issue #144 is resolved try { - Method getMethod = ruby.getClass().getMethod("get", String.class); - return getMethod.invoke(ruby, name); - } catch (NoSuchMethodException nsme) { - throw new RuntimeException(nsme); - } catch (IllegalAccessException iae) { - throw new RuntimeException(iae); - } catch (java.lang.reflect.InvocationTargetException ite) { - throw new RuntimeException(ite); + t.printStackTrace(output); + } catch (NullPointerException npe) { + // TODO(uwe): printStackTrace should not fail + for (java.lang.StackTraceElement ste : t.getStackTrace()) { + output.append(ste.toString() + "\n"); + } } } - public static void defineGlobalVariable(String name, Object object) { - defineGlobalConstant(name, object); - } + private static String scriptsDirName(Context context) { + File storageDir = null; + if (isDebugBuild()) { - public static boolean isDebugBuild() { - return isDebugBuild; - } + // FIXME(uwe): Simplify this as soon as we drop support for android-7 + if (android.os.Build.VERSION.SDK_INT >= 8) { + try { + Method method = context.getClass().getMethod("getExternalFilesDir", String.class); + storageDir = (File) method.invoke(context, (Object) null); + } catch (SecurityException e) { + printStackTrace(e); + } catch (NoSuchMethodException e) { + printStackTrace(e); + } catch (IllegalArgumentException e) { + printStackTrace(e); + } catch (IllegalAccessException e) { + printStackTrace(e); + } catch (InvocationTargetException e) { + printStackTrace(e); + } + } else { + storageDir = new File(Environment.getExternalStorageDirectory(), "Android/data/" + context.getPackageName() + "/files"); + Log.e("Calculated path to sdcard the old way: " + storageDir); + } + // FIXME end + + if (storageDir == null || (!storageDir.exists() && !storageDir.mkdirs())) { + Log.e("Development mode active, but sdcard is not available. Make sure you have added\n\nto your AndroidManifest.xml file."); + storageDir = context.getFilesDir(); + } + } else { + storageDir = context.getFilesDir(); + } + return storageDir.getAbsolutePath() + "/scripts"; + } private static void setDebugBuild(Context context) { PackageManager pm = context.getPackageManager(); @@ -367,110 +482,35 @@ private static void setDebugBuild(Context context) { } } - /************************************************************************************************* - * - * Script Actions - */ - - public static String getScriptFilename() { - return callScriptingContainerMethod(String.class, "getScriptFilename"); + private static void setLocalContextScope(String val) { + localContextScope = val; } - public static void setScriptFilename(String name) { - callScriptingContainerMethod(Void.class, "setScriptFilename", name); + public static void setLocalVariableBehavior(String val) { + localVariableBehavior = val; } - public static void callMethod(Object receiver, String methodName, Object[] args) { + public static void setOutputStream(PrintStream out) { + if (ruby == null) { + output = out; + } else { try { - Method callMethodMethod = ruby.getClass().getMethod("callMethod", Object.class, String.class, Object[].class); - callMethodMethod.invoke(ruby, receiver, methodName, args); - } catch (NoSuchMethodException nsme) { - throw new RuntimeException(nsme); - } catch (IllegalAccessException iae) { - throw new RuntimeException(iae); - } catch (java.lang.reflect.InvocationTargetException ite) { - printStackTrace(ite); - if (isDebugBuild) { - throw new RuntimeException(ite); - } + Method setOutputMethod = ruby.getClass().getMethod("setOutput", PrintStream.class); + setOutputMethod.invoke(ruby, out); + Method setErrorMethod = ruby.getClass().getMethod("setError", PrintStream.class); + setErrorMethod.invoke(ruby, out); + } catch (IllegalArgumentException e) { + handleInitException(e); + } catch (SecurityException e) { + handleInitException(e); + } catch (IllegalAccessException e) { + handleInitException(e); + } catch (InvocationTargetException e) { + handleInitException(e); + } catch (NoSuchMethodException e) { + handleInitException(e); } + } } - public static void callMethod(Object object, String methodName, Object arg) { - callMethod(object, methodName, new Object[] { arg }); - } - - public static void callMethod(Object object, String methodName) { - callMethod(object, methodName, new Object[] {}); - } - - @SuppressWarnings("unchecked") - public static T callMethod(Object receiver, String methodName, Object[] args, Class returnType) { - try { - Method callMethodMethod = ruby.getClass().getMethod("callMethod", Object.class, String.class, Object[].class, Class.class); - return (T) callMethodMethod.invoke(ruby, receiver, methodName, args, returnType); - } catch (NoSuchMethodException nsme) { - throw new RuntimeException(nsme); - } catch (IllegalAccessException iae) { - throw new RuntimeException(iae); - } catch (java.lang.reflect.InvocationTargetException ite) { - printStackTrace(ite); - } - return null; - } - - @SuppressWarnings("unchecked") - public static T callMethod(Object receiver, String methodName, Object arg, Class returnType) { - try { - Method callMethodMethod = ruby.getClass().getMethod("callMethod", Object.class, String.class, Object.class, Class.class); - return (T) callMethodMethod.invoke(ruby, receiver, methodName, arg, returnType); - } catch (NoSuchMethodException nsme) { - throw new RuntimeException(nsme); - } catch (IllegalAccessException iae) { - throw new RuntimeException(iae); - } catch (java.lang.reflect.InvocationTargetException ite) { - printStackTrace(ite); - } - return null; - } - - @SuppressWarnings("unchecked") - public static T callMethod(Object receiver, String methodName, Class returnType) { - try { - Method callMethodMethod = ruby.getClass().getMethod("callMethod", Object.class, String.class, Class.class); - return (T) callMethodMethod.invoke(ruby, receiver, methodName, returnType); - } catch (NoSuchMethodException nsme) { - throw new RuntimeException(nsme); - } catch (IllegalAccessException iae) { - throw new RuntimeException(iae); - } catch (java.lang.reflect.InvocationTargetException ite) { - printStackTrace(ite); - } - return null; - } - - private static void printStackTrace(Throwable t) { - PrintStream out; - try { - Method getOutputMethod = ruby.getClass().getMethod("getOutput"); - out = (PrintStream) getOutputMethod.invoke(ruby); - } catch (java.lang.NoSuchMethodException nsme) { - throw new RuntimeException("ScriptingContainer#getOutput method not found.", nsme); - } catch (java.lang.IllegalAccessException iae) { - throw new RuntimeException("ScriptingContainer#getOutput method not accessable.", iae); - } catch (java.lang.reflect.InvocationTargetException ite) { - throw new RuntimeException("ScriptingContainer#getOutput failed.", ite); - } - - // TODO(uwe): Simplify this when Issue #144 is resolved - try { - t.printStackTrace(out); - } catch (NullPointerException npe) { - // TODO(uwe): printStackTrace should not fail - for (java.lang.StackTraceElement ste : t.getStackTrace()) { - out.append(ste.toString() + "\n"); - } - } - } - } diff --git a/src/org/ruboto/RubotoActivity.java b/src/org/ruboto/RubotoActivity.java index c2acabd..b1bd25f 100644 --- a/src/org/ruboto/RubotoActivity.java +++ b/src/org/ruboto/RubotoActivity.java @@ -8,12 +8,6 @@ import android.os.Bundle; public class RubotoActivity extends android.app.Activity { - private String scriptName; - private String remoteVariable = null; - private Object[] args; - private Bundle configBundle = null; - private Object rubyInstance; - public static final int CB_ACTIVITY_RESULT = 0; public static final int CB_CHILD_TITLE_CHANGED = 1; public static final int CB_CONFIGURATION_CHANGED = 2; @@ -63,7 +57,13 @@ public class RubotoActivity extends android.app.Activity { public static final int CB_KEY_LONG_PRESS = 46; public static final int CB_APPLY_THEME_RESOURCE = 47; + private String rubyClassName; + private String scriptName; + private Object rubyInstance; private Object[] callbackProcs = new Object[48]; + private String remoteVariable = null; + private Object[] args; + private Bundle configBundle = null; public void setCallbackProc(int id, Object obj) { callbackProcs[id] = obj; @@ -78,6 +78,10 @@ public String getRemoteVariableCall(String call) { return (remoteVariable == null ? "" : (remoteVariable + ".")) + call; } + public void setRubyClassName(String name) { + rubyClassName = name; + } + public void setScriptName(String name) { scriptName = name; } @@ -89,6 +93,7 @@ public void setScriptName(String name) { @Override public void onCreate(Bundle bundle) { + System.out.println("RubotoActivity onCreate(): " + getClass().getName()); args = new Object[1]; args[0] = bundle; @@ -98,6 +103,13 @@ public void onCreate(Bundle bundle) { if (configBundle.containsKey("Theme")) { setTheme(configBundle.getInt("Theme")); } + if (configBundle.containsKey("ClassName")) { + if (this.getClass().getName() == RubotoActivity.class.getName()) { + setRubyClassName(configBundle.getString("ClassName")); + } else { + throw new IllegalArgumentException("Only local Intents may set class name."); + } + } if (configBundle.containsKey("Script")) { if (this.getClass().getName() == RubotoActivity.class.getName()) { setScriptName(configBundle.getString("Script")); @@ -107,8 +119,15 @@ public void onCreate(Bundle bundle) { } } + if (rubyClassName == null && scriptName != null) { + rubyClassName = Script.toCamelCase(scriptName); + } + if (scriptName == null && rubyClassName != null) { + setScriptName(Script.toSnakeCase(rubyClassName) + ".rb"); + } + super.onCreate(bundle); - + if (JRubyAdapter.isInitialized()) { prepareJRuby(); loadScript(); @@ -128,26 +147,77 @@ protected void prepareJRuby() { protected void loadScript() { try { if (scriptName != null) { - String rubyClassName = Script.toCamelCase(scriptName); System.out.println("Looking for Ruby class: " + rubyClassName); Object rubyClass = JRubyAdapter.get(rubyClassName); - if (rubyClass == null) { - System.out.println("Loading script: " + scriptName); - JRubyAdapter.exec(new Script(scriptName).getContents()); - rubyClass = JRubyAdapter.get(rubyClassName); + Script rubyScript = new Script(scriptName); + if (rubyScript.exists()) { + String script = rubyScript.getContents(); + if (script.matches("(?s).*class " + rubyClassName + ".*")) { + if (!rubyClassName.equals(getClass().getSimpleName())) { + System.out.println("Script defines methods on meta class"); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven() || JRubyAdapter.isRubyOneEight()) { + JRubyAdapter.put("$java_instance", this); + JRubyAdapter.put(rubyClassName, JRubyAdapter.runScriptlet("class << $java_instance; self; end")); + } else if (JRubyAdapter.isJRubyOneSeven() && JRubyAdapter.isRubyOneNine()) { + JRubyAdapter.runScriptlet("Java::" + getClass().getName() + ".__persistent__ = true"); + JRubyAdapter.put(rubyClassName, JRubyAdapter.runRubyMethod(this, "singleton_class")); + } else { + throw new RuntimeException("Unknown JRuby/Ruby version: " + JRubyAdapter.get("JRUBY_VERSION") + "/" + JRubyAdapter.get("RUBY_VERSION")); + } + } + } + if (rubyClass == null) { + System.out.println("Loading script: " + scriptName); + if (script.matches("(?s).*class " + rubyClassName + ".*")) { + System.out.println("Script contains class definition"); + if (rubyClassName.equals(getClass().getSimpleName())) { + System.out.println("Script has separate Java class"); + // FIXME(uwe): Simplify when we stop support for JRuby < 1.7.0 + if (!JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.runScriptlet("Java::" + getClass().getName() + ".__persistent__ = true"); + } + JRubyAdapter.put(rubyClassName, JRubyAdapter.runScriptlet("Java::" + getClass().getName())); + } + System.out.println("Set class: " + JRubyAdapter.get(rubyClassName)); + } + JRubyAdapter.setScriptFilename(scriptName); + JRubyAdapter.runScriptlet(script); + rubyClass = JRubyAdapter.get(rubyClassName); + } + rubyInstance = this; + } else if (rubyClass != null) { + // We have a predefined Ruby class without corresponding Ruby source file. + System.out.println("Create separate Ruby instance for class: " + rubyClass); + rubyInstance = JRubyAdapter.runRubyMethod(rubyClass, "new"); + JRubyAdapter.runRubyMethod(rubyInstance, "instance_variable_set", "@ruboto_java_instance", this); + } else { + // Neither script file nor predefined class + throw new RuntimeException("Either script or predefined class must be present."); } if (rubyClass != null) { - System.out.println("Instanciating Ruby class: " + rubyClassName); - rubyInstance = JRubyAdapter.callMethod(rubyClass, "new", this, Object.class); - JRubyAdapter.callMethod(rubyInstance, "on_create", args[0]); + System.out.println("Call on_create on: " + rubyInstance + ", " + JRubyAdapter.get("JRUBY_VERSION")); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_create($bundle)"); + } else if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_create", args[0]); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } } } else if (configBundle != null) { - // TODO: Why doesn't this work? - // JRubyAdapter.callMethod(this, "initialize_ruboto"); - JRubyAdapter.execute("$activity.initialize_ruboto"); - // TODO: Why doesn't this work? - // JRubyAdapter.callMethod(this, "on_create", args[0]); - JRubyAdapter.execute("$activity.on_create($bundle)"); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.runScriptlet("$activity.initialize_ruboto"); + JRubyAdapter.runScriptlet("$activity.on_create($bundle)"); + } else if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(this, "initialize_ruboto"); + JRubyAdapter.runRubyMethod(this, "on_create", args[0]); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } } } catch(IOException e){ e.printStackTrace(); @@ -165,914 +235,2270 @@ public boolean rubotoAttachable() { */ public void onActivityResult(int requestCode, int resultCode, android.content.Intent data) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_activity_result"}, Boolean.class)) { - super.onActivityResult(requestCode, resultCode, data); - JRubyAdapter.callMethod(rubyInstance, "on_activity_result" , new Object[]{requestCode, resultCode, data}); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onActivityResult"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_ACTIVITY_RESULT] != null) { super.onActivityResult(requestCode, resultCode, data); - JRubyAdapter.callMethod(rubyInstance, "onActivityResult" , new Object[]{requestCode, resultCode, data}); + JRubyAdapter.runRubyMethod(callbackProcs[CB_ACTIVITY_RESULT], "call" , new Object[]{requestCode, resultCode, data}); } else { - if (callbackProcs != null && callbackProcs[CB_ACTIVITY_RESULT] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_activity_result}")) { super.onActivityResult(requestCode, resultCode, data); - JRubyAdapter.callMethod(callbackProcs[CB_ACTIVITY_RESULT], "call" , new Object[]{requestCode, resultCode, data}); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_requestCode", requestCode); + JRubyAdapter.put("$arg_resultCode", resultCode); + JRubyAdapter.put("$arg_data", data); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_activity_result($arg_requestCode, $arg_resultCode, $arg_data)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_activity_result", new Object[]{requestCode, resultCode, data}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onActivityResult(requestCode, resultCode, data); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onActivityResult}")) { + super.onActivityResult(requestCode, resultCode, data); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_requestCode", requestCode); + JRubyAdapter.put("$arg_resultCode", resultCode); + JRubyAdapter.put("$arg_data", data); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onActivityResult($arg_requestCode, $arg_resultCode, $arg_data)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onActivityResult", new Object[]{requestCode, resultCode, data}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onActivityResult(requestCode, resultCode, data); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onActivityResult"); + super.onActivityResult(requestCode, resultCode, data); } } public void onChildTitleChanged(android.app.Activity childActivity, java.lang.CharSequence title) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_child_title_changed"}, Boolean.class)) { - super.onChildTitleChanged(childActivity, title); - JRubyAdapter.callMethod(rubyInstance, "on_child_title_changed" , new Object[]{childActivity, title}); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onChildTitleChanged"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_CHILD_TITLE_CHANGED] != null) { super.onChildTitleChanged(childActivity, title); - JRubyAdapter.callMethod(rubyInstance, "onChildTitleChanged" , new Object[]{childActivity, title}); + JRubyAdapter.runRubyMethod(callbackProcs[CB_CHILD_TITLE_CHANGED], "call" , new Object[]{childActivity, title}); } else { - if (callbackProcs != null && callbackProcs[CB_CHILD_TITLE_CHANGED] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_child_title_changed}")) { super.onChildTitleChanged(childActivity, title); - JRubyAdapter.callMethod(callbackProcs[CB_CHILD_TITLE_CHANGED], "call" , new Object[]{childActivity, title}); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_childActivity", childActivity); + JRubyAdapter.put("$arg_title", title); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_child_title_changed($arg_childActivity, $arg_title)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_child_title_changed", new Object[]{childActivity, title}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onChildTitleChanged(childActivity, title); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onChildTitleChanged}")) { + super.onChildTitleChanged(childActivity, title); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_childActivity", childActivity); + JRubyAdapter.put("$arg_title", title); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onChildTitleChanged($arg_childActivity, $arg_title)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onChildTitleChanged", new Object[]{childActivity, title}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onChildTitleChanged(childActivity, title); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onChildTitleChanged"); + super.onChildTitleChanged(childActivity, title); } } public void onConfigurationChanged(android.content.res.Configuration newConfig) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_configuration_changed"}, Boolean.class)) { - super.onConfigurationChanged(newConfig); - JRubyAdapter.callMethod(rubyInstance, "on_configuration_changed" , newConfig); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onConfigurationChanged"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_CONFIGURATION_CHANGED] != null) { super.onConfigurationChanged(newConfig); - JRubyAdapter.callMethod(rubyInstance, "onConfigurationChanged" , newConfig); + JRubyAdapter.runRubyMethod(callbackProcs[CB_CONFIGURATION_CHANGED], "call" , newConfig); } else { - if (callbackProcs != null && callbackProcs[CB_CONFIGURATION_CHANGED] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_configuration_changed}")) { super.onConfigurationChanged(newConfig); - JRubyAdapter.callMethod(callbackProcs[CB_CONFIGURATION_CHANGED], "call" , newConfig); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_newConfig", newConfig); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_configuration_changed($arg_newConfig)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_configuration_changed", newConfig); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onConfigurationChanged(newConfig); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onConfigurationChanged}")) { + super.onConfigurationChanged(newConfig); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_newConfig", newConfig); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onConfigurationChanged($arg_newConfig)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onConfigurationChanged", newConfig); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onConfigurationChanged(newConfig); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onConfigurationChanged"); + super.onConfigurationChanged(newConfig); } } public void onContentChanged() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_content_changed"}, Boolean.class)) { - super.onContentChanged(); - JRubyAdapter.callMethod(rubyInstance, "on_content_changed" ); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onContentChanged"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_CONTENT_CHANGED] != null) { super.onContentChanged(); - JRubyAdapter.callMethod(rubyInstance, "onContentChanged" ); + JRubyAdapter.runRubyMethod(callbackProcs[CB_CONTENT_CHANGED], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_CONTENT_CHANGED] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_content_changed}")) { super.onContentChanged(); - JRubyAdapter.callMethod(callbackProcs[CB_CONTENT_CHANGED], "call" ); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_content_changed()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_content_changed"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onContentChanged(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onContentChanged}")) { + super.onContentChanged(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onContentChanged()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onContentChanged"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onContentChanged(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onContentChanged"); + super.onContentChanged(); } } public boolean onContextItemSelected(android.view.MenuItem item) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_context_item_selected"}, Boolean.class)) { - super.onContextItemSelected(item); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_context_item_selected" , item, Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onContextItemSelected"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_CONTEXT_ITEM_SELECTED] != null) { super.onContextItemSelected(item); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onContextItemSelected" , item, Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_CONTEXT_ITEM_SELECTED], "call" , item); } else { - if (callbackProcs != null && callbackProcs[CB_CONTEXT_ITEM_SELECTED] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_context_item_selected}")) { super.onContextItemSelected(item); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_CONTEXT_ITEM_SELECTED], "call" , item, Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_item", item); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_context_item_selected($arg_item)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_context_item_selected", item); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onContextItemSelected(item); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onContextItemSelected}")) { + super.onContextItemSelected(item); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_item", item); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onContextItemSelected($arg_item)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onContextItemSelected", item); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onContextItemSelected(item); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onContextItemSelected"); + return super.onContextItemSelected(item); } } public void onContextMenuClosed(android.view.Menu menu) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_context_menu_closed"}, Boolean.class)) { - super.onContextMenuClosed(menu); - JRubyAdapter.callMethod(rubyInstance, "on_context_menu_closed" , menu); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onContextMenuClosed"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_CONTEXT_MENU_CLOSED] != null) { super.onContextMenuClosed(menu); - JRubyAdapter.callMethod(rubyInstance, "onContextMenuClosed" , menu); + JRubyAdapter.runRubyMethod(callbackProcs[CB_CONTEXT_MENU_CLOSED], "call" , menu); } else { - if (callbackProcs != null && callbackProcs[CB_CONTEXT_MENU_CLOSED] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_context_menu_closed}")) { super.onContextMenuClosed(menu); - JRubyAdapter.callMethod(callbackProcs[CB_CONTEXT_MENU_CLOSED], "call" , menu); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_context_menu_closed($arg_menu)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_context_menu_closed", menu); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onContextMenuClosed(menu); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onContextMenuClosed}")) { + super.onContextMenuClosed(menu); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onContextMenuClosed($arg_menu)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onContextMenuClosed", menu); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onContextMenuClosed(menu); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onContextMenuClosed"); + super.onContextMenuClosed(menu); } } public void onCreateContextMenu(android.view.ContextMenu menu, android.view.View v, android.view.ContextMenu.ContextMenuInfo menuInfo) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_create_context_menu"}, Boolean.class)) { - super.onCreateContextMenu(menu, v, menuInfo); - JRubyAdapter.callMethod(rubyInstance, "on_create_context_menu" , new Object[]{menu, v, menuInfo}); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onCreateContextMenu"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_CREATE_CONTEXT_MENU] != null) { super.onCreateContextMenu(menu, v, menuInfo); - JRubyAdapter.callMethod(rubyInstance, "onCreateContextMenu" , new Object[]{menu, v, menuInfo}); + JRubyAdapter.runRubyMethod(callbackProcs[CB_CREATE_CONTEXT_MENU], "call" , new Object[]{menu, v, menuInfo}); } else { - if (callbackProcs != null && callbackProcs[CB_CREATE_CONTEXT_MENU] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_create_context_menu}")) { super.onCreateContextMenu(menu, v, menuInfo); - JRubyAdapter.callMethod(callbackProcs[CB_CREATE_CONTEXT_MENU], "call" , new Object[]{menu, v, menuInfo}); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$arg_v", v); + JRubyAdapter.put("$arg_menuInfo", menuInfo); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_create_context_menu($arg_menu, $arg_v, $arg_menuInfo)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_create_context_menu", new Object[]{menu, v, menuInfo}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onCreateContextMenu(menu, v, menuInfo); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onCreateContextMenu}")) { + super.onCreateContextMenu(menu, v, menuInfo); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$arg_v", v); + JRubyAdapter.put("$arg_menuInfo", menuInfo); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onCreateContextMenu($arg_menu, $arg_v, $arg_menuInfo)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onCreateContextMenu", new Object[]{menu, v, menuInfo}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onCreateContextMenu(menu, v, menuInfo); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onCreateContextMenu"); + super.onCreateContextMenu(menu, v, menuInfo); } } public java.lang.CharSequence onCreateDescription() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_create_description"}, Boolean.class)) { - super.onCreateDescription(); - return (java.lang.CharSequence) JRubyAdapter.callMethod(rubyInstance, "on_create_description" , java.lang.CharSequence.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onCreateDescription"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_CREATE_DESCRIPTION] != null) { super.onCreateDescription(); - return (java.lang.CharSequence) JRubyAdapter.callMethod(rubyInstance, "onCreateDescription" , java.lang.CharSequence.class); + return (java.lang.CharSequence) JRubyAdapter.runRubyMethod(java.lang.CharSequence.class, callbackProcs[CB_CREATE_DESCRIPTION], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_CREATE_DESCRIPTION] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_create_description}")) { super.onCreateDescription(); - return (java.lang.CharSequence) JRubyAdapter.callMethod(callbackProcs[CB_CREATE_DESCRIPTION], "call" , java.lang.CharSequence.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (java.lang.CharSequence) JRubyAdapter.runScriptlet("$ruby_instance.on_create_description()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (java.lang.CharSequence) JRubyAdapter.runRubyMethod(java.lang.CharSequence.class, rubyInstance, "on_create_description"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onCreateDescription(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onCreateDescription}")) { + super.onCreateDescription(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (java.lang.CharSequence) JRubyAdapter.runScriptlet("$ruby_instance.onCreateDescription()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (java.lang.CharSequence) JRubyAdapter.runRubyMethod(java.lang.CharSequence.class, rubyInstance, "onCreateDescription"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onCreateDescription(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onCreateDescription"); + return super.onCreateDescription(); } } public boolean onCreateOptionsMenu(android.view.Menu menu) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_create_options_menu"}, Boolean.class)) { - super.onCreateOptionsMenu(menu); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_create_options_menu" , menu, Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onCreateOptionsMenu"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_CREATE_OPTIONS_MENU] != null) { super.onCreateOptionsMenu(menu); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onCreateOptionsMenu" , menu, Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_CREATE_OPTIONS_MENU], "call" , menu); } else { - if (callbackProcs != null && callbackProcs[CB_CREATE_OPTIONS_MENU] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_create_options_menu}")) { super.onCreateOptionsMenu(menu); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_CREATE_OPTIONS_MENU], "call" , menu, Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_create_options_menu($arg_menu)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_create_options_menu", menu); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onCreateOptionsMenu(menu); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onCreateOptionsMenu}")) { + super.onCreateOptionsMenu(menu); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onCreateOptionsMenu($arg_menu)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onCreateOptionsMenu", menu); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onCreateOptionsMenu(menu); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onCreateOptionsMenu"); + return super.onCreateOptionsMenu(menu); } } public boolean onCreatePanelMenu(int featureId, android.view.Menu menu) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_create_panel_menu"}, Boolean.class)) { - super.onCreatePanelMenu(featureId, menu); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_create_panel_menu" , new Object[]{featureId, menu}, Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onCreatePanelMenu"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_CREATE_PANEL_MENU] != null) { super.onCreatePanelMenu(featureId, menu); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onCreatePanelMenu" , new Object[]{featureId, menu}, Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_CREATE_PANEL_MENU], "call" , new Object[]{featureId, menu}); } else { - if (callbackProcs != null && callbackProcs[CB_CREATE_PANEL_MENU] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_create_panel_menu}")) { super.onCreatePanelMenu(featureId, menu); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_CREATE_PANEL_MENU], "call" , new Object[]{featureId, menu}, Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_featureId", featureId); + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_create_panel_menu($arg_featureId, $arg_menu)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_create_panel_menu", new Object[]{featureId, menu}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onCreatePanelMenu(featureId, menu); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onCreatePanelMenu}")) { + super.onCreatePanelMenu(featureId, menu); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_featureId", featureId); + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onCreatePanelMenu($arg_featureId, $arg_menu)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onCreatePanelMenu", new Object[]{featureId, menu}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onCreatePanelMenu(featureId, menu); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onCreatePanelMenu"); + return super.onCreatePanelMenu(featureId, menu); } } public android.view.View onCreatePanelView(int featureId) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_create_panel_view"}, Boolean.class)) { - super.onCreatePanelView(featureId); - return (android.view.View) JRubyAdapter.callMethod(rubyInstance, "on_create_panel_view" , featureId, android.view.View.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onCreatePanelView"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_CREATE_PANEL_VIEW] != null) { super.onCreatePanelView(featureId); - return (android.view.View) JRubyAdapter.callMethod(rubyInstance, "onCreatePanelView" , featureId, android.view.View.class); + return (android.view.View) JRubyAdapter.runRubyMethod(android.view.View.class, callbackProcs[CB_CREATE_PANEL_VIEW], "call" , featureId); } else { - if (callbackProcs != null && callbackProcs[CB_CREATE_PANEL_VIEW] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_create_panel_view}")) { super.onCreatePanelView(featureId); - return (android.view.View) JRubyAdapter.callMethod(callbackProcs[CB_CREATE_PANEL_VIEW], "call" , featureId, android.view.View.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_featureId", featureId); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (android.view.View) JRubyAdapter.runScriptlet("$ruby_instance.on_create_panel_view($arg_featureId)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (android.view.View) JRubyAdapter.runRubyMethod(android.view.View.class, rubyInstance, "on_create_panel_view", featureId); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onCreatePanelView(featureId); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onCreatePanelView}")) { + super.onCreatePanelView(featureId); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_featureId", featureId); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (android.view.View) JRubyAdapter.runScriptlet("$ruby_instance.onCreatePanelView($arg_featureId)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (android.view.View) JRubyAdapter.runRubyMethod(android.view.View.class, rubyInstance, "onCreatePanelView", featureId); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onCreatePanelView(featureId); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onCreatePanelView"); + return super.onCreatePanelView(featureId); } } public boolean onCreateThumbnail(android.graphics.Bitmap outBitmap, android.graphics.Canvas canvas) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_create_thumbnail"}, Boolean.class)) { - super.onCreateThumbnail(outBitmap, canvas); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_create_thumbnail" , new Object[]{outBitmap, canvas}, Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onCreateThumbnail"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_CREATE_THUMBNAIL] != null) { super.onCreateThumbnail(outBitmap, canvas); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onCreateThumbnail" , new Object[]{outBitmap, canvas}, Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_CREATE_THUMBNAIL], "call" , new Object[]{outBitmap, canvas}); } else { - if (callbackProcs != null && callbackProcs[CB_CREATE_THUMBNAIL] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_create_thumbnail}")) { super.onCreateThumbnail(outBitmap, canvas); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_CREATE_THUMBNAIL], "call" , new Object[]{outBitmap, canvas}, Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_outBitmap", outBitmap); + JRubyAdapter.put("$arg_canvas", canvas); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_create_thumbnail($arg_outBitmap, $arg_canvas)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_create_thumbnail", new Object[]{outBitmap, canvas}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onCreateThumbnail(outBitmap, canvas); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onCreateThumbnail}")) { + super.onCreateThumbnail(outBitmap, canvas); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_outBitmap", outBitmap); + JRubyAdapter.put("$arg_canvas", canvas); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onCreateThumbnail($arg_outBitmap, $arg_canvas)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onCreateThumbnail", new Object[]{outBitmap, canvas}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onCreateThumbnail(outBitmap, canvas); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onCreateThumbnail"); + return super.onCreateThumbnail(outBitmap, canvas); } } public android.view.View onCreateView(java.lang.String name, android.content.Context context, android.util.AttributeSet attrs) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_create_view"}, Boolean.class)) { - super.onCreateView(name, context, attrs); - return (android.view.View) JRubyAdapter.callMethod(rubyInstance, "on_create_view" , new Object[]{name, context, attrs}, android.view.View.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onCreateView"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_CREATE_VIEW] != null) { super.onCreateView(name, context, attrs); - return (android.view.View) JRubyAdapter.callMethod(rubyInstance, "onCreateView" , new Object[]{name, context, attrs}, android.view.View.class); + return (android.view.View) JRubyAdapter.runRubyMethod(android.view.View.class, callbackProcs[CB_CREATE_VIEW], "call" , new Object[]{name, context, attrs}); } else { - if (callbackProcs != null && callbackProcs[CB_CREATE_VIEW] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_create_view}")) { super.onCreateView(name, context, attrs); - return (android.view.View) JRubyAdapter.callMethod(callbackProcs[CB_CREATE_VIEW], "call" , new Object[]{name, context, attrs}, android.view.View.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_name", name); + JRubyAdapter.put("$arg_context", context); + JRubyAdapter.put("$arg_attrs", attrs); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (android.view.View) JRubyAdapter.runScriptlet("$ruby_instance.on_create_view($arg_name, $arg_context, $arg_attrs)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (android.view.View) JRubyAdapter.runRubyMethod(android.view.View.class, rubyInstance, "on_create_view", new Object[]{name, context, attrs}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onCreateView(name, context, attrs); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onCreateView}")) { + super.onCreateView(name, context, attrs); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_name", name); + JRubyAdapter.put("$arg_context", context); + JRubyAdapter.put("$arg_attrs", attrs); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (android.view.View) JRubyAdapter.runScriptlet("$ruby_instance.onCreateView($arg_name, $arg_context, $arg_attrs)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (android.view.View) JRubyAdapter.runRubyMethod(android.view.View.class, rubyInstance, "onCreateView", new Object[]{name, context, attrs}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onCreateView(name, context, attrs); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onCreateView"); + return super.onCreateView(name, context, attrs); } } public void onDestroy() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_destroy"}, Boolean.class)) { - super.onDestroy(); - JRubyAdapter.callMethod(rubyInstance, "on_destroy" ); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onDestroy"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_DESTROY] != null) { super.onDestroy(); - JRubyAdapter.callMethod(rubyInstance, "onDestroy" ); + JRubyAdapter.runRubyMethod(callbackProcs[CB_DESTROY], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_DESTROY] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_destroy}")) { super.onDestroy(); - JRubyAdapter.callMethod(callbackProcs[CB_DESTROY], "call" ); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_destroy()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_destroy"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onDestroy(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onDestroy}")) { + super.onDestroy(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onDestroy()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onDestroy"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onDestroy(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onDestroy"); + super.onDestroy(); } } public boolean onKeyDown(int keyCode, android.view.KeyEvent event) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_key_down"}, Boolean.class)) { - super.onKeyDown(keyCode, event); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_key_down" , new Object[]{keyCode, event}, Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onKeyDown"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_KEY_DOWN] != null) { super.onKeyDown(keyCode, event); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onKeyDown" , new Object[]{keyCode, event}, Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_KEY_DOWN], "call" , new Object[]{keyCode, event}); } else { - if (callbackProcs != null && callbackProcs[CB_KEY_DOWN] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_key_down}")) { super.onKeyDown(keyCode, event); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_KEY_DOWN], "call" , new Object[]{keyCode, event}, Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_keyCode", keyCode); + JRubyAdapter.put("$arg_event", event); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_key_down($arg_keyCode, $arg_event)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_key_down", new Object[]{keyCode, event}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onKeyDown(keyCode, event); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onKeyDown}")) { + super.onKeyDown(keyCode, event); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_keyCode", keyCode); + JRubyAdapter.put("$arg_event", event); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onKeyDown($arg_keyCode, $arg_event)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onKeyDown", new Object[]{keyCode, event}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onKeyDown(keyCode, event); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onKeyDown"); + return super.onKeyDown(keyCode, event); } } public boolean onKeyMultiple(int keyCode, int repeatCount, android.view.KeyEvent event) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_key_multiple"}, Boolean.class)) { - super.onKeyMultiple(keyCode, repeatCount, event); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_key_multiple" , new Object[]{keyCode, repeatCount, event}, Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onKeyMultiple"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_KEY_MULTIPLE] != null) { super.onKeyMultiple(keyCode, repeatCount, event); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onKeyMultiple" , new Object[]{keyCode, repeatCount, event}, Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_KEY_MULTIPLE], "call" , new Object[]{keyCode, repeatCount, event}); } else { - if (callbackProcs != null && callbackProcs[CB_KEY_MULTIPLE] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_key_multiple}")) { super.onKeyMultiple(keyCode, repeatCount, event); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_KEY_MULTIPLE], "call" , new Object[]{keyCode, repeatCount, event}, Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_keyCode", keyCode); + JRubyAdapter.put("$arg_repeatCount", repeatCount); + JRubyAdapter.put("$arg_event", event); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_key_multiple($arg_keyCode, $arg_repeatCount, $arg_event)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_key_multiple", new Object[]{keyCode, repeatCount, event}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onKeyMultiple(keyCode, repeatCount, event); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onKeyMultiple}")) { + super.onKeyMultiple(keyCode, repeatCount, event); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_keyCode", keyCode); + JRubyAdapter.put("$arg_repeatCount", repeatCount); + JRubyAdapter.put("$arg_event", event); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onKeyMultiple($arg_keyCode, $arg_repeatCount, $arg_event)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onKeyMultiple", new Object[]{keyCode, repeatCount, event}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onKeyMultiple(keyCode, repeatCount, event); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onKeyMultiple"); + return super.onKeyMultiple(keyCode, repeatCount, event); } } public boolean onKeyUp(int keyCode, android.view.KeyEvent event) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_key_up"}, Boolean.class)) { - super.onKeyUp(keyCode, event); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_key_up" , new Object[]{keyCode, event}, Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onKeyUp"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_KEY_UP] != null) { super.onKeyUp(keyCode, event); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onKeyUp" , new Object[]{keyCode, event}, Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_KEY_UP], "call" , new Object[]{keyCode, event}); } else { - if (callbackProcs != null && callbackProcs[CB_KEY_UP] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_key_up}")) { super.onKeyUp(keyCode, event); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_KEY_UP], "call" , new Object[]{keyCode, event}, Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_keyCode", keyCode); + JRubyAdapter.put("$arg_event", event); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_key_up($arg_keyCode, $arg_event)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_key_up", new Object[]{keyCode, event}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onKeyUp(keyCode, event); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onKeyUp}")) { + super.onKeyUp(keyCode, event); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_keyCode", keyCode); + JRubyAdapter.put("$arg_event", event); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onKeyUp($arg_keyCode, $arg_event)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onKeyUp", new Object[]{keyCode, event}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onKeyUp(keyCode, event); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onKeyUp"); + return super.onKeyUp(keyCode, event); } } public void onLowMemory() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_low_memory"}, Boolean.class)) { - super.onLowMemory(); - JRubyAdapter.callMethod(rubyInstance, "on_low_memory" ); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onLowMemory"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_LOW_MEMORY] != null) { super.onLowMemory(); - JRubyAdapter.callMethod(rubyInstance, "onLowMemory" ); + JRubyAdapter.runRubyMethod(callbackProcs[CB_LOW_MEMORY], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_LOW_MEMORY] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_low_memory}")) { super.onLowMemory(); - JRubyAdapter.callMethod(callbackProcs[CB_LOW_MEMORY], "call" ); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_low_memory()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_low_memory"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onLowMemory(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onLowMemory}")) { + super.onLowMemory(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onLowMemory()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onLowMemory"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onLowMemory(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onLowMemory"); + super.onLowMemory(); } } public boolean onMenuItemSelected(int featureId, android.view.MenuItem item) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_menu_item_selected"}, Boolean.class)) { - super.onMenuItemSelected(featureId, item); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_menu_item_selected" , new Object[]{featureId, item}, Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onMenuItemSelected"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_MENU_ITEM_SELECTED] != null) { super.onMenuItemSelected(featureId, item); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onMenuItemSelected" , new Object[]{featureId, item}, Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_MENU_ITEM_SELECTED], "call" , new Object[]{featureId, item}); } else { - if (callbackProcs != null && callbackProcs[CB_MENU_ITEM_SELECTED] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_menu_item_selected}")) { super.onMenuItemSelected(featureId, item); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_MENU_ITEM_SELECTED], "call" , new Object[]{featureId, item}, Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_featureId", featureId); + JRubyAdapter.put("$arg_item", item); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_menu_item_selected($arg_featureId, $arg_item)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_menu_item_selected", new Object[]{featureId, item}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onMenuItemSelected(featureId, item); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onMenuItemSelected}")) { + super.onMenuItemSelected(featureId, item); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_featureId", featureId); + JRubyAdapter.put("$arg_item", item); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onMenuItemSelected($arg_featureId, $arg_item)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onMenuItemSelected", new Object[]{featureId, item}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onMenuItemSelected(featureId, item); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onMenuItemSelected"); + return super.onMenuItemSelected(featureId, item); } } public boolean onMenuOpened(int featureId, android.view.Menu menu) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_menu_opened"}, Boolean.class)) { - super.onMenuOpened(featureId, menu); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_menu_opened" , new Object[]{featureId, menu}, Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onMenuOpened"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_MENU_OPENED] != null) { super.onMenuOpened(featureId, menu); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onMenuOpened" , new Object[]{featureId, menu}, Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_MENU_OPENED], "call" , new Object[]{featureId, menu}); } else { - if (callbackProcs != null && callbackProcs[CB_MENU_OPENED] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_menu_opened}")) { super.onMenuOpened(featureId, menu); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_MENU_OPENED], "call" , new Object[]{featureId, menu}, Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_featureId", featureId); + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_menu_opened($arg_featureId, $arg_menu)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_menu_opened", new Object[]{featureId, menu}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onMenuOpened(featureId, menu); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onMenuOpened}")) { + super.onMenuOpened(featureId, menu); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_featureId", featureId); + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onMenuOpened($arg_featureId, $arg_menu)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onMenuOpened", new Object[]{featureId, menu}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onMenuOpened(featureId, menu); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onMenuOpened"); + return super.onMenuOpened(featureId, menu); } } public void onNewIntent(android.content.Intent intent) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_new_intent"}, Boolean.class)) { - super.onNewIntent(intent); - JRubyAdapter.callMethod(rubyInstance, "on_new_intent" , intent); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onNewIntent"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_NEW_INTENT] != null) { super.onNewIntent(intent); - JRubyAdapter.callMethod(rubyInstance, "onNewIntent" , intent); + JRubyAdapter.runRubyMethod(callbackProcs[CB_NEW_INTENT], "call" , intent); } else { - if (callbackProcs != null && callbackProcs[CB_NEW_INTENT] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_new_intent}")) { super.onNewIntent(intent); - JRubyAdapter.callMethod(callbackProcs[CB_NEW_INTENT], "call" , intent); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_intent", intent); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_new_intent($arg_intent)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_new_intent", intent); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onNewIntent(intent); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onNewIntent}")) { + super.onNewIntent(intent); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_intent", intent); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onNewIntent($arg_intent)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onNewIntent", intent); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onNewIntent(intent); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onNewIntent"); + super.onNewIntent(intent); } } public boolean onOptionsItemSelected(android.view.MenuItem item) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_options_item_selected"}, Boolean.class)) { - super.onOptionsItemSelected(item); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_options_item_selected" , item, Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onOptionsItemSelected"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_OPTIONS_ITEM_SELECTED] != null) { super.onOptionsItemSelected(item); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onOptionsItemSelected" , item, Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_OPTIONS_ITEM_SELECTED], "call" , item); } else { - if (callbackProcs != null && callbackProcs[CB_OPTIONS_ITEM_SELECTED] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_options_item_selected}")) { super.onOptionsItemSelected(item); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_OPTIONS_ITEM_SELECTED], "call" , item, Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_item", item); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_options_item_selected($arg_item)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_options_item_selected", item); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onOptionsItemSelected(item); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onOptionsItemSelected}")) { + super.onOptionsItemSelected(item); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_item", item); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onOptionsItemSelected($arg_item)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onOptionsItemSelected", item); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onOptionsItemSelected(item); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onOptionsItemSelected"); + return super.onOptionsItemSelected(item); } } public void onOptionsMenuClosed(android.view.Menu menu) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_options_menu_closed"}, Boolean.class)) { - super.onOptionsMenuClosed(menu); - JRubyAdapter.callMethod(rubyInstance, "on_options_menu_closed" , menu); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onOptionsMenuClosed"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_OPTIONS_MENU_CLOSED] != null) { super.onOptionsMenuClosed(menu); - JRubyAdapter.callMethod(rubyInstance, "onOptionsMenuClosed" , menu); + JRubyAdapter.runRubyMethod(callbackProcs[CB_OPTIONS_MENU_CLOSED], "call" , menu); } else { - if (callbackProcs != null && callbackProcs[CB_OPTIONS_MENU_CLOSED] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_options_menu_closed}")) { super.onOptionsMenuClosed(menu); - JRubyAdapter.callMethod(callbackProcs[CB_OPTIONS_MENU_CLOSED], "call" , menu); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_options_menu_closed($arg_menu)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_options_menu_closed", menu); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onOptionsMenuClosed(menu); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onOptionsMenuClosed}")) { + super.onOptionsMenuClosed(menu); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onOptionsMenuClosed($arg_menu)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onOptionsMenuClosed", menu); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onOptionsMenuClosed(menu); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onOptionsMenuClosed"); + super.onOptionsMenuClosed(menu); } } public void onPanelClosed(int featureId, android.view.Menu menu) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_panel_closed"}, Boolean.class)) { - super.onPanelClosed(featureId, menu); - JRubyAdapter.callMethod(rubyInstance, "on_panel_closed" , new Object[]{featureId, menu}); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onPanelClosed"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_PANEL_CLOSED] != null) { super.onPanelClosed(featureId, menu); - JRubyAdapter.callMethod(rubyInstance, "onPanelClosed" , new Object[]{featureId, menu}); + JRubyAdapter.runRubyMethod(callbackProcs[CB_PANEL_CLOSED], "call" , new Object[]{featureId, menu}); } else { - if (callbackProcs != null && callbackProcs[CB_PANEL_CLOSED] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_panel_closed}")) { super.onPanelClosed(featureId, menu); - JRubyAdapter.callMethod(callbackProcs[CB_PANEL_CLOSED], "call" , new Object[]{featureId, menu}); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_featureId", featureId); + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_panel_closed($arg_featureId, $arg_menu)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_panel_closed", new Object[]{featureId, menu}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onPanelClosed(featureId, menu); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onPanelClosed}")) { + super.onPanelClosed(featureId, menu); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_featureId", featureId); + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onPanelClosed($arg_featureId, $arg_menu)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onPanelClosed", new Object[]{featureId, menu}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onPanelClosed(featureId, menu); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onPanelClosed"); + super.onPanelClosed(featureId, menu); } } public void onPause() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_pause"}, Boolean.class)) { - super.onPause(); - JRubyAdapter.callMethod(rubyInstance, "on_pause" ); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onPause"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_PAUSE] != null) { super.onPause(); - JRubyAdapter.callMethod(rubyInstance, "onPause" ); + JRubyAdapter.runRubyMethod(callbackProcs[CB_PAUSE], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_PAUSE] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_pause}")) { super.onPause(); - JRubyAdapter.callMethod(callbackProcs[CB_PAUSE], "call" ); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_pause()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_pause"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onPause(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onPause}")) { + super.onPause(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onPause()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onPause"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onPause(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onPause"); + super.onPause(); } } public void onPostCreate(android.os.Bundle savedInstanceState) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_post_create"}, Boolean.class)) { - super.onPostCreate(savedInstanceState); - JRubyAdapter.callMethod(rubyInstance, "on_post_create" , savedInstanceState); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onPostCreate"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_POST_CREATE] != null) { super.onPostCreate(savedInstanceState); - JRubyAdapter.callMethod(rubyInstance, "onPostCreate" , savedInstanceState); + JRubyAdapter.runRubyMethod(callbackProcs[CB_POST_CREATE], "call" , savedInstanceState); } else { - if (callbackProcs != null && callbackProcs[CB_POST_CREATE] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_post_create}")) { super.onPostCreate(savedInstanceState); - JRubyAdapter.callMethod(callbackProcs[CB_POST_CREATE], "call" , savedInstanceState); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_savedInstanceState", savedInstanceState); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_post_create($arg_savedInstanceState)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_post_create", savedInstanceState); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onPostCreate(savedInstanceState); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onPostCreate}")) { + super.onPostCreate(savedInstanceState); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_savedInstanceState", savedInstanceState); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onPostCreate($arg_savedInstanceState)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onPostCreate", savedInstanceState); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onPostCreate(savedInstanceState); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onPostCreate"); + super.onPostCreate(savedInstanceState); } } public void onPostResume() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_post_resume"}, Boolean.class)) { - super.onPostResume(); - JRubyAdapter.callMethod(rubyInstance, "on_post_resume" ); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onPostResume"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_POST_RESUME] != null) { super.onPostResume(); - JRubyAdapter.callMethod(rubyInstance, "onPostResume" ); + JRubyAdapter.runRubyMethod(callbackProcs[CB_POST_RESUME], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_POST_RESUME] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_post_resume}")) { super.onPostResume(); - JRubyAdapter.callMethod(callbackProcs[CB_POST_RESUME], "call" ); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_post_resume()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_post_resume"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onPostResume(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onPostResume}")) { + super.onPostResume(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onPostResume()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onPostResume"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onPostResume(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onPostResume"); + super.onPostResume(); } } public boolean onPrepareOptionsMenu(android.view.Menu menu) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_prepare_options_menu"}, Boolean.class)) { - super.onPrepareOptionsMenu(menu); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_prepare_options_menu" , menu, Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onPrepareOptionsMenu"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_PREPARE_OPTIONS_MENU] != null) { super.onPrepareOptionsMenu(menu); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onPrepareOptionsMenu" , menu, Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_PREPARE_OPTIONS_MENU], "call" , menu); } else { - if (callbackProcs != null && callbackProcs[CB_PREPARE_OPTIONS_MENU] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_prepare_options_menu}")) { super.onPrepareOptionsMenu(menu); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_PREPARE_OPTIONS_MENU], "call" , menu, Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_prepare_options_menu($arg_menu)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_prepare_options_menu", menu); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onPrepareOptionsMenu(menu); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onPrepareOptionsMenu}")) { + super.onPrepareOptionsMenu(menu); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onPrepareOptionsMenu($arg_menu)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onPrepareOptionsMenu", menu); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onPrepareOptionsMenu(menu); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onPrepareOptionsMenu"); + return super.onPrepareOptionsMenu(menu); } } public boolean onPreparePanel(int featureId, android.view.View view, android.view.Menu menu) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_prepare_panel"}, Boolean.class)) { - super.onPreparePanel(featureId, view, menu); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_prepare_panel" , new Object[]{featureId, view, menu}, Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onPreparePanel"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_PREPARE_PANEL] != null) { super.onPreparePanel(featureId, view, menu); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onPreparePanel" , new Object[]{featureId, view, menu}, Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_PREPARE_PANEL], "call" , new Object[]{featureId, view, menu}); } else { - if (callbackProcs != null && callbackProcs[CB_PREPARE_PANEL] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_prepare_panel}")) { super.onPreparePanel(featureId, view, menu); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_PREPARE_PANEL], "call" , new Object[]{featureId, view, menu}, Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_featureId", featureId); + JRubyAdapter.put("$arg_view", view); + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_prepare_panel($arg_featureId, $arg_view, $arg_menu)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_prepare_panel", new Object[]{featureId, view, menu}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onPreparePanel(featureId, view, menu); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onPreparePanel}")) { + super.onPreparePanel(featureId, view, menu); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_featureId", featureId); + JRubyAdapter.put("$arg_view", view); + JRubyAdapter.put("$arg_menu", menu); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onPreparePanel($arg_featureId, $arg_view, $arg_menu)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onPreparePanel", new Object[]{featureId, view, menu}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onPreparePanel(featureId, view, menu); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onPreparePanel"); + return super.onPreparePanel(featureId, view, menu); } } public void onRestart() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_restart"}, Boolean.class)) { - super.onRestart(); - JRubyAdapter.callMethod(rubyInstance, "on_restart" ); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onRestart"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_RESTART] != null) { super.onRestart(); - JRubyAdapter.callMethod(rubyInstance, "onRestart" ); + JRubyAdapter.runRubyMethod(callbackProcs[CB_RESTART], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_RESTART] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_restart}")) { super.onRestart(); - JRubyAdapter.callMethod(callbackProcs[CB_RESTART], "call" ); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_restart()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_restart"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onRestart(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onRestart}")) { + super.onRestart(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onRestart()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onRestart"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onRestart(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onRestart"); + super.onRestart(); } } public void onRestoreInstanceState(android.os.Bundle savedInstanceState) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_restore_instance_state"}, Boolean.class)) { - super.onRestoreInstanceState(savedInstanceState); - JRubyAdapter.callMethod(rubyInstance, "on_restore_instance_state" , savedInstanceState); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onRestoreInstanceState"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_RESTORE_INSTANCE_STATE] != null) { super.onRestoreInstanceState(savedInstanceState); - JRubyAdapter.callMethod(rubyInstance, "onRestoreInstanceState" , savedInstanceState); + JRubyAdapter.runRubyMethod(callbackProcs[CB_RESTORE_INSTANCE_STATE], "call" , savedInstanceState); } else { - if (callbackProcs != null && callbackProcs[CB_RESTORE_INSTANCE_STATE] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_restore_instance_state}")) { super.onRestoreInstanceState(savedInstanceState); - JRubyAdapter.callMethod(callbackProcs[CB_RESTORE_INSTANCE_STATE], "call" , savedInstanceState); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_savedInstanceState", savedInstanceState); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_restore_instance_state($arg_savedInstanceState)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_restore_instance_state", savedInstanceState); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onRestoreInstanceState(savedInstanceState); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onRestoreInstanceState}")) { + super.onRestoreInstanceState(savedInstanceState); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_savedInstanceState", savedInstanceState); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onRestoreInstanceState($arg_savedInstanceState)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onRestoreInstanceState", savedInstanceState); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onRestoreInstanceState(savedInstanceState); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onRestoreInstanceState"); + super.onRestoreInstanceState(savedInstanceState); } } public void onResume() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_resume"}, Boolean.class)) { - super.onResume(); - JRubyAdapter.callMethod(rubyInstance, "on_resume" ); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onResume"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_RESUME] != null) { super.onResume(); - JRubyAdapter.callMethod(rubyInstance, "onResume" ); + JRubyAdapter.runRubyMethod(callbackProcs[CB_RESUME], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_RESUME] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_resume}")) { super.onResume(); - JRubyAdapter.callMethod(callbackProcs[CB_RESUME], "call" ); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_resume()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_resume"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onResume(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onResume}")) { + super.onResume(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onResume()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onResume"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onResume(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onResume"); + super.onResume(); } } public void onSaveInstanceState(android.os.Bundle outState) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_save_instance_state"}, Boolean.class)) { - super.onSaveInstanceState(outState); - JRubyAdapter.callMethod(rubyInstance, "on_save_instance_state" , outState); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onSaveInstanceState"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_SAVE_INSTANCE_STATE] != null) { super.onSaveInstanceState(outState); - JRubyAdapter.callMethod(rubyInstance, "onSaveInstanceState" , outState); + JRubyAdapter.runRubyMethod(callbackProcs[CB_SAVE_INSTANCE_STATE], "call" , outState); } else { - if (callbackProcs != null && callbackProcs[CB_SAVE_INSTANCE_STATE] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_save_instance_state}")) { super.onSaveInstanceState(outState); - JRubyAdapter.callMethod(callbackProcs[CB_SAVE_INSTANCE_STATE], "call" , outState); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_outState", outState); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_save_instance_state($arg_outState)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_save_instance_state", outState); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onSaveInstanceState(outState); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onSaveInstanceState}")) { + super.onSaveInstanceState(outState); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_outState", outState); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onSaveInstanceState($arg_outState)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onSaveInstanceState", outState); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onSaveInstanceState(outState); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onSaveInstanceState"); + super.onSaveInstanceState(outState); } } public boolean onSearchRequested() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_search_requested"}, Boolean.class)) { - super.onSearchRequested(); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_search_requested" , Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onSearchRequested"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_SEARCH_REQUESTED] != null) { super.onSearchRequested(); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onSearchRequested" , Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_SEARCH_REQUESTED], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_SEARCH_REQUESTED] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_search_requested}")) { super.onSearchRequested(); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_SEARCH_REQUESTED], "call" , Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_search_requested()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_search_requested"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onSearchRequested(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onSearchRequested}")) { + super.onSearchRequested(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onSearchRequested()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onSearchRequested"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onSearchRequested(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onSearchRequested"); + return super.onSearchRequested(); } } public void onStart() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_start"}, Boolean.class)) { - super.onStart(); - JRubyAdapter.callMethod(rubyInstance, "on_start" ); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onStart"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_START] != null) { super.onStart(); - JRubyAdapter.callMethod(rubyInstance, "onStart" ); + JRubyAdapter.runRubyMethod(callbackProcs[CB_START], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_START] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_start}")) { super.onStart(); - JRubyAdapter.callMethod(callbackProcs[CB_START], "call" ); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_start()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_start"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onStart(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onStart}")) { + super.onStart(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onStart()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onStart"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onStart(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onStart"); + super.onStart(); } } public void onStop() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_stop"}, Boolean.class)) { - super.onStop(); - JRubyAdapter.callMethod(rubyInstance, "on_stop" ); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onStop"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_STOP] != null) { super.onStop(); - JRubyAdapter.callMethod(rubyInstance, "onStop" ); + JRubyAdapter.runRubyMethod(callbackProcs[CB_STOP], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_STOP] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_stop}")) { super.onStop(); - JRubyAdapter.callMethod(callbackProcs[CB_STOP], "call" ); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_stop()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_stop"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onStop(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onStop}")) { + super.onStop(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onStop()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onStop"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onStop(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onStop"); + super.onStop(); } } public void onTitleChanged(java.lang.CharSequence title, int color) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_title_changed"}, Boolean.class)) { - super.onTitleChanged(title, color); - JRubyAdapter.callMethod(rubyInstance, "on_title_changed" , new Object[]{title, color}); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onTitleChanged"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_TITLE_CHANGED] != null) { super.onTitleChanged(title, color); - JRubyAdapter.callMethod(rubyInstance, "onTitleChanged" , new Object[]{title, color}); + JRubyAdapter.runRubyMethod(callbackProcs[CB_TITLE_CHANGED], "call" , new Object[]{title, color}); } else { - if (callbackProcs != null && callbackProcs[CB_TITLE_CHANGED] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_title_changed}")) { super.onTitleChanged(title, color); - JRubyAdapter.callMethod(callbackProcs[CB_TITLE_CHANGED], "call" , new Object[]{title, color}); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_title", title); + JRubyAdapter.put("$arg_color", color); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_title_changed($arg_title, $arg_color)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_title_changed", new Object[]{title, color}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onTitleChanged(title, color); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onTitleChanged}")) { + super.onTitleChanged(title, color); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_title", title); + JRubyAdapter.put("$arg_color", color); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onTitleChanged($arg_title, $arg_color)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onTitleChanged", new Object[]{title, color}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onTitleChanged(title, color); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onTitleChanged"); + super.onTitleChanged(title, color); } } public boolean onTouchEvent(android.view.MotionEvent event) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_touch_event"}, Boolean.class)) { - super.onTouchEvent(event); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_touch_event" , event, Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onTouchEvent"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_TOUCH_EVENT] != null) { super.onTouchEvent(event); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onTouchEvent" , event, Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_TOUCH_EVENT], "call" , event); } else { - if (callbackProcs != null && callbackProcs[CB_TOUCH_EVENT] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_touch_event}")) { super.onTouchEvent(event); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_TOUCH_EVENT], "call" , event, Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_event", event); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_touch_event($arg_event)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_touch_event", event); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onTouchEvent(event); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onTouchEvent}")) { + super.onTouchEvent(event); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_event", event); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onTouchEvent($arg_event)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onTouchEvent", event); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onTouchEvent(event); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onTouchEvent"); + return super.onTouchEvent(event); } } public boolean onTrackballEvent(android.view.MotionEvent event) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_trackball_event"}, Boolean.class)) { - super.onTrackballEvent(event); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_trackball_event" , event, Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onTrackballEvent"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_TRACKBALL_EVENT] != null) { super.onTrackballEvent(event); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onTrackballEvent" , event, Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_TRACKBALL_EVENT], "call" , event); } else { - if (callbackProcs != null && callbackProcs[CB_TRACKBALL_EVENT] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_trackball_event}")) { super.onTrackballEvent(event); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_TRACKBALL_EVENT], "call" , event, Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_event", event); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_trackball_event($arg_event)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_trackball_event", event); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onTrackballEvent(event); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onTrackballEvent}")) { + super.onTrackballEvent(event); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_event", event); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onTrackballEvent($arg_event)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onTrackballEvent", event); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onTrackballEvent(event); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onTrackballEvent"); + return super.onTrackballEvent(event); } } public void onWindowAttributesChanged(android.view.WindowManager.LayoutParams params) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_window_attributes_changed"}, Boolean.class)) { - super.onWindowAttributesChanged(params); - JRubyAdapter.callMethod(rubyInstance, "on_window_attributes_changed" , params); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onWindowAttributesChanged"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_WINDOW_ATTRIBUTES_CHANGED] != null) { super.onWindowAttributesChanged(params); - JRubyAdapter.callMethod(rubyInstance, "onWindowAttributesChanged" , params); + JRubyAdapter.runRubyMethod(callbackProcs[CB_WINDOW_ATTRIBUTES_CHANGED], "call" , params); } else { - if (callbackProcs != null && callbackProcs[CB_WINDOW_ATTRIBUTES_CHANGED] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_window_attributes_changed}")) { super.onWindowAttributesChanged(params); - JRubyAdapter.callMethod(callbackProcs[CB_WINDOW_ATTRIBUTES_CHANGED], "call" , params); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_params", params); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_window_attributes_changed($arg_params)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_window_attributes_changed", params); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onWindowAttributesChanged(params); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onWindowAttributesChanged}")) { + super.onWindowAttributesChanged(params); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_params", params); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onWindowAttributesChanged($arg_params)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onWindowAttributesChanged", params); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onWindowAttributesChanged(params); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onWindowAttributesChanged"); + super.onWindowAttributesChanged(params); } } public void onWindowFocusChanged(boolean hasFocus) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_window_focus_changed"}, Boolean.class)) { - super.onWindowFocusChanged(hasFocus); - JRubyAdapter.callMethod(rubyInstance, "on_window_focus_changed" , hasFocus); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onWindowFocusChanged"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_WINDOW_FOCUS_CHANGED] != null) { super.onWindowFocusChanged(hasFocus); - JRubyAdapter.callMethod(rubyInstance, "onWindowFocusChanged" , hasFocus); + JRubyAdapter.runRubyMethod(callbackProcs[CB_WINDOW_FOCUS_CHANGED], "call" , hasFocus); } else { - if (callbackProcs != null && callbackProcs[CB_WINDOW_FOCUS_CHANGED] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_window_focus_changed}")) { super.onWindowFocusChanged(hasFocus); - JRubyAdapter.callMethod(callbackProcs[CB_WINDOW_FOCUS_CHANGED], "call" , hasFocus); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_hasFocus", hasFocus); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_window_focus_changed($arg_hasFocus)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_window_focus_changed", hasFocus); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onWindowFocusChanged(hasFocus); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onWindowFocusChanged}")) { + super.onWindowFocusChanged(hasFocus); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_hasFocus", hasFocus); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onWindowFocusChanged($arg_hasFocus)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onWindowFocusChanged", hasFocus); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onWindowFocusChanged(hasFocus); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onWindowFocusChanged"); + super.onWindowFocusChanged(hasFocus); } } public void onUserInteraction() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_user_interaction"}, Boolean.class)) { - super.onUserInteraction(); - JRubyAdapter.callMethod(rubyInstance, "on_user_interaction" ); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onUserInteraction"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_USER_INTERACTION] != null) { super.onUserInteraction(); - JRubyAdapter.callMethod(rubyInstance, "onUserInteraction" ); + JRubyAdapter.runRubyMethod(callbackProcs[CB_USER_INTERACTION], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_USER_INTERACTION] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_user_interaction}")) { super.onUserInteraction(); - JRubyAdapter.callMethod(callbackProcs[CB_USER_INTERACTION], "call" ); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_user_interaction()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_user_interaction"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onUserInteraction(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onUserInteraction}")) { + super.onUserInteraction(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onUserInteraction()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onUserInteraction"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onUserInteraction(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onUserInteraction"); + super.onUserInteraction(); } } public void onUserLeaveHint() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_user_leave_hint"}, Boolean.class)) { - super.onUserLeaveHint(); - JRubyAdapter.callMethod(rubyInstance, "on_user_leave_hint" ); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onUserLeaveHint"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_USER_LEAVE_HINT] != null) { super.onUserLeaveHint(); - JRubyAdapter.callMethod(rubyInstance, "onUserLeaveHint" ); + JRubyAdapter.runRubyMethod(callbackProcs[CB_USER_LEAVE_HINT], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_USER_LEAVE_HINT] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_user_leave_hint}")) { super.onUserLeaveHint(); - JRubyAdapter.callMethod(callbackProcs[CB_USER_LEAVE_HINT], "call" ); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_user_leave_hint()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_user_leave_hint"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onUserLeaveHint(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onUserLeaveHint}")) { + super.onUserLeaveHint(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onUserLeaveHint()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onUserLeaveHint"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onUserLeaveHint(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onUserLeaveHint"); + super.onUserLeaveHint(); } } public void onAttachedToWindow() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_attached_to_window"}, Boolean.class)) { - super.onAttachedToWindow(); - JRubyAdapter.callMethod(rubyInstance, "on_attached_to_window" ); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onAttachedToWindow"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_ATTACHED_TO_WINDOW] != null) { super.onAttachedToWindow(); - JRubyAdapter.callMethod(rubyInstance, "onAttachedToWindow" ); + JRubyAdapter.runRubyMethod(callbackProcs[CB_ATTACHED_TO_WINDOW], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_ATTACHED_TO_WINDOW] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_attached_to_window}")) { super.onAttachedToWindow(); - JRubyAdapter.callMethod(callbackProcs[CB_ATTACHED_TO_WINDOW], "call" ); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_attached_to_window()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_attached_to_window"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onAttachedToWindow(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onAttachedToWindow}")) { + super.onAttachedToWindow(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onAttachedToWindow()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onAttachedToWindow"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onAttachedToWindow(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onAttachedToWindow"); + super.onAttachedToWindow(); } } public void onBackPressed() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_back_pressed"}, Boolean.class)) { - super.onBackPressed(); - JRubyAdapter.callMethod(rubyInstance, "on_back_pressed" ); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onBackPressed"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_BACK_PRESSED] != null) { super.onBackPressed(); - JRubyAdapter.callMethod(rubyInstance, "onBackPressed" ); + JRubyAdapter.runRubyMethod(callbackProcs[CB_BACK_PRESSED], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_BACK_PRESSED] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_back_pressed}")) { super.onBackPressed(); - JRubyAdapter.callMethod(callbackProcs[CB_BACK_PRESSED], "call" ); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_back_pressed()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_back_pressed"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onBackPressed(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onBackPressed}")) { + super.onBackPressed(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onBackPressed()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onBackPressed"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onBackPressed(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onBackPressed"); + super.onBackPressed(); } } public void onDetachedFromWindow() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_detached_from_window"}, Boolean.class)) { - super.onDetachedFromWindow(); - JRubyAdapter.callMethod(rubyInstance, "on_detached_from_window" ); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onDetachedFromWindow"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_DETACHED_FROM_WINDOW] != null) { super.onDetachedFromWindow(); - JRubyAdapter.callMethod(rubyInstance, "onDetachedFromWindow" ); + JRubyAdapter.runRubyMethod(callbackProcs[CB_DETACHED_FROM_WINDOW], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_DETACHED_FROM_WINDOW] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_detached_from_window}")) { super.onDetachedFromWindow(); - JRubyAdapter.callMethod(callbackProcs[CB_DETACHED_FROM_WINDOW], "call" ); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_detached_from_window()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_detached_from_window"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onDetachedFromWindow(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onDetachedFromWindow}")) { + super.onDetachedFromWindow(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onDetachedFromWindow()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onDetachedFromWindow"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onDetachedFromWindow(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onDetachedFromWindow"); + super.onDetachedFromWindow(); } } public boolean onKeyLongPress(int keyCode, android.view.KeyEvent event) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_key_long_press"}, Boolean.class)) { - super.onKeyLongPress(keyCode, event); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_key_long_press" , new Object[]{keyCode, event}, Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onKeyLongPress"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_KEY_LONG_PRESS] != null) { super.onKeyLongPress(keyCode, event); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onKeyLongPress" , new Object[]{keyCode, event}, Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_KEY_LONG_PRESS], "call" , new Object[]{keyCode, event}); } else { - if (callbackProcs != null && callbackProcs[CB_KEY_LONG_PRESS] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_key_long_press}")) { super.onKeyLongPress(keyCode, event); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_KEY_LONG_PRESS], "call" , new Object[]{keyCode, event}, Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_keyCode", keyCode); + JRubyAdapter.put("$arg_event", event); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_key_long_press($arg_keyCode, $arg_event)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_key_long_press", new Object[]{keyCode, event}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onKeyLongPress(keyCode, event); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onKeyLongPress}")) { + super.onKeyLongPress(keyCode, event); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_keyCode", keyCode); + JRubyAdapter.put("$arg_event", event); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onKeyLongPress($arg_keyCode, $arg_event)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onKeyLongPress", new Object[]{keyCode, event}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onKeyLongPress(keyCode, event); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onKeyLongPress"); + return super.onKeyLongPress(keyCode, event); } } public void onApplyThemeResource(android.content.res.Resources.Theme theme, int resid, boolean first) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_apply_theme_resource"}, Boolean.class)) { - super.onApplyThemeResource(theme, resid, first); - JRubyAdapter.callMethod(rubyInstance, "on_apply_theme_resource" , new Object[]{theme, resid, first}); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onApplyThemeResource"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_APPLY_THEME_RESOURCE] != null) { super.onApplyThemeResource(theme, resid, first); - JRubyAdapter.callMethod(rubyInstance, "onApplyThemeResource" , new Object[]{theme, resid, first}); + JRubyAdapter.runRubyMethod(callbackProcs[CB_APPLY_THEME_RESOURCE], "call" , new Object[]{theme, resid, first}); } else { - if (callbackProcs != null && callbackProcs[CB_APPLY_THEME_RESOURCE] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_apply_theme_resource}")) { super.onApplyThemeResource(theme, resid, first); - JRubyAdapter.callMethod(callbackProcs[CB_APPLY_THEME_RESOURCE], "call" , new Object[]{theme, resid, first}); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_theme", theme); + JRubyAdapter.put("$arg_resid", resid); + JRubyAdapter.put("$arg_first", first); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_apply_theme_resource($arg_theme, $arg_resid, $arg_first)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_apply_theme_resource", new Object[]{theme, resid, first}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onApplyThemeResource(theme, resid, first); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onApplyThemeResource}")) { + super.onApplyThemeResource(theme, resid, first); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_theme", theme); + JRubyAdapter.put("$arg_resid", resid); + JRubyAdapter.put("$arg_first", first); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onApplyThemeResource($arg_theme, $arg_resid, $arg_first)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onApplyThemeResource", new Object[]{theme, resid, first}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onApplyThemeResource(theme, resid, first); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoActivity#onApplyThemeResource"); + super.onApplyThemeResource(theme, resid, first); } } diff --git a/src/org/ruboto/RubotoBroadcastReceiver.java b/src/org/ruboto/RubotoBroadcastReceiver.java index 21ed9ec..3f9ba1d 100644 --- a/src/org/ruboto/RubotoBroadcastReceiver.java +++ b/src/org/ruboto/RubotoBroadcastReceiver.java @@ -3,8 +3,10 @@ import java.io.IOException; public class RubotoBroadcastReceiver extends android.content.BroadcastReceiver { - private String scriptName = null; + private String rubyClassName; + private String scriptName; private Object rubyInstance; + private Object[] callbackProcs = new Object[1]; public void setCallbackProc(int id, Object obj) { // Error: no callbacks @@ -32,48 +34,82 @@ public RubotoBroadcastReceiver(String name) { } protected void loadScript() { + rubyInstance = this; // TODO(uwe): Only needed for non-class-based definitions // Can be removed if we stop supporting non-class-based definitions JRubyAdapter.put("$broadcast_receiver", this); // TODO end - if (scriptName != null) { - try { + try { + if (scriptName != null) { String rubyClassName = Script.toCamelCase(scriptName); System.out.println("Looking for Ruby class: " + rubyClassName); - Object rubyClass = JRubyAdapter.get(rubyClassName); + Object rubyClass = null; + String script = new Script(scriptName).getContents(); + if (script.matches("(?s).*class " + rubyClassName + ".*")) { + if (!rubyClassName.equals(getClass().getSimpleName())) { + System.out.println("Script defines methods on meta class"); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven() || JRubyAdapter.isRubyOneEight()) { + JRubyAdapter.put("$java_instance", this); + JRubyAdapter.put(rubyClassName, JRubyAdapter.runScriptlet("class << $java_instance; self; end")); + } else if (JRubyAdapter.isJRubyOneSeven() && JRubyAdapter.isRubyOneNine()) { + JRubyAdapter.put(rubyClassName, JRubyAdapter.runRubyMethod(this, "singleton_class")); + } else { + throw new RuntimeException("Unknown JRuby/Ruby version: " + JRubyAdapter.get("JRUBY_VERSION") + "/" + JRubyAdapter.get("RUBY_VERSION")); + } + } + } else { + rubyClass = JRubyAdapter.get(rubyClassName); + } if (rubyClass == null) { System.out.println("Loading script: " + scriptName); - JRubyAdapter.exec(new Script(scriptName).getContents()); + if (script.matches("(?s).*class " + rubyClassName + ".*")) { + System.out.println("Script contains class definition"); + if (rubyClassName.equals(getClass().getSimpleName())) { + System.out.println("Script has separate Java class"); + + // TODO(uwe): Why doesnt this work? + // JRubyAdapter.put(rubyClassName, JRubyAdapter.runScriptlet("Java::" + getClass().getName())); + + // TODO(uwe): Workaround... + JRubyAdapter.runScriptlet(rubyClassName + " = Java::" + getClass().getName()); + } + // System.out.println("Set class: " + JRubyAdapter.get(rubyClassName)); + } + JRubyAdapter.setScriptFilename(scriptName); + JRubyAdapter.runScriptlet(script); rubyClass = JRubyAdapter.get(rubyClassName); } - if (rubyClass != null) { - System.out.println("Instanciating Ruby class: " + rubyClassName); - rubyInstance = JRubyAdapter.callMethod(rubyClass, "new", this, Object.class); - } - } catch(IOException e) { - throw new RuntimeException("IOException loading broadcast receiver script", e); } + } catch(IOException e) { + throw new RuntimeException("IOException loading broadcast receiver script", e); } } public void onReceive(android.content.Context context, android.content.Intent intent) { try { - System.out.println("onReceive: " + rubyInstance); - if (rubyInstance != null) { - JRubyAdapter.callMethod(rubyInstance, "on_receive", new Object[]{context, intent}); + Log.d("onReceive: " + this); + + // FIXME(uwe): Only needed for older broadcast receiver using callbacks + // FIXME(uwe): Remove if we stop suppporting callbacks (to avoid global variables). + JRubyAdapter.put("$context", context); + JRubyAdapter.put("$intent", intent); + JRubyAdapter.put("$broadcast_receiver", this); + // FIXME end + + // FIXME(uwe): Simplify when we stop supporting JRuby 1.6.x + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.runScriptlet("$broadcast_receiver.on_receive($context, $intent)"); + } else if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(this, "on_receive", new Object[]{context, intent}); } else { - // TODO(uwe): Only needed for non-class-based definitions - // Can be removed if we stop supporting non-class-based definitions - JRubyAdapter.put("$context", context); - JRubyAdapter.put("$broadcast_receiver", this); - JRubyAdapter.put("$intent", intent); - JRubyAdapter.execute("$broadcast_receiver.on_receive($context, $intent)"); - // TODO end - } + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } } catch(Exception e) { e.printStackTrace(); } } -} + +} diff --git a/src/org/ruboto/RubotoService.java b/src/org/ruboto/RubotoService.java index 6e63fde..495fb5a 100644 --- a/src/org/ruboto/RubotoService.java +++ b/src/org/ruboto/RubotoService.java @@ -4,10 +4,6 @@ import java.io.IOException; public class RubotoService extends android.app.Service { - private String scriptName; - public Object[] args; - private Object rubyInstance; - public static final int CB_BIND = 0; public static final int CB_CONFIGURATION_CHANGED = 1; public static final int CB_DESTROY = 2; @@ -16,61 +12,119 @@ public class RubotoService extends android.app.Service { public static final int CB_UNBIND = 5; public static final int CB_START_COMMAND = 6; - private Object[] callbackProcs = new Object[7]; + private String rubyClassName; + private String scriptName; + private Object rubyInstance; + private Object[] callbackProcs = new Object[7]; + public Object[] args; - public void setCallbackProc(int id, Object obj) { - callbackProcs[id] = obj; - } + public void setCallbackProc(int id, Object obj) { + callbackProcs[id] = obj; + } - public void setScriptName(String name){ - scriptName = name; - } + public void setScriptName(String name){ + scriptName = name; + } - /**************************************************************************************** - * - * Activity Lifecycle: onCreate - */ - - @Override - public void onCreate() { - System.out.println("RubotoService.onCreate()"); - args = new Object[0]; - - super.onCreate(); - - if (JRubyAdapter.setUpJRuby(this)) { - // TODO(uwe): Only needed for non-class-based definitions - // Can be removed if we stop supporting non-class-based definitions - JRubyAdapter.defineGlobalVariable("$context", this); - JRubyAdapter.defineGlobalVariable("$service", this); - // TODO end - - try { - if (scriptName != null) { - String rubyClassName = Script.toCamelCase(scriptName); - System.out.println("Looking for Ruby class: " + rubyClassName); - Object rubyClass = JRubyAdapter.get(rubyClassName); - if (rubyClass == null) { - System.out.println("Loading script: " + scriptName); - JRubyAdapter.exec(new Script(scriptName).getContents()); - rubyClass = JRubyAdapter.get(rubyClassName); - } - if (rubyClass != null) { - System.out.println("Instanciating Ruby class: " + rubyClassName); - rubyInstance = JRubyAdapter.callMethod(rubyClass, "new", this, Object.class); - JRubyAdapter.callMethod(rubyInstance, "on_create"); + /**************************************************************************************** + * + * Service Lifecycle: onCreate + */ + + @Override + public void onCreate() { + // Return if we are called from JRuby to avoid infinite recursion. + StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); + for(StackTraceElement e : stackTraceElements){ + if (e.getClassName().equals("java.lang.reflect.Method") && e.getMethodName().equals("invokeNative")) { + return; + } + if (e.getClassName().equals("android.app.ActivityThread") && e.getMethodName().equals("handleCreateService")) { + break; + } + } + + System.out.println("RubotoService.onCreate()"); + args = new Object[0]; + + super.onCreate(); + + if (JRubyAdapter.setUpJRuby(this)) { + rubyInstance = this; + + // TODO(uwe): Only needed for non-class-based definitions + // Can be removed if we stop supporting non-class-based definitions + JRubyAdapter.defineGlobalVariable("$context", this); + JRubyAdapter.defineGlobalVariable("$service", this); + // TODO end + + try { + if (scriptName != null) { + String rubyClassName = Script.toCamelCase(scriptName); + System.out.println("Looking for Ruby class: " + rubyClassName); + Object rubyClass = null; + String script = new Script(scriptName).getContents(); + if (script.matches("(?s).*class " + rubyClassName + ".*")) { + if (!rubyClassName.equals(getClass().getSimpleName())) { + System.out.println("Script defines methods on meta class"); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven() || JRubyAdapter.isRubyOneEight()) { + JRubyAdapter.put("$java_instance", this); + JRubyAdapter.put(rubyClassName, JRubyAdapter.runScriptlet("class << $java_instance; self; end")); + } else if (JRubyAdapter.isJRubyOneSeven() && JRubyAdapter.isRubyOneNine()) { + JRubyAdapter.put(rubyClassName, JRubyAdapter.runRubyMethod(this, "singleton_class")); + } else { + throw new RuntimeException("Unknown JRuby/Ruby version: " + JRubyAdapter.get("JRUBY_VERSION") + "/" + JRubyAdapter.get("RUBY_VERSION")); + } + } + } else { + rubyClass = JRubyAdapter.get(rubyClassName); + } + if (rubyClass == null) { + System.out.println("Loading script: " + scriptName); + if (script.matches("(?s).*class " + rubyClassName + ".*")) { + System.out.println("Script contains class definition"); + if (rubyClassName.equals(getClass().getSimpleName())) { + System.out.println("Script has separate Java class"); + JRubyAdapter.put(rubyClassName, JRubyAdapter.runScriptlet("Java::" + getClass().getName())); + } + // System.out.println("Set class: " + JRubyAdapter.get(rubyClassName)); + } + JRubyAdapter.setScriptFilename(scriptName); + JRubyAdapter.runScriptlet(script); + rubyClass = JRubyAdapter.get(rubyClassName); + } + if (rubyClass != null) { + System.out.println("Call on_create on: " + this); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", this); + JRubyAdapter.runScriptlet("$ruby_instance.on_create"); + } else if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(this, "on_create"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.runScriptlet("$service.initialize_ruboto"); + JRubyAdapter.runScriptlet("$service.on_create"); + } else if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(this, "initialize_ruboto"); + JRubyAdapter.runRubyMethod(this, "on_create", args[0]); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } } - } else { - JRubyAdapter.execute("$service.initialize_ruboto"); - JRubyAdapter.execute("$service.on_create"); + } catch(IOException e) { + e.printStackTrace(); } - } catch(IOException e) { - e.printStackTrace(); + } else { + // FIXME(uwe): What to do if the Ruboto Core platform cannot be found? } - } else { - // FIXME(uwe): What to do if the Ruboto Core plarform cannot be found? } - } /**************************************************************************************** * @@ -78,135 +132,329 @@ public void onCreate() { */ public android.os.IBinder onBind(android.content.Intent intent) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_bind"}, Boolean.class)) { - return (android.os.IBinder) JRubyAdapter.callMethod(rubyInstance, "on_bind" , intent, android.os.IBinder.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onBind"}, Boolean.class)) { - return (android.os.IBinder) JRubyAdapter.callMethod(rubyInstance, "onBind" , intent, android.os.IBinder.class); + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_BIND] != null) { + return (android.os.IBinder) JRubyAdapter.runRubyMethod(android.os.IBinder.class, callbackProcs[CB_BIND], "call" , intent); } else { - if (callbackProcs != null && callbackProcs[CB_BIND] != null) { - return (android.os.IBinder) JRubyAdapter.callMethod(callbackProcs[CB_BIND], "call" , intent, android.os.IBinder.class); + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_bind}")) { + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_intent", intent); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (android.os.IBinder) JRubyAdapter.runScriptlet("$ruby_instance.on_bind($arg_intent)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (android.os.IBinder) JRubyAdapter.runRubyMethod(android.os.IBinder.class, rubyInstance, "on_bind", intent); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return null; + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onBind}")) { + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_intent", intent); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (android.os.IBinder) JRubyAdapter.runScriptlet("$ruby_instance.onBind($arg_intent)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (android.os.IBinder) JRubyAdapter.runRubyMethod(android.os.IBinder.class, rubyInstance, "onBind", intent); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return null; + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoService#onBind"); + return null; } } public void onConfigurationChanged(android.content.res.Configuration newConfig) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_configuration_changed"}, Boolean.class)) { - super.onConfigurationChanged(newConfig); - JRubyAdapter.callMethod(rubyInstance, "on_configuration_changed" , newConfig); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onConfigurationChanged"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_CONFIGURATION_CHANGED] != null) { super.onConfigurationChanged(newConfig); - JRubyAdapter.callMethod(rubyInstance, "onConfigurationChanged" , newConfig); + JRubyAdapter.runRubyMethod(callbackProcs[CB_CONFIGURATION_CHANGED], "call" , newConfig); } else { - if (callbackProcs != null && callbackProcs[CB_CONFIGURATION_CHANGED] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_configuration_changed}")) { super.onConfigurationChanged(newConfig); - JRubyAdapter.callMethod(callbackProcs[CB_CONFIGURATION_CHANGED], "call" , newConfig); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_newConfig", newConfig); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_configuration_changed($arg_newConfig)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_configuration_changed", newConfig); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onConfigurationChanged(newConfig); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onConfigurationChanged}")) { + super.onConfigurationChanged(newConfig); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_newConfig", newConfig); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onConfigurationChanged($arg_newConfig)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onConfigurationChanged", newConfig); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onConfigurationChanged(newConfig); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoService#onConfigurationChanged"); + super.onConfigurationChanged(newConfig); } } public void onDestroy() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_destroy"}, Boolean.class)) { - super.onDestroy(); - JRubyAdapter.callMethod(rubyInstance, "on_destroy" ); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onDestroy"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_DESTROY] != null) { super.onDestroy(); - JRubyAdapter.callMethod(rubyInstance, "onDestroy" ); + JRubyAdapter.runRubyMethod(callbackProcs[CB_DESTROY], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_DESTROY] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_destroy}")) { super.onDestroy(); - JRubyAdapter.callMethod(callbackProcs[CB_DESTROY], "call" ); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_destroy()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_destroy"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onDestroy(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onDestroy}")) { + super.onDestroy(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onDestroy()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onDestroy"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onDestroy(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoService#onDestroy"); + super.onDestroy(); } } public void onLowMemory() { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_low_memory"}, Boolean.class)) { - super.onLowMemory(); - JRubyAdapter.callMethod(rubyInstance, "on_low_memory" ); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onLowMemory"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_LOW_MEMORY] != null) { super.onLowMemory(); - JRubyAdapter.callMethod(rubyInstance, "onLowMemory" ); + JRubyAdapter.runRubyMethod(callbackProcs[CB_LOW_MEMORY], "call" ); } else { - if (callbackProcs != null && callbackProcs[CB_LOW_MEMORY] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_low_memory}")) { super.onLowMemory(); - JRubyAdapter.callMethod(callbackProcs[CB_LOW_MEMORY], "call" ); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_low_memory()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_low_memory"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onLowMemory(); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onLowMemory}")) { + super.onLowMemory(); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onLowMemory()"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onLowMemory"); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onLowMemory(); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoService#onLowMemory"); + super.onLowMemory(); } } public void onRebind(android.content.Intent intent) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_rebind"}, Boolean.class)) { - super.onRebind(intent); - JRubyAdapter.callMethod(rubyInstance, "on_rebind" , intent); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onRebind"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_REBIND] != null) { super.onRebind(intent); - JRubyAdapter.callMethod(rubyInstance, "onRebind" , intent); + JRubyAdapter.runRubyMethod(callbackProcs[CB_REBIND], "call" , intent); } else { - if (callbackProcs != null && callbackProcs[CB_REBIND] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_rebind}")) { super.onRebind(intent); - JRubyAdapter.callMethod(callbackProcs[CB_REBIND], "call" , intent); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_intent", intent); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.on_rebind($arg_intent)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "on_rebind", intent); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - super.onRebind(intent); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onRebind}")) { + super.onRebind(intent); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_intent", intent); + JRubyAdapter.put("$ruby_instance", rubyInstance); + JRubyAdapter.runScriptlet("$ruby_instance.onRebind($arg_intent)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + JRubyAdapter.runRubyMethod(rubyInstance, "onRebind", intent); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + super.onRebind(intent); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoService#onRebind"); + super.onRebind(intent); } } public boolean onUnbind(android.content.Intent intent) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_unbind"}, Boolean.class)) { - super.onUnbind(intent); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "on_unbind" , intent, Boolean.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onUnbind"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_UNBIND] != null) { super.onUnbind(intent); - return (Boolean) JRubyAdapter.callMethod(rubyInstance, "onUnbind" , intent, Boolean.class); + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, callbackProcs[CB_UNBIND], "call" , intent); } else { - if (callbackProcs != null && callbackProcs[CB_UNBIND] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_unbind}")) { super.onUnbind(intent); - return (Boolean) JRubyAdapter.callMethod(callbackProcs[CB_UNBIND], "call" , intent, Boolean.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_intent", intent); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.on_unbind($arg_intent)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "on_unbind", intent); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onUnbind(intent); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onUnbind}")) { + super.onUnbind(intent); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_intent", intent); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Boolean) JRubyAdapter.runScriptlet("$ruby_instance.onUnbind($arg_intent)"); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Boolean) JRubyAdapter.runRubyMethod(Boolean.class, rubyInstance, "onUnbind", intent); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onUnbind(intent); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoService#onUnbind"); + return super.onUnbind(intent); } } public int onStartCommand(android.content.Intent intent, int flags, int startId) { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"on_start_command"}, Boolean.class)) { - super.onStartCommand(intent, flags, startId); - return (Integer) JRubyAdapter.callMethod(rubyInstance, "on_start_command" , new Object[]{intent, flags, startId}, Integer.class); - } else { - if (rubyInstance != null && JRubyAdapter.callMethod(rubyInstance, "respond_to?" , new Object[]{"onStartCommand"}, Boolean.class)) { + if (JRubyAdapter.isInitialized()) { + if (callbackProcs != null && callbackProcs[CB_START_COMMAND] != null) { super.onStartCommand(intent, flags, startId); - return (Integer) JRubyAdapter.callMethod(rubyInstance, "onStartCommand" , new Object[]{intent, flags, startId}, Integer.class); + return (Integer) JRubyAdapter.runRubyMethod(Integer.class, callbackProcs[CB_START_COMMAND], "call" , new Object[]{intent, flags, startId}); } else { - if (callbackProcs != null && callbackProcs[CB_START_COMMAND] != null) { + String rubyClassName = Script.toCamelCase(scriptName); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :on_start_command}")) { super.onStartCommand(intent, flags, startId); - return (Integer) JRubyAdapter.callMethod(callbackProcs[CB_START_COMMAND], "call" , new Object[]{intent, flags, startId}, Integer.class); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_intent", intent); + JRubyAdapter.put("$arg_flags", flags); + JRubyAdapter.put("$arg_startId", startId); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Integer) ((Number)JRubyAdapter.runScriptlet("$ruby_instance.on_start_command($arg_intent, $arg_flags, $arg_startId)")).intValue(); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Integer) JRubyAdapter.runRubyMethod(Integer.class, rubyInstance, "on_start_command", new Object[]{intent, flags, startId}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } } else { - return super.onStartCommand(intent, flags, startId); + if ((Boolean)JRubyAdapter.runScriptlet("defined?(" + rubyClassName + ") == 'constant' && " + rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onStartCommand}")) { + super.onStartCommand(intent, flags, startId); + // FIXME(uwe): Simplify when we stop support for RubotoCore 0.4.7 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$arg_intent", intent); + JRubyAdapter.put("$arg_flags", flags); + JRubyAdapter.put("$arg_startId", startId); + JRubyAdapter.put("$ruby_instance", rubyInstance); + return (Integer) ((Number)JRubyAdapter.runScriptlet("$ruby_instance.onStartCommand($arg_intent, $arg_flags, $arg_startId)")).intValue(); + } else { + if (JRubyAdapter.isJRubyOneSeven()) { + return (Integer) JRubyAdapter.runRubyMethod(Integer.class, rubyInstance, "onStartCommand", new Object[]{intent, flags, startId}); + } else { + throw new RuntimeException("Unknown JRuby version: " + JRubyAdapter.get("JRUBY_VERSION")); + } + } + } else { + return super.onStartCommand(intent, flags, startId); + } } } + } else { + Log.i("Method called before JRuby runtime was initialized: RubotoService#onStartCommand"); + return super.onStartCommand(intent, flags, startId); } } } - - diff --git a/src/org/ruboto/Script.java b/src/org/ruboto/Script.java index 2a2c93f..2d5cc03 100644 --- a/src/org/ruboto/Script.java +++ b/src/org/ruboto/Script.java @@ -7,98 +7,148 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import android.content.Context; import android.content.res.AssetManager; +import android.os.Environment; public class Script { - private static String scriptsDir = "scripts"; - private static File scriptsDirFile = null; + private static String[] scriptsDir = new String[]{"scripts"}; - private String name = null; + private String name; - /************************************************************************************************* - * - * Static Methods: Scripts Directory - */ public static void setDir(String dir) { - scriptsDir = dir; - scriptsDirFile = new File(dir); + scriptsDir[0] = dir; if (JRubyAdapter.isInitialized()) { Log.d("Changing JRuby current directory to " + scriptsDir); - JRubyAdapter.callScriptingContainerMethod(Void.class, "setCurrentDirectory", scriptsDir); + JRubyAdapter.callScriptingContainerMethod(Void.class, "setCurrentDirectory", dir); } } - + public static String getDir() { - return scriptsDir; + return scriptsDir[0]; } public static File getDirFile() { - return scriptsDirFile; + return new File(getDir()); } - private static void copyScripts(String from, File to, AssetManager assets) { - try { - byte[] buffer = new byte[8192]; - for (String f : assets.list(from)) { - File dest = new File(to, f); + public File getFile() { + return new File(getDir(), name); + } - if (dest.exists()) { - continue; - } + public static void addDir(String dir) { + String[] oldScriptsDir = scriptsDir; + scriptsDir = new String[scriptsDir.length + 1]; + scriptsDir[0] = dir; + for(int i = 0 ; i < oldScriptsDir.length ; i++) { + scriptsDir[i + 1] = oldScriptsDir[i]; + } + } - Log.d("copying file from " + from + "/" + f + " to " + dest); - - if (assets.list(from + "/" + f).length == 0) { - InputStream is = assets.open(from + "/" + f); - OutputStream fos = new BufferedOutputStream(new FileOutputStream(dest), 8192); - - int n; - while ((n = is.read(buffer, 0, buffer.length)) != -1) { - fos.write(buffer, 0, n); - } - is.close(); - fos.close(); - } else { - dest.mkdir(); - copyScripts(from + "/" + f, dest, assets); - } - } - } catch (IOException iox) { - Log.e("error copying scripts", iox); + public static String toCamelCase(String s) { + if (s == null) { + return null; } + String[] parts = s.replace(".rb", "").split("_"); + for (int i = 0 ; i < parts.length ; i++) { + parts[i] = parts[i].substring(0,1).toUpperCase() + parts[i].substring(1); + } + return java.util.Arrays.toString(parts).replace(", ", "").replaceAll("[\\[\\]]", ""); } - public static void copyAssets(Context context, String directory) { - File dest = new File(scriptsDirFile.getParentFile(), directory); - if (dest.exists() || dest.mkdir()) { - copyScripts(directory, dest, context.getAssets()); - } else { - throw new RuntimeException("Unable to create scripts directory: " + dest); - } + public static String toSnakeCase(String s) { + return s.replaceAll( + String.format("%s|%s|%s", + "(?<=[A-Z])(?=[A-Z][a-z])", + "(?<=[^A-Z])(?=[A-Z])", + "(?<=[A-Za-z])(?=[^A-Za-z])" + ), + "_" + ).toLowerCase(); } - - /************************************************************************************************* - * - * Constructors - */ + // Private static methods + + // private static void copyAssets(Context context, String directory) { + // File dest = new File(new File(scriptsDirFile).getParentFile(), directory); + // if (dest.exists() || dest.mkdir()) { + // copyScripts(directory, dest, context.getAssets()); + // } else { + // throw new RuntimeException("Unable to create scripts directory: " + dest); + // } + // } + + // private static void copyScripts(String from, File to, AssetManager assets) { + // try { + // byte[] buffer = new byte[8192]; + // for (String f : assets.list(from)) { + // File dest = new File(to, f); + // + // if (dest.exists()) { + // continue; + // } + // + // Log.d("copying file from " + from + "/" + f + " to " + dest); + // + // if (assets.list(from + "/" + f).length == 0) { + // InputStream is = assets.open(from + "/" + f); + // OutputStream fos = new BufferedOutputStream(new FileOutputStream(dest), 8192); + // + // int n; + // while ((n = is.read(buffer, 0, buffer.length)) != -1) { + // fos.write(buffer, 0, n); + // } + // is.close(); + // fos.close(); + // } else { + // dest.mkdir(); + // copyScripts(from + "/" + f, dest, assets); + // } + // } + // } catch (IOException iox) { + // Log.e("error copying scripts", iox); + // } + // } + + /************************************************************************************************* + * + * Constructors + */ public Script(String name) { this.name = name; } /************************************************************************************************* * - * Attribute Access + * Instance methods */ - - public String getName() { - return name; + public String execute() throws IOException { + return JRubyAdapter.execute(getContents()); } - public File getFile() { - return new File(getDir(), name); + boolean exists() { + for (String dir : scriptsDir) { + System.out.println("Checking file: " + dir + "/" + name); + if (new File(scriptsDir + "/" + name).exists()) { + return true; + } + } + try { + java.io.InputStream is = getClass().getClassLoader().getResourceAsStream(name); + System.out.println("Classpath resource: " + is); + if (is != null) { + is.close(); + return true; + } else { + return false; + } + } catch (IOException ioex) { + System.out.println("Classpath resource exception: " + ioex); + return false; + } } public Script setName(String name) { @@ -115,7 +165,7 @@ public String getContents() throws IOException { } else { is = getClass().getClassLoader().getResourceAsStream(name); } - buffer = new BufferedReader(new java.io.InputStreamReader(is), 8192); + buffer = new BufferedReader(new java.io.InputStreamReader(is), 8192); StringBuilder source = new StringBuilder(); while (true) { String line = buffer.readLine(); @@ -135,32 +185,18 @@ public String getContents() throws IOException { } } - /************************************************************************************************* - * - * Script Actions - */ + // public File getFile() { + // for (String dir : scriptsDir) { + // File f = new File(dir, name); + // if (f.exists()) { + // return f; + // } + // } + // return new File(scriptsDir[0], name); + // } - public static String toSnakeCase(String s) { - return s.replaceAll( - String.format("%s|%s|%s", - "(?<=[A-Z])(?=[A-Z][a-z])", - "(?<=[^A-Z])(?=[A-Z])", - "(?<=[A-Za-z])(?=[^A-Za-z])" - ), - "_" - ).toLowerCase(); - } - - public static String toCamelCase(String s) { - String[] parts = s.replace(".rb", "").split("_"); - for (int i = 0 ; i < parts.length ; i++) { - parts[i] = parts[i].substring(0,1).toUpperCase() + parts[i].substring(1); - } - return java.util.Arrays.toString(parts).replace(", ", "").replaceAll("[\\[\\]]", ""); - } - - public String execute() throws IOException { - return JRubyAdapter.execute(getContents()); + public String getName() { + return name; } } diff --git a/src/org/ruboto/irb/IRB.java b/src/org/ruboto/irb/IRB.java index f041571..61ca6c0 100644 --- a/src/org/ruboto/irb/IRB.java +++ b/src/org/ruboto/irb/IRB.java @@ -112,7 +112,7 @@ public void onCreate(Bundle savedInstanceState) { protected void fireRubotoActivity() { if(appStarted) return; super.fireRubotoActivity(); - configScriptsDir(); + configScriptsDir(true); } public boolean rubotoAttachable() { @@ -166,7 +166,7 @@ public void close() throws IOException { JRubyAdapter.setOutputStream(printStream); irbOutput.append(">> "); - configScriptsDir(); + configScriptsDir(false); editorSetUp(); scriptsListSetUp(); @@ -643,17 +643,17 @@ protected static String scriptsDirName(Activity context) { } } - private void configScriptsDir() { + private void configScriptsDir(boolean checkForUpdate) { IRBScript.setDir(IRB.scriptsDirName(this)); if (!IRBScript.getDirFile().exists()) { // on first install init directory + copy sample scripts copyDemoScripts(DEMO_SCRIPTS, IRBScript.getDirFile()); } else { File from = new File(IRBScript.getDirFile(), "ruboto.rb"); - if (from.exists()) + if (from.exists()) removeOldRubotoScripts(); - if (!checkVersionString()) { + if (checkForUpdate && !checkVersionString()) { // Scripts exist but need updating confirmUpdate(); } diff --git a/src/org/ruboto/irb/ScriptLaunch.java b/src/org/ruboto/irb/ScriptLaunch.java index 44037b6..86e6961 100644 --- a/src/org/ruboto/irb/ScriptLaunch.java +++ b/src/org/ruboto/irb/ScriptLaunch.java @@ -4,7 +4,7 @@ public class ScriptLaunch extends org.ruboto.EntryPointActivity { public void onCreate(Bundle bundle) { - setScriptName("script_launch.rb"); + setRubyClassName(getClass().getSimpleName()); super.onCreate(bundle); } } diff --git a/src/ruboto.rb b/src/ruboto.rb index d6aa020..b508e8a 100644 --- a/src/ruboto.rb +++ b/src/ruboto.rb @@ -13,6 +13,7 @@ ####################################################### require 'ruboto/base' +require 'ruboto/package' require 'ruboto/activity' require 'ruboto/service' require 'ruboto/broadcast_receiver' diff --git a/src/ruboto/activity.rb b/src/ruboto/activity.rb index 59fd89c..b79129a 100644 --- a/src/ruboto/activity.rb +++ b/src/ruboto/activity.rb @@ -1,4 +1,5 @@ require 'ruboto/base' +require 'ruboto/package' ####################################################### # @@ -65,17 +66,13 @@ def start_ruboto_activity(global_variable_name = '$activity', klass=RubotoActivi module Ruboto module Activity - def initialize(java_instance) - @java_instance = java_instance - end - def method_missing(method, *args, &block) - return @java_instance.send(method, *args, &block) if @java_instance.respond_to?(method) + return @ruboto_java_instance.send(method, *args, &block) if @ruboto_java_instance && @ruboto_java_instance.respond_to?(method) super end end end - + def ruboto_configure_activity(klass) klass.class_eval do include Ruboto::Activity diff --git a/src/ruboto/base.rb b/src/ruboto/base.rb index 996bfab..a101b9b 100644 --- a/src/ruboto/base.rb +++ b/src/ruboto/base.rb @@ -17,9 +17,6 @@ def confirm_ruboto_version(required_version, exact=true) require 'java' -$package_name = ($activity || $service || $broadcast_receiver).package_name -$package = eval("Java::#{$package_name}") - # Create convenience method for top-level android package so we do not need to prefix with 'Java::'. module Kernel def android @@ -28,15 +25,6 @@ def android end java_import "android.R" - -module Ruboto - java_import "#{$package_name}.R" - begin - Id = JavaUtilities.get_proxy_class("#{$package_name}.R$id") - rescue NameError - Java::android.util.Log.d "RUBOTO", "no R$id" - end -end AndroidIds = JavaUtilities.get_proxy_class("android.R$id") # @@ -45,8 +33,8 @@ module Ruboto module Ruboto module CallbackClass - def new_with_callbacks &block - new.initialize_ruboto_callbacks &block + def new_with_callbacks(*args, &block) + new(*args).initialize_ruboto_callbacks(&block) end end @@ -57,14 +45,16 @@ def initialize_ruboto_callbacks &block self end - def ruboto_callback_methods - (singleton_methods - ["on_create", "on_receive"]).select{|i| i =~ /^on_/} + def ruboto_callback_methods + # FIXME(uwe): Remove to_sym conversion when we stop supporting Ruby 1.8 mode + (singleton_methods - ["on_create", "on_receive"]).select{|i| self.class.constants.map(&:to_sym).include?(i.to_s.sub(/^on_/, "CB_").upcase.to_sym) || self.class.constants.map(&:to_sym).include?("CB_#{i}".upcase.to_sym)} end - def setup_ruboto_callbacks - ruboto_callback_methods.each do |i| + def setup_ruboto_callbacks + ruboto_callback_methods.each do |i| begin - setCallbackProc(self.class.const_get(i.sub(/^on_/, "CB_").upcase), method(i)) + # FIXME(uwe): Remove to_sym conversion when we stop supporting Ruby 1.8 mode + setCallbackProc((self.class.constants.map(&:to_sym).include?(i.to_s.sub(/^on_/, "CB_").upcase.to_sym) && self.class.const_get(i.to_s.sub(/^on_/, "CB_").upcase)) || (self.class.constants.map(&:to_sym).include?("CB_#{i}".upcase.to_sym) && self.class.const_get("CB_#{i}".upcase)), method(i)) rescue end end diff --git a/src/ruboto/broadcast_receiver.rb b/src/ruboto/broadcast_receiver.rb index 458aac8..e48776c 100644 --- a/src/ruboto/broadcast_receiver.rb +++ b/src/ruboto/broadcast_receiver.rb @@ -31,13 +31,5 @@ def on_receive(context, intent) module Ruboto module BroadcastReceiver - def initialize(java_instance) - @java_instance = java_instance - end - - def method_missing(method, *args, &block) - return @java_instance.send(method, *args, &block) if @java_instance.respond_to?(method) - super - end end end diff --git a/src/ruboto/generate.rb b/src/ruboto/generate.rb index c558ff2..baf02b7 100644 --- a/src/ruboto/generate.rb +++ b/src/ruboto/generate.rb @@ -362,7 +362,7 @@ def ruboto_generate_class(dex_maker, interface_or_class_name, package_class_name # Prepare to call Script to call the method script_class_type_id = TypeId.convert_type("org.ruboto.JRubyAdapter") loadConstant(call_string, "call") - parameter_types = [ret ? TypeId::OBJECT : TypeId::VOID, "callMethod", TypeId::OBJECT, TypeId::STRING] + parameter_types = [TypeId::OBJECT, "callMethod", TypeId::OBJECT, TypeId::STRING] method_parameters = [retObject, block, call_string] # Set up for different arity diff --git a/src/ruboto/package.rb b/src/ruboto/package.rb new file mode 100644 index 0000000..e3e0639 --- /dev/null +++ b/src/ruboto/package.rb @@ -0,0 +1,11 @@ +$package_name = ($activity || $service).package_name +$package = eval("Java::#{$package_name}") + +module Ruboto + java_import "#{$package_name}.R" + begin + Id = JavaUtilities.get_proxy_class("#{$package_name}.R$id") + rescue NameError + Java::android.util.Log.d "RUBOTO", "no R$id" + end +end diff --git a/src/ruboto/service.rb b/src/ruboto/service.rb index 5026d27..97cb77c 100644 --- a/src/ruboto/service.rb +++ b/src/ruboto/service.rb @@ -1,4 +1,5 @@ require 'ruboto/base' +require 'ruboto/package' ####################################################### # @@ -53,14 +54,6 @@ def start_ruboto_service(global_variable_name = '$service', klass=RubotoService, module Ruboto module Service - def initialize(java_instance) - @java_instance = java_instance - end - - def method_missing(method, *args, &block) - return @java_instance.send(method, *args, &block) if @java_instance.respond_to?(method) - super - end end end diff --git a/src/ruboto/version.rb b/src/ruboto/version.rb index 8518dab..6f8cebc 100644 --- a/src/ruboto/version.rb +++ b/src/ruboto/version.rb @@ -1,3 +1,3 @@ module Ruboto - VERSION = '0.7.0.dev' -end \ No newline at end of file + VERSION = '0.8.0' +end diff --git a/src/ruboto/widget.rb b/src/ruboto/widget.rb index 1d6b43e..7be33a3 100644 --- a/src/ruboto/widget.rb +++ b/src/ruboto/widget.rb @@ -1,3 +1,4 @@ +require 'ruboto/base' require 'ruboto/activity' ####################################################### @@ -41,6 +42,10 @@ def configure(context, params = {}) if height = params.delete(:height) getLayoutParams.height = View.convert_constant(height) end + + if margins = params.delete(:margins) + getLayoutParams.set_margins(*margins) + end if layout = params.delete(:layout) lp = getLayoutParams @@ -62,8 +67,18 @@ def configure(context, params = {}) # java_import "android.view.ViewGroup" -View.add_constant_conversion :wrap_content, ViewGroup::LayoutParams::WRAP_CONTENT -View.add_constant_conversion :fill_parent, ViewGroup::LayoutParams::FILL_PARENT +ViewGroup::LayoutParams.constants.each do |i| + View.add_constant_conversion i.downcase.to_sym, ViewGroup::LayoutParams.const_get(i) +end + +# +# Load Gravity constants +# + +java_import "android.view.Gravity" +Gravity.constants.each do |i| + View.add_constant_conversion i.downcase.to_sym, Gravity.const_get(i) +end # # RubotoActivity View Generation diff --git a/test/local.properties b/test/local.properties index 280c234..13d3eb1 100644 --- a/test/local.properties +++ b/test/local.properties @@ -10,4 +10,4 @@ # location of the SDK. This is only used by Ant # For customization when using a Version Control System, please read the # header note. -sdk.dir=/Users/uwe/Desktop/android-sdk-mac_86 +sdk.dir=/Users/uwe/Desktop/android-sdk-macosx diff --git a/test/src/org/ruboto/test/ActivityTest.java b/test/src/org/ruboto/test/ActivityTest.java index 08102bb..d2f384d 100644 --- a/test/src/org/ruboto/test/ActivityTest.java +++ b/test/src/org/ruboto/test/ActivityTest.java @@ -42,11 +42,11 @@ public void run() { Log.i(getClass().getName(), "calling setup"); JRubyAdapter.setScriptFilename(filename); - JRubyAdapter.callMethod(setup, "call", activity); + JRubyAdapter.runRubyMethod(setup, "call", activity); Log.i(getClass().getName(), "setup ok"); JRubyAdapter.setScriptFilename(filename); - JRubyAdapter.callMethod(block, "call", activity); + JRubyAdapter.runRubyMethod(block, "call", activity); JRubyAdapter.setScriptFilename(oldFile); } }; diff --git a/test/src/org/ruboto/test/InstrumentationTestRunner.java b/test/src/org/ruboto/test/InstrumentationTestRunner.java index f0ff9c2..09eaec0 100644 --- a/test/src/org/ruboto/test/InstrumentationTestRunner.java +++ b/test/src/org/ruboto/test/InstrumentationTestRunner.java @@ -46,6 +46,9 @@ public TestSuite getAllTests() { Thread t = new Thread(null, new Runnable() { public void run() { JRubyLoadedOk.set(JRubyAdapter.setUpJRuby(getTargetContext())); + if (!JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.runScriptlet("Java::OrgRubotoTest::InstrumentationTestRunner.__persistent__ = true"); + } } }, "Setup JRuby from instrumentation test runner", 64 * 1024); try { @@ -58,15 +61,17 @@ public void run() { // TODO end if (JRubyLoadedOk.get()) { - loadStep = "Setup global variables"; - - // TODO(uwe): Running with large stack is currently only needed when running with JRuby 1.7.0 and android-10 - // TODO(uwe): Simplify when we stop support for JRuby 1.7.0 or android-10 + loadStep = "Load test helper"; + // TODO(uwe): Running with large stack is currently only needed when running with JRuby 1.7.0.dev and android-10 + // TODO(uwe): Simplify when we stop support for JRuby 1.7.0.dev or android-10 + final IOException[] ioex = new IOException[]{null}; Thread t2 = new Thread(null, new Runnable() { public void run() { - JRubyAdapter.put("$runner", InstrumentationTestRunner.this); - JRubyAdapter.put("$test", InstrumentationTestRunner.this); - JRubyAdapter.put("$suite", suite); + try { + loadScript("test_helper.rb"); + } catch (IOException e) { + ioex[0] = e; + } } }, "Setup JRuby from instrumentation test runner", 64 * 1024); try { @@ -76,11 +81,11 @@ public void run() { Thread.currentThread().interrupt(); throw new RuntimeException("Interrupted starting JRuby", ie); } + if (ioex[0] != null) { + throw ioex[0]; + } // TODO end - loadStep = "Load test helper"; - loadScript("test_helper.rb"); - loadStep = "Get app test source dir"; String test_apk_path = getContext().getPackageManager().getApplicationInfo(getContext().getPackageName(), 0).sourceDir; JarFile jar = new JarFile(test_apk_path); @@ -162,12 +167,21 @@ private void loadScript(String f) throws IOException { } buffer.close(); - String oldFilename = JRubyAdapter.getScriptFilename(); - JRubyAdapter.setScriptFilename(f); - JRubyAdapter.put("$script_code", source.toString()); - JRubyAdapter.setScriptFilename(f); - JRubyAdapter.execute("$test.instance_eval($script_code)"); - JRubyAdapter.setScriptFilename(oldFilename); + // FIXME(uwe): Simplify when we stop supporting JRuby < 1.7.0 + if (JRubyAdapter.isJRubyPreOneSeven()) { + JRubyAdapter.put("$test", this); + JRubyAdapter.put("$script_code", source.toString()); + JRubyAdapter.runScriptlet("$test.instance_eval($script_code)"); + } else { + String oldFilename = JRubyAdapter.getScriptFilename(); + JRubyAdapter.setScriptFilename(f); + if (f.equals("test_helper.rb")) { + JRubyAdapter.runScriptlet(source.toString()); + } else { + JRubyAdapter.runRubyMethod(this, "instance_eval", source.toString()); + } + JRubyAdapter.setScriptFilename(oldFilename); + } Log.d(getClass().getName(), "Test script " + f + " loaded"); }