From e4d13fdb72f8615462f7acbeccd29d4e4d869b3f Mon Sep 17 00:00:00 2001 From: Jiajia Qin Date: Tue, 3 Sep 2013 10:30:08 +0800 Subject: [PATCH] [SystemInfo] Implement SystemInfoOptions support --- examples/system_info.html | 41 ++++++++++++++++++++++++- system_info/system_info_api.js | 63 +++++++++++++++++++++++++++++++++++--- system_info/system_info_context.cc | 4 +-- system_info/system_info_utils.h | 4 +-- 4 files changed, 103 insertions(+), 9 deletions(-) diff --git a/examples/system_info.html b/examples/system_info.html index cdd7c55..843c1af 100644 --- a/examples/system_info.html +++ b/examples/system_info.html @@ -83,7 +83,7 @@ handle("cpu_btn", function() { var id = tizen.systeminfo.addPropertyValueChangeListener( "CPU", function(cpu) { - output.value += '\n Property CPU changed.'; + output.value += '\n Property CPU changed with no option.'; output.value += '\n\t load : ' + parseFloat(cpu.load) * 100 + '%'; output.scrollTop = output.scrollHeight; count += 1; @@ -94,6 +94,45 @@ handle("cpu_btn", function() { tizen.systeminfo.removePropertyValueChangeListener(id); } }); + var id1 = tizen.systeminfo.addPropertyValueChangeListener( + "CPU", + function(cpu) { + output.value += '\n Property CPU changed with highThreshold 0.2.'; + output.value += '\n\t load : ' + parseFloat(cpu.load) * 100 + '%'; + output.scrollTop = output.scrollHeight; + output.scrollTop = output.scrollHeight; + }, + {'highThreshold':0.2}); + + var id2 = tizen.systeminfo.addPropertyValueChangeListener( + "CPU", + function(cpu) { + output.value += '\n Property CPU changed with lowThreshold 1.0.'; + output.value += '\n\t load : ' + parseFloat(cpu.load) * 100 + '%'; + output.scrollTop = output.scrollHeight; + output.scrollTop = output.scrollHeight; + }, + {'lowThreshold':1.0}); + + var id3 = tizen.systeminfo.addPropertyValueChangeListener( + "CPU", + function(cpu) { + output.value += '\n Property CPU changed with highThreshold 1.00, lowThreshold 1.10.'; + output.value += '\n\t load : ' + parseFloat(cpu.load) * 100 + '%'; + output.scrollTop = output.scrollHeight; + output.scrollTop = output.scrollHeight; + }, + {'highThreshold':1.00, 'lowThreshold':1.10}); + + var id4 = tizen.systeminfo.addPropertyValueChangeListener( + "CPU", + function(cpu) { + output.value += '\n Property CPU changed with timeout:6 seconds'; + output.value += '\n\t load : ' + parseFloat(cpu.load) * 100 + '%'; + output.scrollTop = output.scrollHeight; + output.scrollTop = output.scrollHeight; + }, + {'timeout':6000}); tizen.systeminfo.getPropertyValue( "CPU", diff --git a/system_info/system_info_api.js b/system_info/system_info_api.js index 2772543..4ef47ef 100644 --- a/system_info/system_info_api.js +++ b/system_info/system_info_api.js @@ -15,6 +15,14 @@ var postMessage = function(msg, callback) { extension.postMessage(JSON.stringify(msg)); }; +var _checkThreshold = function(value, highThreshold, lowThreshold) { + if ((highThreshold && (highThreshold >= 0) && (value >= highThreshold)) || + (lowThreshold && (lowThreshold >= 0) && (value <= lowThreshold))) + return true; + + return false; +} + extension.setMessageListener(function(json) { var msg = JSON.parse(json); @@ -23,7 +31,52 @@ extension.setMessageListener(function(json) { if (msg.prop && (0 !== msg.prop.length)) { for (var id in _listeners) { if (_listeners[id]["prop"] === msg.prop) { - // FIXME(halton): Here is ignoring option, should be added later + var option = _listeners[id]["option"]; + if (option) { + var currentTime = (new Date()).valueOf(); + var timeout = parseFloat(option["timeout"]); + var highThreshold = parseFloat(option["highThreshold"]); + var lowThreshold = parseFloat(option["lowThreshold"]); + var timeStamp = parseFloat(_listeners[id]["timestamp"]); + if (timeout && (currentTime - timeStamp) > timeout) { + delete _listeners[id]; + if (!_hasListener(msg.prop)) { + var message = { + 'cmd': 'stopListening', + 'prop': msg.prop, + }; + extension.postMessage(JSON.stringify(message)); + return; + } + continue; + } + switch(msg.prop) { + case "BATTERY": + if (_checkThreshold(msg.data.level, highThreshold, lowThreshold)) + _listeners[id]["callback"](msg.data); + break; + case "CPU": + if (_checkThreshold(msg.data.load, highThreshold, lowThreshold)) + _listeners[id]["callback"](msg.data); + break; + case "DISPLAY": + if (_checkThreshold(msg.data.brightness, highThreshold, lowThreshold)) + _listeners[id]["callback"](msg.data); + break; + case "STORAGE": + case "DEVICE_ORIENTATION": + case "BUILD": + case "LOCALE": + case "NETWORK": + case "WIFI_NETWORK": + case "CELLULAR_NETWORK": + case "SIM": + case "PERIPHERAL": + _listeners[id]["callback"](msg.data); + } + _listeners[id]["timestamp"] = currentTime; + continue; + } _listeners[id]["callback"](msg.data); } } @@ -101,21 +154,23 @@ exports.addPropertyValueChangeListener = function(prop, successCallback, option) if (typeof successCallback !== 'function') throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR); - if (option && (typeof listener !== 'object')) + if (option && (typeof option !== 'object')) throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR); if (!_hasListener(prop)) { var msg = { - 'cmd': 'startListen', + 'cmd': 'startListening', 'prop': prop, }; extension.postMessage(JSON.stringify(msg)); } + var timeStamp = (new Date()).valueOf(); var listener = { "prop": prop, "callback": successCallback, "option": option, + "timestamp": timeStamp, }; var listener_id = _next_listener_id; @@ -131,7 +186,7 @@ exports.removePropertyValueChangeListener = function(listenerId) { delete _listeners[listenerId]; if (!_hasListener(prop)) { var msg = { - 'cmd': 'stopListen', + 'cmd': 'stopListening', 'prop': prop, }; extension.postMessage(JSON.stringify(msg)); diff --git a/system_info/system_info_context.cc b/system_info/system_info_context.cc index 9abf3ba..3b82580 100644 --- a/system_info/system_info_context.cc +++ b/system_info/system_info_context.cc @@ -174,9 +174,9 @@ void SystemInfoContext::HandleMessage(const char* message) { if (cmd == "getPropertyValue") { picojson::value output = picojson::value(picojson::object()); HandleGetPropertyValue(input, output); - } else if (cmd == "startListen") { + } else if (cmd == "startListening") { HandleStartListening(input); - } else if (cmd == "stopListen") { + } else if (cmd == "stopListening") { HandleStopListening(input); } } diff --git a/system_info/system_info_utils.h b/system_info/system_info_utils.h index ec2e7c8..bedfaeb 100644 --- a/system_info/system_info_utils.h +++ b/system_info/system_info_utils.h @@ -12,8 +12,8 @@ namespace system_info { -// The default timeout interval is set to 3s to match the top update interval. -const int default_timeout_interval = 3000; +// The default timeout interval is set to 1s to match the top update interval. +const int default_timeout_interval = 1000; int ReadOneByte(const char* path); // Free the returned value after using. -- 2.7.4