From: Lukasz Bardeli Date: Wed, 21 Oct 2015 11:06:39 +0000 (+0200) Subject: [Cordova] NetworkInformation plugin implemented. X-Git-Tag: submit/tizen/20151221.111205^2~83^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d0038e0e088f6fb5016a3cea4b445706f9a358f1;p=platform%2Fcore%2Fapi%2Fcordova-plugins.git [Cordova] NetworkInformation plugin implemented. [Verification] Test pass rate: 100% (4/4/0/0/0) Change-Id: Ic566c97fa34f299b8b91d9309089867601cd6aac Signed-off-by: Lukasz Bardeli --- diff --git a/src/cordova-api.gyp b/src/cordova-api.gyp index bb61934..e0f29c2 100644 --- a/src/cordova-api.gyp +++ b/src/cordova-api.gyp @@ -11,6 +11,7 @@ 'cordova/cordova.gyp:*', 'device/cordova_device.gyp:*', 'file/cordova_file.gyp:*', + 'networkinformation/cordova_networkinformation.gyp:*', ], }, ], # end targets diff --git a/src/networkinformation/cordova_networkinformation.gyp b/src/networkinformation/cordova_networkinformation.gyp new file mode 100644 index 0000000..e22a8d1 --- /dev/null +++ b/src/networkinformation/cordova_networkinformation.gyp @@ -0,0 +1,25 @@ +{ + 'includes':[ + '/usr/include/webapi-plugins/src/common/common.gypi', + ], + 'targets': [ + { + 'target_name': 'tizen_cordova_networkinformation', + 'type': 'loadable_module', + 'sources': [ + 'cordova_networkinformation_api.js', + 'cordova_networkinformation_extension.cc', + 'cordova_networkinformation_extension.h', + ], + 'include_dirs': [ + '../', + '<(SHARED_INTERMEDIATE_DIR)', + ], + 'variables': { + 'packages': [ + 'webapi-plugins', + ], + }, + }, + ], +} diff --git a/src/networkinformation/cordova_networkinformation_api.js b/src/networkinformation/cordova_networkinformation_api.js new file mode 100644 index 0000000..4504096 --- /dev/null +++ b/src/networkinformation/cordova_networkinformation_api.js @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 _global = window || global || {}; + +var Connection = { + UNKNOWN: "unknown", + ETHERNET: "ethernet", + WIFI: "wifi", + CELL_2G: "2g", + CELL_3G: "3g", + CELL_4G: "4g", + CELL:"cellular", + NONE: "none" +}; + +var type = Connection.UNKNOWN; + +var connection = {}; + +Object.defineProperty(connection, 'type', { + get: function() { return type; }, + set: function() {}, + enumerable: true +}); + +var network = { + connection: connection +}; + +function onSuccessCallback(info) { + switch (info.networkType) { + case "NONE": + type = Connection.NONE; + break; + case "2G": + type = Connection.CELL_2G; + break; + case "2.5G": + // TODO consider. In cordova documentation there is no information about 2.5G + // so instead 2G is returned + type = Connection.CELL_2G; + break; + case "3G": + type = Connection.CELL_3G; + break; + case "4G": + type = Connection.CELL_4G; + break; + case "WIFI": + type = Connection.WIFI; + break; + case "ETHERNET": + type = Connection.ETHERNET; + break; + default: + type = Connection.UNKNOWN; + break; + } + + if (Connection.NONE === type || Connection.UNKNOWN === type) { + document.dispatchEvent(new Event("offline")); + } else { + document.dispatchEvent(new Event("online")); + } +} + +function onErrorCallback(e) { + type = Connection.UNKNOWN; + console.error("Error appeared " + e.name + ", message "+ e.message); +} + +tizen.systeminfo.getPropertyValue("NETWORK", onSuccessCallback, onErrorCallback); +// TODO currently method addPropertyValueChangeListener is registered only to SIM1 and +// ignore changes on SIM2 (if device has dual SIM standby mode). Consider to use +// method addPropertyValueChangeListenerArray to get information from both SIM, but +// which type should be returned then? +tizen.systeminfo.addPropertyValueChangeListener("NETWORK", onSuccessCallback, onErrorCallback); + +_global.Connection = Connection; +_global.navigator.connection = connection; +_global.navigator.network = network; + +console.log('Loaded cordova.networkinformation API'); diff --git a/src/networkinformation/cordova_networkinformation_extension.cc b/src/networkinformation/cordova_networkinformation_extension.cc new file mode 100644 index 0000000..fdcec2e --- /dev/null +++ b/src/networkinformation/cordova_networkinformation_extension.cc @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +#include "networkinformation/cordova_networkinformation_extension.h" + +// This will be generated from cordova_networkinformation_api.js +extern const char kSource_cordova_networkinformation_api[]; + +common::Extension* CreateExtension() { + return new extension::cordova::networkinformation::CordovaNetworkInformationExtension(); +} + +namespace extension { +namespace cordova { +namespace networkinformation { + +CordovaNetworkInformationExtension::CordovaNetworkInformationExtension() { + SetExtensionName("tizen.cordova.networkinformation"); + SetJavaScriptAPI(kSource_cordova_networkinformation_api); +} + +CordovaNetworkInformationExtension::~CordovaNetworkInformationExtension() {} + +} // networkinformation +} // cordova +} // extension diff --git a/src/networkinformation/cordova_networkinformation_extension.h b/src/networkinformation/cordova_networkinformation_extension.h new file mode 100644 index 0000000..3168b57 --- /dev/null +++ b/src/networkinformation/cordova_networkinformation_extension.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +#ifndef NETWORKINFORMATION_CORDOVA_NETWORKINFORMATION_EXTENSION_H_ +#define NETWORKINFORMATION_CORDOVA_NETWORKINFORMATION_EXTENSION_H_ + +#include + +namespace extension { +namespace cordova { +namespace networkinformation { + +class CordovaNetworkInformationExtension : public common::Extension { + public: + CordovaNetworkInformationExtension(); + virtual ~CordovaNetworkInformationExtension(); +}; + +} // networkinformation +} // cordova +} // extension + +#endif // NETWORKINFORMATION_CORDOVA_NETWORKINFORMATION_EXTENSION_H_