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 int32_t IsLocked(bool &isLocked) override; 133 bool IsScreenLocked() override; 134 bool GetSecure() override; 135 int32_t Unlock(const sptr<ScreenLockSystemAbilityInterface> &listener) override; 136 int32_t UnlockScreen(const sptr<ScreenLockSystemAbilityInterface> &listener) override; 137 int32_t Lock(const sptr<ScreenLockSystemAbilityInterface> &listener) override; 138 int32_t OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> &listener) override; 139 int32_t SendScreenLockEvent(const std::string &event, int param) override; 140 int Dump(int fd, const std::vector<std::u16string> &args) override; 141 void SetScreenlocked(bool isScreenlocked); 142 void RegisterDisplayPowerEventListener(int32_t times); GetState()143 StateValue &GetState() 144 { 145 return stateValue_; 146 } 147 class ScreenLockDisplayPowerEventListener : public Rosen::IDisplayPowerEventListener { 148 public: 149 void OnDisplayPowerEvent(Rosen::DisplayPowerEvent event, Rosen::EventStatus status) override; 150 }; 151 152 protected: 153 void OnDump() override; 154 void OnStart() override; 155 void OnStop() override; 156 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 157 158 private: 159 void OnBeginScreenOn(); 160 void OnEndScreenOn(); 161 void OnBeginScreenOff(); 162 void OnEndScreenOff(); 163 void OnBeginWakeUp(); 164 void OnEndWakeUp(); 165 void OnBeginSleep(const int why); 166 void OnEndSleep(const int why, const int isTriggered); 167 void OnChangeUser(const int newUserId); 168 void OnScreenlockEnabled(bool enable); 169 void OnExitAnimation(); 170 void OnSystemReady(); 171 void RegisterDumpCommand(); 172 int32_t Init(); 173 void InitServiceHandler(); 174 static bool IsAppInForeground(uint32_t tokenId); 175 void LockScreenEvent(int stateResult); 176 void UnlockScreenEvent(int stateResult); 177 std::string GetScreenlockParameter(const std::string &key) const; 178 bool IsWhiteListApp(uint32_t callingTokenId, const std::string &key); 179 void SystemEventCallBack(const SystemEvent &systemEvent, TraceTaskId traceTaskId = HITRACE_BUTT); 180 int32_t UnlockInner(const sptr<ScreenLockSystemAbilityInterface> &listener); 181 bool IsSystemApp(); 182 183 ServiceRunningState state_; 184 static std::mutex instanceLock_; 185 static sptr<ScreenLockSystemAbility> instance_; 186 static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_; 187 sptr<Rosen::IDisplayPowerEventListener> displayPowerEventListener_; 188 std::mutex listenerMutex_; 189 sptr<ScreenLockSystemAbilityInterface> systemEventListener_; 190 std::vector<sptr<ScreenLockSystemAbilityInterface>> unlockVecListeners_; 191 std::vector<sptr<ScreenLockSystemAbilityInterface>> lockVecListeners_; 192 StateValue stateValue_; 193 std::mutex lock_; 194 const int32_t startTime_ = 1900; 195 const int32_t extraMonth_ = 1; 196 bool flag_ = false; 197 }; 198 } // namespace ScreenLock 199 } // namespace OHOS 200 #endif // SERVICES_INCLUDE_SCLOCK_SERVICES_H 201