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 #include "os_account_manager.h" 34 #include "preferences_util.h" 35 #include "os_account_subscribe_info.h" 36 37 namespace OHOS { 38 namespace ScreenLock { 39 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 40 class StateValue { 41 public: StateValue()42 StateValue(){}; ~StateValue()43 ~StateValue(){}; 44 45 void Reset(); 46 SetScreenlockEnabled(bool screenlockEnabled)47 void SetScreenlockEnabled(bool screenlockEnabled) 48 { 49 screenlockEnabled_ = screenlockEnabled; 50 }; 51 SetScreenState(int32_t screenState)52 void SetScreenState(int32_t screenState) 53 { 54 screenState_ = screenState; 55 }; 56 SetOffReason(int32_t offReason)57 void SetOffReason(int32_t offReason) 58 { 59 offReason_ = offReason; 60 }; 61 SetCurrentUser(int32_t currentUser)62 void SetCurrentUser(int32_t currentUser) 63 { 64 currentUser_ = currentUser; 65 }; 66 SetInteractiveState(int32_t interactiveState)67 void SetInteractiveState(int32_t interactiveState) 68 { 69 interactiveState_ = interactiveState; 70 }; 71 GetScreenlockEnabled()72 bool GetScreenlockEnabled() 73 { 74 return screenlockEnabled_; 75 }; 76 GetScreenState()77 int32_t GetScreenState() 78 { 79 return screenState_; 80 }; 81 GetOffReason()82 int32_t GetOffReason() 83 { 84 return offReason_; 85 }; 86 GetCurrentUser()87 int32_t GetCurrentUser() 88 { 89 return currentUser_; 90 }; 91 GetInteractiveState()92 int32_t GetInteractiveState() 93 { 94 return interactiveState_; 95 }; 96 97 private: 98 std::atomic<bool> screenlockEnabled_ { false }; 99 std::atomic<int32_t> offReason_ {0}; 100 std::atomic<int32_t> currentUser_ {0}; 101 std::atomic<int32_t> screenState_ {0}; 102 std::atomic<int32_t> interactiveState_ {0}; 103 }; 104 105 enum class ScreenState : int32_t { 106 SCREEN_STATE_BEGIN_OFF = 0, 107 SCREEN_STATE_END_OFF = 1, 108 SCREEN_STATE_BEGIN_ON = 2, 109 SCREEN_STATE_END_ON = 3, 110 }; 111 112 enum class InteractiveState : int32_t { 113 INTERACTIVE_STATE_END_SLEEP = 0, 114 INTERACTIVE_STATE_BEGIN_WAKEUP = 1, 115 INTERACTIVE_STATE_END_WAKEUP = 2, 116 INTERACTIVE_STATE_BEGIN_SLEEP = 3, 117 }; 118 119 enum class AuthState : int32_t { 120 UNAUTH = 0, 121 PRE_AUTHED_BY_CREDENTIAL = 1, 122 PRE_AUTHED_BY_FINGERPRINT = 2, 123 PRE_AUTHED_BY_FACE = 3, 124 AUTHED_BY_CREDENTIAL = 4, 125 AUTHED_BY_FINGERPRINT = 5, 126 AUTHED_BY_FACE = 6, 127 }; 128 129 class ScreenLockSystemAbility : public SystemAbility, public ScreenLockManagerStub { 130 DECLARE_SYSTEM_ABILITY(ScreenLockSystemAbility); 131 132 public: 133 DISALLOW_COPY_AND_MOVE(ScreenLockSystemAbility); 134 ScreenLockSystemAbility(int32_t systemAbilityId, bool runOnCreate); 135 ScreenLockSystemAbility(); 136 ~ScreenLockSystemAbility() override; 137 SCREENLOCK_API static sptr<ScreenLockSystemAbility> GetInstance(); 138 int32_t IsLocked(bool &isLocked) override; 139 bool IsScreenLocked() override; 140 int32_t IsLockedWithUserId(int32_t userId, bool &isLocked) override; 141 bool GetSecure() override; 142 int32_t Unlock(const sptr<ScreenLockCallbackInterface> &listener) override; 143 int32_t UnlockScreen(const sptr<ScreenLockCallbackInterface> &listener) override; 144 int32_t Lock(const sptr<ScreenLockCallbackInterface> &listener) override; 145 int32_t OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> &listener) override; 146 int32_t SendScreenLockEvent(const std::string &event, int param) override; 147 int32_t IsScreenLockDisabled(int userId, bool &isDisabled) override; 148 int32_t SetScreenLockDisabled(bool disable, int userId) override; 149 int32_t SetScreenLockAuthState(int authState, int32_t userId, std::string &authToken) override; 150 int32_t GetScreenLockAuthState(int userId, int32_t &authState) override; 151 int32_t RequestStrongAuth(int reasonFlag, int32_t userId) override; 152 int32_t GetStrongAuth(int userId, int32_t &reasonFlag) override; 153 int32_t IsDeviceLocked(int userId, bool &isDeviceLocked) override; 154 int32_t RegisterInnerListener(const int32_t userId, const ListenType listenType, 155 const sptr<InnerListenerIf>& listener) override; 156 int32_t UnRegisterInnerListener(const int32_t userId, const ListenType listenType, 157 const sptr<InnerListenerIf>& listener) override; 158 int Dump(int fd, const std::vector<std::u16string> &args) override; 159 void SetScreenlocked(bool isScreenlocked, const int32_t userId); 160 void RegisterDisplayPowerEventListener(int32_t times); 161 void ResetFfrtQueue(); 162 void StrongAuthChanged(int32_t userId, int32_t reasonFlag); 163 int32_t Lock(int32_t userId) override; 164 void UserIamReadyNotify(const char *value); 165 void OnActiveUser(const int lastUser, const int targetUser); 166 void OnRemoveUser(const int userId); GetState()167 StateValue &GetState() 168 { 169 return stateValue_; 170 } 171 class ScreenLockDisplayPowerEventListener : public Rosen::IDisplayPowerEventListener { 172 public: 173 void OnDisplayPowerEvent(Rosen::DisplayPowerEvent event, Rosen::EventStatus status) override; 174 }; 175 176 class AccountSubscriber : public AccountSA::OsAccountSubscriber { 177 public: 178 explicit AccountSubscriber(const AccountSA::OsAccountSubscribeInfo &subscribeInfo, 179 const std::function<void(const int lastUser, const int targetUser)> &callback); 180 ~AccountSubscriber() override = default; 181 void OnAccountsChanged(const int &id) override; 182 183 private: 184 int userId_{-1}; 185 std::function<void(const int lastUser, const int targetUser)> callback_; 186 }; 187 188 protected: 189 void OnStart() override; 190 void OnStop() override; 191 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 192 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 193 194 private: 195 void OnScreenOn(Rosen::EventStatus status); 196 void OnScreenOff(Rosen::EventStatus status); 197 void OnWakeUp(Rosen::EventStatus status); 198 void OnSleep(Rosen::EventStatus status); 199 void OnExitAnimation(); 200 void OnSystemReady(); 201 void RegisterDumpCommand(); 202 int32_t Init(); 203 void InitUserId(); 204 void InitServiceHandler(); 205 void LockScreenEvent(int stateResult); 206 void UnlockScreenEvent(int stateResult); 207 void SystemEventCallBack(const SystemEvent &systemEvent, TraceTaskId traceTaskId = HITRACE_BUTT); 208 int32_t UnlockInner(const sptr<ScreenLockCallbackInterface> &listener); 209 void PublishEvent(const std::string &eventAction, const int32_t userId); 210 bool IsAppInForeground(int32_t callingPid, uint32_t callingTokenId); 211 bool IsSystemApp(); 212 bool CheckPermission(const std::string &permissionName); 213 void NotifyUnlockListener(const int32_t screenLockResult); 214 void NotifyDisplayEvent(Rosen::DisplayEvent event); 215 bool GetDeviceLockedStateByAuth(int authState); 216 std::shared_ptr<AccountSubscriber> SubscribeAcccount(AccountSA::OS_ACCOUNT_SUBSCRIBE_TYPE subscribeType, 217 const std::function<void(const int lastUser, const int targetUser)> &callback); 218 void AuthStateInit(const int userId); 219 void SubscribeUserIamReady(); 220 void RemoveSubscribeUserIamReady(); 221 bool CheckSystemPermission(); 222 void AppendPrintOtherInfo(std::string &output); 223 void printCallerPid(std::string invokeName); 224 225 ServiceRunningState state_; 226 static std::mutex instanceLock_; 227 static sptr<ScreenLockSystemAbility> instance_; 228 static std::shared_ptr<ffrt::queue> queue_; 229 std::map<AccountSA::OS_ACCOUNT_SUBSCRIBE_TYPE, std::shared_ptr<AccountSubscriber>> accountSubscribers_; 230 std::mutex accountSubscriberMutex_; 231 sptr<Rosen::IDisplayPowerEventListener> displayPowerEventListener_; 232 std::mutex listenerMutex_; 233 sptr<ScreenLockSystemAbilityInterface> systemEventListener_; 234 std::mutex unlockListenerMutex_; 235 std::vector<sptr<ScreenLockCallbackInterface>> unlockVecListeners_; 236 std::vector<int> unlockVecUserIds_; 237 std::mutex lockListenerMutex_; 238 std::vector<sptr<ScreenLockCallbackInterface>> lockVecListeners_; 239 StateValue stateValue_; 240 std::atomic<bool> systemReady_ = false; 241 std::map<int32_t, int32_t> authStateInfo; 242 std::mutex authStateMutex_; 243 std::map<int32_t, bool> isScreenlockedMap_; 244 std::mutex screenLockMutex_; 245 }; 246 } // namespace ScreenLock 247 } // namespace OHOS 248 #endif // SERVICES_INCLUDE_SCLOCK_SERVICES_H 249