1 /* 2 * Copyright (c) 2024 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 #ifndef DISTRIBUTEDDATAMGR_ADAPTER_SCREEN_LOCK_H 16 #define DISTRIBUTEDDATAMGR_ADAPTER_SCREEN_LOCK_H 17 18 #include "common_event_manager.h" 19 #include "common_event_subscriber.h" 20 #include "common_event_support.h" 21 #include "concurrent_map.h" 22 #include "screen/screen_manager.h" 23 24 namespace OHOS { 25 namespace DistributedData { 26 using namespace OHOS::EventFwk; 27 using EventCallback = std::function<void(int32_t user)>; 28 29 class EventSubscriber final : public CommonEventSubscriber { 30 public: ~EventSubscriber()31 ~EventSubscriber() {} 32 explicit EventSubscriber(const CommonEventSubscribeInfo &info); 33 void SetEventCallback(EventCallback callback); 34 void OnReceiveEvent(const CommonEventData &event) override; 35 private: 36 static constexpr const char *USER_ID = "userId"; 37 static constexpr int32_t INVALID_USER = -1; 38 EventCallback eventCallback_ {}; 39 }; 40 41 class ScreenLock : public ScreenManager { 42 public: 43 bool IsLocked(); 44 void Subscribe(std::shared_ptr<Observer> observer) __attribute__((no_sanitize("cfi"))); 45 void Unsubscribe(std::shared_ptr<Observer> observer) __attribute__((no_sanitize("cfi"))); 46 void BindExecutor(std::shared_ptr<ExecutorPool> executors); 47 void SubscribeScreenEvent(); 48 void UnsubscribeScreenEvent(); 49 50 private: 51 static constexpr int32_t MAX_RETRY_TIME = 300; 52 static constexpr int32_t RETRY_WAIT_TIME_S = 1; 53 void NotifyScreenUnlocked(int32_t user); 54 ExecutorPool::Task GetTask(uint32_t retry); 55 ConcurrentMap<std::string, std::shared_ptr<Observer>> observerMap_{}; 56 std::shared_ptr<ExecutorPool> executors_; 57 std::shared_ptr<EventSubscriber> eventSubscriber_{}; 58 }; 59 } // namespace DistributedData 60 } // namespace OHOS 61 #endif //DISTRIBUTEDDATAMGR_ADAPTER_SCREEN_LOCK_H