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);
490 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to create IpcClient", GetErrorMessage(r));
492 requestMessage.Construct();
493 responseMessage.Construct();
495 String serviceId = _SYSTEM_SERVICE_ID;
496 String commandId = _SYSTEM_COMMAND_RESTORE_BRIGHTNESS;
498 requestMessage.Add(serviceId);
499 requestMessage.Add(commandId);
501 unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
502 SysTryReturnResult(NID_SYS, pMsg != null, E_OUT_OF_MEMORY, "It is failed to create Ipc message");
504 r = pIpcClient->SendRequest(pMsg.get());
505 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to ipc message", GetErrorMessage(r));
507 String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
509 SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "It is failed to receive Ipc response.");
510 SysTryReturnResult(NID_SYS, *pResult == _SYSTEM_RESULT_OK, E_SYSTEM, "It is failed to set brightness.");
512 responseMessage.RemoveAll(true);
517 _PowerManagerImpl::SetScreenBrightness(int brightness)
520 int maxBrightness = 0;
521 int actualBrightness = 0;
522 result r = E_SUCCESS;
524 SysTryReturnResult(NID_SYS, (brightness > 0 && brightness < 11), E_OUT_OF_RANGE,
525 "[E_OUT_OF_RANGE] The specified brightness is out of range.");
527 SysLog(NID_SYS, "Set brightness (%d)", brightness);
529 ret = device_get_max_brightness(0, &maxBrightness);
530 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to get the device max brightness");
532 actualBrightness = maxBrightness * brightness / _BRIGHTNESS_RESOLUTION;
534 ArrayList requestMessage;
535 ArrayList responseMessage;
537 unique_ptr<_IpcClient> pIpcClient (new (std::nothrow) _IpcClient());
538 SysTryReturn(NID_SYS, pIpcClient != null, E_OUT_OF_MEMORY, r, "It is failed to create IPC instance.");
540 // if(Tizen::Io::File::IsFileExist(L"/opt/usr/etc/common_service_for_powermanager") == true)
542 SysLog(NID_SYS, "PowerManager is serviced by common-service");
543 r = pIpcClient->Construct(POWER_MANAGER_SERVICE_ID);
547 r = pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID);
550 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to create IpcClient", GetErrorMessage(r));
552 requestMessage.Construct();
553 responseMessage.Construct();
555 String serviceId = _SYSTEM_SERVICE_ID;
556 String commandId = _SYSTEM_COMMAND_CHANGE_BRIGHTNESS;
557 String brightnessValue;
558 brightnessValue.Append(actualBrightness);
560 requestMessage.Add(serviceId);
561 requestMessage.Add(commandId);
562 requestMessage.Add(brightnessValue);
564 unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
565 SysTryReturnResult(NID_SYS, pMsg != null, E_OUT_OF_MEMORY, "It is failed to create Ipc message");
567 r = pIpcClient->SendRequest(pMsg.get());
568 SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to ipc message", GetErrorMessage(r));
570 String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
572 SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "It is failed to receive Ipc response.");
573 SysTryReturnResult(NID_SYS, *pResult == _SYSTEM_RESULT_OK, E_SYSTEM, "It is failed to set brightness.");
575 responseMessage.RemoveAll(true);
580 _PowerManagerImpl::GetScreenBrightness(void)
584 int maxBrightness = -1;
588 ret = device_get_brightness(0, &brightness);
591 SysLogException(NID_SYS, E_SYSTEM, "It failed to get the device brightness");
592 SetLastResult(E_SYSTEM);
593 return _DEACTIVATED_BRIGHTNESS_CONTROL;
601 SysLog(NID_SYS, "System Brightness is %d", brightness);
602 ret = device_get_max_brightness(0, &maxBrightness);
605 SysLogException(NID_SYS, E_SYSTEM, "It failed to get the device max brightness");
606 SetLastResult(E_SYSTEM);
607 return _DEACTIVATED_BRIGHTNESS_CONTROL;
609 return ceil(((_BRIGHTNESS_RESOLUTION / (float) maxBrightness)) * brightness);
613 _PowerManagerImpl::IsScreenOn(void)
615 power_state_e powerState = power_get_state();
617 if (powerState == POWER_STATE_SCREEN_OFF)
628 _PowerManagerImpl::TurnScreenOn(void)
630 result r = E_SUCCESS;
631 int ret = power_wakeup(true);
632 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to unlock the normal screen state");
633 ret = pm_change_state(LCD_NORMAL);
634 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to change to the normal screen state");
640 _PowerManagerImpl::TurnScreenOff(void)
642 result r = E_SUCCESS;
643 int ret = pm_change_state(LCD_OFF);
645 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to unlock the screen off screen state");
651 _PowerManagerImpl::SetScreenEventListener(IScreenEventListener& listener)
653 result r = E_SUCCESS;
655 r = AddScreenEventListener(listener, true);
659 SysLog(NID_SYS, "It failed to register ScreenEventListener");
662 SysLog(NID_SYS,"It successed to register ScreenEventListener");
667 _PowerManagerImpl::AddScreenEventListener(IScreenEventListener& listener, bool isSet)
669 result r = E_SUCCESS;
671 if (__pSceenEventList == null)
673 std::unique_ptr< ArrayListT<IScreenEventListener*> > pSceenEventList(new ArrayListT<IScreenEventListener*>);
674 SysTryReturnResult(NID_SYS, pSceenEventList, E_SYSTEM, "Memory allocation failed");
675 pSceenEventList->Construct();
677 errorCode = vconf_notify_key_changed(VCONFKEY_PM_CURRENT_BRIGHTNESS, ScreenEventVConfCallback, null);
678 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It failed to add ScreenEvent listener on VCONFKEY_PM_CURRENT_BRIGHTNESS.");
680 errorCode = power_set_changed_cb(PowerChangedCallback, null);
681 if(errorCode != POWER_ERROR_NONE)
683 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register power callback. So unregister VCONFKEY_PM_CURRENT_BRIGHTNESS also.");
684 vconf_ignore_key_changed(VCONFKEY_PM_CURRENT_BRIGHTNESS, ScreenEventVConfCallback);
686 SysTryReturnResult(NID_SYS, errorCode == POWER_ERROR_NONE, E_SYSTEM, "It failed to add ScreenEvent listener.");
687 __pSceenEventList = pSceenEventList.release();
688 SysLog(NID_SYS,"It successed to register power_set_changed_cb");
691 if (isSet == true && __pSceenEventList->GetCount() != 0)
693 r = __pSceenEventList->SetAt(&listener, 0);
697 r = __pSceenEventList->Add(&listener);
704 _PowerManagerImpl::RemoveScreenEventListener(IScreenEventListener& listener)
706 result r = E_SUCCESS;
708 SysTryReturnResult(NID_SYS,__pSceenEventList != null, E_OBJ_NOT_FOUND, "IScreenEventListener list is empty");
710 r = __pSceenEventList->Remove(&listener);
711 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_OBJ_NOT_FOUND, "listener is not exist.");
717 _PowerManagerImpl::SetChargingEventListener(IChargingEventListener& listener)
719 result r = E_SUCCESS;
721 r = AddChargingEventListener(listener, true);
725 SysLog(NID_SYS, "It failed to register battery charging event");
732 _PowerManagerImpl::AddChargingEventListener(IChargingEventListener& listener, bool isSet)
734 result r = E_SUCCESS;
736 if (__pChargingEventList == null)
738 int errorCode = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, RuntimeInfoChangedCallback, null);
739 SysTryReturnResult(NID_SYS, errorCode == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to register RUNTIME_INFO_KEY_BATTERY_IS_CHARGING.");
741 __pChargingEventList = new ArrayListT<IChargingEventListener*>;
742 SysTryReturnResult(NID_SYS, __pChargingEventList, E_SYSTEM, "Memory allocation failed");
743 r = __pChargingEventList->Construct();
746 delete __pChargingEventList;
747 __pChargingEventList = null;
749 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to construct charging event list.");
752 if (isSet == false || __pChargingEventList->GetCount() == 0)
754 r = __pChargingEventList->Add(&listener);
758 r = __pChargingEventList->SetAt(&listener, 0);
765 _PowerManagerImpl::RemoveChargingEventListener(IChargingEventListener& listener)
767 result r = E_SUCCESS;
769 SysTryReturnResult(NID_SYS,__pChargingEventList != null, E_OBJ_NOT_FOUND, "__pChargingEventList list is empty");
770 r = __pChargingEventList->Remove(&listener);
771 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_OBJ_NOT_FOUND, "listener is not exist.");
777 _PowerManagerImpl::SetBatteryEventListener(IBatteryEventListener* pListener)
781 if (pListener != null)
784 if(pIBatteryEventListener == null)
786 ret = device_battery_set_cb(DeviceBatteryCallback, null);
789 SysLog(NID_SYS, "It failed to register battery event");
792 pIBatteryEventListener = pListener;
796 pIBatteryEventListener = null;
797 ret = device_battery_unset_cb();
800 SysLog(NID_SYS, "It failed to unregister battery event");
808 _PowerManagerImpl::PowerControl(int deviceId, int level)
811 if(deviceId == _DEVICE_CPU)
813 if(level == _DEVICE_POWER_LEVEL_ON)
815 ret = power_lock_state(POWER_STATE_SCREEN_OFF, 0);
816 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to lock the power state");
817 _deviceCpuPowerOn = true;
821 else if(level == _DEVICE_POWER_LEVEL_OFF)
823 if(_keepScreenOnState == false)
825 ret = power_unlock_state(POWER_STATE_SCREEN_OFF);
826 SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to unlock the power state");
828 _deviceCpuPowerOn = false;
836 _PowerManagerImpl::GetCurrentBatteryLevelInPercentage(void)
839 int batteryLevel = 0;
841 ret = device_battery_get_percent(&batteryLevel);
844 SetLastResult(E_SYSTEM);
851 _PowerManagerImpl::GetCurrentBatteryLevel(void)
853 int batteryLevel = 0;
854 BatteryLevel level = BATTERY_EMPTY;
855 int ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, &batteryLevel);
858 SysLogException(NID_SYS, E_SYSTEM, "It is failed to get battery level.");
864 case VCONFKEY_SYSMAN_BAT_LEVEL_FULL:
865 level = BATTERY_LEVEL_FULL;
867 case VCONFKEY_SYSMAN_BAT_LEVEL_HIGH:
868 level = BATTERY_LEVEL_HIGH;
870 case VCONFKEY_SYSMAN_BAT_LEVEL_LOW:
871 level = BATTERY_LEVEL_LOW;
873 case VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL:
874 level = BATTERY_LEVEL_CRITICAL;
877 level = BATTERY_LEVEL_EMPTY;
885 _PowerManagerImpl::IsCharging(void)
887 bool chargeState = false;
889 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, &chargeState);
892 SetLastResult(E_SYSTEM);
900 _PowerManagerImpl::Add_IPowerManagerEventListener(_IPowerManagerEventListener* listener)
902 _PowerManagerImpl::Init();
903 __powerEventList.Add(listener);
908 _PowerManagerImpl::Remove_IPowerManagerEventListener(_IPowerManagerEventListener* listener)
910 _PowerManagerImpl::Init();
911 __powerEventList.Remove(listener);
916 BootEventVconfCallback(keynode_t* node, void* userData)
918 if (__pBootEventList == null)
920 SysLogException(NID_SYS, E_SYSTEM, "BootEventList is not initialized.");
925 const int errorCode = vconf_get_int(VCONFKEY_SERVICE_READY, &bootReady);
929 SysLogException(NID_SYS, E_SYSTEM, "It failed to get Boot event status");
933 std::unique_ptr< IEnumeratorT<IBootEventListener*> > pEnum(__pBootEventList->GetEnumeratorN());
937 while (pEnum->MoveNext() == E_SUCCESS)
939 IBootEventListener* pListener = null;
940 pEnum->GetCurrent(pListener);
942 pListener->OnBootCompleted();
946 SysLog(NID_SYS, "Finished invoking boot complete event listener.");
950 _PowerManagerImpl::AddBootEventListener(IBootEventListener& listener)
952 if (__pBootEventList == null)
954 std::unique_ptr< ArrayListT<IBootEventListener*> > pBootEventList(new ArrayListT<IBootEventListener*>);
955 SysTryReturnResult(NID_SYS, pBootEventList, E_SYSTEM, "Memory allocation failed");
956 pBootEventList->Construct();
959 errorCode = vconf_notify_key_changed(VCONFKEY_SERVICE_READY, BootEventVconfCallback, null);
960 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It failed to add BootEvent listener.");
962 __pBootEventList = pBootEventList.release();
963 SysLog(NID_SYS, "It successed to register boot event listener.");
966 return __pBootEventList->Add(&listener);
970 _PowerManagerImpl::RemoveBootEventListener(IBootEventListener& listener)
972 result r = E_SUCCESS;
974 SysTryReturnResult(NID_SYS,__pBootEventList != null, E_OBJ_NOT_FOUND, "IBootEventListener list is empty");
976 r = __pBootEventList->Remove(&listener);
977 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_OBJ_NOT_FOUND, "IBootEventListener list is empty");
983 _PowerManagerImpl::GetInstance(PowerManager& powerManager)
985 return powerManager.__pPowerManagerImpl;
988 const _PowerManagerImpl*
989 _PowerManagerImpl::GetInstance(const PowerManager& powerManager)
991 return powerManager.__pPowerManagerImpl;