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 "publish/screen_session_publish.h"
17
18 #include <common_event_manager.h>
19
20 #include "window_manager_hilog.h"
21
22 namespace OHOS::Rosen {
23 const std::string CAST_PLUG_IN_FLAG_DATA = "1";
24 const std::string CAST_PLUG_OUT_FLAG_DATA = "0";
25 const std::string COMMON_EVENT_DISPLAY_ROTATION_CHANGED = "usual.event.dms.rotation_changed";
26 const std::string COMMON_EVENT_CAST_PLUGGED_CHANGED = "usual.event.dms.cast_plugged_changed";
27 const std::string COMMON_EVENT_SMART_NOTIFICATION = "hicare.event.SMART_NOTIFICATION";
28 const std::string COMMON_EVENT_LOW_TEMP_WARNING = "usual.event.thermal.LOW_TEMP_WARNING";
29 const std::string COMMON_EVENT_USER_SWITCHED = "usual.event.USER_SWITCHED";
30 constexpr int32_t PUBLISH_SUCCESS = 0;
31 constexpr int32_t PUBLISH_FAILURE = -1;
32 constexpr int32_t TRANS_CODE_CAST_PLUGGED_CHANGED = 0;
33 constexpr int32_t TRANS_CODE_ROTATION_CHANGED = 1005;
34 constexpr int32_t lOW_TEMP_UID = 1096;
35 constexpr int32_t RECEIVE_FAILURE = -1;
36
37 std::map<std::string, sptr<EventFwk::Want>> ScreenSessionPublish::cesWantMap_ = {
38 {COMMON_EVENT_CAST_PLUGGED_CHANGED, nullptr},
39 {COMMON_EVENT_DISPLAY_ROTATION_CHANGED, nullptr},
40 {COMMON_EVENT_SMART_NOTIFICATION, nullptr}
41 };
42
~ScreenSessionPublish()43 ScreenSessionPublish::~ScreenSessionPublish()
44 {
45 TLOGI(WmsLogTag::DMS, "destory");
46 UnRegisterLowTempSubscriber();
47 UnRegisterUserSwitchedSubscriber();
48 }
49
GetInstance()50 ScreenSessionPublish &ScreenSessionPublish::GetInstance()
51 {
52 static ScreenSessionPublish screenSessionPublish;
53 return screenSessionPublish;
54 }
55
InitPublishEvents()56 void ScreenSessionPublish::InitPublishEvents()
57 {
58 if (publishInfo_ != nullptr) {
59 TLOGE(WmsLogTag::DMS, "ScreenSessionPublish has been initialized");
60 return;
61 }
62 publishInfo_ = new (std::nothrow) EventFwk::CommonEventPublishInfo();
63 if (publishInfo_ == nullptr) {
64 TLOGE(WmsLogTag::DMS, "publishInfo new failed");
65 return;
66 }
67 publishInfo_->SetOrdered(false);
68 for (auto &[event, want] : cesWantMap_) {
69 want = new (std::nothrow) EventFwk::Want();
70 if (want == nullptr) {
71 TLOGE(WmsLogTag::DMS, "common event: %{publish}s new want failed", event.c_str());
72 continue;
73 }
74 want->SetAction(event);
75 }
76 }
77
PublishEvents(const EventFwk::CommonEventData & eventData,std::string bundleName)78 int32_t ScreenSessionPublish::PublishEvents(
79 const EventFwk::CommonEventData& eventData, std::string bundleName)
80 {
81 if (publishInfo_ == nullptr) {
82 TLOGE(WmsLogTag::DMS, "publishInfo is nullptr");
83 return PUBLISH_FAILURE;
84 }
85 if (bundleName != "") {
86 publishInfo_->SetBundleName(bundleName);
87 }
88 bool ret = EventFwk::CommonEventManager::PublishCommonEvent(eventData, *publishInfo_, nullptr);
89 if (!ret) {
90 TLOGE(WmsLogTag::DMS, "PublishCommonEvent failed");
91 return PUBLISH_FAILURE;
92 }
93 TLOGI(WmsLogTag::DMS, "PublishCommonEvent succeed");
94 return PUBLISH_SUCCESS;
95 }
96
PublishCastPluggedEvent(const bool & isEnable)97 void ScreenSessionPublish::PublishCastPluggedEvent(const bool& isEnable)
98 {
99 TLOGI(WmsLogTag::DMS, "start to publish cast plugged event");
100 EventFwk::CommonEventData eventData;
101 eventData.SetCode(TRANS_CODE_CAST_PLUGGED_CHANGED);
102 if (isEnable) {
103 eventData.SetData(CAST_PLUG_IN_FLAG_DATA);
104 } else {
105 eventData.SetData(CAST_PLUG_OUT_FLAG_DATA);
106 }
107 auto want = cesWantMap_[COMMON_EVENT_CAST_PLUGGED_CHANGED];
108 if (want == nullptr) {
109 TLOGE(WmsLogTag::DMS, "want is nullptr");
110 return;
111 }
112 eventData.SetWant(*want);
113 int32_t ret = PublishEvents(eventData);
114 if (ret != PUBLISH_SUCCESS) {
115 TLOGE(WmsLogTag::DMS, "PublishEvents failed");
116 return;
117 }
118 TLOGI(WmsLogTag::DMS, "end of publish cast plugged event");
119 }
120
PublishCastPlugInEvent()121 void ScreenSessionPublish::PublishCastPlugInEvent()
122 {
123 TLOGI(WmsLogTag::DMS, "start to publish cast plug in event");
124 PublishCastPluggedEvent(true);
125 }
126
PublishCastPlugOutEvent()127 void ScreenSessionPublish::PublishCastPlugOutEvent()
128 {
129 TLOGI(WmsLogTag::DMS, "start to publish cast plug out event");
130 PublishCastPluggedEvent(false);
131 }
132
PublishDisplayRotationEvent(const ScreenId & screenId,const Rotation & displayRotation)133 void ScreenSessionPublish::PublishDisplayRotationEvent(
134 const ScreenId& screenId, const Rotation& displayRotation)
135 {
136 TLOGI(WmsLogTag::DMS, "start event, screenId: %{public}d, displayRotation: %{public}d",
137 static_cast<int32_t>(screenId), static_cast<int32_t>(displayRotation));
138 EventFwk::CommonEventData eventData;
139 eventData.SetCode(TRANS_CODE_ROTATION_CHANGED);
140 auto want = cesWantMap_[COMMON_EVENT_DISPLAY_ROTATION_CHANGED];
141 if (want == nullptr) {
142 TLOGE(WmsLogTag::DMS, "want is nullptr");
143 return;
144 }
145 want->SetParam("screenid", static_cast<int32_t>(screenId));
146 want->SetParam("rotation", static_cast<int32_t>(displayRotation));
147 eventData.SetWant(*want);
148 int32_t ret = PublishEvents(eventData);
149 if (ret != PUBLISH_SUCCESS) {
150 TLOGE(WmsLogTag::DMS, "PublishEvents failed");
151 return;
152 }
153 TLOGI(WmsLogTag::DMS, "end event");
154 }
155
PublishSmartNotificationEvent(const std::string & faultDesc,const std::string & faultSuggest)156 void ScreenSessionPublish::PublishSmartNotificationEvent(const std::string& faultDesc, const std::string& faultSuggest)
157 {
158 EventFwk::CommonEventData eventData;
159 auto want = cesWantMap_[COMMON_EVENT_SMART_NOTIFICATION];
160 if (want == nullptr) {
161 TLOGE(WmsLogTag::DMS, "want is nullptr");
162 return;
163 }
164 eventData.SetWant(*want);
165 std::string eventDataStr = "{\"faultDescription\":\"" + faultDesc + "\"," +
166 "\"faultSuggestion\":\"" + faultSuggest + "\"}";
167 eventData.SetData(eventDataStr);
168 int32_t ret = PublishEvents(eventData);
169 if (ret != PUBLISH_SUCCESS) {
170 TLOGE(WmsLogTag::DMS, "PublishEvents failed");
171 return;
172 }
173 TLOGI(WmsLogTag::DMS, "end event, send massage is %{public}s", eventDataStr.c_str());
174 }
175
RegisterLowTempSubscriber()176 bool ScreenSessionPublish::RegisterLowTempSubscriber()
177 {
178 if (subscriber_ != nullptr) {
179 TLOGE(WmsLogTag::DMS, "low temp is registered");
180 return false;
181 }
182 EventFwk::MatchingSkills lowTempSkills = EventFwk::MatchingSkills();
183 lowTempSkills.AddEvent(COMMON_EVENT_LOW_TEMP_WARNING);
184 EventFwk::CommonEventSubscribeInfo lowTempInfo(lowTempSkills);
185 lowTempInfo.SetPublisherUid(lOW_TEMP_UID);
186 lowTempInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
187 subscriber_ = std::make_shared<EventSubscriber>(lowTempInfo);
188 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_)) {
189 TLOGE(WmsLogTag::DMS, "subscribe common event:%{public}s failed!", COMMON_EVENT_LOW_TEMP_WARNING.c_str());
190 return false;
191 }
192 TLOGI(WmsLogTag::DMS, "subscribe common event:%{public}s success.", COMMON_EVENT_LOW_TEMP_WARNING.c_str());
193 return true;
194 }
195
UnRegisterLowTempSubscriber()196 bool ScreenSessionPublish::UnRegisterLowTempSubscriber()
197 {
198 if (subscriber_ == nullptr) {
199 TLOGE(WmsLogTag::DMS, "subscriber_ is nullptr");
200 return false;
201 }
202 EventFwk::MatchingSkills lowTempSkills = EventFwk::MatchingSkills();
203 lowTempSkills.RemoveEvent(COMMON_EVENT_LOW_TEMP_WARNING);
204 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_)) {
205 TLOGE(WmsLogTag::DMS, "unsubscribe common event:%{public}s failed!", COMMON_EVENT_LOW_TEMP_WARNING.c_str());
206 return false;
207 }
208 TLOGI(WmsLogTag::DMS, "unsubscribe common event:%{public}s success.", COMMON_EVENT_LOW_TEMP_WARNING.c_str());
209 return true;
210 }
211
RegisterUserSwitchedSubscriber()212 bool ScreenSessionPublish::RegisterUserSwitchedSubscriber()
213 {
214 if (userSwitchedSubscriber_ != nullptr) {
215 TLOGE(WmsLogTag::DMS, "user switched is registered");
216 return false;
217 }
218 EventFwk::MatchingSkills userSwitchedSkills = EventFwk::MatchingSkills();
219 userSwitchedSkills.AddEvent(COMMON_EVENT_USER_SWITCHED);
220 EventFwk::CommonEventSubscribeInfo userSwitchedInfo(userSwitchedSkills);
221 userSwitchedInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
222 userSwitchedSubscriber_ = std::make_shared<EventSubscriber>(userSwitchedInfo);
223 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(userSwitchedSubscriber_)) {
224 TLOGE(WmsLogTag::DMS, "subscribe common event:%{public}s failed!", COMMON_EVENT_USER_SWITCHED.c_str());
225 return false;
226 }
227 TLOGI(WmsLogTag::DMS, "subscribe common event:%{public}s success.", COMMON_EVENT_USER_SWITCHED.c_str());
228 return true;
229 }
230
UnRegisterUserSwitchedSubscriber()231 bool ScreenSessionPublish::UnRegisterUserSwitchedSubscriber()
232 {
233 if (userSwitchedSubscriber_ == nullptr) {
234 TLOGE(WmsLogTag::DMS, "userSwitchedSubscriber_ is nullptr");
235 return false;
236 }
237 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(userSwitchedSubscriber_)) {
238 TLOGE(WmsLogTag::DMS, "unsubscribe common event:%{public}s failed!", COMMON_EVENT_USER_SWITCHED.c_str());
239 return false;
240 }
241 TLOGI(WmsLogTag::DMS, "unsubscribe common event:%{public}s success.", COMMON_EVENT_USER_SWITCHED.c_str());
242 return true;
243 }
244
OnReceiveEvent(const EventFwk::CommonEventData & data)245 void ScreenSessionPublish::EventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data)
246 {
247 std::string action = data.GetWant().GetAction();
248 if (action == COMMON_EVENT_LOW_TEMP_WARNING) {
249 int32_t lowTemp = data.GetWant().GetIntParam("lowTempWarning", RECEIVE_FAILURE);
250 ScreenSessionManager::GetInstance().SetLowTemp(static_cast<LowTempMode>(lowTemp));
251 TLOGI(WmsLogTag::DMS, "receive common event:%{public}s sucess, lowTempWarning is:%{public}d",
252 COMMON_EVENT_LOW_TEMP_WARNING.c_str(), lowTemp);
253 } else if (action == COMMON_EVENT_USER_SWITCHED) {
254 TLOGI(WmsLogTag::DMS, "receive common event:%{public}s sucess", COMMON_EVENT_USER_SWITCHED.c_str());
255 ScreenSessionManager::GetInstance().NotifyCastWhenSwitchScbNode();
256 }
257 }
258 } // namespace OHOS::Rosen