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 _APPID_LENGTH = 10;
66 static const int _DEACTIVATED_BRIGHTNESS_CONTROL = -1;
67 static const float _BRIGHTNESS_RESOLUTION = 10.0;
68 static const wchar_t* POWER_MANAGER_SERVICE_ID = L"osp.sys.ipcserver.powermanager";
70 #ifndef VCONFKEY_SERVICE_READY
71 #define VCONFKEY_SERVICE_READY "memory/deviced/boot_power_on"
74 bool _activeApp = false;
75 bool _deviceCpuPowerOn = false;
76 bool _keepCpuAwake = false;
77 bool _keepScreenOnState = false;
79 IBatteryEventListener* pIBatteryEventListener = null;
81 ArrayListT<IScreenEventListener*>* __pSceenEventList = null;
82 ArrayListT<IChargingEventListener*>* __pChargingEventList = null;
83 ArrayListT<_IPowerManagerEventListener*> __powerEventList;
84 ArrayListT<IBootEventListener*>* __pBootEventList = null;
86 bool _PowerManagerImpl::_InitPowerManager = false;
89 class _ActiveEventListener
90 : public IActiveAppEventListener
93 virtual void OnActiveAppChanged(const Tizen::App::AppId& appId);
97 _ActiveEventListener::OnActiveAppChanged(const Tizen::App::AppId& appId)
100 SysLog(NID_SYS, "Active App is %ls.", appId.GetPointer());
102 if(appId == _AppInfo::GetApplicationId())
110 if (_keepScreenOnState == true) //In case of Screen power control, it has to be released automatically, when application goes deactive state.
112 ret = pm_unlock_state(LCD_NORMAL, GOTO_STATE_NOW);
113 SysTryReturnVoidResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to unlock the power state");
114 _keepScreenOnState = false;
117 if (_keepCpuAwake == true || _deviceCpuPowerOn == true) //In case of CPU power control, it has to be keep despite of deactive state.
119 ret = power_lock_state(POWER_STATE_SCREEN_OFF, 0);
120 SysTryReturnVoidResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to lock the power state");
125 _ActiveEventListener* pActiveEventListener = null;
128 BatteryLevelVConfCallback(keynode_t* node, void* userData)
130 SysLog(NID_SYS, "Battery Warn Level is changed");
131 BatteryLevel batteryLevel;
133 if (strcmp(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, vconf_keynode_get_name(node)) == 0)
136 if(vconf_get_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, &level) >= 0)
140 case VCONFKEY_SYSMAN_BAT_LEVEL_EMPTY:
141 batteryLevel = BATTERY_EMPTY;
143 case VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL:
144 batteryLevel = BATTERY_CRITICAL;
146 case VCONFKEY_SYSMAN_BAT_LEVEL_LOW:
147 batteryLevel = BATTERY_LOW;
149 case VCONFKEY_SYSMAN_BAT_LEVEL_HIGH:
150 batteryLevel = BATTERY_HIGH;
152 case VCONFKEY_SYSMAN_BAT_LEVEL_FULL:
153 batteryLevel = BATTERY_FULL;
156 SysLogException(NID_SYS, E_SYSTEM, "Unavailable battery level is required.");
159 _AppImpl* pAppImpl = _AppImpl::GetInstance();
162 SysLog(NID_SYS, "Battery Level event is forwarded");
163 pAppImpl->OnBatteryLevelChanged(batteryLevel);
170 DeviceBatteryCallback(int percent, void* userData)
172 SysLog(NID_SYS, "Battery Level is called %d", percent);
174 if (pIBatteryEventListener != null)
176 pIBatteryEventListener->OnBatteryLevelChangedInPercentage(percent);
181 RuntimeInfoChangedCallback(runtime_info_key_e key, void* userData)
185 if(_AppInfo::GetAppState() == RUNNING)
187 if (__pChargingEventList != null)
189 bool charging = false;
190 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, &charging);
193 for(int i = 0; i < __pChargingEventList->GetCount(); i++)
195 result r = E_SUCCESS;
196 IChargingEventListener* pIChargingEventListener = null;
198 r = __pChargingEventList->GetAt(i, pIChargingEventListener);
200 if (IsFailed(r) == false)
202 pIChargingEventListener->OnChargingStateChanged(charging);
212 PowerChangedCallback(power_state_e state, void* userData)
214 std::unique_ptr< IEnumeratorT<_IPowerManagerEventListener*> > pEnumerator(__powerEventList.GetEnumeratorN());
215 _IPowerManagerEventListener* pIPowerManagerEventListener = null;
217 if (__pSceenEventList != null)
219 if (state == POWER_STATE_SCREEN_OFF)
221 for (int i = 0; i < __pSceenEventList->GetCount(); i++)
223 IScreenEventListener* pIScreenEventListener = null;
224 result r = E_SUCCESS;
225 r = __pSceenEventList->GetAt(i, pIScreenEventListener);
226 if (IsFailed(r) == false)
228 pIScreenEventListener->OnScreenOff();
232 if(pEnumerator != null)
234 while(pEnumerator->MoveNext() == E_SUCCESS)
236 pEnumerator->GetCurrent(pIPowerManagerEventListener);
237 if(pIPowerManagerEventListener != null)
239 pIPowerManagerEventListener->OnScreenStateChanged(false);
244 else if(state == POWER_STATE_NORMAL)
246 for (int i = 0; i < __pSceenEventList->GetCount(); i++)
248 IScreenEventListener* pIScreenEventListener = null;
249 result r = E_SUCCESS;
250 r = __pSceenEventList->GetAt(i, pIScreenEventListener);
251 if (IsFailed(r) == false)
253 pIScreenEventListener->OnScreenOn();
257 if(pEnumerator != null)
259 while(pEnumerator->MoveNext() == E_SUCCESS)
261 pEnumerator->GetCurrent(pIPowerManagerEventListener);
262 if(pIPowerManagerEventListener != null)
264 pIPowerManagerEventListener->OnScreenStateChanged(true);
273 ScreenEventVConfCallback(keynode_t* node, void* userData)
276 int sysBrightness = 0;
278 if (__pSceenEventList == null)
280 SysLogException(NID_SYS, E_SYSTEM, "SceenEventList is not initialized.");
283 errorCode = vconf_get_int(VCONFKEY_PM_CURRENT_BRIGHTNESS, &sysBrightness);
287 SysLogException(NID_SYS, E_SYSTEM, "It failed to get Screen brightness");
291 for (int i = 0; i < __pSceenEventList->GetCount(); i++)
293 IScreenEventListener* pIScreenEventListener = null;
294 __pSceenEventList->GetAt(i, pIScreenEventListener);
295 if (pIScreenEventListener != null)
298 int maxBrightness = -1;
299 int ret = device_get_max_brightness(0, &maxBrightness);
302 SysLogException(NID_SYS, E_SYSTEM, "It failed to get the device max brightness");
305 sysBrightness = (sysBrightness == 0) ? 1 : sysBrightness;
306 brightness = (int)ceil(((_BRIGHTNESS_RESOLUTION / (float) maxBrightness)) * sysBrightness);
307 pIScreenEventListener->OnScreenBrightnessChanged(brightness);
312 _PowerManagerImpl::_PowerManagerImpl(void)
316 _PowerManagerImpl::~_PowerManagerImpl(void)
321 _PowerManagerImpl::Init()
323 if(_InitPowerManager == false)
325 __powerEventList.Construct();
326 pActiveEventListener = new (std::nothrow) _ActiveEventListener();
327 _AppManagerImpl* pAppManager = _AppManagerImpl::GetInstance();
328 pAppManager->AddActiveAppEventListener(*pActiveEventListener);
329 _InitPowerManager = true;
334 _PowerManagerImpl::Term()
336 if(_InitPowerManager == true)
338 _AppManagerImpl* pAppManager = _AppManagerImpl::GetInstance();
339 pAppManager->RemoveActiveAppEventListener(*pActiveEventListener);
340 delete(pActiveEventListener);
341 pActiveEventListener = null;
343 _InitPowerManager = false;
348 _PowerManagerImpl::IsActive(void)
351 result r = E_SUCCESS;
352 _AppManagerImpl* pAppManagerImpl = _AppManagerImpl::GetInstance();
354 if(pAppManagerImpl == null)
360 r = pAppManagerImpl->GetActiveApp(appId);
364 SysLog(NID_SYS, "current active is:%ls, required:%ls", appId.GetPointer(), _AppInfo::GetApplicationId().GetPointer());
365 if(appId == _AppInfo::GetApplicationId())
383 _PowerManagerImpl::KeepScreenOnState(bool keepOn, bool dimming)
386 unsigned int state = LCD_DIM, flag = GOTO_STATE_NOW;
387 _PowerManagerImpl::Init();
388 SysLog(NID_SYS, "Power control option: keep %d, dimming %d", keepOn, dimming);
390 if (keepOn == true && IsActive() == true)
395 if(power_get_state() == POWER_STATE_NORMAL)
397 flag = STAY_CUR_STATE;
404 ret = pm_lock_state(state, flag, 0);
405 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to lock the screen state");
406 _keepScreenOnState = true;
410 ret = pm_unlock_state(LCD_NORMAL, GOTO_STATE_NOW);
411 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to unlock the screen state");
412 _keepScreenOnState = false;
415 SysLog(NID_SYS, "change screen power policy: keepOn(%d) and dimming(%d)", keepOn, dimming);
421 _PowerManagerImpl::KeepCpuAwake(bool enable)
424 _PowerManagerImpl::Init();
428 ret = power_lock_state(POWER_STATE_SCREEN_OFF, 0);
429 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to lock the power state");
430 _keepCpuAwake = true;
434 if(_deviceCpuPowerOn == false)
436 ret = power_unlock_state(POWER_STATE_SCREEN_OFF);
437 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to unlock the power state");
439 _keepCpuAwake = false;
442 SysLog(NID_SYS, "change cpu power policy: keepAwake(%s)", enable ? "true" : "false");
447 _PowerManagerImpl::InitBatteryEvent(void)
449 int ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, BatteryLevelVConfCallback, null);
450 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to set the battery event");
456 _PowerManagerImpl::OnForeground(void)
462 _PowerManagerImpl::OnBackground(void)
468 _PowerManagerImpl::RestoreScreenBrightness(void)
470 result r = E_SUCCESS;
472 ArrayList requestMessage;
473 ArrayList responseMessage;
475 SysTryReturn(NID_SYS, IsActive() == true, E_SUCCESS, r, "It is not active app.");
477 unique_ptr<_IpcClient> pIpcClient (new (std::nothrow) _IpcClient());
478 SysTryReturn(NID_SYS, pIpcClient != null, E_OUT_OF_MEMORY, r, "It is failed to create IPC instance.");
480 if(Tizen::Io::File::IsFileExist(L"/opt/usr/etc/common_service_for_powermanager") == true)
482 SysLog(NID_SYS, "PowerManager is serviced by common-service");
483 r = pIpcClient->Construct(POWER_MANAGER_SERVICE_ID);
487 r = pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID);
489 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to create IpcClient", GetErrorMessage(r));
491 requestMessage.Construct();
492 responseMessage.Construct();
494 String serviceId = _SYSTEM_SERVICE_ID;
495 String commandId = _SYSTEM_COMMAND_RESTORE_BRIGHTNESS;
497 requestMessage.Add(serviceId);
498 requestMessage.Add(commandId);
500 unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
501 SysTryReturnResult(NID_SYS, pMsg != null, E_OUT_OF_MEMORY, "It is failed to create Ipc message");
503 r = pIpcClient->SendRequest(pMsg.get());
504 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to ipc message", GetErrorMessage(r));
506 String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
508 SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "It is failed to receive Ipc response.");
509 SysTryReturnResult(NID_SYS, *pResult == _SYSTEM_RESULT_OK, E_SYSTEM, "It is failed to set brightness.");
511 responseMessage.RemoveAll(true);
516 _PowerManagerImpl::SetScreenBrightness(int brightness)
519 int maxBrightness = 0;
520 int actualBrightness = 0;
521 result r = E_SUCCESS;
523 SysTryReturnResult(NID_SYS, (brightness > 0 && brightness < 11), E_OUT_OF_RANGE,
524 "[E_OUT_OF_RANGE] The specified brightness is out of range.");
526 SysLog(NID_SYS, "Set brightness (%d)", brightness);
528 ret = device_get_max_brightness(0, &maxBrightness);
529 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to get the device max brightness");
531 actualBrightness = maxBrightness * brightness / _BRIGHTNESS_RESOLUTION;
533 ArrayList requestMessage;
534 ArrayList responseMessage;
536 unique_ptr<_IpcClient> pIpcClient (new (std::nothrow) _IpcClient());
537 SysTryReturn(NID_SYS, pIpcClient != null, E_OUT_OF_MEMORY, r, "It is failed to create IPC instance.");
539 if(Tizen::Io::File::IsFileExist(L"/opt/usr/etc/common_service_for_powermanager") == true)
541 SysLog(NID_SYS, "PowerManager is serviced by common-service");
542 r = pIpcClient->Construct(POWER_MANAGER_SERVICE_ID);
546 r = pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID);
548 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to create IpcClient", GetErrorMessage(r));
550 requestMessage.Construct();
551 responseMessage.Construct();
553 String serviceId = _SYSTEM_SERVICE_ID;
554 String commandId = _SYSTEM_COMMAND_CHANGE_BRIGHTNESS;
555 String brightnessValue;
556 brightnessValue.Append(actualBrightness);
558 requestMessage.Add(serviceId);
559 requestMessage.Add(commandId);
560 requestMessage.Add(brightnessValue);
562 unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
563 SysTryReturnResult(NID_SYS, pMsg != null, E_OUT_OF_MEMORY, "It is failed to create Ipc message");
565 r = pIpcClient->SendRequest(pMsg.get());
566 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to ipc message", GetErrorMessage(r));
568 String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
570 SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "It is failed to receive Ipc response.");
571 SysTryReturnResult(NID_SYS, *pResult == _SYSTEM_RESULT_OK, E_SYSTEM, "It is failed to set brightness.");
573 responseMessage.RemoveAll(true);
578 _PowerManagerImpl::GetScreenBrightness(void)
582 int maxBrightness = -1;
586 ret = device_get_brightness(0, &brightness);
589 SysLogException(NID_SYS, E_SYSTEM, "It failed to get the device brightness");
590 SetLastResult(E_SYSTEM);
591 return _DEACTIVATED_BRIGHTNESS_CONTROL;
599 SysLog(NID_SYS, "System Brightness is %d", brightness);
600 ret = device_get_max_brightness(0, &maxBrightness);
603 SysLogException(NID_SYS, E_SYSTEM, "It failed to get the device max brightness");
604 SetLastResult(E_SYSTEM);
605 return _DEACTIVATED_BRIGHTNESS_CONTROL;
607 return ceil(((_BRIGHTNESS_RESOLUTION / (float) maxBrightness)) * brightness);
611 _PowerManagerImpl::IsScreenOn(void)
613 power_state_e powerState = power_get_state();
615 if (powerState == POWER_STATE_SCREEN_OFF)
626 _PowerManagerImpl::TurnScreenOn(void)
628 result r = E_SUCCESS;
629 int ret = power_wakeup(true);
630 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to unlock the normal screen state");
631 ret = pm_change_state(LCD_NORMAL);
632 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to change to the normal screen state");
638 _PowerManagerImpl::TurnScreenOff(void)
640 result r = E_SUCCESS;
641 int ret = pm_change_state(LCD_OFF);
643 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to unlock the screen off screen state");
649 _PowerManagerImpl::SetScreenEventListener(IScreenEventListener& listener)
651 result r = E_SUCCESS;
653 r = AddScreenEventListener(listener, true);
657 SysLog(NID_SYS, "It failed to register ScreenEventListener");
660 SysLog(NID_SYS,"It successed to register ScreenEventListener");
665 _PowerManagerImpl::AddScreenEventListener(IScreenEventListener& listener, bool isSet)
667 result r = E_SUCCESS;
669 if (__pSceenEventList == null)
671 std::unique_ptr< ArrayListT<IScreenEventListener*> > pSceenEventList(new ArrayListT<IScreenEventListener*>);
672 SysTryReturnResult(NID_SYS, pSceenEventList, E_SYSTEM, "Memory allocation failed");
673 pSceenEventList->Construct();
675 errorCode = vconf_notify_key_changed(VCONFKEY_PM_CURRENT_BRIGHTNESS, ScreenEventVConfCallback, null);
676 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It failed to add ScreenEvent listener on VCONFKEY_PM_CURRENT_BRIGHTNESS.");
678 errorCode = power_set_changed_cb(PowerChangedCallback, null);
679 if(errorCode != POWER_ERROR_NONE)
681 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register power callback. So unregister VCONFKEY_PM_CURRENT_BRIGHTNESS also.");
682 vconf_ignore_key_changed(VCONFKEY_PM_CURRENT_BRIGHTNESS, ScreenEventVConfCallback);
684 SysTryReturnResult(NID_SYS, errorCode == POWER_ERROR_NONE, E_SYSTEM, "It failed to add ScreenEvent listener.");
685 __pSceenEventList = pSceenEventList.release();
686 SysLog(NID_SYS,"It successed to register power_set_changed_cb");
689 if (isSet == true && __pSceenEventList->GetCount() != 0)
691 r = __pSceenEventList->SetAt(&listener, 0);
695 r = __pSceenEventList->Add(&listener);
702 _PowerManagerImpl::RemoveScreenEventListener(IScreenEventListener& listener)
704 result r = E_SUCCESS;
706 SysTryReturnResult(NID_SYS,__pSceenEventList != null, E_OBJ_NOT_FOUND, "IScreenEventListener list is empty");
708 r = __pSceenEventList->Remove(&listener);
709 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_OBJ_NOT_FOUND, "listener is not exist.");
715 _PowerManagerImpl::SetChargingEventListener(IChargingEventListener& listener)
717 result r = E_SUCCESS;
719 r = AddChargingEventListener(listener, true);
723 SysLog(NID_SYS, "It failed to register battery charging event");
730 _PowerManagerImpl::AddChargingEventListener(IChargingEventListener& listener, bool isSet)
732 result r = E_SUCCESS;
734 if (__pChargingEventList == null)
736 int errorCode = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, RuntimeInfoChangedCallback, null);
737 SysTryReturnResult(NID_SYS, errorCode == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to register RUNTIME_INFO_KEY_BATTERY_IS_CHARGING.");
739 __pChargingEventList = new ArrayListT<IChargingEventListener*>;
740 SysTryReturnResult(NID_SYS, __pChargingEventList, E_SYSTEM, "Memory allocation failed");
741 r = __pChargingEventList->Construct();
744 delete __pChargingEventList;
745 __pChargingEventList = null;
747 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to construct charging event list.");
750 if (isSet == false || __pChargingEventList->GetCount() == 0)
752 r = __pChargingEventList->Add(&listener);
756 r = __pChargingEventList->SetAt(&listener, 0);
763 _PowerManagerImpl::RemoveChargingEventListener(IChargingEventListener& listener)
765 result r = E_SUCCESS;
767 SysTryReturnResult(NID_SYS,__pChargingEventList != null, E_OBJ_NOT_FOUND, "__pChargingEventList list is empty");
768 r = __pChargingEventList->Remove(&listener);
769 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_OBJ_NOT_FOUND, "listener is not exist.");
775 _PowerManagerImpl::SetBatteryEventListener(IBatteryEventListener* pListener)
779 if (pListener != null)
782 if(pIBatteryEventListener == null)
784 ret = device_battery_set_cb(DeviceBatteryCallback, null);
787 SysLog(NID_SYS, "It failed to register battery event");
790 pIBatteryEventListener = pListener;
794 pIBatteryEventListener = null;
795 ret = device_battery_unset_cb();
798 SysLog(NID_SYS, "It failed to unregister battery event");
806 _PowerManagerImpl::PowerControl(int deviceId, int level)
809 if(deviceId == _DEVICE_CPU)
811 if(level == _DEVICE_POWER_LEVEL_ON)
813 ret = power_lock_state(POWER_STATE_SCREEN_OFF, 0);
814 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to lock the power state");
815 _deviceCpuPowerOn = true;
819 else if(level == _DEVICE_POWER_LEVEL_OFF)
821 if(_keepScreenOnState == false)
823 ret = power_unlock_state(POWER_STATE_SCREEN_OFF);
824 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to unlock the power state");
826 _deviceCpuPowerOn = false;
834 _PowerManagerImpl::GetCurrentBatteryLevelInPercentage(void)
837 int batteryLevel = 0;
839 ret = device_battery_get_percent(&batteryLevel);
842 SetLastResult(E_SYSTEM);
849 _PowerManagerImpl::GetCurrentBatteryLevel(void)
851 int batteryLevel = 0;
852 BatteryLevel level = BATTERY_EMPTY;
853 int ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, &batteryLevel);
856 SysLogException(NID_SYS, E_SYSTEM, "It is failed to get battery level.");
862 case VCONFKEY_SYSMAN_BAT_LEVEL_FULL:
863 level = BATTERY_LEVEL_FULL;
865 case VCONFKEY_SYSMAN_BAT_LEVEL_HIGH:
866 level = BATTERY_LEVEL_HIGH;
868 case VCONFKEY_SYSMAN_BAT_LEVEL_LOW:
869 level = BATTERY_LEVEL_LOW;
871 case VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL:
872 level = BATTERY_LEVEL_CRITICAL;
875 level = BATTERY_LEVEL_EMPTY;
883 _PowerManagerImpl::IsCharging(void)
885 bool chargeState = false;
887 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, &chargeState);
890 SetLastResult(E_SYSTEM);
898 _PowerManagerImpl::Add_IPowerManagerEventListener(_IPowerManagerEventListener* listener)
900 _PowerManagerImpl::Init();
901 __powerEventList.Add(listener);
906 _PowerManagerImpl::Remove_IPowerManagerEventListener(_IPowerManagerEventListener* listener)
908 _PowerManagerImpl::Init();
909 __powerEventList.Remove(listener);
914 BootEventVconfCallback(keynode_t* node, void* userData)
916 if (__pBootEventList == null)
918 SysLogException(NID_SYS, E_SYSTEM, "BootEventList is not initialized.");
923 const int errorCode = vconf_get_int(VCONFKEY_SERVICE_READY, &bootReady);
927 SysLogException(NID_SYS, E_SYSTEM, "It failed to get Boot event status");
931 std::unique_ptr< IEnumeratorT<IBootEventListener*> > pEnum(__pBootEventList->GetEnumeratorN());
935 while (pEnum->MoveNext() == E_SUCCESS)
937 IBootEventListener* pListener = null;
938 pEnum->GetCurrent(pListener);
940 pListener->OnBootCompleted();
944 SysLog(NID_SYS, "Finished invoking boot complete event listener.");
948 _PowerManagerImpl::AddBootEventListener(IBootEventListener& listener)
950 if (__pBootEventList == null)
952 std::unique_ptr< ArrayListT<IBootEventListener*> > pBootEventList(new ArrayListT<IBootEventListener*>);
953 SysTryReturnResult(NID_SYS, pBootEventList, E_SYSTEM, "Memory allocation failed");
954 pBootEventList->Construct();
957 errorCode = vconf_notify_key_changed(VCONFKEY_SERVICE_READY, BootEventVconfCallback, null);
958 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It failed to add BootEvent listener.");
960 __pBootEventList = pBootEventList.release();
961 SysLog(NID_SYS, "It successed to register boot event listener.");
964 return __pBootEventList->Add(&listener);
968 _PowerManagerImpl::RemoveBootEventListener(IBootEventListener& listener)
970 result r = E_SUCCESS;
972 SysTryReturnResult(NID_SYS,__pBootEventList != null, E_OBJ_NOT_FOUND, "IBootEventListener list is empty");
974 r = __pBootEventList->Remove(&listener);
975 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_OBJ_NOT_FOUND, "IBootEventListener list is empty");
981 _PowerManagerImpl::GetInstance(PowerManager& powerManager)
983 return powerManager.__pPowerManagerImpl;
986 const _PowerManagerImpl*
987 _PowerManagerImpl::GetInstance(const PowerManager& powerManager)
989 return powerManager.__pPowerManagerImpl;