[SystemInfo][BATTERY] Using vconf for Tizen mobile
authorfyraimar <fyraimar@gmail.com>
Fri, 16 Aug 2013 07:22:07 +0000 (15:22 +0800)
committerHalton Huo <halton.huo@intel.com>
Fri, 16 Aug 2013 03:27:15 +0000 (11:27 +0800)
system_info/system_info.gyp
system_info/system_info_battery_desktop.cc [moved from system_info/system_info_battery.cc with 100% similarity]
system_info/system_info_battery_mobile.cc [new file with mode: 0644]
system_info/system_info_build_mobile.cc
system_info/system_info_locale_mobile.cc

index 55d9c68..d65c17d 100644 (file)
@@ -42,8 +42,9 @@
       ],
       'sources': [
         'system_info_api.js',
-        'system_info_battery.cc',
         'system_info_battery.h',
+        'system_info_battery_desktop.cc',
+        'system_info_battery_mobile.cc',
         'system_info_build.h',
         'system_info_build_desktop.cc',
         'system_info_build_mobile.cc',
diff --git a/system_info/system_info_battery_mobile.cc b/system_info/system_info_battery_mobile.cc
new file mode 100644 (file)
index 0000000..6e2dced
--- /dev/null
@@ -0,0 +1,92 @@
+// Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "system_info/system_info_battery.h"
+
+#include <vconf.h>
+#include <string>
+
+#include "common/picojson.h"
+#include "system_info/system_info_utils.h"
+
+SysInfoBattery::SysInfoBattery(ContextAPI* api)
+    : level_(0.0),
+      charging_(false),
+      stopping_(false) {
+  api_ = api;
+}
+
+SysInfoBattery::~SysInfoBattery() {
+}
+
+void SysInfoBattery::Get(picojson::value& error,
+                         picojson::value& data) {
+  if (!Update(error)) {
+    system_info::SetPicoJsonObjectValue(error, "message",
+        picojson::value("Battery not found."));
+    return;
+  }
+
+  SetData(data);
+  system_info::SetPicoJsonObjectValue(error, "message", picojson::value(""));
+}
+
+bool SysInfoBattery::Update(picojson::value& error) {
+  int level_info = 0;
+  int charging_info = 0;
+
+  if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY, &level_info) != 0)
+    return false;
+
+  level_ = static_cast<double>(level_info)/100;
+
+  if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, &charging_info) != 0)
+    return false;
+
+  charging_ = (charging_info == 0) ? false : true;
+
+  return true;
+}
+
+gboolean SysInfoBattery::OnUpdateTimeout(gpointer user_data) {
+  SysInfoBattery* instance = static_cast<SysInfoBattery*>(user_data);
+
+  if (instance->stopping_) {
+    instance->stopping_ = false;
+    return FALSE;
+  }
+
+  double old_level = instance->level_;
+  double old_charging = instance->charging_;
+  picojson::value error = picojson::value(picojson::object());
+  if (!instance->Update(error)) {
+    // Fail to update, wait for next round
+    return TRUE;
+  }
+
+  if ((old_level != instance->level_) ||
+      (old_charging != instance->charging_)) {
+    picojson::value output = picojson::value(picojson::object());
+    picojson::value data = picojson::value(picojson::object());
+
+    instance->SetData(data);
+    system_info::SetPicoJsonObjectValue(output, "cmd",
+        picojson::value("SystemInfoPropertyValueChanged"));
+    system_info::SetPicoJsonObjectValue(output, "prop",
+        picojson::value("BATTERY"));
+    system_info::SetPicoJsonObjectValue(output, "data", data);
+
+    std::string result = output.serialize();
+    instance->api_->PostMessage(result.c_str());
+  }
+
+  return TRUE;
+}
+
+void SysInfoBattery::SetData(picojson::value& data) {
+  system_info::SetPicoJsonObjectValue(data, "level",
+      picojson::value(level_));
+  system_info::SetPicoJsonObjectValue(data, "isCharging",
+      picojson::value(charging_));
+}
index 0c5a704..1618017 100644 (file)
@@ -52,15 +52,25 @@ bool SysInfoBuild::UpdateHardware() {
       != SYSTEM_INFO_ERROR_NONE)
     return false;
 
-  model_ = hardware_info;
-  free(hardware_info);
+  if (hardware_info) {
+    model_ = hardware_info;
+    free(hardware_info);
+    hardware_info = NULL;
+  } else {
+    model_ = "";
+  }
 
   if (system_info_get_value_string(SYSTEM_INFO_KEY_MANUFACTURER, &hardware_info)
       != SYSTEM_INFO_ERROR_NONE)
     return false;
 
-  manufacturer_ = hardware_info;
-  free(hardware_info);
+  if (hardware_info) {
+    manufacturer_ = hardware_info;
+    free(hardware_info);
+    hardware_info = NULL;
+  } else {
+    manufacturer_ = "";
+  }
 
   return true;
 }
@@ -72,8 +82,13 @@ bool SysInfoBuild::UpdateOSBuild() {
       != SYSTEM_INFO_ERROR_NONE)
     return false;
 
-  buildversion_ = build_info;
-  free(build_info);
+  if (build_info) {
+    buildversion_ = build_info;
+    free(build_info);
+    build_info = NULL;
+  } else {
+    buildversion_ = "";
+  }
 
   return true;
 }
index d15320f..43ca17b 100644 (file)
@@ -51,8 +51,13 @@ bool SysInfoLocale::UpdateLanguage() {
       != RUNTIME_INFO_ERROR_NONE)
     return false;
 
-  language_ = language_info;
-  free(language_info);
+  if (language_info) {
+    language_ = language_info;
+    free(language_info);
+    language_info = NULL;
+  } else {
+    language_ ="";
+  }
 
   return true;
 }
@@ -64,8 +69,13 @@ bool SysInfoLocale::UpdateCountry() {
       != RUNTIME_INFO_ERROR_NONE)
     return false;
 
-  country_  = country_info;
-  free(country_info);
+  if (country_info) {
+    country_ = country_info;
+    free(country_info);
+    country_info = NULL;
+  } else {
+    country_ ="";
+  }
 
   return true;
 }