2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FSys_PowerManagerImpl.cpp
19 * @brief This is the implementation file for _PowerManagerImpl class.
22 #include <unique_ptr.h>
27 #include <runtime_info.h>
31 #include <FAppIActiveAppEventListener.h>
32 #include <FBaseSysLog.h>
33 #include <FBaseColArrayListT.h>
34 #include <FBaseColIMapEnumerator.h>
35 #include <FSysIBatteryEventListener.h>
36 #include <FSysIChargingEventListener.h>
37 #include <FSysIScreenEventListener.h>
38 #include <FSysIBootEventListener.h>
39 #include <FSys_IPowerManagerEventListener.h>
41 #include <FApp_AppImpl.h>
42 #include <FApp_AppInfo.h>
43 #include <FApp_AppManagerImpl.h>
44 #include <FBase_NativeError.h>
45 #include <FIo_AppServiceIpcMessages.h>
46 #include <FIo_IpcClient.h>
47 #include <FSys_Types.h>
48 #include <FSys_BatteryImpl.h>
49 #include <FSys_DeviceManagerImpl.h>
50 #include <FSys_PowerManagerImpl.h>
54 using namespace Tizen::App;
55 using namespace Tizen::Base;
56 using namespace Tizen::Base::Collection;
57 using namespace Tizen::Io;
59 namespace Tizen { namespace System
62 static const int _DEVICE_CPU = 1;
63 static const int _DEVICE_POWER_LEVEL_ON = 1;
64 static const int _DEVICE_POWER_LEVEL_OFF = 0;
65 static const int _DEACTIVATED_BRIGHTNESS_CONTROL = -1;
66 static const float _BRIGHTNESS_RESOLUTION = 10.0;
67 static const wchar_t* POWER_MANAGER_SERVICE_ID = L"osp.sys.ipcserver.powermanager";
69 #ifndef VCONFKEY_SERVICE_READY
70 #define VCONFKEY_SERVICE_READY "memory/deviced/boot_power_on"
73 bool _activeApp = false;
74 bool _deviceCpuPowerOn = false;
75 bool _keepCpuAwake = false;
76 bool _keepScreenOnState = false;
78 IBatteryEventListener* pIBatteryEventListener = null;
80 ArrayListT<IScreenEventListener*>* __pSceenEventList = null;
81 ArrayListT<IChargingEventListener*>* __pChargingEventList = null;
82 ArrayListT<_IPowerManagerEventListener*> __powerEventList;
83 ArrayListT<IBootEventListener*>* __pBootEventList = null;
85 bool _PowerManagerImpl::_InitPowerManager = false;
88 class _ActiveEventListener
89 : public IActiveAppEventListener
92 virtual void OnActiveAppChanged(const Tizen::App::AppId& appId);
96 _ActiveEventListener::OnActiveAppChanged(const Tizen::App::AppId& appId)
99 SysLog(NID_SYS, "Active App is %ls.", appId.GetPointer());
101 if(appId == _AppInfo::GetApplicationId())
109 if (_keepScreenOnState == true) //In case of Screen power control, it has to be released automatically, when application goes deactive state.
111 ret = pm_unlock_state(LCD_NORMAL, GOTO_STATE_NOW);
112 SysTryReturnVoidResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to unlock the power state");
113 _keepScreenOnState = false;
116 if (_keepCpuAwake == true || _deviceCpuPowerOn == true) //In case of CPU power control, it has to be keep despite of deactive state.
118 ret = power_lock_state(POWER_STATE_SCREEN_OFF, 0);
119 SysTryReturnVoidResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to lock the power state");
124 _ActiveEventListener* pActiveEventListener = null;
127 BatteryLevelVConfCallback(keynode_t* node, void* userData)
129 SysLog(NID_SYS, "Battery Warn Level is changed");
130 BatteryLevel batteryLevel;
132 if (strcmp(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, vconf_keynode_get_name(node)) == 0)
135 if(vconf_get_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, &level) >= 0)
139 case VCONFKEY_SYSMAN_BAT_LEVEL_EMPTY:
140 batteryLevel = BATTERY_EMPTY;
142 case VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL:
143 batteryLevel = BATTERY_CRITICAL;
145 case VCONFKEY_SYSMAN_BAT_LEVEL_LOW:
146 batteryLevel = BATTERY_LOW;
148 case VCONFKEY_SYSMAN_BAT_LEVEL_HIGH:
149 batteryLevel = BATTERY_HIGH;
151 case VCONFKEY_SYSMAN_BAT_LEVEL_FULL:
152 batteryLevel = BATTERY_FULL;
155 SysLogException(NID_SYS, E_SYSTEM, "Unavailable battery level is required.");
158 _AppImpl* pAppImpl = _AppImpl::GetInstance();
161 SysLog(NID_SYS, "Battery Level event is forwarded");
162 pAppImpl->OnBatteryLevelChanged(batteryLevel);
169 DeviceBatteryCallback(int percent, void* userData)
171 SysLog(NID_SYS, "Battery Level is called %d", percent);
173 if (pIBatteryEventListener != null)
175 pIBatteryEventListener->OnBatteryLevelChangedInPercentage(percent);
180 RuntimeInfoChangedCallback(runtime_info_key_e key, void* userData)
184 if(_AppInfo::GetAppState() == RUNNING)
186 if (__pChargingEventList != null)
188 bool charging = false;
189 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, &charging);
192 for(int i = 0; i < __pChargingEventList->GetCount(); i++)
194 result r = E_SUCCESS;
195 IChargingEventListener* pIChargingEventListener = null;
197 r = __pChargingEventList->GetAt(i, pIChargingEventListener);
199 if (IsFailed(r) == false)
201 pIChargingEventListener->OnChargingStateChanged(charging);
211 PowerChangedCallback(power_state_e state, void* userData)
213 std::unique_ptr< IEnumeratorT<_IPowerManagerEventListener*> > pEnumerator(__powerEventList.GetEnumeratorN());
214 _IPowerManagerEventListener* pIPowerManagerEventListener = null;
216 if (__pSceenEventList != null)
218 if (state == POWER_STATE_SCREEN_OFF)
220 for (int i = 0; i < __pSceenEventList->GetCount(); i++)
222 IScreenEventListener* pIScreenEventListener = null;
223 result r = E_SUCCESS;
224 r = __pSceenEventList->GetAt(i, pIScreenEventListener);
225 if (IsFailed(r) == false)
227 pIScreenEventListener->OnScreenOff();
231 if(pEnumerator != null)
233 while(pEnumerator->MoveNext() == E_SUCCESS)
235 pEnumerator->GetCurrent(pIPowerManagerEventListener);
236 if(pIPowerManagerEventListener != null)
238 pIPowerManagerEventListener->OnScreenStateChanged(false);
243 else if(state == POWER_STATE_NORMAL)
245 for (int i = 0; i < __pSceenEventList->GetCount(); i++)
247 IScreenEventListener* pIScreenEventListener = null;
248 result r = E_SUCCESS;
249 r = __pSceenEventList->GetAt(i, pIScreenEventListener);
250 if (IsFailed(r) == false)
252 pIScreenEventListener->OnScreenOn();
256 if(pEnumerator != null)
258 while(pEnumerator->MoveNext() == E_SUCCESS)
260 pEnumerator->GetCurrent(pIPowerManagerEventListener);
261 if(pIPowerManagerEventListener != null)
263 pIPowerManagerEventListener->OnScreenStateChanged(true);
272 ScreenEventVConfCallback(keynode_t* node, void* userData)
275 int sysBrightness = 0;
277 if (__pSceenEventList == null)
279 SysLogException(NID_SYS, E_SYSTEM, "SceenEventList is not initialized.");
282 errorCode = vconf_get_int(VCONFKEY_PM_CURRENT_BRIGHTNESS, &sysBrightness);
286 SysLogException(NID_SYS, E_SYSTEM, "It failed to get Screen brightness");
290 for (int i = 0; i < __pSceenEventList->GetCount(); i++)
292 IScreenEventListener* pIScreenEventListener = null;
293 __pSceenEventList->GetAt(i, pIScreenEventListener);
294 if (pIScreenEventListener != null)
297 int maxBrightness = -1;
298 int ret = device_get_max_brightness(0, &maxBrightness);
301 SysLogException(NID_SYS, E_SYSTEM, "It failed to get the device max brightness");
304 sysBrightness = (sysBrightness == 0) ? 1 : sysBrightness;
305 brightness = (int)ceil(((_BRIGHTNESS_RESOLUTION / (float) maxBrightness)) * sysBrightness);
306 pIScreenEventListener->OnScreenBrightnessChanged(brightness);
311 _PowerManagerImpl::_PowerManagerImpl(void)
315 _PowerManagerImpl::~_PowerManagerImpl(void)
320 _PowerManagerImpl::Init()
322 if(_InitPowerManager == false)
324 __powerEventList.Construct();
325 pActiveEventListener = new (std::nothrow) _ActiveEventListener();
326 _AppManagerImpl* pAppManager = _AppManagerImpl::GetInstance();
327 pAppManager->AddActiveAppEventListener(*pActiveEventListener);
328 _InitPowerManager = true;
333 _PowerManagerImpl::Term()
335 if(_InitPowerManager == true)
337 _AppManagerImpl* pAppManager = _AppManagerImpl::GetInstance();
338 pAppManager->RemoveActiveAppEventListener(*pActiveEventListener);
339 delete(pActiveEventListener);
340 pActiveEventListener = null;
342 _InitPowerManager = false;
347 _PowerManagerImpl::IsActive(void)
350 result r = E_SUCCESS;
351 _AppManagerImpl* pAppManagerImpl = _AppManagerImpl::GetInstance();
353 if(pAppManagerImpl == null)
359 r = pAppManagerImpl->GetActiveApp(appId);
363 SysLog(NID_SYS, "current active is:%ls, required:%ls", appId.GetPointer(), _AppInfo::GetApplicationId().GetPointer());
364 if(appId == _AppInfo::GetApplicationId())
382 _PowerManagerImpl::KeepScreenOnState(bool keepOn, bool dimming)
385 unsigned int state = LCD_DIM, flag = GOTO_STATE_NOW;
386 _PowerManagerImpl::Init();
387 SysLog(NID_SYS, "Power control option: keep %d, dimming %d", keepOn, dimming);
389 if (keepOn == true && IsActive() == true)
394 if(power_get_state() == POWER_STATE_NORMAL)
396 flag = STAY_CUR_STATE;
403 ret = pm_lock_state(state, flag, 0);
404 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to lock the screen state");
405 _keepScreenOnState = true;
409 ret = pm_unlock_state(LCD_NORMAL, GOTO_STATE_NOW);
410 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to unlock the screen state");
411 _keepScreenOnState = false;
414 SysLog(NID_SYS, "change screen power policy: keepOn(%d) and dimming(%d)", keepOn, dimming);
420 _PowerManagerImpl::KeepCpuAwake(bool enable)
423 _PowerManagerImpl::Init();
427 ret = power_lock_state(POWER_STATE_SCREEN_OFF, 0);
428 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to lock the power state");
429 _keepCpuAwake = true;
433 if(_deviceCpuPowerOn == false)
435 ret = power_unlock_state(POWER_STATE_SCREEN_OFF);
436 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to unlock the power state");
438 _keepCpuAwake = false;
441 SysLog(NID_SYS, "change cpu power policy: keepAwake(%s)", enable ? "true" : "false");
446 _PowerManagerImpl::InitBatteryEvent(void)
448 int ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, BatteryLevelVConfCallback, null);
449 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to set the battery event");
455 _PowerManagerImpl::OnForeground(void)
461 _PowerManagerImpl::OnBackground(void)
467 _PowerManagerImpl::RestoreScreenBrightness(void)
469 result r = E_SUCCESS;
471 ArrayList requestMessage;
472 ArrayList responseMessage;
474 SysTryReturn(NID_SYS, IsActive() == true, E_SUCCESS, r, "It is not active app.");
476 unique_ptr<_IpcClient> pIpcClient (new (std::nothrow) _IpcClient());
477 SysTryReturn(NID_SYS, pIpcClient != null, E_OUT_OF_MEMORY, r, "It is failed to create IPC instance.");
479 SysLog(NID_SYS, "PowerManager is serviced by common-service");
480 r = pIpcClient->Construct(POWER_MANAGER_SERVICE_ID);
481 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to create IpcClient", GetErrorMessage(r));
483 requestMessage.Construct();
484 responseMessage.Construct();
486 String serviceId = _SYSTEM_SERVICE_ID;
487 String commandId = _SYSTEM_COMMAND_RESTORE_BRIGHTNESS;
489 requestMessage.Add(serviceId);
490 requestMessage.Add(commandId);
492 unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
493 SysTryReturnResult(NID_SYS, pMsg != null, E_OUT_OF_MEMORY, "It is failed to create Ipc message");
495 r = pIpcClient->SendRequest(pMsg.get());
496 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to ipc message", GetErrorMessage(r));
498 String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
500 SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "It is failed to receive Ipc response.");
501 SysTryReturnResult(NID_SYS, *pResult == _SYSTEM_RESULT_OK, E_SYSTEM, "It is failed to set brightness.");
503 responseMessage.RemoveAll(true);
508 _PowerManagerImpl::SetScreenBrightness(int brightness)
511 int maxBrightness = 0;
512 int actualBrightness = 0;
513 result r = E_SUCCESS;
515 SysTryReturnResult(NID_SYS, (brightness > 0 && brightness < 11), E_OUT_OF_RANGE,
516 "[E_OUT_OF_RANGE] The specified brightness is out of range.");
518 SysLog(NID_SYS, "Set brightness (%d)", brightness);
520 ret = device_get_max_brightness(0, &maxBrightness);
521 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to get the device max brightness");
523 actualBrightness = maxBrightness * brightness / _BRIGHTNESS_RESOLUTION;
525 ArrayList requestMessage;
526 ArrayList responseMessage;
528 unique_ptr<_IpcClient> pIpcClient (new (std::nothrow) _IpcClient());
529 SysTryReturn(NID_SYS, pIpcClient != null, E_OUT_OF_MEMORY, r, "It is failed to create IPC instance.");
531 SysLog(NID_SYS, "PowerManager is serviced by common-service");
532 r = pIpcClient->Construct(POWER_MANAGER_SERVICE_ID);
533 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to create IpcClient", GetErrorMessage(r));
535 requestMessage.Construct();
536 responseMessage.Construct();
538 String serviceId = _SYSTEM_SERVICE_ID;
539 String commandId = _SYSTEM_COMMAND_CHANGE_BRIGHTNESS;
540 String brightnessValue;
541 brightnessValue.Append(actualBrightness);
543 requestMessage.Add(serviceId);
544 requestMessage.Add(commandId);
545 requestMessage.Add(brightnessValue);
547 unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
548 SysTryReturnResult(NID_SYS, pMsg != null, E_OUT_OF_MEMORY, "It is failed to create Ipc message");
550 r = pIpcClient->SendRequest(pMsg.get());
551 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to ipc message", GetErrorMessage(r));
553 String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
555 SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "It is failed to receive Ipc response.");
556 SysTryReturnResult(NID_SYS, *pResult == _SYSTEM_RESULT_OK, E_SYSTEM, "It is failed to set brightness.");
558 responseMessage.RemoveAll(true);
563 _PowerManagerImpl::GetScreenBrightness(void)
567 int maxBrightness = -1;
571 ret = device_get_brightness(0, &brightness);
574 SysLogException(NID_SYS, E_SYSTEM, "It failed to get the device brightness");
575 SetLastResult(E_SYSTEM);
576 return _DEACTIVATED_BRIGHTNESS_CONTROL;
584 SysLog(NID_SYS, "System Brightness is %d", brightness);
585 ret = device_get_max_brightness(0, &maxBrightness);
588 SysLogException(NID_SYS, E_SYSTEM, "It failed to get the device max brightness");
589 SetLastResult(E_SYSTEM);
590 return _DEACTIVATED_BRIGHTNESS_CONTROL;
592 return ceil(((_BRIGHTNESS_RESOLUTION / (float) maxBrightness)) * brightness);
596 _PowerManagerImpl::IsScreenOn(void)
598 power_state_e powerState = power_get_state();
600 if (powerState == POWER_STATE_SCREEN_OFF)
611 _PowerManagerImpl::TurnScreenOn(void)
613 result r = E_SUCCESS;
614 int ret = power_wakeup(true);
615 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to unlock the normal screen state");
616 ret = pm_change_state(LCD_NORMAL);
617 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to change to the normal screen state");
623 _PowerManagerImpl::TurnScreenOff(void)
625 result r = E_SUCCESS;
626 int ret = pm_change_state(LCD_OFF);
628 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to unlock the screen off screen state");
634 _PowerManagerImpl::SetScreenEventListener(IScreenEventListener& listener)
636 result r = E_SUCCESS;
638 r = AddScreenEventListener(listener, true);
642 SysLog(NID_SYS, "It failed to register ScreenEventListener");
645 SysLog(NID_SYS,"It successed to register ScreenEventListener");
650 _PowerManagerImpl::AddScreenEventListener(IScreenEventListener& listener, bool isSet)
652 result r = E_SUCCESS;
654 if (__pSceenEventList == null)
656 std::unique_ptr< ArrayListT<IScreenEventListener*> > pSceenEventList(new ArrayListT<IScreenEventListener*>);
657 SysTryReturnResult(NID_SYS, pSceenEventList, E_SYSTEM, "Memory allocation failed");
658 pSceenEventList->Construct();
660 errorCode = vconf_notify_key_changed(VCONFKEY_PM_CURRENT_BRIGHTNESS, ScreenEventVConfCallback, null);
661 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It failed to add ScreenEvent listener on VCONFKEY_PM_CURRENT_BRIGHTNESS.");
663 errorCode = power_set_changed_cb(PowerChangedCallback, null);
664 if(errorCode != POWER_ERROR_NONE)
666 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register power callback. So unregister VCONFKEY_PM_CURRENT_BRIGHTNESS also.");
667 vconf_ignore_key_changed(VCONFKEY_PM_CURRENT_BRIGHTNESS, ScreenEventVConfCallback);
669 SysTryReturnResult(NID_SYS, errorCode == POWER_ERROR_NONE, E_SYSTEM, "It failed to add ScreenEvent listener.");
670 __pSceenEventList = pSceenEventList.release();
671 SysLog(NID_SYS,"It successed to register power_set_changed_cb");
674 if (isSet == true && __pSceenEventList->GetCount() != 0)
676 r = __pSceenEventList->SetAt(&listener, 0);
680 r = __pSceenEventList->Add(&listener);
687 _PowerManagerImpl::RemoveScreenEventListener(IScreenEventListener& listener)
689 result r = E_SUCCESS;
691 SysTryReturnResult(NID_SYS,__pSceenEventList != null, E_OBJ_NOT_FOUND, "IScreenEventListener list is empty");
693 r = __pSceenEventList->Remove(&listener);
694 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_OBJ_NOT_FOUND, "listener is not exist.");
700 _PowerManagerImpl::SetChargingEventListener(IChargingEventListener& listener)
702 result r = E_SUCCESS;
704 r = AddChargingEventListener(listener, true);
708 SysLog(NID_SYS, "It failed to register battery charging event");
715 _PowerManagerImpl::AddChargingEventListener(IChargingEventListener& listener, bool isSet)
717 result r = E_SUCCESS;
719 if (__pChargingEventList == null)
721 int errorCode = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, RuntimeInfoChangedCallback, null);
722 SysTryReturnResult(NID_SYS, errorCode == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to register RUNTIME_INFO_KEY_BATTERY_IS_CHARGING.");
724 __pChargingEventList = new ArrayListT<IChargingEventListener*>;
725 SysTryReturnResult(NID_SYS, __pChargingEventList, E_SYSTEM, "Memory allocation failed");
726 r = __pChargingEventList->Construct();
729 delete __pChargingEventList;
730 __pChargingEventList = null;
732 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to construct charging event list.");
735 if (isSet == false || __pChargingEventList->GetCount() == 0)
737 r = __pChargingEventList->Add(&listener);
741 r = __pChargingEventList->SetAt(&listener, 0);
748 _PowerManagerImpl::RemoveChargingEventListener(IChargingEventListener& listener)
750 result r = E_SUCCESS;
752 SysTryReturnResult(NID_SYS,__pChargingEventList != null, E_OBJ_NOT_FOUND, "__pChargingEventList list is empty");
753 r = __pChargingEventList->Remove(&listener);
754 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_OBJ_NOT_FOUND, "listener is not exist.");
760 _PowerManagerImpl::SetBatteryEventListener(IBatteryEventListener* pListener)
764 if (pListener != null)
767 if(pIBatteryEventListener == null)
769 ret = device_battery_set_cb(DeviceBatteryCallback, null);
772 SysLog(NID_SYS, "It failed to register battery event");
775 pIBatteryEventListener = pListener;
779 pIBatteryEventListener = null;
780 ret = device_battery_unset_cb();
783 SysLog(NID_SYS, "It failed to unregister battery event");
791 _PowerManagerImpl::PowerControl(int deviceId, int level)
794 if(deviceId == _DEVICE_CPU)
796 if(level == _DEVICE_POWER_LEVEL_ON)
798 ret = power_lock_state(POWER_STATE_SCREEN_OFF, 0);
799 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to lock the power state");
800 _deviceCpuPowerOn = true;
804 else if(level == _DEVICE_POWER_LEVEL_OFF)
806 if(_keepScreenOnState == false)
808 ret = power_unlock_state(POWER_STATE_SCREEN_OFF);
809 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to unlock the power state");
811 _deviceCpuPowerOn = false;
819 _PowerManagerImpl::GetCurrentBatteryLevelInPercentage(void)
822 int batteryLevel = 0;
824 ret = device_battery_get_percent(&batteryLevel);
827 SetLastResult(E_SYSTEM);
834 _PowerManagerImpl::GetCurrentBatteryLevel(void)
836 int batteryLevel = 0;
837 BatteryLevel level = BATTERY_EMPTY;
838 int ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, &batteryLevel);
841 SysLogException(NID_SYS, E_SYSTEM, "It is failed to get battery level.");
847 case VCONFKEY_SYSMAN_BAT_LEVEL_FULL:
848 level = BATTERY_LEVEL_FULL;
850 case VCONFKEY_SYSMAN_BAT_LEVEL_HIGH:
851 level = BATTERY_LEVEL_HIGH;
853 case VCONFKEY_SYSMAN_BAT_LEVEL_LOW:
854 level = BATTERY_LEVEL_LOW;
856 case VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL:
857 level = BATTERY_LEVEL_CRITICAL;
860 level = BATTERY_LEVEL_EMPTY;
868 _PowerManagerImpl::IsCharging(void)
870 bool chargeState = false;
872 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, &chargeState);
875 SetLastResult(E_SYSTEM);
883 _PowerManagerImpl::Add_IPowerManagerEventListener(_IPowerManagerEventListener* listener)
885 _PowerManagerImpl::Init();
886 __powerEventList.Add(listener);
891 _PowerManagerImpl::Remove_IPowerManagerEventListener(_IPowerManagerEventListener* listener)
893 _PowerManagerImpl::Init();
894 __powerEventList.Remove(listener);
899 BootEventVconfCallback(keynode_t* node, void* userData)
901 if (__pBootEventList == null)
903 SysLogException(NID_SYS, E_SYSTEM, "BootEventList is not initialized.");
908 const int errorCode = vconf_get_int(VCONFKEY_SERVICE_READY, &bootReady);
912 SysLogException(NID_SYS, E_SYSTEM, "It failed to get Boot event status");
916 std::unique_ptr< IEnumeratorT<IBootEventListener*> > pEnum(__pBootEventList->GetEnumeratorN());
920 while (pEnum->MoveNext() == E_SUCCESS)
922 IBootEventListener* pListener = null;
923 pEnum->GetCurrent(pListener);
925 pListener->OnBootCompleted();
929 SysLog(NID_SYS, "Finished invoking boot complete event listener.");
933 _PowerManagerImpl::AddBootEventListener(IBootEventListener& listener)
935 if (__pBootEventList == null)
937 std::unique_ptr< ArrayListT<IBootEventListener*> > pBootEventList(new ArrayListT<IBootEventListener*>);
938 SysTryReturnResult(NID_SYS, pBootEventList, E_SYSTEM, "Memory allocation failed");
939 pBootEventList->Construct();
942 errorCode = vconf_notify_key_changed(VCONFKEY_SERVICE_READY, BootEventVconfCallback, null);
943 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It failed to add BootEvent listener.");
945 __pBootEventList = pBootEventList.release();
946 SysLog(NID_SYS, "It successed to register boot event listener.");
949 return __pBootEventList->Add(&listener);
953 _PowerManagerImpl::RemoveBootEventListener(IBootEventListener& listener)
955 result r = E_SUCCESS;
957 SysTryReturnResult(NID_SYS,__pBootEventList != null, E_OBJ_NOT_FOUND, "IBootEventListener list is empty");
959 r = __pBootEventList->Remove(&listener);
960 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_OBJ_NOT_FOUND, "IBootEventListener list is empty");
966 _PowerManagerImpl::GetInstance(PowerManager& powerManager)
968 return powerManager.__pPowerManagerImpl;
971 const _PowerManagerImpl*
972 _PowerManagerImpl::GetInstance(const PowerManager& powerManager)
974 return powerManager.__pPowerManagerImpl;