220e82f560d184dfca236c44eb46c20e70afd659
[platform/framework/native/appfw.git] / src / system / FSys_PowerManagerImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file                FSys_PowerManagerImpl.cpp
19  * @brief               This is the implementation file for _PowerManagerImpl class.
20  */
21
22 #include <unique_ptr.h>
23
24 #include <pmapi.h>
25 #include <power.h>
26 #include <device.h>
27 #include <runtime_info.h>
28 #include <vconf.h>
29
30 #include <FIo.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>
40
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>
51
52 using namespace std;
53
54 using namespace Tizen::App;
55 using namespace Tizen::Base;
56 using namespace Tizen::Base::Collection;
57 using namespace Tizen::Io;
58
59 namespace Tizen { namespace System
60 {
61
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";
69
70 #ifndef VCONFKEY_SERVICE_READY
71 #define VCONFKEY_SERVICE_READY  "memory/deviced/boot_power_on"
72 #endif
73
74 bool _activeApp = false;
75 bool _deviceCpuPowerOn = false;
76 bool _keepCpuAwake = false;
77 bool _keepScreenOnState = false;
78
79 IBatteryEventListener* pIBatteryEventListener = null;
80
81 ArrayListT<IScreenEventListener*>*      __pSceenEventList = null;
82 ArrayListT<IChargingEventListener*>*    __pChargingEventList = null;
83 ArrayListT<_IPowerManagerEventListener*>        __powerEventList;
84 ArrayListT<IBootEventListener*>*        __pBootEventList = null;
85
86 bool _PowerManagerImpl::_InitPowerManager = false;
87
88
89 class _ActiveEventListener
90         : public IActiveAppEventListener
91 {
92 public:
93         virtual void OnActiveAppChanged(const Tizen::App::AppId& appId);
94 };
95
96 void
97 _ActiveEventListener::OnActiveAppChanged(const Tizen::App::AppId& appId)
98 {
99         int ret = 0;
100         SysLog(NID_SYS, "Active App is %ls.", appId.GetPointer());
101
102         if(appId == _AppInfo::GetApplicationId())
103         {
104                 _activeApp = true;
105         }
106         else
107         {
108                 _activeApp = false;
109
110                 if (_keepScreenOnState == true) //In case of Screen power control, it has to be released automatically, when application goes deactive state.
111                 {
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;
115                 }
116
117                 if (_keepCpuAwake == true || _deviceCpuPowerOn == true) //In case of CPU power control, it has to be keep despite of deactive state.
118                 {
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");
121                 }
122         }
123 }
124
125  _ActiveEventListener* pActiveEventListener = null;
126
127 void
128 BatteryLevelVConfCallback(keynode_t* node, void* userData)
129 {
130         SysLog(NID_SYS, "Battery Warn Level is changed");
131         BatteryLevel    batteryLevel;
132
133         if (strcmp(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, vconf_keynode_get_name(node)) == 0)
134         {
135                 int level = 0;
136                 if(vconf_get_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, &level) >= 0)
137                 {
138                         switch(level)
139                         {
140                         case VCONFKEY_SYSMAN_BAT_LEVEL_EMPTY:
141                                 batteryLevel = BATTERY_EMPTY;
142                                 break;
143                         case VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL:
144                                 batteryLevel = BATTERY_CRITICAL;
145                                 break;
146                         case VCONFKEY_SYSMAN_BAT_LEVEL_LOW:
147                                 batteryLevel = BATTERY_LOW;
148                                 break;
149                         case VCONFKEY_SYSMAN_BAT_LEVEL_HIGH:
150                                 batteryLevel = BATTERY_HIGH;
151                                 break;
152                         case VCONFKEY_SYSMAN_BAT_LEVEL_FULL:
153                                 batteryLevel = BATTERY_FULL;
154                                 break;
155                         default:
156                                 SysLogException(NID_SYS, E_SYSTEM, "Unavailable battery level is required.");
157                                 return;
158                         }
159                         _AppImpl* pAppImpl = _AppImpl::GetInstance();
160                         if(pAppImpl != null)
161                         {
162                                 SysLog(NID_SYS, "Battery Level event is forwarded");
163                                 pAppImpl->OnBatteryLevelChanged(batteryLevel);
164                         }
165                 }
166         }
167 }
168
169 void
170 DeviceBatteryCallback(int percent, void* userData)
171 {
172         SysLog(NID_SYS, "Battery Level is called %d", percent);
173
174         if (pIBatteryEventListener != null)
175         {
176                 pIBatteryEventListener->OnBatteryLevelChangedInPercentage(percent);
177         }
178 }
179
180 void
181 RuntimeInfoChangedCallback(runtime_info_key_e key, void* userData)
182 {
183         int ret = 0;
184
185         if(_AppInfo::GetAppState() == RUNNING)
186         {
187                 if (__pChargingEventList != null)
188                 {
189                         bool charging = false;
190                         ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, &charging);
191                         if (ret == 0)
192                         {
193                                 for(int i = 0; i < __pChargingEventList->GetCount(); i++)
194                                 {
195                                         result r = E_SUCCESS;
196                                         IChargingEventListener* pIChargingEventListener = null;
197
198                                         r = __pChargingEventList->GetAt(i, pIChargingEventListener);
199
200                                         if (IsFailed(r) == false)
201                                         {
202                                                 pIChargingEventListener->OnChargingStateChanged(charging);
203                                         }
204                                 }
205
206                         }
207                 }
208         }
209 }
210
211 void
212 PowerChangedCallback(power_state_e state, void* userData)
213 {
214         std::unique_ptr< IEnumeratorT<_IPowerManagerEventListener*> > pEnumerator(__powerEventList.GetEnumeratorN());
215         _IPowerManagerEventListener* pIPowerManagerEventListener = null;
216
217         if (__pSceenEventList != null)
218         {
219                 if (state == POWER_STATE_SCREEN_OFF)
220                 {
221                         for (int i = 0; i < __pSceenEventList->GetCount(); i++)
222                         {
223                                 IScreenEventListener* pIScreenEventListener = null;
224                                 result r = E_SUCCESS;
225                                 r = __pSceenEventList->GetAt(i, pIScreenEventListener);
226                                 if (IsFailed(r) == false)
227                                 {
228                                         pIScreenEventListener->OnScreenOff();
229                                 }
230                         }
231
232                         if(pEnumerator != null)
233                         {
234                                 while(pEnumerator->MoveNext() == E_SUCCESS)
235                                 {
236                                         pEnumerator->GetCurrent(pIPowerManagerEventListener);
237                                         if(pIPowerManagerEventListener != null)
238                                         {
239                                                 pIPowerManagerEventListener->OnScreenStateChanged(false);
240                                         }
241                                 }
242                         }
243                 }
244                 else if(state == POWER_STATE_NORMAL)
245                 {
246                         for (int i = 0; i < __pSceenEventList->GetCount(); i++)
247                         {
248                                 IScreenEventListener* pIScreenEventListener = null;
249                                 result r = E_SUCCESS;
250                                 r = __pSceenEventList->GetAt(i, pIScreenEventListener);
251                                 if (IsFailed(r) == false)
252                                 {
253                                         pIScreenEventListener->OnScreenOn();
254                                 }
255                         }
256
257                         if(pEnumerator != null)
258                         {
259                                 while(pEnumerator->MoveNext() == E_SUCCESS)
260                                 {
261                                         pEnumerator->GetCurrent(pIPowerManagerEventListener);
262                                         if(pIPowerManagerEventListener != null)
263                                         {
264                                                 pIPowerManagerEventListener->OnScreenStateChanged(true);
265                                         }
266                                 }
267                         }
268                 }
269         }
270 }
271
272 void
273 ScreenEventVConfCallback(keynode_t* node, void* userData)
274 {
275         int errorCode = 0;
276         int sysBrightness = 0;
277
278         if (__pSceenEventList == null)
279         {
280                 SysLogException(NID_SYS, E_SYSTEM, "SceenEventList is not initialized.");
281                 return;
282         }
283         errorCode = vconf_get_int(VCONFKEY_PM_CURRENT_BRIGHTNESS, &sysBrightness);
284
285         if ( errorCode != 0)
286         {
287                 SysLogException(NID_SYS, E_SYSTEM, "It failed to get Screen brightness");
288                 return;
289         }
290
291         for (int i = 0; i < __pSceenEventList->GetCount(); i++)
292         {
293                 IScreenEventListener* pIScreenEventListener = null;
294                 __pSceenEventList->GetAt(i, pIScreenEventListener);
295                 if (pIScreenEventListener != null)
296                 {
297                         int brightness = -1;
298                         int maxBrightness = -1;
299                         int ret = device_get_max_brightness(0, &maxBrightness);
300                         if (ret != 0)
301                         {
302                                 SysLogException(NID_SYS, E_SYSTEM, "It failed to get the device max brightness");
303                                 maxBrightness = 100;
304                         }
305                         sysBrightness = (sysBrightness == 0) ? 1 : sysBrightness;
306                         brightness = (int)ceil(((_BRIGHTNESS_RESOLUTION / (float) maxBrightness)) * sysBrightness);
307                         pIScreenEventListener->OnScreenBrightnessChanged(brightness);
308                 }
309         }
310 }
311
312 _PowerManagerImpl::_PowerManagerImpl(void)
313 {
314 }
315
316 _PowerManagerImpl::~_PowerManagerImpl(void)
317 {
318 }
319
320 void
321 _PowerManagerImpl::Init()
322 {
323         if(_InitPowerManager == false)
324         {
325                 __powerEventList.Construct();
326                 pActiveEventListener = new (std::nothrow) _ActiveEventListener();
327                 _AppManagerImpl* pAppManager = _AppManagerImpl::GetInstance();
328                 pAppManager->AddActiveAppEventListener(*pActiveEventListener);
329                 _InitPowerManager = true;
330         }
331 }
332
333 void
334 _PowerManagerImpl::Term()
335 {
336         if(_InitPowerManager == true)
337         {
338                 _AppManagerImpl* pAppManager = _AppManagerImpl::GetInstance();
339                 pAppManager->RemoveActiveAppEventListener(*pActiveEventListener);
340                 delete(pActiveEventListener);
341                 pActiveEventListener = null;
342
343                 _InitPowerManager = false;
344         }
345 }
346
347 bool
348 _PowerManagerImpl::IsActive(void)
349 {
350         AppId appId;
351         result r = E_SUCCESS;
352         _AppManagerImpl* pAppManagerImpl = _AppManagerImpl::GetInstance();
353
354         if(pAppManagerImpl == null)
355         {
356                 _activeApp = false;
357         }
358         else
359         {
360                 r = pAppManagerImpl->GetActiveApp(appId);
361
362                 if(r == E_SUCCESS)
363                 {
364                         SysLog(NID_SYS, "current active is:%ls, required:%ls", appId.GetPointer(), _AppInfo::GetApplicationId().GetPointer());
365                         if(appId == _AppInfo::GetApplicationId())
366                         {
367                                 _activeApp = true;
368                         }
369                         else
370                         {
371                                 _activeApp = false;
372                         }
373                 }
374                 else
375                 {
376                         _activeApp = false;
377                 }
378         }
379         return _activeApp;
380 }
381
382 result
383 _PowerManagerImpl::KeepScreenOnState(bool keepOn, bool dimming)
384 {
385         int ret = -1;
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);
389
390         if (keepOn == true && IsActive() == true)
391         {
392                 if (dimming)
393                 {
394                         state = LCD_DIM;
395                         if(power_get_state() == POWER_STATE_NORMAL)
396                         {
397                                 flag = STAY_CUR_STATE;
398                         }
399                 }
400                 else
401                 {
402                         state = LCD_NORMAL;
403                 }
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;
407         }
408         else
409         {
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;
413         }
414
415         SysLog(NID_SYS, "change screen power policy: keepOn(%d) and dimming(%d)", keepOn, dimming);
416
417         return E_SUCCESS;
418 }
419
420 result
421 _PowerManagerImpl::KeepCpuAwake(bool enable)
422 {
423         int ret = 0;
424         _PowerManagerImpl::Init();
425
426         if (enable)
427         {
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;
431         }
432         else
433         {
434                  if(_deviceCpuPowerOn == false)
435                 {
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");
438                 }
439                 _keepCpuAwake = false;
440         }
441
442         SysLog(NID_SYS, "change cpu power policy: keepAwake(%s)", enable ? "true" : "false");
443         return E_SUCCESS;
444 }
445
446 result
447 _PowerManagerImpl::InitBatteryEvent(void)
448 {
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");
451
452         return E_SUCCESS;
453 }
454
455 result
456 _PowerManagerImpl::OnForeground(void)
457 {
458         return E_SUCCESS;
459 }
460
461 result
462 _PowerManagerImpl::OnBackground(void)
463 {
464         return E_SUCCESS;
465 }
466
467 result
468 _PowerManagerImpl::RestoreScreenBrightness(void)
469 {
470         result r = E_SUCCESS;
471
472         ArrayList requestMessage;
473         ArrayList responseMessage;
474
475         SysTryReturn(NID_SYS, IsActive() == true, E_SUCCESS, r, "It is not active app.");
476
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.");
479
480 //      if(Tizen::Io::File::IsFileExist(L"/opt/usr/etc/common_service_for_powermanager") == true)
481         {
482                 SysLog(NID_SYS, "PowerManager is serviced by common-service");
483                 r = pIpcClient->Construct(POWER_MANAGER_SERVICE_ID);
484         }
485 /*      else
486         {
487                 r = pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID);
488         }
489 */
490         SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to create IpcClient", GetErrorMessage(r));
491
492         requestMessage.Construct();
493         responseMessage.Construct();
494
495         String serviceId = _SYSTEM_SERVICE_ID;
496         String commandId = _SYSTEM_COMMAND_RESTORE_BRIGHTNESS;
497
498         requestMessage.Add(serviceId);
499         requestMessage.Add(commandId);
500
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");
503
504         r = pIpcClient->SendRequest(pMsg.get());
505         SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to ipc message", GetErrorMessage(r));
506
507         String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
508
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.");
511
512         responseMessage.RemoveAll(true);
513         return E_SUCCESS;
514 }
515
516 result
517 _PowerManagerImpl::SetScreenBrightness(int brightness)
518 {
519         int ret = 0;
520         int maxBrightness = 0;
521         int actualBrightness = 0;
522         result r = E_SUCCESS;
523
524         SysTryReturnResult(NID_SYS, (brightness > 0 && brightness < 11), E_OUT_OF_RANGE,
525                                   "[E_OUT_OF_RANGE] The specified brightness is out of range.");
526
527         SysLog(NID_SYS, "Set brightness (%d)", brightness);
528
529         ret = device_get_max_brightness(0, &maxBrightness);
530         SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to get the device max brightness");
531
532         actualBrightness = maxBrightness * brightness / _BRIGHTNESS_RESOLUTION;
533
534         ArrayList requestMessage;
535         ArrayList responseMessage;
536
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.");
539
540 //      if(Tizen::Io::File::IsFileExist(L"/opt/usr/etc/common_service_for_powermanager") == true)
541         {
542                 SysLog(NID_SYS, "PowerManager is serviced by common-service");
543                 r = pIpcClient->Construct(POWER_MANAGER_SERVICE_ID);
544         }
545 /*        else
546         {
547                 r = pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID);
548         }
549 */
550         SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to create IpcClient", GetErrorMessage(r));
551
552         requestMessage.Construct();
553         responseMessage.Construct();
554
555         String serviceId = _SYSTEM_SERVICE_ID;
556         String commandId = _SYSTEM_COMMAND_CHANGE_BRIGHTNESS;
557         String brightnessValue;
558         brightnessValue.Append(actualBrightness);
559
560         requestMessage.Add(serviceId);
561         requestMessage.Add(commandId);
562         requestMessage.Add(brightnessValue);
563
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");
566
567         r = pIpcClient->SendRequest(pMsg.get());
568         SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to ipc message", GetErrorMessage(r));
569
570         String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
571
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.");
574
575         responseMessage.RemoveAll(true);
576         return E_SUCCESS;
577 }
578
579 int
580 _PowerManagerImpl::GetScreenBrightness(void)
581 {
582         int ret = 0;
583         int brightness = -1;
584         int maxBrightness = -1;
585
586         ClearLastResult();
587
588         ret = device_get_brightness(0, &brightness);
589         if (ret != 0)
590         {
591                 SysLogException(NID_SYS, E_SYSTEM, "It failed to get the device brightness");
592                 SetLastResult(E_SYSTEM);
593                 return _DEACTIVATED_BRIGHTNESS_CONTROL;
594         }
595
596         if (brightness == 0)
597         {
598                 brightness = 1;
599         }
600
601         SysLog(NID_SYS, "System Brightness is %d", brightness);
602         ret = device_get_max_brightness(0, &maxBrightness);
603         if (ret != 0)
604         {
605                 SysLogException(NID_SYS, E_SYSTEM, "It failed to get the device max brightness");
606                 SetLastResult(E_SYSTEM);
607                 return _DEACTIVATED_BRIGHTNESS_CONTROL;
608         }
609         return ceil(((_BRIGHTNESS_RESOLUTION / (float) maxBrightness)) * brightness);
610 }
611
612 bool
613 _PowerManagerImpl::IsScreenOn(void)
614 {
615         power_state_e powerState = power_get_state();
616
617         if (powerState == POWER_STATE_SCREEN_OFF)
618         {
619                 return false;
620         }
621         else
622         {
623                 return true;
624         }
625 }
626
627 result
628 _PowerManagerImpl::TurnScreenOn(void)
629 {
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");
635
636         return r;
637 }
638
639 result
640 _PowerManagerImpl::TurnScreenOff(void)
641 {
642         result r = E_SUCCESS;
643         int ret = pm_change_state(LCD_OFF);
644
645         SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to unlock the screen off screen state");
646
647         return r;
648 }
649
650 void
651 _PowerManagerImpl::SetScreenEventListener(IScreenEventListener& listener)
652 {
653         result r = E_SUCCESS;
654
655         r = AddScreenEventListener(listener, true);
656
657         if (IsFailed(r))
658         {
659                 SysLog(NID_SYS, "It failed to register ScreenEventListener");
660                 return;
661         }
662         SysLog(NID_SYS,"It successed to register ScreenEventListener");
663 }
664
665
666 result
667 _PowerManagerImpl::AddScreenEventListener(IScreenEventListener& listener, bool isSet)
668 {
669         result r = E_SUCCESS;
670
671         if (__pSceenEventList == null)
672         {
673                 std::unique_ptr< ArrayListT<IScreenEventListener*> > pSceenEventList(new ArrayListT<IScreenEventListener*>);
674                 SysTryReturnResult(NID_SYS, pSceenEventList, E_SYSTEM, "Memory allocation failed");
675                 pSceenEventList->Construct();
676                 int errorCode = 0;
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.");
679
680                 errorCode = power_set_changed_cb(PowerChangedCallback, null);
681                 if(errorCode != POWER_ERROR_NONE)
682                 {
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);
685                 }
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");
689         }
690
691         if (isSet == true && __pSceenEventList->GetCount() != 0)
692         {
693                 r = __pSceenEventList->SetAt(&listener, 0);
694         }
695         else
696         {
697                 r = __pSceenEventList->Add(&listener);
698         }
699         return r;
700 }
701
702
703 result
704 _PowerManagerImpl::RemoveScreenEventListener(IScreenEventListener& listener)
705 {
706         result r = E_SUCCESS;
707
708         SysTryReturnResult(NID_SYS,__pSceenEventList != null, E_OBJ_NOT_FOUND, "IScreenEventListener list is empty");
709
710         r = __pSceenEventList->Remove(&listener);
711         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_OBJ_NOT_FOUND, "listener is not exist.");
712         return E_SUCCESS;
713 }
714
715
716 void
717 _PowerManagerImpl::SetChargingEventListener(IChargingEventListener& listener)
718 {
719         result r = E_SUCCESS;
720
721         r = AddChargingEventListener(listener, true);
722
723         if (IsFailed(r))
724         {
725                 SysLog(NID_SYS, "It failed to register battery charging  event");
726                 return;
727         }
728 }
729
730
731 result
732 _PowerManagerImpl::AddChargingEventListener(IChargingEventListener& listener, bool isSet)
733 {
734         result r = E_SUCCESS;
735
736         if (__pChargingEventList == null)
737         {
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.");
740
741                 __pChargingEventList = new ArrayListT<IChargingEventListener*>;
742                 SysTryReturnResult(NID_SYS, __pChargingEventList, E_SYSTEM, "Memory allocation failed");
743                 r = __pChargingEventList->Construct();
744                 if(r != E_SUCCESS)
745                 {
746                         delete __pChargingEventList;
747                         __pChargingEventList = null;
748                 }
749                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to construct charging event list.");
750         }
751
752         if (isSet == false || __pChargingEventList->GetCount() == 0)
753         {
754                 r = __pChargingEventList->Add(&listener);
755         }
756         else
757         {
758                 r = __pChargingEventList->SetAt(&listener, 0);
759         }
760         return r;
761 }
762
763
764 result
765 _PowerManagerImpl::RemoveChargingEventListener(IChargingEventListener& listener)
766 {
767         result r = E_SUCCESS;
768
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.");
772
773         return E_SUCCESS;
774 }
775
776 result
777 _PowerManagerImpl::SetBatteryEventListener(IBatteryEventListener* pListener)
778 {
779         int ret = 0;
780
781         if (pListener != null)
782         {
783
784                 if(pIBatteryEventListener == null)
785                 {
786                         ret = device_battery_set_cb(DeviceBatteryCallback, null);
787                         if (ret != 0)
788                         {
789                                 SysLog(NID_SYS, "It failed to register battery event");
790                         }
791                 }
792                 pIBatteryEventListener = pListener;
793         }
794         else
795         {
796                 pIBatteryEventListener = null;
797                 ret = device_battery_unset_cb();
798                 if (ret != 0)
799                 {
800                         SysLog(NID_SYS, "It failed to unregister battery event");
801                 }
802
803         }
804         return E_SUCCESS;
805 }
806
807 result
808 _PowerManagerImpl::PowerControl(int deviceId, int level)
809 {
810         int ret = 0;
811         if(deviceId == _DEVICE_CPU)
812         {
813                 if(level == _DEVICE_POWER_LEVEL_ON)
814                 {
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;
818
819                         return E_SUCCESS;
820                 }
821                 else if(level == _DEVICE_POWER_LEVEL_OFF)
822                 {
823                         if(_keepScreenOnState == false)
824                         {
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");
827                         }
828                         _deviceCpuPowerOn = false;
829                         return E_SUCCESS;
830                 }
831         }
832         return E_SYSTEM;
833 }
834
835 int
836 _PowerManagerImpl::GetCurrentBatteryLevelInPercentage(void)
837 {
838         int ret = 0;
839         int batteryLevel = 0;
840
841         ret = device_battery_get_percent(&batteryLevel);
842         if(ret != 0)
843         {
844                 SetLastResult(E_SYSTEM);
845         }
846
847         return batteryLevel;
848 }
849
850 BatteryLevel
851 _PowerManagerImpl::GetCurrentBatteryLevel(void)
852 {
853         int batteryLevel = 0;
854         BatteryLevel level = BATTERY_EMPTY;
855         int ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, &batteryLevel);
856         if(ret != 0)
857         {
858                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to get battery level.");
859                 return level;
860         }
861
862         switch(batteryLevel)
863         {
864         case VCONFKEY_SYSMAN_BAT_LEVEL_FULL:
865                 level = BATTERY_LEVEL_FULL;
866                 break;
867         case VCONFKEY_SYSMAN_BAT_LEVEL_HIGH:
868                 level = BATTERY_LEVEL_HIGH;
869                 break;
870         case VCONFKEY_SYSMAN_BAT_LEVEL_LOW:
871                 level = BATTERY_LEVEL_LOW;
872                 break;
873         case VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL:
874                 level = BATTERY_LEVEL_CRITICAL;
875                 break;
876         default:
877                 level = BATTERY_LEVEL_EMPTY;
878                 break;
879         }
880
881         return level;
882 }
883
884 bool
885 _PowerManagerImpl::IsCharging(void)
886 {
887         bool chargeState = false;
888         int ret = 0;
889         ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, &chargeState);
890         if(ret != 0)
891         {
892                 SetLastResult(E_SYSTEM);
893                 return chargeState;
894         }
895
896         return chargeState;
897 }
898
899 result
900 _PowerManagerImpl::Add_IPowerManagerEventListener(_IPowerManagerEventListener* listener)
901 {
902         _PowerManagerImpl::Init();
903         __powerEventList.Add(listener);
904         return E_SUCCESS;
905 }
906
907 result
908 _PowerManagerImpl::Remove_IPowerManagerEventListener(_IPowerManagerEventListener* listener)
909 {
910         _PowerManagerImpl::Init();
911         __powerEventList.Remove(listener);
912         return E_SUCCESS;
913 }
914
915 static void
916 BootEventVconfCallback(keynode_t* node, void* userData)
917 {
918         if (__pBootEventList == null)
919         {
920                 SysLogException(NID_SYS, E_SYSTEM, "BootEventList is not initialized.");
921                 return;
922         }
923
924         int bootReady = -1;
925         const int errorCode = vconf_get_int(VCONFKEY_SERVICE_READY, &bootReady);
926
927         if (errorCode != 0)
928         {
929                 SysLogException(NID_SYS, E_SYSTEM, "It failed to get Boot event status");
930                 return;
931         }
932
933         std::unique_ptr< IEnumeratorT<IBootEventListener*> > pEnum(__pBootEventList->GetEnumeratorN());
934
935         if (pEnum.get())
936         {
937                 while (pEnum->MoveNext() == E_SUCCESS)
938                 {
939                         IBootEventListener* pListener = null;
940                         pEnum->GetCurrent(pListener);
941
942                         pListener->OnBootCompleted();
943                 }
944         }
945
946         SysLog(NID_SYS, "Finished invoking boot complete event listener.");
947 }
948
949 result
950 _PowerManagerImpl::AddBootEventListener(IBootEventListener& listener)
951 {
952         if (__pBootEventList == null)
953         {
954                 std::unique_ptr< ArrayListT<IBootEventListener*> > pBootEventList(new ArrayListT<IBootEventListener*>);
955                 SysTryReturnResult(NID_SYS, pBootEventList, E_SYSTEM, "Memory allocation failed");
956                 pBootEventList->Construct();
957
958                 int errorCode = 0;
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.");
961
962                 __pBootEventList = pBootEventList.release();
963                 SysLog(NID_SYS, "It successed to register boot event listener.");
964         }
965
966         return __pBootEventList->Add(&listener);
967 }
968
969 result
970 _PowerManagerImpl::RemoveBootEventListener(IBootEventListener& listener)
971 {
972         result r = E_SUCCESS;
973
974         SysTryReturnResult(NID_SYS,__pBootEventList != null, E_OBJ_NOT_FOUND, "IBootEventListener list is empty");
975
976         r = __pBootEventList->Remove(&listener);
977         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_OBJ_NOT_FOUND, "IBootEventListener list is empty");
978
979         return E_SUCCESS;
980 }
981
982 _PowerManagerImpl*
983 _PowerManagerImpl::GetInstance(PowerManager& powerManager)
984 {
985         return powerManager.__pPowerManagerImpl;
986 }
987
988 const _PowerManagerImpl*
989 _PowerManagerImpl::GetInstance(const PowerManager& powerManager)
990 {
991         return powerManager.__pPowerManagerImpl;
992 }
993 } } //Tizen::System