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