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 
SetScreenlocked(bool isScreenlocked)47     void SetScreenlocked(bool isScreenlocked)
48     {
49         isScreenlocked_ = isScreenlocked;
50     };
51 
SetScreenlockEnabled(bool screenlockEnabled)52     void SetScreenlockEnabled(bool screenlockEnabled)
53     {
54         screenlockEnabled_ = screenlockEnabled;
55     };
56 
SetScreenState(int32_t screenState)57     void SetScreenState(int32_t screenState)
58     {
59         screenState_ = screenState;
60     };
61 
SetOffReason(int32_t offReason)62     void SetOffReason(int32_t offReason)
63     {
64         offReason_ = offReason;
65     };
66 
SetCurrentUser(int32_t currentUser)67     void SetCurrentUser(int32_t currentUser)
68     {
69         currentUser_ = currentUser;
70     };
71 
SetInteractiveState(int32_t interactiveState)72     void SetInteractiveState(int32_t interactiveState)
73     {
74         interactiveState_ = interactiveState;
75     };
76 
GetScreenlockedState()77     bool GetScreenlockedState()
78     {
79         return isScreenlocked_;
80     };
81 
GetScreenlockEnabled()82     bool GetScreenlockEnabled()
83     {
84         return screenlockEnabled_;
85     };
86 
GetScreenState()87     int32_t GetScreenState()
88     {
89         return screenState_;
90     };
91 
GetOffReason()92     int32_t GetOffReason()
93     {
94         return offReason_;
95     };
96 
GetCurrentUser()97     int32_t GetCurrentUser()
98     {
99         return currentUser_;
100     };
101 
GetInteractiveState()102     int32_t GetInteractiveState()
103     {
104         return interactiveState_;
105     };
106 
107 private:
108     std::atomic<bool> isScreenlocked_ { false };
109     std::atomic<bool> screenlockEnabled_ { false };
110     std::atomic<int32_t> offReason_ {0};
111     std::atomic<int32_t> currentUser_ {0};
112     std::atomic<int32_t> screenState_ {0};
113     std::atomic<int32_t> interactiveState_ {0};
114 };
115 
116 enum class ScreenState : int32_t {
117     SCREEN_STATE_BEGIN_OFF = 0,
118     SCREEN_STATE_END_OFF = 1,
119     SCREEN_STATE_BEGIN_ON = 2,
120     SCREEN_STATE_END_ON = 3,
121 };
122 
123 enum class InteractiveState : int32_t {
124     INTERACTIVE_STATE_END_SLEEP = 0,
125     INTERACTIVE_STATE_BEGIN_WAKEUP = 1,
126     INTERACTIVE_STATE_END_WAKEUP = 2,
127     INTERACTIVE_STATE_BEGIN_SLEEP = 3,
128 };
129 
130 enum class AuthState : int32_t {
131     UNAUTH = 0,
132     PRE_AUTHED_BY_CREDENTIAL = 1,
133     PRE_AUTHED_BY_BIOMIETRIC = 2,
134     AUTHED_BY_CREDENTIAL = 3,
135     AUTHED_BY_BIOMIETRIC = 4,
136 };
137 
138 class ScreenLockSystemAbility : public SystemAbility, public ScreenLockManagerStub {
139     DECLARE_SYSTEM_ABILITY(ScreenLockSystemAbility);
140 
141 public:
142     DISALLOW_COPY_AND_MOVE(ScreenLockSystemAbility);
143     ScreenLockSystemAbility(int32_t systemAbilityId, bool runOnCreate);
144     ScreenLockSystemAbility();
145     ~ScreenLockSystemAbility() override;
146     SCREENLOCK_API static sptr<ScreenLockSystemAbility> GetInstance();
147     int32_t IsLocked(bool &isLocked) override;
148     bool IsScreenLocked() override;
149     bool GetSecure() override;
150     int32_t Unlock(const sptr<ScreenLockCallbackInterface> &listener) override;
151     int32_t UnlockScreen(const sptr<ScreenLockCallbackInterface> &listener) override;
152     int32_t Lock(const sptr<ScreenLockCallbackInterface> &listener) override;
153     int32_t OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> &listener) override;
154     int32_t SendScreenLockEvent(const std::string &event, int param) override;
155     int32_t IsScreenLockDisabled(int userId, bool &isDisabled) override;
156     int32_t SetScreenLockDisabled(bool disable, int userId) override;
157     int32_t SetScreenLockAuthState(int authState, int32_t userId, std::string &authToken) override;
158     int32_t GetScreenLockAuthState(int userId, int32_t &authState) override;
159     int32_t RequestStrongAuth(int reasonFlag, int32_t userId) override;
160     int32_t GetStrongAuth(int userId, int32_t &reasonFlag) override;
161     int Dump(int fd, const std::vector<std::u16string> &args) override;
162     void SetScreenlocked(bool isScreenlocked);
163     void RegisterDisplayPowerEventListener(int32_t times);
164     void ResetFfrtQueue();
165     void StrongAuthChanged(int32_t userId, int32_t reasonFlag);
166     int32_t Lock(int32_t userId) override;
GetState()167     StateValue &GetState()
168     {
169         return stateValue_;
170     }
171     class ScreenLockDisplayPowerEventListener : public Rosen::IDisplayPowerEventListener {
172     public:
173         void OnDisplayPowerEvent(Rosen::DisplayPowerEvent event, Rosen::EventStatus status) override;
174     };
175 
176     class AccountSubscriber : public AccountSA::OsAccountSubscriber {
177     public:
178         explicit AccountSubscriber(const AccountSA::OsAccountSubscribeInfo &subscribeInfo);
179         ~AccountSubscriber() override = default;
GetUserId()180         int GetUserId() { return userId_; }
181         void OnAccountsChanged(const int &id) override;
182 
183     private:
184         int userId_{-1};
185     };
186 
187 protected:
188     void OnStart() override;
189     void OnStop() override;
190     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
191 
192 private:
193     void OnScreenOn(Rosen::EventStatus status);
194     void OnScreenOff(Rosen::EventStatus status);
195     void OnWakeUp(Rosen::EventStatus status);
196     void OnSleep(Rosen::EventStatus status);
197     void OnExitAnimation();
198     void OnSystemReady();
199     void RegisterDumpCommand();
200     int32_t Init();
201     void InitUserId();
202     void InitServiceHandler();
203     void LockScreenEvent(int stateResult);
204     void UnlockScreenEvent(int stateResult);
205     void SystemEventCallBack(const SystemEvent &systemEvent, TraceTaskId traceTaskId = HITRACE_BUTT);
206     int32_t UnlockInner(const sptr<ScreenLockCallbackInterface> &listener);
207     void PublishEvent(const std::string &eventAction);
208     bool IsAppInForeground(int32_t callingPid, uint32_t callingTokenId);
209     bool IsSystemApp();
210     bool CheckPermission(const std::string &permissionName);
211     void NotifyUnlockListener(const int32_t screenLockResult);
212     void NotifyDisplayEvent(Rosen::DisplayEvent event);
213 
214     ServiceRunningState state_;
215     static std::mutex instanceLock_;
216     static sptr<ScreenLockSystemAbility> instance_;
217     static std::shared_ptr<ffrt::queue> queue_;
218     std::shared_ptr<AccountSubscriber> accountSubscriber_;
219     sptr<Rosen::IDisplayPowerEventListener> displayPowerEventListener_;
220     std::mutex listenerMutex_;
221     sptr<ScreenLockSystemAbilityInterface> systemEventListener_;
222     std::mutex unlockListenerMutex_;
223     std::vector<sptr<ScreenLockCallbackInterface>> unlockVecListeners_;
224     std::mutex lockListenerMutex_;
225     std::vector<sptr<ScreenLockCallbackInterface>> lockVecListeners_;
226     StateValue stateValue_;
227     std::atomic<bool> systemReady_ = false;
228     std::map<int32_t, int32_t> authStateInfo;
229 };
230 } // namespace ScreenLock
231 } // namespace OHOS
232 #endif // SERVICES_INCLUDE_SCLOCK_SERVICES_H
233