[SystemInfo] Memory property implemented
authorPiotr Kosko <p.kosko@samsung.com>
Thu, 18 Dec 2014 08:45:23 +0000 (09:45 +0100)
committerPiotr Kosko <p.kosko@samsung.com>
Thu, 18 Dec 2014 08:54:05 +0000 (09:54 +0100)
[Feature] getPropertyValue and addPropertyValueChangeListener for MEMORY work.

[Verification] Code compiles without errors.
  Tested in console. Getting property and registering listener is possible.

Change-Id: I8999df5b0e80b49dd11dcbd6c9122b9d71b1e1c3
Signed-off-by: Piotr Kosko <p.kosko@samsung.com>
src/systeminfo/systeminfo-utils.cpp
src/systeminfo/systeminfo-utils.h
src/systeminfo/systeminfo_api.js
src/systeminfo/systeminfo_instance.cc

index 037ec9d0683cea111fd6a6a001b0f6af6f32c75e..e66f5dad989b622e8c21ed0710359e02b088c111 100644 (file)
@@ -69,6 +69,8 @@ namespace extension {
 namespace systeminfo {
 
 namespace {
+const std::string MEMORY_STATE_NORMAL = "NORMAL";
+const std::string MEMORY_STATE_WARNING = "WARNING";
 const int MEMORY_TO_BYTE = 1024;
 }
 using namespace common;
@@ -87,6 +89,7 @@ static void OnNetworkValueChangedCb(const char* ipv4_address,
         const char* ipv6_address, void* event_ptr);
 static void OnCellularNetworkValueChangedCb(keynode_t *node, void *event_ptr);
 static void OnPeripheralChangedCb(keynode_t* node, void* event_ptr);
+static void OnMemoryChangedCb(keynode_t* node, void* event_ptr);
 
 static void SimCphsValueCallback(TapiHandle *handle, int result, void *data, void *user_data);
 static void SimMsisdnValueCallback(TapiHandle *handle, int result, void *data, void *user_data);
@@ -719,6 +722,8 @@ public:
     void UnregisterCellularNetworkListener();
     void RegisterPeripheralListener(const SysteminfoUtilsCallback& callback);
     void UnregisterPeripheralListener();
+    void RegisterMemoryListener(const SysteminfoUtilsCallback& callback);
+    void UnregisterMemoryListener();
 
     void SetCpuInfoLoad(double load);
     void SetAvailableCapacityInternal(unsigned long long capacity);
@@ -737,6 +742,7 @@ public:
             const char* ipv6_address, void* event_ptr);
     void OnCellularNetworkValueCallback(keynode_t *node, void *event_ptr);
     void OnPeripheralChangedCallback(keynode_t* node, void* event_ptr);
+    void OnMemoryChangedCallback(keynode_t* node, void* event_ptr);
 
     TapiHandle* GetTapiHandle();
     TapiHandle** GetTapiHandles();
@@ -769,6 +775,7 @@ private:
     SysteminfoUtilsCallback m_wifi_network_listener;
     SysteminfoUtilsCallback m_cellular_network_listener;
     SysteminfoUtilsCallback m_peripheral_listener;
+    SysteminfoUtilsCallback m_memory_listener;
 
     TapiHandle *m_tapi_handles[TAPI_HANDLE_MAX+1];
     //for ip change callback
@@ -794,6 +801,7 @@ SystemInfoListeners::SystemInfoListeners():
         m_wifi_network_listener(nullptr),
         m_cellular_network_listener(nullptr),
         m_peripheral_listener(nullptr),
+        m_memory_listener(nullptr),
         m_connection_handle(nullptr)
 {
     LOGD("Entered");
@@ -810,6 +818,7 @@ SystemInfoListeners::~SystemInfoListeners(){
     UnregisterWifiNetworkListener();
     UnregisterCellularNetworkListener();
     UnregisterPeripheralListener();
+    UnregisterMemoryListener();
 
     unsigned int i = 0;
     while(m_tapi_handles[i]) {
@@ -1108,6 +1117,35 @@ void SystemInfoListeners::UnregisterPeripheralListener()
     }
 }
 
+void SystemInfoListeners::RegisterMemoryListener(const SysteminfoUtilsCallback& callback)
+{
+    if (nullptr == m_memory_listener) {
+        try {
+            int value = 0;
+            if (-1 != vconf_get_int(VCONFKEY_SYSMAN_LOW_MEMORY, &value)) {
+                RegisterVconfCallback(VCONFKEY_SYSMAN_LOW_MEMORY, OnMemoryChangedCb);
+            }
+        } catch (const PlatformException& e) {
+            // empty on purpose
+        }
+        LOGD("Added callback for MEMORY");
+        m_memory_listener = callback;
+    }
+}
+
+void SystemInfoListeners::UnregisterMemoryListener()
+{
+    if (nullptr != m_memory_listener) {
+        try {
+            UnregisterVconfCallback(VCONFKEY_SYSMAN_LOW_MEMORY, OnMemoryChangedCb);
+        } catch (const PlatformException& e) {
+            // empty on purpose
+        }
+        LOGD("Removed callback for MEMORY");
+        m_memory_listener = nullptr;
+    }
+}
+
 void SystemInfoListeners::SetCpuInfoLoad(double load)
 {
     m_cpu_load = load;
@@ -1234,6 +1272,13 @@ void SystemInfoListeners::OnPeripheralChangedCallback(keynode_t* /*node*/, void*
     }
 }
 
+void SystemInfoListeners::OnMemoryChangedCallback(keynode_t* /*node*/, void* /*event_ptr*/)
+{
+    if (nullptr != m_memory_listener) {
+        m_memory_listener();
+    }
+}
+
 void SystemInfoListeners::InitTapiHandles()
 {
     LOGD("Entered");
@@ -1409,6 +1454,12 @@ void OnPeripheralChangedCb(keynode_t* node, void* event_ptr)
     system_info_listeners.OnPeripheralChangedCallback(node, event_ptr);
 }
 
+void OnMemoryChangedCb(keynode_t* node, void* event_ptr)
+{
+    LOGD("");
+    system_info_listeners.OnMemoryChangedCallback(node, event_ptr);
+}
+
 /////////////////////////// SysteminfoUtils ////////////////////////////////
 
 static bool GetValueBool(const char *key) {
@@ -1552,7 +1603,7 @@ unsigned long SysteminfoUtils::GetCount(const std::string& property)
             "DISPLAY" == property || "DEVICE_ORIENTATION" == property ||
             "BUILD" == property || "LOCALE" == property || "NETWORK" == property ||
             "WIFI_NETWORK" == property || "CELLULAR_NETWORK" == property ||
-            "PERIPHERAL" == property) {
+            "PERIPHERAL" == property || "MEMORY" == property) {
         count = DEFAULT_PROPERTY_COUNT;
     } else if ("SIM" == property) {
         count = sim_mgr.GetSimCount(system_info_listeners.GetTapiHandles());
@@ -1603,6 +1654,8 @@ picojson::value SysteminfoUtils::GetPropertyValue(const std::string& property, b
             ReportSim(result_obj, i);
         } else if ("PERIPHERAL" == property) {
             ReportPeripheral(result_obj);
+        } else if ("MEMORY" == property) {
+            ReportMemory(result_obj);
         } else {
             LOGD("Property with given id is not supported");
             throw NotSupportedException("Property with given id is not supported");
@@ -2236,6 +2289,26 @@ void SysteminfoUtils::ReportPeripheral(picojson::object& out) {
     out.insert(std::make_pair(kVideoOutputString, false));
 }
 
+void SysteminfoUtils::ReportMemory(picojson::object& out) {
+    std::string state = MEMORY_STATE_NORMAL;
+    try {
+        int status = GetVconfInt(VCONFKEY_SYSMAN_LOW_MEMORY);
+        switch (status) {
+        case VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING:
+        case VCONFKEY_SYSMAN_LOW_MEMORY_HARD_WARNING:
+            state = MEMORY_STATE_WARNING;
+            break;
+        case VCONFKEY_SYSMAN_LOW_MEMORY_NORMAL:
+        default:
+            state = MEMORY_STATE_NORMAL;
+        }
+    } catch (const PlatformException& e) {
+        // empty on purpose
+    }
+
+    out.insert(std::make_pair("state", state));
+}
+
 static void CreateStorageInfo(const std::string& type, struct statfs& fs, picojson::object* out) {
     out->insert(std::make_pair("type", type));
     out->insert(std::make_pair("capacity", std::to_string(
@@ -2383,6 +2456,17 @@ void SysteminfoUtils::UnregisterPeripheralListener()
     system_info_listeners.UnregisterPeripheralListener();
 }
 
+void SysteminfoUtils::RegisterMemoryListener(const SysteminfoUtilsCallback& callback)
+{
+    system_info_listeners.RegisterMemoryListener(callback);
+}
+
+void SysteminfoUtils::UnregisterMemoryListener()
+{
+    system_info_listeners.UnregisterMemoryListener();
+}
+
+
 static bool CheckStringCapability(const std::string& key, std::string* value)
 {
     LOGD("Entered CheckStringCapability");
index 33a25f830ef6ff03b6ab84a2f684b849c4ad5b9e..a247c6f37aa039c49ce070d5ef149080da85c8e3 100644 (file)
@@ -61,6 +61,8 @@ public:
     static void UnregisterCellularNetworkListener();
     static void RegisterPeripheralListener(const SysteminfoUtilsCallback& callback);
     static void UnregisterPeripheralListener();
+    static void RegisterMemoryListener(const SysteminfoUtilsCallback& callback);
+    static void UnregisterMemoryListener();
 
 private:
     static void ReportBattery(picojson::object& out);
@@ -76,6 +78,7 @@ private:
     static void ReportCellularNetwork(picojson::object& out);
     static void ReportSim(picojson::object& out, unsigned long count);
     static void ReportPeripheral(picojson::object& out);
+    static void ReportMemory(picojson::object& out);
 
     static void ReportStorage(picojson::object& out);
 };
index 0a9c8ca9627f492978fa2731f080ff9b4a9083af..3bcdc750b25b56dabb13fda5f49d7e7098ed66a1 100644 (file)
@@ -274,7 +274,8 @@ var SystemInfoPropertyId = {
         WIFI_NETWORK : 'WIFI_NETWORK',
         CELLULAR_NETWORK : 'CELLULAR_NETWORK',
         SIM : 'SIM',
-        PERIPHERAL : 'PERIPHERAL'
+        PERIPHERAL : 'PERIPHERAL',
+        MEMORY : 'MEMORY'
 };
 
 //class SystemInfoDeviceCapability ////////////////////////////////////////////////////
@@ -818,6 +819,13 @@ function SystemInfoPeripheral(data) {
     });
 }
 
+//class SystemInfoMemory ////////////////////////////////////////////////////
+function SystemInfoMemory(data) {
+    Object.defineProperties(this, {
+        state : {value: data.state, writable: false, enumerable: true}
+    });
+}
+
 //class SystemInfo ////////////////////////////////////////////////////
 var SystemInfo = function() {
 };
@@ -935,6 +943,7 @@ var _wifiNetworkStr = SystemInfoPropertyId.WIFI_NETWORK;
 var _cellularNetworkStr = SystemInfoPropertyId.CELLULAR_NETWORK;
 var _simStr = SystemInfoPropertyId.SIM;
 var _peripheralStr = SystemInfoPropertyId.PERIPHERAL;
+var _memoryStr = SystemInfoPropertyId.MEMORY;
 
 var _nextId = 0;
 
@@ -1133,6 +1142,22 @@ function _systeminfoPeripheralListenerCallback(event) {
     }
 }
 
+function _systeminfoMemoryListenerCallback(event) {
+    var property = _memoryStr;
+    var eventObj = JSON.parse(event);
+    var callbacks = _propertyContainer[property].callbacks;
+
+    for (var watchId in callbacks) {
+        if (callbacks.hasOwnProperty(watchId)) {
+            var listener = callbacks[watchId];
+            var propObj = !listener.isArrayType ?
+                    _createProperty(property, eventObj.result.array[0]) :
+                        _createPropertyArray(property, eventObj.result);
+            callbacks[watchId].callback(propObj);
+        }
+    }
+}
+
 var _propertyContainer = {
         'BATTERY' : {
             callbacks : {},
@@ -1205,6 +1230,12 @@ var _propertyContainer = {
             constructor : SystemInfoPeripheral,
             broadcastFunction : _systeminfoPeripheralListenerCallback,
             signalLabel : 'SystemInfoPeripheralChangeBroadcast'
+        },
+        'MEMORY' : {
+            callbacks : {},
+            constructor : SystemInfoMemory,
+            broadcastFunction : _systeminfoMemoryListenerCallback,
+            signalLabel : 'SystemInfoMemoryChangeBroadcast'
         }
 };
 
index a95866d4477d77d8f2b20f82b05a3e05de7856d1..0a9a653d00242b73b20d4c3e00de03e2bd26b9d5 100644 (file)
@@ -31,6 +31,7 @@ static void OnNetworkChangedCallback();
 static void OnWifiNetworkChangedCallback();
 static void OnCellularNetworkChangedCallback();
 static void OnPeripheralChangedCallback();
+static void OnMemoryChangedCallback();
 
 namespace {
 const std::string kPropertyIdBattery = "BATTERY";
@@ -45,6 +46,7 @@ const std::string kPropertyIdWifiNetwork = "WIFI_NETWORK";
 const std::string kPropertyIdCellularNetwork = "CELLULAR_NETWORK";
 const std::string kPropertyIdSim = "SIM";
 const std::string kPropertyIdPeripheral = "PERIPHERAL";
+const std::string kPropertyIdMemory= "MEMORY";
 }
 
 SysteminfoInstance& SysteminfoInstance::getInstance() {
@@ -320,6 +322,8 @@ void SysteminfoInstance::AddPropertyValueChangeListener(const picojson::value& a
             LoggerW("SIM listener is not supported by Core API - ignoring");
         } else if (property_name == kPropertyIdPeripheral) {
             SysteminfoUtils::RegisterPeripheralListener(OnPeripheralChangedCallback);
+        } else if (property_name == kPropertyIdMemory) {
+            SysteminfoUtils::RegisterMemoryListener(OnMemoryChangedCallback);
         } else {
             LoggerE("Not supported property");
             throw InvalidValuesException("Not supported property");
@@ -404,6 +408,8 @@ void SysteminfoInstance::RemovePropertyValueChangeListener(const picojson::value
             LoggerW("SIM listener is not supported by Core API - ignoring");
         } else if (property_name == kPropertyIdPeripheral) {
             SysteminfoUtils::UnregisterPeripheralListener();
+        } else if (property_name == kPropertyIdMemory) {
+            SysteminfoUtils::UnregisterMemoryListener();
         } else {
             LoggerE("Not supported property");
             throw InvalidValuesException("Not supported property");
@@ -553,5 +559,18 @@ void OnPeripheralChangedCallback()
     SysteminfoInstance::getInstance().PostMessage(response->serialize().c_str());
 }
 
+void OnMemoryChangedCallback()
+{
+    LoggerD("");
+    const std::shared_ptr<picojson::value>& response =
+            std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
+    response->get<picojson::object>()["propertyId"] = picojson::value(kPropertyIdMemory);
+
+    picojson::value result = SysteminfoUtils::GetPropertyValue(kPropertyIdMemory, true);
+    ReportSuccess(result,response->get<picojson::object>());
+
+    SysteminfoInstance::getInstance().PostMessage(response->serialize().c_str());
+}
+
 } // namespace systeminfo
 } // namespace extension