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