1 /*
2 * Copyright (c) 2024-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
16 #include "camera_common_event_manager.h"
17
18 namespace OHOS {
19 namespace CameraStandard {
20
21 sptr<CameraCommonEventManager> CameraCommonEventManager::instance_ = nullptr;
22 std::mutex CameraCommonEventManager::instanceMutex_;
23 std::mutex CameraCommonEventManager::mapMutex_;
CameraCommonEventManager()24 CameraCommonEventManager::CameraCommonEventManager()
25 {}
26
~CameraCommonEventManager()27 CameraCommonEventManager::~CameraCommonEventManager()
28 {}
29
GetInstance()30 sptr<CameraCommonEventManager> CameraCommonEventManager::GetInstance()
31 {
32 CHECK_RETURN_RET(instance_ != nullptr, instance_);
33 std::lock_guard<std::mutex> lock(instanceMutex_);
34 CHECK_RETURN_RET(instance_ != nullptr, instance_);
35 instance_ = new (std::nothrow) CameraCommonEventManager();
36 return instance_;
37 }
38
SubscribeCommonEvent(const std::string & eventName,EventReceiver receiver)39 void CameraCommonEventManager::SubscribeCommonEvent(const std::string &eventName, EventReceiver receiver)
40 {
41 MEDIA_INFO_LOG("SubscribeCommonEvent eventName=%{public}s", eventName.c_str());
42 EventFwk::MatchingSkills matchingSkills;
43 matchingSkills.AddEvent(eventName);
44 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
45 auto subscriberPtr = std::make_shared<CameraCommonEventSubscriber>(subscribeInfo, receiver);
46 if (subscriberPtr == nullptr) {
47 MEDIA_INFO_LOG("subscriberPtr is nullptr");
48 return;
49 }
50 if (EventFwk::CommonEventManager::SubscribeCommonEvent(subscriberPtr)) {
51 MEDIA_INFO_LOG("SubscribeCommonEvent success");
52 {
53 std::lock_guard<std::mutex> lock(mapMutex_);
54 commonEventSubscriberMap_.insert(std::make_pair(eventName, subscriberPtr));
55 }
56 }
57 }
58
UnSubscribeCommonEvent(const std::string & eventName)59 void CameraCommonEventManager::UnSubscribeCommonEvent(const std::string &eventName)
60 {
61 MEDIA_INFO_LOG("UnSubscribeCommonEvent eventName=%{public}s", eventName.c_str());
62 std::lock_guard<std::mutex> lock(mapMutex_);
63 auto subscriber = commonEventSubscriberMap_.find(eventName);
64 if (subscriber == commonEventSubscriberMap_.end()) {
65 MEDIA_INFO_LOG("UnSubscribeCommonEvent event:%{public}s is not subscribed", eventName.c_str());
66 return;
67 }
68 auto commonEventSubscriber = subscriber->second;
69 CHECK_EXECUTE(commonEventSubscriber != nullptr,
70 EventFwk::CommonEventManager::UnSubscribeCommonEvent(commonEventSubscriber));
71 commonEventSubscriberMap_.erase(subscriber);
72 }
73
IsScreenLocked()74 bool CameraCommonEventManager::IsScreenLocked()
75 {
76 return isScreenLocked_;
77 }
78
SetScreenLocked(bool status)79 void CameraCommonEventManager::SetScreenLocked(bool status)
80 {
81 isScreenLocked_ = status;
82 }
83
CameraCommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,EventReceiver receiver)84 CameraCommonEventManager::CameraCommonEventSubscriber::CameraCommonEventSubscriber(
85 const EventFwk::CommonEventSubscribeInfo &subscribeInfo, EventReceiver receiver)
86 : EventFwk::CommonEventSubscriber(subscribeInfo), eventReceiver_(receiver) {}
87
OnReceiveEvent(const EventFwk::CommonEventData & data)88 void CameraCommonEventManager::CameraCommonEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
89 {
90 if (eventReceiver_ == nullptr) {
91 MEDIA_INFO_LOG("eventReceiver_ is nullptr");
92 return;
93 }
94 eventReceiver_(data);
95 }
96
97 } // namespace CameraStandard
98 } // namespace OHOS