tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Systeminfo / SystemInfoBattery.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <string>
19 #include <vconf.h>
20 #include <PlatformException.h>
21 #include <Logger.h>
22
23 #include "SystemInfoBattery.h"
24 #include "SystemInfoUtil.h"
25
26 namespace DeviceAPI {
27 namespace SystemInfo {
28
29 namespace {
30     const double REMANING_BATTERY_CHARGE_MAX = 100.0;
31 }
32
33 SystemInfoBattery::SystemInfoBattery()
34 {
35     LOGD("Entered");
36
37     m_level = fetchLevel();
38     m_is_charging = fetchIsCharging();
39 }
40
41 SystemInfoBattery::~SystemInfoBattery()
42 {
43     LOGD("Entered");
44 }
45
46 double SystemInfoBattery::getLevel() const
47 {
48     return m_level;
49 }
50
51 bool SystemInfoBattery::isCharging() const
52 {
53     return m_is_charging;
54 }
55
56 double SystemInfoBattery::fetchLevel()
57 {
58     LOGD("Entered");
59     int value = 0;
60
61     int ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY, &value);
62     if (ret != VCONF_OK) {
63         std::string log_msg = "Failed to get battery capacity";
64         LOGE("%s", log_msg.c_str());
65         SystemInfoUtil::throwSystemInfoException(0, log_msg);
66     }
67
68     return static_cast<double>(value) / REMANING_BATTERY_CHARGE_MAX;
69 }
70
71 bool SystemInfoBattery::fetchIsCharging()
72 {
73     LOGD("Entered");
74     int value = 0;
75
76     int ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, &value);
77     if (ret != VCONF_OK) {
78         std::string log_msg = "Failed to get battery change";
79         LOGE("%s", log_msg.c_str());
80         SystemInfoUtil::throwSystemInfoException(0, log_msg);
81     }
82
83     return (0 != value);
84 }
85
86 } // SystemInfo
87 } // DeviceAPI