apply dynamic singleton pattern to internal classes and add retry code to AppWidgetMa...
[framework/osp/shell.git] / src / core / FShell_LockManagerImpl.cpp
1 //
2 // Copyright (c) 2013 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        FShell_LockManagerImpl.cpp
19  * @brief       This is the implementation for the _LockManagerImpl class.
20  */
21
22 #include <unique_ptr.h>
23 #include <vconf.h>
24 #include <FBaseLog.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseRt.h>
27 #include <FShell_LockManagerImpl.h>
28 #include <FShell_LockManagerProxy.h>
29
30 namespace Tizen { namespace Shell
31 {
32
33 #define VCONFKEY_IDLE_LOCK_STATE                    "memory/idle_lock/state"
34 #define VCONFKEY_SETAPPL_PREFIX  "db/setting"
35 #define VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT          VCONFKEY_SETAPPL_PREFIX"/screen_lock_type"
36
37 enum
38 {
39         VCONFKEY_IDLE_UNLOCK = 0x00,
40         VCONFKEY_IDLE_LOCK
41 };
42
43 enum
44 {
45        SETTING_SCREEN_LOCK_TYPE_SWIPE = 0,
46        SETTING_SCREEN_LOCK_TYPE_MOTION,
47        SETTING_SCREEN_LOCK_TYPE_FACE_AND_VOICE,
48        SETTING_SCREEN_LOCK_TYPE_SIMPLE_PASSWORD,
49        SETTING_SCREEN_LOCK_TYPE_PASSWORD,
50        SETTING_SCREEN_LOCK_TYPE_OTHER,
51        SETTING_SCREEN_LOCK_TYPE_MAX
52 };
53
54
55 _LockManagerImpl* _LockManagerImpl::__pTheInstance = null;
56
57 _LockManagerImpl::_LockManagerImpl(void)
58 {
59 }
60
61 _LockManagerImpl::~_LockManagerImpl(void)
62 {
63 }
64
65 void
66 _LockManagerImpl::InitSingleton(void)
67 {
68         std::unique_ptr<_LockManagerImpl> pInst(new (std::nothrow) _LockManagerImpl());
69         SysTryReturnVoidResult(NID_SHELL, pInst, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
70
71         result r = pInst->Construct();
72         SysTryReturnVoidResult(NID_SHELL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
73
74         __pTheInstance = pInst.release();
75         std::atexit(DestroySingleton);
76 }
77
78 void
79 _LockManagerImpl::DestroySingleton(void)
80 {
81         delete __pTheInstance;
82 }
83
84 _LockManagerImpl*
85 _LockManagerImpl::GetInstance(void)
86 {
87         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
88         if (__pTheInstance == null)
89         {
90                 ClearLastResult();
91                 pthread_once(&onceBlock, InitSingleton);
92                 result r = GetLastResult();
93                 if (IsFailed(r))
94                 {
95                         onceBlock = PTHREAD_ONCE_INIT;
96                 }
97         }
98         return __pTheInstance;
99 }
100
101 result
102 _LockManagerImpl::Construct(void)
103 {
104         return E_SUCCESS;
105 }
106
107 bool
108 _LockManagerImpl::IsLocked(void) const
109 {
110         int idleLockState = 0;
111         vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &idleLockState);
112         return (idleLockState == VCONFKEY_IDLE_LOCK);
113 }
114
115 bool
116 _LockManagerImpl::IsSecureMode(void) const
117 {
118         int screenLockType = 0;
119         vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &screenLockType);
120         
121         switch(screenLockType)
122         {
123                 case SETTING_SCREEN_LOCK_TYPE_SWIPE:
124                 case SETTING_SCREEN_LOCK_TYPE_MOTION:
125                         return false;
126
127                 case SETTING_SCREEN_LOCK_TYPE_FACE_AND_VOICE:
128                 case SETTING_SCREEN_LOCK_TYPE_SIMPLE_PASSWORD:
129                 case SETTING_SCREEN_LOCK_TYPE_PASSWORD:
130                 case SETTING_SCREEN_LOCK_TYPE_OTHER:
131                         return true;
132
133                 default:
134                         SysAssert(false);
135         }
136
137         return false;
138 }
139
140 result
141 _LockManagerImpl::Unlock(void)
142 {
143         if( IsLocked() == false )
144         {
145                 return E_SUCCESS;
146         }
147
148         _LockManagerProxy* pLockManagerProxy = _LockManagerProxy::GetInstance();
149         SysTryReturnResult(NID_SHELL, pLockManagerProxy, E_SYSTEM, "Failed to _LockManagerProxy::GetInstance()!");
150
151         return pLockManagerProxy->Unlock();
152 }
153
154
155 }} // Tizen::Shell