Fix N_SE-56436 for Screen lock.
[platform/framework/native/appfw.git] / src / system / FSys_PowerManagerImpl.cpp
index d0e6d72..98bd6d7 100644 (file)
@@ -27,6 +27,7 @@
 #include <runtime_info.h>
 #include <vconf.h>
 
+#include <FIo.h>
 #include <FAppIActiveAppEventListener.h>
 #include <FBaseSysLog.h>
 #include <FBaseColArrayListT.h>
@@ -61,17 +62,17 @@ namespace Tizen { namespace System
 static const int _DEVICE_CPU = 1;
 static const int _DEVICE_POWER_LEVEL_ON = 1;
 static const int _DEVICE_POWER_LEVEL_OFF = 0;
-static const int _APPID_LENGTH = 10;
 static const int _DEACTIVATED_BRIGHTNESS_CONTROL = -1;
 static const float _BRIGHTNESS_RESOLUTION = 10.0;
-
-static const wchar_t* _POWER_MANAGER_SERVICE_ID = L"osp.sys.ipcserver.powermanager";
+static const wchar_t* POWER_MANAGER_SERVICE_ID = L"osp.sys.ipcserver.powermanager";
 
 #ifndef VCONFKEY_SERVICE_READY
 #define VCONFKEY_SERVICE_READY "memory/deviced/boot_power_on"
 #endif
 
 bool _activeApp = false;
+unsigned int _dimmingFlag = GOTO_STATE_NOW;
+unsigned int _screenOnState = LCD_DIM;
 bool _deviceCpuPowerOn = false;
 bool _keepCpuAwake = false;
 bool _keepScreenOnState = false;
@@ -97,11 +98,18 @@ void
 _ActiveEventListener::OnActiveAppChanged(const Tizen::App::AppId& appId)
 {
        int ret = 0;
-       SysLog(NID_SYS, "Active App is %ls.", appId.GetPointer());
+       SysLog(NID_SYS, "Active App is %ls. GetApp id %ls", appId.GetPointer(), _AppInfo::GetApplicationId().GetPointer());
 
        if(appId == _AppInfo::GetApplicationId())
        {
                _activeApp = true;
+               if (_keepScreenOnState == true)
+               {
+                       int ret = -1;
+                       ret = pm_lock_state(_screenOnState, _dimmingFlag, 0);
+                       SysTryReturnVoidResult(NID_SYS, ret == 0,E_SYSTEM, "[E_SYSTEM] It failed to lock the screen state. error code [%d]", ret);
+                       SysLog(NID_SYS, "Screen on state is recoveryed.");
+               }
        }
        else
        {
@@ -111,7 +119,6 @@ _ActiveEventListener::OnActiveAppChanged(const Tizen::App::AppId& appId)
                {
                        ret = pm_unlock_state(LCD_NORMAL, GOTO_STATE_NOW);
                        SysTryReturnVoidResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to unlock the power state");
-                       _keepScreenOnState = false;
                }
 
                if (_keepCpuAwake == true || _deviceCpuPowerOn == true) //In case of CPU power control, it has to be keep despite of deactive state.
@@ -401,6 +408,8 @@ _PowerManagerImpl::KeepScreenOnState(bool keepOn, bool dimming)
                {
                        state = LCD_NORMAL;
                }
+               _dimmingFlag = flag;
+               _screenOnState = state;
                ret = pm_lock_state(state, flag, 0);
                SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] It failed to lock the screen state");
                _keepScreenOnState = true;
@@ -446,7 +455,7 @@ _PowerManagerImpl::KeepCpuAwake(bool enable)
 result
 _PowerManagerImpl::InitBatteryEvent(void)
 {
-       int ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, BatteryLevelVConfCallback, null); 
+       int ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, BatteryLevelVConfCallback, null);
        SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "It failed to set the battery event");
 
        return E_SUCCESS;
@@ -475,10 +484,10 @@ _PowerManagerImpl::RestoreScreenBrightness(void)
        SysTryReturn(NID_SYS, IsActive() == true, E_SUCCESS, r, "It is not active app.");
 
        unique_ptr<_IpcClient> pIpcClient (new (std::nothrow) _IpcClient());
-
        SysTryReturn(NID_SYS, pIpcClient != null, E_OUT_OF_MEMORY, r, "It is failed to create IPC instance.");
 
-       r = pIpcClient->Construct(_POWER_MANAGER_SERVICE_ID);
+       SysLog(NID_SYS, "PowerManager is serviced by common-service");
+       r = pIpcClient->Construct(POWER_MANAGER_SERVICE_ID);
        SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to create IpcClient", GetErrorMessage(r));
 
        requestMessage.Construct();
@@ -513,7 +522,6 @@ _PowerManagerImpl::SetScreenBrightness(int brightness)
        int actualBrightness = 0;
        result r = E_SUCCESS;
 
-       SysTryReturn(NID_SYS, IsActive() == true, E_SUCCESS, r, "It is not active app.");
        SysTryReturnResult(NID_SYS, (brightness > 0 && brightness < 11), E_OUT_OF_RANGE,
                                  "[E_OUT_OF_RANGE] The specified brightness is out of range.");
 
