• 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();
148     int32_t Lock(int32_t userId) override;
GetState()149     StateValue &GetState()
150     {
151         return stateValue_;
152     }
153     class ScreenLockDisplayPowerEventListener : public Rosen::IDisplayPowerEventListener {
154     public:
155         void OnDisplayPowerEvent(Rosen::DisplayPowerEvent event, Rosen::EventStatus status) override;
156     };
157 
158 protected:
159     void OnStart() override;
160     void OnStop() override;
161     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
162 
163 private:
164     void OnScreenOn(Rosen::EventStatus status);
165     void OnScreenOff(Rosen::EventStatus status);
166     void OnWakeUp(Rosen::EventStatus status);
167     void OnSleep(Rosen::EventStatus status);
168     void OnExitAnimation();
169     void OnSystemReady();
170     void RegisterDumpCommand();
171     int32_t Init();
172     void InitServiceHandler();
173     void LockScreenEvent(int stateResult);
174     void UnlockScreenEvent(int stateResult);
175     void SystemEventCallBack(const SystemEvent &systemEvent, TraceTaskId traceTaskId = HITRACE_BUTT);
176     int32_t UnlockInner(const sptr<ScreenLockCallbackInterface> &listener);
177     void PublishEvent(const std::string &eventAction);
178     bool IsAppInForeground(int32_t callingPid, uint32_t callingTokenId);
179     bool IsSystemApp();
180     bool CheckPermission(const std::string &permissionName);
181     void NotifyUnlockListener(const int32_t screenLockResult);
182     void NotifyDisplayEvent(Rosen::DisplayEvent event);
183 
184     ServiceRunningState state_;
185     static std::mutex instanceLock_;
186     static sptr<ScreenLockSystemAbility> instance_;
187     static std::shared_ptr<ffrt::queue> queue_;
188     sptr<Rosen::IDisplayPowerEventListener> displayPowerEventListener_;
189     std::mutex listenerMutex_;
190     sptr<ScreenLockSystemAbilityInterface> systemEventListener_;
191     std::mutex unlockListenerMutex_;
192     std::vector<sptr<ScreenLockCallbackInterface>> unlockVecListeners_;
193     std::mutex lockListenerMutex_;
194     std::vector<sptr<ScreenLockCallbackInterface>> lockVecListeners_;
195     StateValue stateValue_;
196     std::atomic<bool> systemReady_ = false;
197 };
198 } // namespace ScreenLock
199 } // namespace OHOS
200 #endif // SERVICES_INCLUDE_SCLOCK_SERVICES_H
201