Skip to content

Commit

Permalink
JXMobile native interface
Browse files Browse the repository at this point in the history
- Common (cross platform) base for mobile specific API

- JS Error reporting back to native land
  • Loading branch information
obastemur committed Jul 9, 2015
1 parent c99c463 commit 3f04c3e
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 30 deletions.
69 changes: 39 additions & 30 deletions app/jxcore_cordova.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ internal_methods['loadMainFile'] = function (filePath, callback_) {
require(path.join(process.cwd(), filePath));
} catch (e) {
result = false;
Error.captureStackTrace(e);
err = e;
Error.captureStackTrace(err);
console.error("loadMainFile", e);
JXMobile('OnError').callNative(e.message, JSON.stringify(e.stack));
}
callback_(result, !err ? null : err.message + "\n" + err.stack);
};
Expand All @@ -192,39 +192,43 @@ JXMobile.executeJSON = function (json, callbackId) {

var internal = internal_methods[json.methodName];
var fnc = jx_methods[json.methodName];

if (internal) {
var cb = new MakeCallback(callbackId).callback
json.params.push(cb);
internal.apply(null, json.params);
return;
} else if (fnc) {
if (!fnc.is_synced) {
if (!json.params || (json.params.length == 1 && json.params[0] === null)) {
json.params = [];
try {
if (internal) {
var cb = new MakeCallback(callbackId).callback
json.params.push(cb);
internal.apply(null, json.params);
return;
} else if (fnc) {
if (!fnc.is_synced) {
if (!json.params || (json.params.length == 1 && json.params[0] === null)) {
json.params = [];
}
json.params[json.params.length] = new MakeCallback(callbackId).callback;
}
json.params[json.params.length] = new MakeCallback(callbackId).callback;
}

var ret_val = fnc.method.apply(null, json.params);
if (fnc.is_synced && callbackId) {
new MakeCallback(callbackId).callback(ret_val);
} else {
return ret_val;
}
return;
} else if (json.methodName && json.methodName.length>3 && json.methodName.substr(0,3) === "RC-") {
var cb = new MakeCallback(callbackId).callback
json.params.push(cb);
fnc = ui_methods[json.methodName.substr(3)];
if (fnc && fnc.returnCallback) {
fnc.returnCallback.apply(null, json.params);
delete ui_methods[json.methodName.substr(3)];
var ret_val = fnc.method.apply(null, json.params);
if (fnc.is_synced && callbackId) {
new MakeCallback(callbackId).callback(ret_val);
} else {
return ret_val;
}
return;
} else if (json.methodName && json.methodName.length>3 && json.methodName.substr(0,3) === "RC-") {
var cb = new MakeCallback(callbackId).callback
json.params.push(cb);
fnc = ui_methods[json.methodName.substr(3)];
if (fnc && fnc.returnCallback) {
fnc.returnCallback.apply(null, json.params);
delete ui_methods[json.methodName.substr(3)];
return;
}
}
}

console.error("JXcore: Method Doesn't Exist [", json.methodName, "] Did you register it?");
throw new Error("JXcore: Method Doesn't Exist [", json.methodName, "] Did you register it?");
} catch(e) {
Error.captureStackTrace(e);
JXMobile('OnError').callNative(e.message, JSON.stringify(e.stack));
}
};

console.warn("Platform", process.platform);
Expand Down Expand Up @@ -421,4 +425,9 @@ if (isAndroid) {
jxcore.tasks.register(process.setPaths);
}

process.on('uncaughtException', function (e) {
Error.captureStackTrace(e);
JXMobile('OnError').callNative(e.message, JSON.stringify(e.stack));
});

console.log("JXcore Cordova bridge is ready!");
3 changes: 3 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<source-file src="src/ios/CDVJXcore.m" />
<header-file src="src/ios/JXcore.h" />
<source-file src="src/ios/JXcore.m" />
<header-file src="src/ios/JXMobile.h" />
<source-file src="src/ios/JXMobile.m" />
<header-file src="src/ios/JXcoreExtension.h" />
<!--<source-file src="src/ios/JXcoreExtension.m" /> OPTIONAL-->
<header-file src="bin/jx.h" />
Expand Down Expand Up @@ -58,6 +60,7 @@
<source-file src="app/jxcore_cordova.js" target-dir="assets/" />

<source-file src="src/android/java/io/jxcore/node/FileManager.java" target-dir="src/io/jxcore/node/" />
<source-file src="src/android/java/io/jxcore/node/JXMobile.java" target-dir="src/io/jxcore/node/" />
<source-file src="src/android/java/io/jxcore/node/jxcore.java" target-dir="src/io/jxcore/node/" />
<source-file src="src/android/java/io/jxcore/node/JXcoreExtension.java" target-dir="src/io/jxcore/node/" />
<source-file src="src/android/libs/armeabi" target-dir="libs/" />
Expand Down
25 changes: 25 additions & 0 deletions src/android/java/io/jxcore/node/JXMobile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// License information is available from LICENSE file

package io.jxcore.node;

import io.jxcore.node.jxcore.JXcoreCallback;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.util.Log;

public class JXMobile {
public static void Initialize() {
jxcore.RegisterMethod("OnError", new JXcoreCallback() {
@SuppressLint("NewApi")
@Override
public void Receiver(ArrayList<Object> params, String callbackId) {
String message = (String) params.get(0);
String stack = (String) params.get(1);

Log.e("jxcore", "Error!: " + message + "\nStack: " + stack);
}
});
}
}
6 changes: 6 additions & 0 deletions src/android/java/io/jxcore/node/JXcoreExtension.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// License information is available from LICENSE file

/*
* This file is only a sample
* You are free to update it with the methods you need
* from the Native Mobile API
*/

package io.jxcore.node;

import io.jxcore.node.jxcore.JXcoreCallback;
Expand Down
1 change: 1 addition & 0 deletions src/android/java/io/jxcore/node/jxcore.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public void Receiver(ArrayList<Object> params, String callbackId) {
});

JXcoreExtension.LoadExtensions();
JXMobile.Initialize();

if (!new_instance)
return;
Expand Down
3 changes: 3 additions & 0 deletions src/ios/CDVJXcore.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import <Cordova/CDV.h>
#import "CDVJXcore.h"
#import "JXcoreExtension.h"
#import "JXMobile.h"


static CDVJXcore *activeDevice = nil;
Expand Down Expand Up @@ -108,6 +109,8 @@ - (void)pluginInitialize {
id extension = [[extensionClass alloc] init];
[extension defineMethods];
}

[JXMobile defineMethods];
}

- (void)Evaluate:(CDVInvokedUrlCommand *)command {
Expand Down
11 changes: 11 additions & 0 deletions src/ios/JXMobile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// See LICENSE file

#ifndef JXcordova_JXMobile_h
#define JXcordova_JXMobile_h

@interface JXMobile : NSObject
{}
+ (void) defineMethods;
@end

#endif
20 changes: 20 additions & 0 deletions src/ios/JXMobile.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// See LICENSE file

#import <Foundation/Foundation.h>
#import "JXcore.h"
#import "JXMobile.h"
#import "CDVJXcore.h"

@implementation JXMobile
{}

+ (void) defineMethods {
// Listen to Errors on the JS land
[JXcore addNativeBlock:^(NSArray *params, NSString *callbackId) {
NSString *errorMessage = (NSString*)[params objectAtIndex:0];
NSString *errorStack = (NSString*)[params objectAtIndex:1];

NSLog(@"Error!: %@\nStack:%@\n", errorMessage, errorStack);
} withName:@"OnError"];
}
@end

0 comments on commit 3f04c3e

Please sign in to comment.