@@ -530,7 +538,8 @@ _PowerManagerImpl::SetScreenBrightness(int brightness)
        unique_ptr<_IpcClient> pIpcClient (new (std::nothrow) _IpcClient());
        SysTryReturn(NID_SYS, pIpcClient != null, E_OUT_OF_MEMORY, r, "It is failed to create IPC instance.");
 
-       r = pIpcClient->Construct(_POWER_MANAGER_SERVICE_ID);
+       SysLog(NID_SYS, "PowerManager is serviced by common-service");
+       r = pIpcClient->Construct(POWER_MANAGER_SERVICE_ID);
        SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] It failed to create IpcClient", GetErrorMessage(r));
 
        requestMessage.Construct();
@@ -659,10 +668,15 @@ _PowerManagerImpl::AddScreenEventListener(IScreenEventListener& listener, bool i
                pSceenEventList->Construct();
                int errorCode = 0;
                errorCode = vconf_notify_key_changed(VCONFKEY_PM_CURRENT_BRIGHTNESS, ScreenEventVConfCallback, null);
-               SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It failed to add ScreenEvent listener.");
+               SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It failed to add ScreenEvent listener on VCONFKEY_PM_CURRENT_BRIGHTNESS.");
 
                errorCode = power_set_changed_cb(PowerChangedCallback, null);
-               SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It failed to add ScreenEvent listener.");
+               if(errorCode != POWER_ERROR_NONE)
+               {
+                       SysLogException(NID_SYS, E_SYSTEM, "It is failed to register power callback. So unregister VCONFKEY_PM_CURRENT_BRIGHTNESS also.");
+                       vconf_ignore_key_changed(VCONFKEY_PM_CURRENT_BRIGHTNESS, ScreenEventVConfCallback);
+               }
+               SysTryReturnResult(NID_SYS, errorCode == POWER_ERROR_NONE, E_SYSTEM, "It failed to add ScreenEvent listener.");
                __pSceenEventList = pSceenEventList.release();
                SysLog(NID_SYS,"It successed to register power_set_changed_cb");
        }
@@ -687,13 +701,8 @@ _PowerManagerImpl::RemoveScreenEventListener(IScreenEventListener& listener)
        SysTryReturnResult(NID_SYS,__pSceenEventList != null, E_OBJ_NOT_FOUND, "IScreenEventListener list is empty");
 
        r = __pSceenEventList->Remove(&listener);
-
-       if (__pSceenEventList->GetCount() == 0)
-       {
-               delete __pSceenEventList;
-               __pSceenEventList = null;
-       }
-       return r;
+       SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_OBJ_NOT_FOUND, "listener is not exist.");
+       return E_SUCCESS;
 }
 
 
@@ -719,18 +728,18 @@ _PowerManagerImpl::AddChargingEventListener(IChargingEventListener& listener, bo
 
         if (__pChargingEventList == null)
         {
-                __pChargingEventList = new ArrayListT<IChargingEventListener*>;
-                SysTryReturnResult(NID_SYS, __pChargingEventList, E_SYSTEM, "Memory allocation failed");
-                __pChargingEventList->Construct();
-                int errorCode = 0;
-               errorCode = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, RuntimeInfoChangedCallback, null);
-               if (errorCode != 0)
-                {
-                        SysLog(NID_SYS, "It failed to register battery charging  event");
+                int errorCode = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, RuntimeInfoChangedCallback, null);
+               SysTryReturnResult(NID_SYS, errorCode == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to register RUNTIME_INFO_KEY_BATTERY_IS_CHARGING.");
+
+               __pChargingEventList = new ArrayListT<IChargingEventListener*>;
+               SysTryReturnResult(NID_SYS, __pChargingEventList, E_SYSTEM, "Memory allocation failed");
+               r = __pChargingEventList->Construct();
+               if(r != E_SUCCESS)
+               {
                        delete __pChargingEventList;
                        __pChargingEventList = null;
-                       return E_SYSTEM;
-                }
+               }
+               SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to construct charging event list.");
         }
 
        if (isSet == false || __pChargingEventList->GetCount() == 0)
@@ -751,15 +760,10 @@ _PowerManagerImpl::RemoveChargingEventListener(IChargingEventListener& listener)
         result r = E_SUCCESS;
 
         SysTryReturnResult(NID_SYS,__pChargingEventList != null, E_OBJ_NOT_FOUND, "__pChargingEventList list is empty");
-
         r = __pChargingEventList->Remove(&listener);
+       SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_OBJ_NOT_FOUND, "listener is not exist.");
 
-        if (__pChargingEventList->GetCount() == 0)
-        {
-                delete __pChargingEventList;
-                __pChargingEventList = null;
-        }
-        return r;
+        return E_SUCCESS;
 }
 
 result
@@ -963,12 +967,7 @@ _PowerManagerImpl::RemoveBootEventListener(IBootEventListener& listener)
        SysTryReturnResult(NID_SYS,__pBootEventList != null, E_OBJ_NOT_FOUND, "IBootEventListener list is empty");
 
        r = __pBootEventList->Remove(&listener);
-
-       if (__pBootEventList->GetCount() == 0)
-       {
-               delete __pBootEventList;
-               __pBootEventList = null;
-       }
+       SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_OBJ_NOT_FOUND, "IBootEventListener list is empty");
 
        return E_SUCCESS;
 }