• 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 
34 namespace OHOS {
35 namespace ScreenLock {
36 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
37 class StateValue {
38 public:
StateValue()39     StateValue(){};
~StateValue()40     ~StateValue(){};
41 
42     void Reset();
43 
SetScreenlocked(bool isScreenlocked)44     void SetScreenlocked(bool isScreenlocked)
45     {
46         isScreenlocked_ = isScreenlocked;
47     };
48 
SetScreenlockEnabled(bool screenlockEnabled)49     void SetScreenlockEnabled(bool screenlockEnabled)
50     {
51         screenlockEnabled_ = screenlockEnabled;
52     };
53 
SetScreenState(int32_t screenState)54     void SetScreenState(int32_t screenState)
55     {
56         screenState_ = screenState;
57     };
58 
SetOffReason(int32_t offReason)59     void SetOffReason(int32_t offReason)
60     {
61         offReason_ = offReason;
62     };
63 
SetCurrentUser(int32_t currentUser)64     void SetCurrentUser(int32_t currentUser)
65     {
66         currentUser_ = currentUser;
67     };
68 
SetInteractiveState(int32_t interactiveState)69     void SetInteractiveState(int32_t interactiveState)
70     {
71         interactiveState_ = interactiveState;
72     };
73 
GetScreenlockedState()74     bool GetScreenlockedState()
75     {
76         return isScreenlocked_;
77     };
78 
GetScreenlockEnabled()79     bool GetScreenlockEnabled()
80     {
81         return screenlockEnabled_;
82     };
83 
GetScreenState()84     int32_t GetScreenState()
85     {
86         return screenState_;
87     };
88 
GetOffReason()89     int32_t GetOffReason()
90     {
91         return offReason_;
92     };
93 
GetCurrentUser()94     int32_t GetCurrentUser()
95     {
96         return currentUser_;
97     };
98 
GetInteractiveState()99     int32_t GetInteractiveState()
100     {
101         return interactiveState_;
102     };
103 
104 private:
105     std::atomic<bool> isScreenlocked_ = false;
106     std::atomic<bool> screenlockEnabled_ = false;
107     std::atomic<int32_t> offReason_ = 0;
108     std::atomic<int32_t> currentUser_ = 0;
109     std::atomic<int32_t> screenState_ = 0;
110     std::atomic<int32_t> interactiveState_ = 0;
111 };
112 
113 enum class ScreenState : int32_t {
114     SCREEN_STATE_BEGIN_OFF = 0,
115     SCREEN_STATE_END_OFF = 1,
116     SCREEN_STATE_BEGIN_ON = 2,
117     SCREEN_STATE_END_ON = 3,
118 };
119 
120 enum class InteractiveState : int32_t {
121     INTERACTIVE_STATE_END_SLEEP = 0,
122     INTERACTIVE_STATE_BEGIN_WAKEUP = 1,
123     INTERACTIVE_STATE_END_WAKEUP = 2,
124     INTERACTIVE_STATE_BEGIN_SLEEP = 3,
125 };
126 
127 class ScreenLockSystemAbility : public SystemAbility, public ScreenLockManagerStub {
128     DECLARE_SYSTEM_ABILITY(ScreenLockSystemAbility);
129 
130 public:
131     DISALLOW_COPY_AND_MOVE(ScreenLockSystemAbility);
132     ScreenLockSystemAbility(int32_t systemAbilityId, bool runOnCreate);
133     ScreenLockSystemAbility();
134     ~ScreenLockSystemAbility() override;
135     SCREENLOCK_API static sptr<ScreenLockSystemAbility> GetInstance();
136     int32_t IsLocked(bool &isLocked) override;
137     bool IsScreenLocked() override;
138     bool GetSecure() override;
139     int32_t Unlock(const sptr<ScreenLockCallbackInterface> &listener) override;
140     int32_t UnlockScreen(const sptr<ScreenLockCallbackInterface> &listener) override;
141     int32_t Lock(const sptr<ScreenLockCallbackInterface> &listener) override;
142     int32_t OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> &listener) override;
143     int32_t SendScreenLockEvent(const std::string &event, int param) override;
144     int Dump(int fd, const std::vector<std::u16string> &args) override;
145     void SetScreenlocked(bool isScreenlocked);
146     void RegisterDisplayPowerEventListener(int32_t times);
147     void ResetFfrtQueue();
GetState()148     StateValue &GetState()
149     {
150         return stateValue_;
151     }
152     class ScreenLockDisplayPowerEventListener : public Rosen::IDisplayPowerEventListener {
153     public:
154         void OnDisplayPowerEvent(Rosen::DisplayPowerEvent event, Rosen::EventStatus status) override;
155     };
156 
157 protected:
158     void OnStart() override;
159     void OnStop() override;
160     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
161 
162 private:
163     void OnBeginScreenOn();
164     void OnEndScreenOn();
165     void OnBeginScreenOff();
166     void OnEndScreenOff();
167     void OnBeginWakeUp();
168     void OnEndWakeUp();
169     void OnBeginSleep(const int why);
170     void OnEndSleep(const int why, const int isTriggered);
171     void OnChangeUser(const int newUserId);
172     void OnScreenlockEnabled(bool enabled);
173     void OnExitAnimation();
174     void OnSystemReady();
175     void RegisterDumpCommand();
176     int32_t Init();
177     void InitServiceHandler();
178     void LockScreenEvent(int stateResult);
179     void UnlockScreenEvent(int stateResult);
180     void SystemEventCallBack(const SystemEvent &systemEvent, TraceTaskId traceTaskId = HITRACE_BUTT);
181     int32_t UnlockInner(const sptr<ScreenLockCallbackInterface> &listener);
182     void PublishEvent(const std::string &eventAction);
183     bool IsAppInForeground(int32_t uid);
184     bool IsSystemApp();
185     bool CheckPermission(const std::string &permissionName);
186     void NotifyUnlockListener(const int32_t screenLockResult);
187     void Initialize();
188     void NotifyDisplayEvent(Rosen::DisplayEvent event);
189 
190     ServiceRunningState state_;
191     static std::mutex instanceLock_;
192     static sptr<ScreenLockSystemAbility> instance_;
193     static std::shared_ptr<ffrt::queue> queue_;
194     sptr<Rosen::IDisplayPowerEventListener> displayPowerEventListener_;
195     std::mutex listenerMutex_;
196     sptr<ScreenLockSystemAbilityInterface> systemEventListener_;
197     std::mutex unlockListenerMutex_;
198     std::vector<sptr<ScreenLockCallbackInterface>> unlockVecListeners_;
199     std::mutex lockListenerMutex_;
200     std::vector<sptr<ScreenLockCallbackInterface>> lockVecListeners_;
201     StateValue stateValue_;
202     const int32_t startTime_ = 1900;
203     const int32_t extraMonth_ = 1;
204     bool flag_ = false;
205 };
206 } // namespace ScreenLock
207 } // namespace OHOS
208 #endif // SERVICES_INCLUDE_SCLOCK_SERVICES_H
209