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 
16 #include "dm_screen_common_event.h"
17 
18 #include <pthread.h>
19 #include <thread>
20 
21 #include "common_event_support.h"
22 #include "dm_anonymous.h"
23 #include "dm_constants.h"
24 #include "dm_log.h"
25 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
26 #include "ffrt.h"
27 #endif
28 #include "iservice_registry.h"
29 #include "multiple_user_connector.h"
30 #include "system_ability_definition.h"
31 
32 namespace OHOS {
33 namespace DistributedHardware {
34 using OHOS::EventFwk::MatchingSkills;
35 using OHOS::EventFwk::CommonEventManager;
36 
37 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
38 constexpr const char* DEAL_THREAD = "screen_lock";
39 #endif
40 constexpr int32_t MAX_TRY_TIMES = 3;
41 
GetSubscriberEventNameVec() const42 std::vector<std::string> DmScreenEventSubscriber::GetSubscriberEventNameVec() const
43 {
44     return eventNameVec_;
45 }
46 
~DmScreenCommonEventManager()47 DmScreenCommonEventManager::~DmScreenCommonEventManager()
48 {
49     DmScreenCommonEventManager::UnsubscribeScreenCommonEvent();
50 }
51 
SubscribeScreenCommonEvent(const std::vector<std::string> & eventNameVec,const ScreenEventCallback & callback)52 bool DmScreenCommonEventManager::SubscribeScreenCommonEvent(const std::vector<std::string> &eventNameVec,
53     const ScreenEventCallback &callback)
54 {
55     if (eventNameVec.empty() || callback == nullptr) {
56         LOGE("eventNameVec is empty or callback is nullptr.");
57         return false;
58     }
59     std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
60     if (eventValidFlag_) {
61         LOGE("failed to subscribe screen commom eventName size: %{public}zu", eventNameVec.size());
62         return false;
63     }
64 
65     MatchingSkills matchingSkills;
66     for (auto &item : eventNameVec) {
67         matchingSkills.AddEvent(item);
68     }
69     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
70     subscriber_ = std::make_shared<DmScreenEventSubscriber>(subscriberInfo, callback, eventNameVec);
71     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
72     if (samgrProxy == nullptr) {
73         LOGE("samgrProxy is nullptr");
74         subscriber_ = nullptr;
75         return false;
76     }
77     statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(subscriber_);
78     if (statusChangeListener_ == nullptr) {
79         LOGE("statusChangeListener_ is nullptr");
80         subscriber_ = nullptr;
81         return false;
82     }
83     while (counter_ != MAX_TRY_TIMES) {
84         if (samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_) == ERR_OK) {
85             LOGI("SubscribeScreenEvent success.");
86             counter_ = 0;
87             break;
88         }
89         if (++counter_ == MAX_TRY_TIMES) {
90             LOGI("SubscribeScreenEvent failed.");
91         }
92         sleep(1);
93     }
94     eventNameVec_ = eventNameVec;
95     eventValidFlag_ = true;
96     LOGI("success to subscribe screen commom event name size: %{public}zu", eventNameVec.size());
97     return true;
98 }
99 
UnsubscribeScreenCommonEvent()100 bool DmScreenCommonEventManager::UnsubscribeScreenCommonEvent()
101 {
102     std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
103     if (!eventValidFlag_) {
104         LOGE("failed to unsubscribe screen commom event name size: %{public}zu because event is invalid.",
105             eventNameVec_.size());
106         return false;
107     }
108     if (subscriber_ != nullptr) {
109         LOGI("start to unsubscribe screen commom event name size: %{public}zu", eventNameVec_.size());
110         if (!CommonEventManager::UnSubscribeCommonEvent(subscriber_)) {
111             LOGE("failed to unsubscribe screen commom event name size: %{public}zu", eventNameVec_.size());
112             return false;
113         }
114         LOGI("success to unsubscribe screen commom event name size: %{public}zu", eventNameVec_.size());
115         subscriber_ = nullptr;
116     }
117     if (statusChangeListener_ != nullptr) {
118         auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
119         if (samgrProxy == nullptr) {
120             LOGE("samgrProxy is nullptr");
121             return false;
122         }
123         int32_t ret = samgrProxy->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
124         if (ret != ERR_OK) {
125             LOGE("failed to unsubscribe system ability COMMON_EVENT_SERVICE_ID ret:%{public}d", ret);
126             return false;
127         }
128         statusChangeListener_ = nullptr;
129     }
130 
131     LOGI("success to unsubscribe screen commom event name size: %{public}zu", eventNameVec_.size());
132     eventValidFlag_ = false;
133     return true;
134 }
135 
OnReceiveEvent(const CommonEventData & data)136 void DmScreenEventSubscriber::OnReceiveEvent(const CommonEventData &data)
137 {
138     std::string receiveEvent = data.GetWant().GetAction();
139     LOGI("Received screen event: %{public}s", receiveEvent.c_str());
140     if (receiveEvent != EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) {
141         return;
142     }
143 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
144     ffrt::submit([=]() { callback_(receiveEvent); });
145 #else
146     std::thread dealThread(callback_, receiveEvent);
147     int32_t ret = pthread_setname_np(dealThread.native_handle(), DEAL_THREAD);
148     if (ret != DM_OK) {
149         LOGE("dealThread setname failed.");
150     }
151     dealThread.detach();
152 #endif
153 }
154 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)155 void DmScreenCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
156     int32_t systemAbilityId, const std::string& deviceId)
157 {
158     LOGI("systemAbility is added with said: %{public}d.", systemAbilityId);
159     if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
160         return;
161     }
162     if (changeSubscriber_ == nullptr) {
163         LOGE("failed to subscribe screen commom event because changeSubscriber_ is nullptr.");
164         return;
165     }
166     std::vector<std::string> eventNameVec = changeSubscriber_->GetSubscriberEventNameVec();
167     LOGI("start to subscribe screen commom eventName: %{public}zu", eventNameVec.size());
168     if (!CommonEventManager::SubscribeCommonEvent(changeSubscriber_)) {
169         LOGE("failed to subscribe screen commom event: %{public}zu", eventNameVec.size());
170     }
171 }
172 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)173 void DmScreenCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
174     int32_t systemAbilityId, const std::string& deviceId)
175 {
176     LOGI("systemAbility is removed with said: %{public}d.", systemAbilityId);
177 }
178 } // namespace DistributedHardware
179 } // namespace OHOS
180