• 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 <mutex>
20 #include <string>
21 #include <vector>
22 
23 #include "dm_common.h"
24 #include "event_handler.h"
25 #include "iremote_object.h"
26 #include "system_ability.h"
27 #include "window_manager.h"
28 
29 #include "screenlock_manager_stub.h"
30 #include "screenlock_system_ability_interface.h"
31 
32 namespace OHOS {
33 namespace ScreenLock {
34 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
35 class IKeyguardStateCallback;
36 class StateValue {
37 public:
StateValue()38     StateValue() {};
~StateValue()39     ~StateValue() {};
40 
41     void Reset();
42 
SetScreenlocked(bool isScreenlocked)43     void SetScreenlocked(bool isScreenlocked)
44     {
45         isScreenlocked_ = isScreenlocked;
46     };
47 
SetScreenlockEnabled(bool screenlockEnabled)48     void SetScreenlockEnabled(bool screenlockEnabled)
49     {
50         screenlockEnabled_ = screenlockEnabled;
51     };
52 
SetScreenState(int screenState)53     void SetScreenState(int screenState)
54     {
55         screenState_ = screenState;
56     };
57 
SetOffReason(int offReason)58     void SetOffReason(int offReason)
59     {
60         offReason_ = offReason;
61     };
62 
SetCurrentUser(int currentUser)63     void SetCurrentUser(int currentUser)
64     {
65         currentUser_ = currentUser;
66     };
67 
SetInteractiveState(int interactiveState)68     void SetInteractiveState(int interactiveState)
69     {
70         interactiveState_ = interactiveState;
71     };
72 
GetScreenlockedState()73     bool GetScreenlockedState()
74     {
75         return isScreenlocked_;
76     };
77 
GetScreenlockEnabled()78     bool GetScreenlockEnabled()
79     {
80         return screenlockEnabled_;
81     };
82 
GetScreenState()83     int GetScreenState()
84     {
85         return screenState_;
86     };
87 
GetOffReason()88     int GetOffReason()
89     {
90         return offReason_;
91     };
92 
GetCurrentUser()93     int GetCurrentUser()
94     {
95         return currentUser_;
96     };
97 
GetInteractiveState()98     int GetInteractiveState()
99     {
100         return interactiveState_;
101     };
102 
103 private:
104     bool isScreenlocked_ = false;
105     bool screenlockEnabled_ = false;
106     int offReason_ = 0;
107     int currentUser_ = 0;
108     int screenState_ = 0;
109     int interactiveState_ = 0;
110 };
111 
112 enum class ScreenState {
113     SCREEN_STATE_BEGIN_OFF = 0,
114     SCREEN_STATE_END_OFF = 1,
115     SCREEN_STATE_BEGIN_ON = 2,
116     SCREEN_STATE_END_ON = 3,
117 };
118 
119 enum class InteractiveState {
120     INTERACTIVE_STATE_END_SLEEP = 0,
121     INTERACTIVE_STATE_BEGIN_WAKEUP = 1,
122     INTERACTIVE_STATE_END_WAKEUP = 2,
123     INTERACTIVE_STATE_BEGIN_SLEEP = 3,
124 };
125 
126 class ScreenLockSystemAbility : public SystemAbility, public ScreenLockManagerStub {
127     DECLARE_SYSTEM_ABILITY(ScreenLockSystemAbility);
128 
129 public:
130     DISALLOW_COPY_AND_MOVE(ScreenLockSystemAbility);
131     ScreenLockSystemAbility(int32_t systemAbilityId, bool runOnCreate);
132     ScreenLockSystemAbility();
133     ~ScreenLockSystemAbility();
134     static sptr<ScreenLockSystemAbility> GetInstance();
135     bool IsScreenLocked() override;
136     bool GetSecure() override;
137     void RequestUnlock(const sptr<ScreenLockSystemAbilityInterface> &listener) override;
138     bool On(const sptr<ScreenLockSystemAbilityInterface> &listener, const std::string &type) override;
139     bool Off(const std::string &type) override;
140     bool SendScreenLockEvent(const std::string &event, int param) override;
141     void SetScreenlocked(bool isScreenlocked);
GetState()142     StateValue &GetState()
143     {
144         return stateValue_;
145     }
146     bool Test_SetScreenLocked(const bool isScreenlocked) override;
147     bool Test_RuntimeNotify(const std::string &event, int param) override;
148     int Test_GetRuntimeState(const std::string &event) override;
149 
150     class ScreenLockFocusChangedListener : public Rosen::IFocusChangedListener {
151     public:
152         void OnFocused(const sptr<Rosen::FocusChangeInfo> &focusChangeInfo) override;
153         void OnUnfocused(const sptr<Rosen::FocusChangeInfo> &focusChangeInfo) override;
154     };
155 
156     class ScreenLockDisplayPowerEventListener : public Rosen::IDisplayPowerEventListener {
157     public:
158         void OnDisplayPowerEvent(Rosen::DisplayPowerEvent event, Rosen::EventStatus status) override;
159     };
160 
161 protected:
162     void OnDump() override;
163     void OnStart() override;
164     void OnStop() override;
165 
166 private:
167     void OnBeginScreenOn();
168     void OnEndScreenOn();
169     void OnBeginScreenOff();
170     void OnEndScreenOff();
171     void OnBeginWakeUp();
172     void OnEndWakeUp();
173     void OnBeginSleep(const int why);
174     void OnEndSleep(const int why, const int isTriggered);
175     void OnChangeUser(const int newUserId);
176     void OnScreenlockEnabled(bool enable);
177     void OnExitAnimation();
178     void OnSystemReady();
179     int32_t Init();
180     ServiceRunningState state_;
181     void InitServiceHandler();
182     static std::mutex instanceLock_;
183     static sptr<ScreenLockSystemAbility> instance_;
184     static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
185     sptr<Rosen::IFocusChangedListener> focusChangedListener_;
186     sptr<Rosen::IDisplayPowerEventListener> displayPowerEventListener_;
187     std::map<std::string, sptr<ScreenLockSystemAbilityInterface>> registeredListeners_;
188     std::vector<sptr<ScreenLockSystemAbilityInterface>> unlockVecListeners_;
189     StateValue stateValue_;
190     std::mutex listenerMapMutex_;
191     std::mutex lock_;
192     const int32_t startTime_ = 1900;
193     const int32_t extraMonth_ = 1;
194     bool isFoucs_ = false;
195     bool flag_ = false;
196 };
197 } // namespace ScreenLock
198 } // namespace OHOS
199 #endif // SERVICES_INCLUDE_SCLOCK_SERVICES_H
200