• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef SERVICES_INCLUDE_SCLOCK_SERVICES_H
17 #define SERVICES_INCLUDE_SCLOCK_SERVICES_H
18 
19 #include <atomic>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 
24 #include "dm_common.h"
25 #include "event_handler.h"
26 #include "ffrt.h"
27 #include "iremote_object.h"
28 #include "screenlock_callback_interface.h"
29 #include "screenlock_manager_stub.h"
30 #include "screenlock_system_ability_interface.h"
31 #include "system_ability.h"
32 #include "visibility.h"
33 #include "os_account_manager.h"
34 #include "preferences_util.h"
35 #include "os_account_subscribe_info.h"
36 
37 namespace OHOS {
38 namespace ScreenLock {
39 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
40 class StateValue {
41 public:
StateValue()42     StateValue(){};
~StateValue()43     ~StateValue(){};
44 
45     void Reset();
46 
SetScreenlockEnabled(bool screenlockEnabled)47     void SetScreenlockEnabled(bool screenlockEnabled)
48     {
49         screenlockEnabled_ = screenlockEnabled;
50     };
51 
SetScreenState(int32_t screenState)52     void SetScreenState(int32_t screenState)
53     {
54         screenState_ = screenState;
55     };
56 
SetOffReason(int32_t offReason)57     void SetOffReason(int32_t offReason)
58     {
59         offReason_ = offReason;
60     };
61 
SetCurrentUser(int32_t currentUser)62     void SetCurrentUser(int32_t currentUser)
63     {
64         currentUser_ = currentUser;
65     };
66 
SetInteractiveState(int32_t interactiveState)67     void SetInteractiveState(int32_t interactiveState)
68     {
69         interactiveState_ = interactiveState;
70     };
71 
GetScreenlockEnabled()72     bool GetScreenlockEnabled()
73     {
74         return screenlockEnabled_;
75     };
76 
GetScreenState()77     int32_t GetScreenState()
78     {
79         return screenState_;
80     };
81 
GetOffReason()82     int32_t GetOffReason()
83     {
84         return offReason_;
85     };
86 
GetCurrentUser()87     int32_t GetCurrentUser()
88     {
89         return currentUser_;
90     };
91 
GetInteractiveState()92     int32_t GetInteractiveState()
93     {
94         return interactiveState_;
95     };
96 
97 private:
98     std::atomic<bool> screenlockEnabled_ { false };
99     std::atomic<int32_t> offReason_ {0};
100     std::atomic<int32_t> currentUser_ {0};
101     std::atomic<int32_t> screenState_ {0};
102     std::atomic<int32_t> interactiveState_ {0};
103 };
104 
105 enum class ScreenState : int32_t {
106     SCREEN_STATE_BEGIN_OFF = 0,
107     SCREEN_STATE_END_OFF = 1,
108     SCREEN_STATE_BEGIN_ON = 2,
109     SCREEN_STATE_END_ON = 3,
110 };
111 
112 enum class InteractiveState : int32_t {
113     INTERACTIVE_STATE_END_SLEEP = 0,
114     INTERACTIVE_STATE_BEGIN_WAKEUP = 1,
115     INTERACTIVE_STATE_END_WAKEUP = 2,
116     INTERACTIVE_STATE_BEGIN_SLEEP = 3,
117 };
118 
119 enum class AuthState : int32_t {
120     UNAUTH = 0,
121     PRE_AUTHED_BY_CREDENTIAL = 1,
122     PRE_AUTHED_BY_FINGERPRINT = 2,
123     PRE_AUTHED_BY_FACE = 3,
124     AUTHED_BY_CREDENTIAL = 4,
125     AUTHED_BY_FINGERPRINT = 5,
126     AUTHED_BY_FACE = 6,
127 };
128 
129 class ScreenLockSystemAbility : public SystemAbility, public ScreenLockManagerStub {
130     DECLARE_SYSTEM_ABILITY(ScreenLockSystemAbility);
131 
132 public:
133     DISALLOW_COPY_AND_MOVE(ScreenLockSystemAbility);
134     ScreenLockSystemAbility(int32_t systemAbilityId, bool runOnCreate);
135     ScreenLockSystemAbility();
136     ~ScreenLockSystemAbility() override;
137     SCREENLOCK_API static sptr<ScreenLockSystemAbility> GetInstance();
138     int32_t IsLocked(bool &isLocked) override;
139     bool IsScreenLocked() override;
140     int32_t IsLockedWithUserId(int32_t userId, bool &isLocked) override;
141     bool GetSecure() override;
142     int32_t Unlock(const sptr<ScreenLockCallbackInterface> &listener) override;
143     int32_t UnlockScreen(const sptr<ScreenLockCallbackInterface> &listener) override;
144     int32_t Lock(const sptr<ScreenLockCallbackInterface> &listener) override;
145     int32_t OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> &listener) override;
146     int32_t SendScreenLockEvent(const std::string &event, int param) override;
147     int32_t IsScreenLockDisabled(int userId, bool &isDisabled) override;
148     int32_t SetScreenLockDisabled(bool disable, int userId) override;
149     int32_t SetScreenLockAuthState(int authState, int32_t userId, std::string &authToken) override;
150     int32_t GetScreenLockAuthState(int userId, int32_t &authState) override;
151     int32_t RequestStrongAuth(int reasonFlag, int32_t userId) override;
152     int32_t GetStrongAuth(int userId, int32_t &reasonFlag) override;
153     int32_t IsDeviceLocked(int userId, bool &isDeviceLocked) override;
154     int32_t RegisterInnerListener(const int32_t userId, const ListenType listenType,
155                                   const sptr<InnerListenerIf>& listener) override;
156     int32_t UnRegisterInnerListener(const int32_t userId, const ListenType listenType,
157                                          const sptr<InnerListenerIf>& listener) override;
158     int Dump(int fd, const std::vector<std::u16string> &args) override;
159     void SetScreenlocked(bool isScreenlocked, const int32_t userId);
160     void RegisterDisplayPowerEventListener(int32_t times);
161     void ResetFfrtQueue();
162     void StrongAuthChanged(int32_t userId, int32_t reasonFlag);
163     int32_t Lock(int32_t userId) override;
GetState()164     StateValue &GetState()
165     {
166         return stateValue_;
167     }
168     class ScreenLockDisplayPowerEventListener : public Rosen::IDisplayPowerEventListener {
169     public:
170         void OnDisplayPowerEvent(Rosen::DisplayPowerEvent event, Rosen::EventStatus status) override;
171     };
172 
173     class AccountSubscriber : public AccountSA::OsAccountSubscriber {
174     public:
175         explicit AccountSubscriber(const AccountSA::OsAccountSubscribeInfo &subscribeInfo);
176         ~AccountSubscriber() override = default;
GetUserId()177         int GetUserId() { return userId_; }
178         void OnAccountsChanged(const int &id) override;
179 
180     private:
181         int userId_{-1};
182     };
183 
184     class AccountRemoveSubscriber : public AccountSA::OsAccountSubscriber {
185     public:
186         explicit AccountRemoveSubscriber(const AccountSA::OsAccountSubscribeInfo &subscribeInfo);
187         ~AccountRemoveSubscriber() override = default;
188         void OnAccountsChanged(const int &id) override;
189     };
190 
191 protected:
192     void OnStart() override;
193     void OnStop() override;
194     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
195 
196 private:
197     void OnScreenOn(Rosen::EventStatus status);
198     void OnScreenOff(Rosen::EventStatus status);
199     void OnWakeUp(Rosen::EventStatus status);
200     void OnSleep(Rosen::EventStatus status);
201     void OnExitAnimation();
202     void OnSystemReady();
203     void RegisterDumpCommand();
204     int32_t Init();
205     void InitUserId();
206     void InitServiceHandler();
207     void LockScreenEvent(int stateResult);
208     void UnlockScreenEvent(int stateResult);
209     void SystemEventCallBack(const SystemEvent &systemEvent, TraceTaskId traceTaskId = HITRACE_BUTT);
210     int32_t UnlockInner(const sptr<ScreenLockCallbackInterface> &listener);
211     void PublishEvent(const std::string &eventAction, const int32_t userId);
212     bool IsAppInForeground(int32_t callingPid, uint32_t callingTokenId);
213     bool IsSystemApp();
214     bool CheckPermission(const std::string &permissionName);
215     void NotifyUnlockListener(const int32_t screenLockResult);
216     void NotifyDisplayEvent(Rosen::DisplayEvent event);
217     bool getDeviceLockedStateByAuth(int authState);
218     void onRemoveUser(const int userId);
219     void subscribeAcccount();
220     void authStateInit(const int userId);
221 
222     ServiceRunningState state_;
223     static std::mutex instanceLock_;
224     static sptr<ScreenLockSystemAbility> instance_;
225     static std::shared_ptr<ffrt::queue> queue_;
226     std::shared_ptr<AccountSubscriber> accountSubscriber_;
227     std::mutex accountSubscriberMutex_;
228     std::shared_ptr<AccountRemoveSubscriber> accountRemoveSubscriber_;
229     std::mutex accounRemovetSubscriberMutex_;
230     sptr<Rosen::IDisplayPowerEventListener> displayPowerEventListener_;
231     std::mutex listenerMutex_;
232     sptr<ScreenLockSystemAbilityInterface> systemEventListener_;
233     std::mutex unlockListenerMutex_;
234     std::vector<sptr<ScreenLockCallbackInterface>> unlockVecListeners_;
235     std::mutex lockListenerMutex_;
236     std::vector<sptr<ScreenLockCallbackInterface>> lockVecListeners_;
237     StateValue stateValue_;
238     std::atomic<bool> systemReady_ = false;
239     std::map<int32_t, int32_t> authStateInfo;
240     std::mutex authStateMutex_;
241     std::map<int32_t, bool> isScreenlockedMap_;
242     std::mutex screenLockMutex_;
243 };
244 } // namespace ScreenLock
245 } // namespace OHOS
246 #endif // SERVICES_INCLUDE_SCLOCK_SERVICES_H
247