You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by lo...@apache.org on 2013/05/14 06:15:12 UTC
[05/50] [abbrv] webworks commit: Initial work on cleaning up the exec
chain, including the addition of a Battery plugin
Initial work on cleaning up the exec chain, including the addition of a Battery plugin
Reviewed by Rosa Tse <rt...@blackberry.com>
Tested by Rosa Tse <rt...@blackberry.com>
Project: http://git-wip-us.apache.org/repos/asf/cordova-blackberry/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-blackberry/commit/a00b6741
Tree: http://git-wip-us.apache.org/repos/asf/cordova-blackberry/tree/a00b6741
Diff: http://git-wip-us.apache.org/repos/asf/cordova-blackberry/diff/a00b6741
Branch: refs/heads/master
Commit: a00b67419b36f854083ad96b0d4e7101146c06a8
Parents: 9160b4a
Author: Bryan Higgins <bh...@blackberry.com>
Authored: Tue Mar 26 09:52:44 2013 -0400
Committer: Bryan Higgins <bh...@blackberry.com>
Committed: Fri May 3 10:13:29 2013 -0400
----------------------------------------------------------------------
.../bin/templates/project/plugins/Battery/index.js | 49 +++++++
.../bin/templates/project/plugins/Device/index.js | 19 ++--
.../project/plugins/NetworkStatus/index.js | 3 +-
blackberry10/bin/test/plugins/Battery/index.js | 85 +++++++++++
blackberry10/bin/test/plugins/Device/index.js | 28 +++--
.../bin/test/plugins/NetworkStatus/index.js | 25 +++-
blackberry10/framework/lib/PluginResult.js | 52 +++++++
blackberry10/framework/lib/server.js | 2 +-
blackberry10/framework/test/unit/lib/server.js | 8 +-
blackberry10/javascript/cordova.blackberry10.js | 112 +++++----------
10 files changed, 272 insertions(+), 111 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/a00b6741/blackberry10/bin/templates/project/plugins/Battery/index.js
----------------------------------------------------------------------
diff --git a/blackberry10/bin/templates/project/plugins/Battery/index.js b/blackberry10/bin/templates/project/plugins/Battery/index.js
new file mode 100644
index 0000000..fcac7b2
--- /dev/null
+++ b/blackberry10/bin/templates/project/plugins/Battery/index.js
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2010-2011 Research In Motion Limited.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+var SYSTEM_EVENTS = ["device.battery.statusChange",
+ "device.battery.chargeLow",
+ "device.battery.chargeCritical"],
+ clientListener;
+
+module.exports = {
+ start: function (success, fail, args, env) {
+ var result = new PluginResult(args, env);
+ if (!!clientListener) {
+ result.error("Battery listener already running");
+ } else {
+ clientListener = function (info) {
+ result.callbackOk(info, true);
+ };
+ SYSTEM_EVENTS.forEach(function (event) {
+ window.qnx.webplatform.device.addEventListener(event, clientListener);
+ });
+ result.noResult(true);
+ }
+ },
+ stop: function (success, fail, args, env) {
+ var result = new PluginResult(args, env);
+ if (!clientListener) {
+ result.error("Battery listener has not started");
+ } else {
+ SYSTEM_EVENTS.forEach(function (event) {
+ window.qnx.webplatform.device.removeEventListener(event, clientListener);
+ });
+ clientListener = null;
+ result.noResult(false);
+ }
+ }
+};
http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/a00b6741/blackberry10/bin/templates/project/plugins/Device/index.js
----------------------------------------------------------------------
diff --git a/blackberry10/bin/templates/project/plugins/Device/index.js b/blackberry10/bin/templates/project/plugins/Device/index.js
index 61be46a..41b454c 100644
--- a/blackberry10/bin/templates/project/plugins/Device/index.js
+++ b/blackberry10/bin/templates/project/plugins/Device/index.js
@@ -16,14 +16,15 @@
module.exports = {
getDeviceInfo: function (success, fail, args, env) {
- var info = {
- platform: "blackberry10",
- version: window.qnx.webplatform.device.scmBundle,
- model: window.qnx.webplatform.device.modelName,
- name: window.qnx.webplatform.device.modelName, // deprecated: please use device.model
- uuid: window.qnx.webplatform.device.devicePin,
- cordova: "2.5.0"
- };
- success(info);
+ var result = new PluginResult(args, env),
+ info = {
+ platform: "blackberry10",
+ version: window.qnx.webplatform.device.scmBundle,
+ model: window.qnx.webplatform.device.modelName,
+ name: window.qnx.webplatform.device.modelName, // deprecated: please use device.model
+ uuid: window.qnx.webplatform.device.devicePin,
+ cordova: "2.5.0"
+ };
+ result.ok(info);
}
};
http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/a00b6741/blackberry10/bin/templates/project/plugins/NetworkStatus/index.js
----------------------------------------------------------------------
diff --git a/blackberry10/bin/templates/project/plugins/NetworkStatus/index.js b/blackberry10/bin/templates/project/plugins/NetworkStatus/index.js
index ae6cdf2..5a991fe 100644
--- a/blackberry10/bin/templates/project/plugins/NetworkStatus/index.js
+++ b/blackberry10/bin/templates/project/plugins/NetworkStatus/index.js
@@ -53,6 +53,7 @@ function currentConnectionType() {
module.exports = {
getConnectionInfo: function (success, fail, args, env) {
- success(currentConnectionType());
+ var result = new PluginResult(args, env);
+ result.ok(currentConnectionType());
}
};
http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/a00b6741/blackberry10/bin/test/plugins/Battery/index.js
----------------------------------------------------------------------
diff --git a/blackberry10/bin/test/plugins/Battery/index.js b/blackberry10/bin/test/plugins/Battery/index.js
new file mode 100644
index 0000000..3495629
--- /dev/null
+++ b/blackberry10/bin/test/plugins/Battery/index.js
@@ -0,0 +1,85 @@
+/*
+* Copyright 2013 Research In Motion Limited.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+describe("Battery", function () {
+
+ var _apiDir = __dirname + "./../../../templates/project/plugins/Battery/",
+ index,
+ callback,
+ result = {
+ ok: jasmine.createSpy(),
+ error: jasmine.createSpy(),
+ noResult: jasmine.createSpy(),
+ callbackOk: jasmine.createSpy()
+ };
+
+ beforeEach(function () {
+ index = require(_apiDir + "index");
+ GLOBAL.window = {
+ qnx: {
+ webplatform: {
+ device: {
+ addEventListener: jasmine.createSpy().andCallFake(function (evt, cb) {
+ callback = cb;
+ }),
+ removeEventListener: jasmine.createSpy()
+ }
+ }
+ }
+ };
+ GLOBAL.PluginResult = function () {
+ return result;
+ };
+ });
+
+ afterEach(function () {
+ index = null;
+ delete GLOBAL.window;
+ delete GLOBAL.PluginResult;
+ });
+
+ describe("start", function () {
+
+ it("calls noResult and keeps callbacks", function () {
+ index.start();
+ expect(window.qnx.webplatform.device.addEventListener).toHaveBeenCalled();
+ expect(result.noResult).toHaveBeenCalledWith(true);
+ });
+
+ it("callback calls ok and keeps callbacks", function () {
+ callback("OK");
+ expect(result.callbackOk).toHaveBeenCalledWith("OK", true);
+ });
+
+ it("calls error if already started", function () {
+ index.start();
+ expect(window.qnx.webplatform.device.addEventListener).not.toHaveBeenCalled();
+ expect(result.error).toHaveBeenCalled();
+ });
+
+
+ });
+
+ describe("stop", function () {
+
+ it("calls noResult and does not keep callbacks", function () {
+ index.stop();
+ expect(window.qnx.webplatform.device.removeEventListener).toHaveBeenCalled();
+ expect(result.noResult).toHaveBeenCalledWith(false);
+ });
+
+ });
+});
http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/a00b6741/blackberry10/bin/test/plugins/Device/index.js
----------------------------------------------------------------------
diff --git a/blackberry10/bin/test/plugins/Device/index.js b/blackberry10/bin/test/plugins/Device/index.js
index bdfcfa4..9792b09 100644
--- a/blackberry10/bin/test/plugins/Device/index.js
+++ b/blackberry10/bin/test/plugins/Device/index.js
@@ -13,10 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-var _apiDir = __dirname + "./../../../templates/project/plugins/Device/",
-index;
describe("Device", function () {
+
+ var _apiDir = __dirname + "./../../../templates/project/plugins/Device/",
+ index,
+ result = {
+ ok: jasmine.createSpy()
+ };
+
beforeEach(function () {
index = require(_apiDir + "index");
});
@@ -35,34 +40,37 @@ describe("Device", function () {
}
}
};
+ GLOBAL.PluginResult = function () {
+ return result;
+ };
});
afterEach(function () {
delete GLOBAL.window;
+ delete GLOBAL.PluginResult;
});
- it("calls success with the Device info", function () {
+ it("calls ok with the Device info", function () {
var mockedDevice = {
scmBundle: "1.0.0.0",
modelName: "q10",
devicePin: (new Date()).getTime()
- },
- success = jasmine.createSpy().andCallFake(function (deviceInfo) {
+ };
+
+ result.ok = jasmine.createSpy().andCallFake(function (deviceInfo) {
expect(deviceInfo.platform).toEqual("blackberry10");
expect(deviceInfo.version).toEqual(mockedDevice.scmBundle);
expect(deviceInfo.model).toEqual(mockedDevice.modelName);
expect(deviceInfo.name).toEqual(mockedDevice.modelName);
expect(deviceInfo.uuid).toEqual(mockedDevice.devicePin);
expect(deviceInfo.cordova).toBeDefined();
- }),
- fail = jasmine.createSpy();
+ });
window.qnx.webplatform.device = mockedDevice;
- index.getDeviceInfo(success, fail);
+ index.getDeviceInfo();
- expect(success).toHaveBeenCalled();
- expect(fail).not.toHaveBeenCalled();
+ expect(result.ok).toHaveBeenCalled();
});
});
});
http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/a00b6741/blackberry10/bin/test/plugins/NetworkStatus/index.js
----------------------------------------------------------------------
diff --git a/blackberry10/bin/test/plugins/NetworkStatus/index.js b/blackberry10/bin/test/plugins/NetworkStatus/index.js
index 87f6f0b..7d0371c 100644
--- a/blackberry10/bin/test/plugins/NetworkStatus/index.js
+++ b/blackberry10/bin/test/plugins/NetworkStatus/index.js
@@ -13,10 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-var _apiDir = __dirname + "./../../../templates/project/plugins/NetworkStatus/",
-index;
+
describe("NetworkStatus", function () {
+ var _apiDir = __dirname + "./../../../templates/project/plugins/NetworkStatus/",
+ index,
+ result = {
+ ok: jasmine.createSpy(),
+ error: jasmine.createSpy()
+ };
+
beforeEach(function () {
index = require(_apiDir + "index");
});
@@ -35,10 +41,14 @@ describe("NetworkStatus", function () {
}
}
};
+ GLOBAL.PluginResult = function () {
+ return result;
+ };
});
afterEach(function () {
delete GLOBAL.window;
+ delete GLOBAL.PluginResult;
});
function testConnection(expectedResult, mockedType, mockedTechnology) {
@@ -47,18 +57,16 @@ describe("NetworkStatus", function () {
type: mockedType,
technology: mockedTechnology
}
- },
- success = jasmine.createSpy(),
- fail = jasmine.createSpy();
+ };
if (mockedType) {
window.qnx.webplatform.device = mockedDevice;
}
- index.getConnectionInfo(success, fail);
+ index.getConnectionInfo();
- expect(success).toHaveBeenCalledWith(expectedResult);
- expect(fail).not.toHaveBeenCalled();
+ expect(result.ok).toHaveBeenCalledWith(expectedResult);
+ expect(result.error).not.toHaveBeenCalled();
}
it("calls success with a wired connection", function () {
@@ -106,4 +114,5 @@ describe("NetworkStatus", function () {
});
});
+
});
http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/a00b6741/blackberry10/framework/lib/PluginResult.js
----------------------------------------------------------------------
diff --git a/blackberry10/framework/lib/PluginResult.js b/blackberry10/framework/lib/PluginResult.js
new file mode 100644
index 0000000..cbb57ff
--- /dev/null
+++ b/blackberry10/framework/lib/PluginResult.js
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2013 Research In Motion Limited.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+function PluginResult (args, env) {
+
+ var CALLBACK_STATUS_NO_RESULT = 0,
+ CALLBACK_STATUS_OK = 1,
+ CALLBACK_STATUS_ERROR = 9,
+ send = function (data) {
+ env.response.send(200, encodeURIComponent(JSON.stringify(data)));
+ },
+ callback = function (success, status, data, keepCallback) {
+ var executeString = "cordova.callbackFromNative(" + decodeURIComponent(args.callbackId) +
+ ", " + !!success + ", " + status + ", " + data + ", " + !!keepCallback + ");";
+ env.webview.executeJavaScript(executeString);
+ };
+
+ this.noResult = function (keepCallback) {
+ send({ code: CALLBACK_STATUS_NO_RESULT, keepCallback: !!keepCallback });
+ };
+
+ this.error = function (msg) {
+ send({ code: CALLBACK_STATUS_ERROR, msg: msg, keepCallback: false });
+ };
+
+ this.ok = function (data) {
+ send({ code: CALLBACK_STATUS_OK, data: data, keepCallback: false });
+ };
+
+ this.callbackOk = function (data, keepCallback) {
+ callback(true, CALLBACK_STATUS_OK, JSON.stringify(data), keepCallback);
+ };
+
+ this.callbackError = function (msg, keepCallback) {
+ callback(false, CALLBACK_STATUS_ERROR, JSON.stringify(msg), keepCallback);
+ };
+}
+
+window.PluginResult = PluginResult;
http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/a00b6741/blackberry10/framework/lib/server.js
----------------------------------------------------------------------
diff --git a/blackberry10/framework/lib/server.js b/blackberry10/framework/lib/server.js
index 9c14b1e..15b95b0 100644
--- a/blackberry10/framework/lib/server.js
+++ b/blackberry10/framework/lib/server.js
@@ -91,7 +91,7 @@ module.exports = {
plugin[req.params.action](req,
function (result) {
res.send(200, encodeURIComponent(JSON.stringify({
- code: 1,
+ code: 42,
data: result
})));
},
http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/a00b6741/blackberry10/framework/test/unit/lib/server.js
----------------------------------------------------------------------
diff --git a/blackberry10/framework/test/unit/lib/server.js b/blackberry10/framework/test/unit/lib/server.js
index 1d517b4..2b89b0a 100644
--- a/blackberry10/framework/test/unit/lib/server.js
+++ b/blackberry10/framework/test/unit/lib/server.js
@@ -181,7 +181,7 @@ describe("server", function () {
);
});
- it("returns the result and code 1 when success callback called", function () {
+ it("returns the result and code 42 when success callback called", function () {
spyOn(plugin, "exec").andCallFake(function (request, succ, fail, body) {
succ(["MyFeatureId"]);
});
@@ -191,7 +191,7 @@ describe("server", function () {
server.handle(req, res);
expect(res.send).toHaveBeenCalledWith(200, encodeURIComponent(JSON.stringify({
- code: 1,
+ code: 42,
data: ["MyFeatureId"]
})));
});
@@ -259,7 +259,7 @@ describe("server", function () {
);
});
- it("returns the result and code 1 when success callback called", function () {
+ it("returns the result and code 42 when success callback called", function () {
var expectedResult = {"getReadOnlyFields": "Yogi bear"};
spyOn(applicationAPIServer, "getReadOnlyFields").andCallFake(function (success, fail) {
@@ -269,7 +269,7 @@ describe("server", function () {
server.handle(req, res);
expect(res.send).toHaveBeenCalledWith(200, encodeURIComponent(JSON.stringify({
- code: 1,
+ code: 42,
data: expectedResult
})));
});
http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/a00b6741/blackberry10/javascript/cordova.blackberry10.js
----------------------------------------------------------------------
diff --git a/blackberry10/javascript/cordova.blackberry10.js b/blackberry10/javascript/cordova.blackberry10.js
index 2c169c5..36d9ad1 100644
--- a/blackberry10/javascript/cordova.blackberry10.js
+++ b/blackberry10/javascript/cordova.blackberry10.js
@@ -1,8 +1,8 @@
// Platform: blackberry10
-// commit 81a30b465c3f84cc020dda5a7ede6e83a8e44385
+// commit a69c579c923e9bb0b709a64b782d55a137edb043
-// File generated at :: Tue Mar 19 2013 13:22:52 GMT-0400 (EDT)
+// File generated at :: Tue Mar 26 2013 15:21:10 GMT-0400 (EDT)
/*
Licensed to the Apache Software Foundation (ASF) under one
@@ -968,39 +968,8 @@ var cordova = require('cordova'),
module.exports = function(success, fail, service, action, args) {
try {
- var manager = require('cordova/plugin/blackberry10/manager'),
- v = manager.exec(success, fail, service, action, args);
-
- // If status is OK, then return value back to caller
- if (v.status == cordova.callbackStatus.OK) {
-
- // If there is a success callback, then call it now with returned value
- if (success) {
- try {
- success(v.message);
- }
- catch (e) {
- console.log("Error in success callback: "+cordova.callbackId+" = "+e);
- }
- }
- return v.message;
- } else if (v.status == cordova.callbackStatus.NO_RESULT) {
-
- } else {
- // If error, then display error
- console.log("Error: Status="+v.status+" Message="+v.message);
-
- // If there is a fail callback, then call it now with returned value
- if (fail) {
- try {
- fail(v.message);
- }
- catch (e) {
- console.log("Error in error callback: "+cordova.callbackId+" = "+e);
- }
- }
- return null;
- }
+ require('cordova/plugin/blackberry10/manager').exec(success, fail, service, action, args);
+ return null;
} catch (e) {
utils.alert("Error: "+e);
}
@@ -3898,31 +3867,6 @@ module.exports = {
});
-// file: lib/blackberry10/plugin/blackberry10/battery.js
-define("cordova/plugin/blackberry10/battery", function(require, exports, module) {
-
-var cordova = require('cordova'),
- interval;
-
-module.exports = {
- start: function (args, win, fail) {
- interval = window.setInterval(function () {
- win({
- level: navigator.webkitBattery.level * 100,
- isPlugged: navigator.webkitBattery.charging
- });
- }, 500);
- return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "WebWorks Is On It" };
- },
-
- stop: function (args, win, fail) {
- window.clearInterval(interval);
- return { "status" : cordova.callbackStatus.OK, "message" : "stopped" };
- }
-};
-
-});
-
// file: lib/blackberry10/plugin/blackberry10/camera.js
define("cordova/plugin/blackberry10/camera", function(require, exports, module) {
@@ -4785,7 +4729,6 @@ define("cordova/plugin/blackberry10/manager", function(require, exports, module)
var cordova = require('cordova'),
plugins = {
'Accelerometer' : require('cordova/plugin/blackberry10/accelerometer'),
- 'Battery' : require('cordova/plugin/blackberry10/battery'),
'Compass' : require('cordova/plugin/blackberry10/magnetometer'),
'Camera' : require('cordova/plugin/blackberry10/camera'),
'Capture' : require('cordova/plugin/blackberry10/capture'),
@@ -5102,7 +5045,7 @@ module.exports = {
if (plugins.hasOwnProperty(plugin) && plugins[plugin].modules) {
for (i = 0; i < plugins[plugin].modules.length; i++) {
script = document.createElement('script');
- script.src = 'plugins/' + plugin + '/' + plugins[plugin].modules[i];
+ script.src = 'local:///plugins/' + plugin + '/' + plugins[plugin].modules[i];
script.onload = function () {
if (--count === 0 && typeof callback === 'function') {
build(plugins);
@@ -5114,13 +5057,16 @@ module.exports = {
}
}
}
+ if (count === 0) {
+ callback();
+ }
},
getPlugins: function (success, error) {
var request,
response;
request = new XMLHttpRequest();
- request.open('GET', 'plugins/plugins.json', true);
+ request.open('GET', 'local:///plugins/plugins.json', true);
request.onreadystatechange = function () {
if (request.readyState === 4) {
if (request.status === 200) {
@@ -7634,20 +7580,8 @@ window.cordova = require('cordova');
data;
request.send(JSON.stringify(params));
-
response = JSON.parse(decodeURIComponent(request.responseText) || "null");
- errored = response.code < 0;
- cb = errored ? error : success;
- data = errored ? response.msg : response.data;
-
- if (cb) {
- cb(data, response);
- }
- else if (errored) {
- throw data;
- }
-
- return { status: 0 };
+ return response;
};
}
@@ -7655,7 +7589,10 @@ window.cordova = require('cordova');
exec: function (success, fail, service, action, args) {
var uri = service + "/" + action,
request = new RemoteFunctionCall(uri),
- name;
+ callbackId = service + cordova.callbackId++,
+ response,
+ name,
+ didSucceed;
for (name in args) {
if (Object.hasOwnProperty.call(args, name)) {
@@ -7663,7 +7600,26 @@ window.cordova = require('cordova');
}
}
- return request.makeSyncCall(success, fail);
+ cordova.callbacks[callbackId] = {success:success, fail:fail};
+ request.addParam("callbackId", callbackId);
+
+ response = request.makeSyncCall();
+
+ //Old WebWorks Extension success
+ if (response.code === 42) {
+ if (success) {
+ success(response.data, response);
+ }
+ delete cordova.callbacks[callbackId];
+ } else if (response.code < 0) {
+ if (fail) {
+ fail(response.msg, response);
+ }
+ delete cordova.callbacks[callbackId];
+ } else {
+ didSucceed = response.code === cordova.callbackStatus.OK || response.code === cordova.callbackStatus.NO_RESULT;
+ cordova.callbackFromNative(callbackId, didSucceed, response.code, [didSucceed ? response.data : response.msg], !!response.keepCallback);
+ }
},
defineReadOnlyField: function (obj, field, value) {
Object.defineProperty(obj, field, {