From 5f962c9bb33b10827ef0f8470a0dab5fe518a8f6 Mon Sep 17 00:00:00 2001 From: Pawel Andruszkiewicz Date: Fri, 30 Oct 2015 09:35:57 +0100 Subject: [PATCH] [Device] Device adapted to cordova plugin model. [Verification] All tests pass. Change-Id: Ia903ce46fad08eeb46ec94d59395c81dc96220ad Signed-off-by: Pawel Andruszkiewicz --- src/device/cordova_device_api.js | 56 ++++++++++----- src/lib/cordova_plugins.js | 10 ++- .../plugins/cordova-plugin-device/www/device.js | 81 ++++++++++++++++++++++ 3 files changed, 130 insertions(+), 17 deletions(-) create mode 100644 src/lib/plugins/cordova-plugin-device/www/device.js diff --git a/src/device/cordova_device_api.js b/src/device/cordova_device_api.js index 06e5d11..5ce4de0 100755 --- a/src/device/cordova_device_api.js +++ b/src/device/cordova_device_api.js @@ -14,26 +14,50 @@ * limitations under the License. */ -var _global = window || global || {}; - -var device = { - cordova: cordova.version, - model: tizen.systeminfo.getCapability('http://tizen.org/system/model_name'), - platform: tizen.systeminfo.getCapability('http://tizen.org/system/platform.name'), - uuid: tizen.systeminfo.getCapability('http://tizen.org/system/tizenid'), - version: tizen.systeminfo.getCapability('http://tizen.org/feature/platform.version'), - manufacturer: tizen.systeminfo.getCapability('http://tizen.org/system/manufacturer') // not documented, but required by tests +// TODO: remove when added to public cordova repository -> begin +var plugin_name = 'cordova-plugin-device.tizen.Device'; + +cordova.define(plugin_name, function(require, exports, module) { +// TODO: remove -> end + +function DeviceInfo() { + this.cordovaVersion = require('cordova/platform').cordovaVersion; + this.model = tizen.systeminfo.getCapability('http://tizen.org/system/model_name'); + this.platform = tizen.systeminfo.getCapability('http://tizen.org/system/platform.name'); + this.uuid = tizen.systeminfo.getCapability('http://tizen.org/system/tizenid'); + this.version = tizen.systeminfo.getCapability('http://tizen.org/feature/platform.version'); + this.manufacturer = tizen.systeminfo.getCapability('http://tizen.org/system/manufacturer'); +} + +var di; + +exports = { + getDeviceInfo: function (success, error) { + if (!di) { + di = new DeviceInfo(); + } + success({ + cordova: di.cordovaVersion, + model: di.model, + platform: di.platform, + uuid: di.uuid, + version: di.version, + manufacturer: di.manufacturer // not documented, but required by tests + }); + } }; -Object.freeze(device); -Object.defineProperty(_global, 'device', { - configurable: false, - enumerable: true, - writable: false, - value: device -}); +require("cordova/exec/proxy").add("Device", exports); console.log('Loaded cordova.device API'); +//TODO: remove when added to public cordova repository -> begin +}); + exports = function(require) { + // this plugin is not loaded via cordova_plugins.js, we need to manually add + // it to module mapper + var mm = require('cordova/modulemapper'); + mm.runs(plugin_name); }; +//TODO: remove -> end diff --git a/src/lib/cordova_plugins.js b/src/lib/cordova_plugins.js index 239fb9b..f56a068 100644 --- a/src/lib/cordova_plugins.js +++ b/src/lib/cordova_plugins.js @@ -1,6 +1,13 @@ cordova.define('cordova/plugin_list', function(require, exports, module) { module.exports = [ { + "file": "plugins/cordova-plugin-device/www/device.js", + "id": "cordova-plugin-device.device", + "clobbers": [ + "device" + ] + }, + { "file": "plugins/cordova-plugin-file/www/DirectoryEntry.js", "id": "cordova-plugin-file.DirectoryEntry", "clobbers": [ @@ -142,7 +149,8 @@ module.exports = [ module.exports.metadata = // TOP OF METADATA { + "cordova-plugin-device": "1.0.1", "cordova-plugin-file": "3.0.0", } // BOTTOM OF METADATA -}); \ No newline at end of file +}); diff --git a/src/lib/plugins/cordova-plugin-device/www/device.js b/src/lib/plugins/cordova-plugin-device/www/device.js new file mode 100644 index 0000000..023bafd --- /dev/null +++ b/src/lib/plugins/cordova-plugin-device/www/device.js @@ -0,0 +1,81 @@ +cordova.define("cordova-plugin-device.device", function(require, exports, module) { /* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 argscheck = require('cordova/argscheck'), + channel = require('cordova/channel'), + utils = require('cordova/utils'), + exec = require('cordova/exec'), + cordova = require('cordova'); + +channel.createSticky('onCordovaInfoReady'); +// Tell cordova channel to wait on the CordovaInfoReady event +channel.waitForInitialization('onCordovaInfoReady'); + +/** + * This represents the mobile device, and provides properties for inspecting the model, version, UUID of the + * phone, etc. + * @constructor + */ +function Device() { + this.available = false; + this.platform = null; + this.version = null; + this.uuid = null; + this.cordova = null; + this.model = null; + this.manufacturer = null; + + var me = this; + + channel.onCordovaReady.subscribe(function() { + me.getInfo(function(info) { + //ignoring info.cordova returning from native, we should use value from cordova.version defined in cordova.js + //TODO: CB-5105 native implementations should not return info.cordova + var buildLabel = cordova.version; + me.available = true; + me.platform = info.platform; + me.version = info.version; + me.uuid = info.uuid; + me.cordova = buildLabel; + me.model = info.model; + me.manufacturer = info.manufacturer || 'unknown'; + channel.onCordovaInfoReady.fire(); + },function(e) { + me.available = false; + utils.alert("[ERROR] Error initializing Cordova: " + e); + }); + }); +} + +/** + * Get device info + * + * @param {Function} successCallback The function to call when the heading data is available + * @param {Function} errorCallback The function to call when there is an error getting the heading data. (OPTIONAL) + */ +Device.prototype.getInfo = function(successCallback, errorCallback) { + argscheck.checkArgs('fF', 'Device.getInfo', arguments); + exec(successCallback, errorCallback, "Device", "getDeviceInfo", []); +}; + +module.exports = new Device(); + +}); -- 2.7.4