[SystemInfo] Added callback for camera flash change
authorWojciech Kosowicz <w.kosowicz@samsung.com>
Wed, 25 Mar 2015 10:31:58 +0000 (11:31 +0100)
committerWojciech Kosowicz <w.kosowicz@samsung.com>
Fri, 24 Apr 2015 12:50:54 +0000 (21:50 +0900)
Unable to test it out as the callback does not come back
from the platform.

[Verification] Tct without changes

Change-Id: I76155c46d7f966a49a11c454a8bcf1c3a3c2cae0
Signed-off-by: Wojciech Kosowicz <w.kosowicz@samsung.com>
src/systeminfo/systeminfo-utils.cpp
src/systeminfo/systeminfo-utils.h
src/systeminfo/systeminfo_api.js
src/systeminfo/systeminfo_instance.cc

index 86384fd085e48a3902f36a5b40f338f623cb5d14..c14550c100a1f109dc25f0832590a277f26ef2fd 100644 (file)
@@ -32,6 +32,8 @@
 #include <ITapiModem.h>
 #include <ITapiSim.h>
 #include <device.h>
+#include <device/callback.h>
+#include <device/device-error.h>
 #include <sensor_internal.h>
 
 #include "common/logger.h"
@@ -93,6 +95,7 @@ static void OnNetworkValueChangedCb(const char* ipv4_address,
 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 OnBrightnessChangedCb(device_callback_e type, void *value, void *user_data);
 
 static void SimCphsValueCallback(TapiHandle *handle, int result, void *data, void *user_data);
 static void SimMsisdnValueCallback(TapiHandle *handle, int result, void *data, void *user_data);
@@ -492,6 +495,9 @@ class SystemInfoListeners {
   PlatformResult RegisterMemoryListener(const SysteminfoUtilsCallback& callback,
                                         SysteminfoInstance& instance);
   PlatformResult UnregisterMemoryListener();
+  PlatformResult RegisterCameraFlashListener(const SysteminfoUtilsCallback& callback,
+                                        SysteminfoInstance& instance);
+  PlatformResult UnregisterCameraFlashListener();
 
   void SetCpuInfoLoad(double load);
   void SetAvailableCapacityInternal(unsigned long long capacity);
@@ -512,6 +518,7 @@ class SystemInfoListeners {
   void OnCellularNetworkValueCallback(keynode_t *node, void *event_ptr);
   void OnPeripheralChangedCallback(keynode_t* node, void* event_ptr);
   void OnMemoryChangedCallback(keynode_t* node, void* event_ptr);
+  void OnBrightnessChangedCallback(device_callback_e type, void* value, void* user_data);
 
   TapiHandle* GetTapiHandle();
   TapiHandle** GetTapiHandles();
@@ -548,6 +555,7 @@ class SystemInfoListeners {
   SysteminfoUtilsCallback m_cellular_network_listener;
   SysteminfoUtilsCallback m_peripheral_listener;
   SysteminfoUtilsCallback m_memory_listener;
+  SysteminfoUtilsCallback m_camera_flash_listener;
 
   TapiHandle *m_tapi_handles[TAPI_HANDLE_MAX+1];
   //for ip change callback
@@ -577,7 +585,8 @@ SystemInfoListeners::SystemInfoListeners():
             m_peripheral_listener(nullptr),
             m_memory_listener(nullptr),
             m_connection_handle(nullptr),
-            m_sensor_handle(-1)
+            m_sensor_handle(-1),
+            m_camera_flash_listener(nullptr)
 {
   LoggerD("Entered");
 }
@@ -1034,6 +1043,35 @@ PlatformResult SystemInfoListeners::UnregisterMemoryListener()
   return PlatformResult(ErrorCode::NO_ERROR);
 }
 
+PlatformResult SystemInfoListeners::RegisterCameraFlashListener(const SysteminfoUtilsCallback& callback,
+                                                           SysteminfoInstance& instance)
+{
+  if (nullptr == m_camera_flash_listener) {
+    if (DEVICE_ERROR_NONE != device_add_callback(DEVICE_CALLBACK_FLASH_BRIGHTNESS,
+                              OnBrightnessChangedCb, static_cast<void*>(&instance))) {
+        return PlatformResult(ErrorCode::UNKNOWN_ERR);
+      }
+      m_camera_flash_listener = callback;
+  }
+    return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+PlatformResult SystemInfoListeners::UnregisterCameraFlashListener()
+{
+  if (nullptr != m_camera_flash_listener) {
+    PlatformResult ret = PlatformResult(ErrorCode::NO_ERROR);
+    int value = 0;
+    if (DEVICE_ERROR_NONE != device_remove_callback(DEVICE_CALLBACK_FLASH_BRIGHTNESS,
+                                                 OnBrightnessChangedCb)) {
+      return PlatformResult(ErrorCode::UNKNOWN_ERR);
+    }
+    LoggerD("Removed callback for camera_flash");
+    m_camera_flash_listener = nullptr;
+  }
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+
 void SystemInfoListeners::SetCpuInfoLoad(double load)
 {
   m_cpu_load = load;
@@ -1187,6 +1225,14 @@ void SystemInfoListeners::OnMemoryChangedCallback(keynode_t* /*node*/, void* eve
   }
 }
 
+void SystemInfoListeners::OnBrightnessChangedCallback(device_callback_e type, void* value, void* user_data)
+{
+  if (nullptr != m_camera_flash_listener) {
+    SysteminfoInstance* instance = static_cast<SysteminfoInstance*>(user_data);
+    m_camera_flash_listener(*instance);
+  }
+}
+
 void SystemInfoListeners::InitTapiHandles()
 {
   LoggerD("Entered");
@@ -1380,6 +1426,14 @@ void OnMemoryChangedCb(keynode_t* node, void* event_ptr)
   system_info_listeners.OnMemoryChangedCallback(node, event_ptr);
 }
 
+void OnBrightnessChangedCb(device_callback_e type, void* value, void* user_data)
+{
+  LoggerD("");
+  if (type == DEVICE_CALLBACK_FLASH_BRIGHTNESS) {
+    system_info_listeners.OnBrightnessChangedCallback(type, value, user_data);
+  }
+}
+
 /////////////////////////// SysteminfoUtils ////////////////////////////////
 
 PlatformResult SystemInfoDeviceCapability::GetValueBool(const char *key, bool* value) {
@@ -2566,6 +2620,17 @@ PlatformResult SysteminfoUtils::UnregisterMemoryListener()
   return system_info_listeners.UnregisterMemoryListener();
 }
 
+PlatformResult SysteminfoUtils::RegisterCameraFlashListener(const SysteminfoUtilsCallback& callback,
+                                                       SysteminfoInstance& instance)
+{
+  return system_info_listeners.RegisterCameraFlashListener(callback, instance);
+}
+
+PlatformResult SysteminfoUtils::UnregisterCameraFlashListener()
+{
+  return system_info_listeners.UnregisterCameraFlashListener();
+}
+
 
 static PlatformResult CheckStringCapability(const std::string& key, std::string* value, bool* fetched)
 {
index a36651a4f7ee478ebd81ce8031796d1e097fe8f6..eff8b24bc1cb1aa94ec68a74bd0517ffc6817149 100644 (file)
@@ -78,6 +78,9 @@ class SysteminfoUtils {
   static common::PlatformResult RegisterMemoryListener(const SysteminfoUtilsCallback& callback,
                                                        SysteminfoInstance& instance);
   static common::PlatformResult UnregisterMemoryListener();
+  static common::PlatformResult RegisterCameraFlashListener(const SysteminfoUtilsCallback& callback,
+                                                       SysteminfoInstance& instance);
+  static common::PlatformResult UnregisterCameraFlashListener();
 
  private:
   static common::PlatformResult ReportProperty(const std::string& property, int index,
index 1e1c339b789e9ea8094f0fad581bafabc575f1cd..60903a0911524aa9b3d99580913cefa5d58a9c12 100644 (file)
@@ -654,7 +654,7 @@ function SystemInfoCameraFlash(data) {
   var getBrightness = function() {
       var result = native_.callSync('SystemInfo_getBrightness', {});
       if (native_.isSuccess(result)) {
-        return Converter_.toLong(native_.getResultObject(result));
+        return Converter_.toLong(native_.getResultObject(result)) / this.levels;
       }
       return null;
     };
@@ -678,6 +678,7 @@ SystemInfoCameraFlash.prototype.setBrightness = function(brightness) {
   var args = validator_.validateArgs(arguments, [
     {name: 'brightness', type: types_.LONG}
   ]);
+  args.brightness = args.brightness * this.levels;
 
   var result = native_.callSync('SystemInfo_setBrightness', args);
   if (native_.isFailure(result)) {
@@ -801,6 +802,7 @@ var _cellularNetworkStr = SystemInfoPropertyId.CELLULAR_NETWORK;
 var _simStr = SystemInfoPropertyId.SIM;
 var _peripheralStr = SystemInfoPropertyId.PERIPHERAL;
 var _memoryStr = SystemInfoPropertyId.MEMORY;
+var _cameraFlashStr = SystemInfoPropertyId.CAMERA_FLASH;
 
 var _nextId = 0;
 
@@ -1003,8 +1005,19 @@ function _systeminfoMemoryListenerCallback(eventObj) {
     }
 }
 
-function  _systeminfoCameraFlashListenerCallback(eventObject) {
-  //TODO fill this up
+function  _systeminfoCameraFlashListenerCallback(eventObj) {
+    var property = _cameraFlashStr;
+    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 = {
index 2b6c6d8133530a8402f6721ec80fbe6084d33b5a..9f06033d47f751dc01d44436702611056f1a6656 100644 (file)
@@ -33,6 +33,7 @@ static void OnWifiNetworkChangedCallback(SysteminfoInstance& instance);
 static void OnCellularNetworkChangedCallback(SysteminfoInstance& instance);
 static void OnPeripheralChangedCallback(SysteminfoInstance& instance);
 static void OnMemoryChangedCallback(SysteminfoInstance& instance);
+static void OnBrigthnessChangedCallback(SysteminfoInstance& instance);
 
 namespace {
 const std::string kPropertyIdString = "propertyId";
@@ -52,6 +53,7 @@ const std::string kPropertyIdCellularNetwork = "CELLULAR_NETWORK";
 const std::string kPropertyIdSim = "SIM";
 const std::string kPropertyIdPeripheral = "PERIPHERAL";
 const std::string kPropertyIdMemory= "MEMORY";
+const std::string kPropertyIdCameraFlash= "CAMERA_FLASH";
 
 const std::string kPrivilegeLED = "http://tizen.org/privilege/led";
 
@@ -103,6 +105,7 @@ const std::string kPrivilegeLED = "http://tizen.org/privilege/led";
 }
 
 SysteminfoInstance::SysteminfoInstance() {
+
   using std::placeholders::_1;
   using std::placeholders::_2;
 
@@ -329,6 +332,8 @@ void SysteminfoInstance::AddPropertyValueChangeListener(const picojson::value& a
     ret = SysteminfoUtils::RegisterPeripheralListener(OnPeripheralChangedCallback, *this);
   } else if (property_name == kPropertyIdMemory) {
     ret = SysteminfoUtils::RegisterMemoryListener(OnMemoryChangedCallback, *this);
+  } else if (property_name == kPropertyIdCameraFlash) {
+    ret = SysteminfoUtils::RegisterCameraFlashListener(OnBrigthnessChangedCallback, *this);
   } else {
     LoggerE("Not supported property");
     ret = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Not supported property");
@@ -437,6 +442,8 @@ void SysteminfoInstance::RemovePropertyValueChangeListener(const picojson::value
     ret = SysteminfoUtils::UnregisterPeripheralListener();
   } else if (property_name == kPropertyIdMemory) {
     ret = SysteminfoUtils::UnregisterMemoryListener();
+  } else if (property_name == kPropertyIdCameraFlash) {
+    ret = SysteminfoUtils::UnregisterCameraFlashListener();
   } else {
     LoggerE("Not supported property");
     ret = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Not supported property");
@@ -682,5 +689,21 @@ void OnMemoryChangedCallback(SysteminfoInstance& instance)
   }
 }
 
+void OnBrigthnessChangedCallback(SysteminfoInstance &instance)
+{
+    LoggerD("");
+    const std::shared_ptr<picojson::value>& response =
+        std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
+    response->get<picojson::object>()[kPropertyIdString] = picojson::value(kPropertyIdCameraFlash);
+    response->get<picojson::object>()[kListenerIdString] = picojson::value(kListenerConstValue);
+
+    picojson::value result = picojson::value(picojson::object());
+    PlatformResult ret = SysteminfoUtils::GetPropertyValue(kPropertyIdCameraFlash, true, result);
+    if (ret.IsSuccess()) {
+      ReportSuccess(result,response->get<picojson::object>());
+      instance.PostMessage(response->serialize().c_str());
+    }
+}
+
 } // namespace systeminfo
 } // namespace extension