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,
137 "start to publish display rotation event, screenId: %{public}d, displayRotation: %{public}d",
138 static_cast<int32_t>(screenId), static_cast<int32_t>(displayRotation));
139 EventFwk::CommonEventData eventData;
140 eventData.SetCode(TRANS_CODE_ROTATION_CHANGED);
141 auto want = cesWantMap_[COMMON_EVENT_DISPLAY_ROTATION_CHANGED];
142 if (want == nullptr) {
143 TLOGE(WmsLogTag::DMS, "want is nullptr");
144 return;
145 }
146 want->SetParam("screenid", static_cast<int32_t>(screenId));
147 want->SetParam("rotation", static_cast<int32_t>(displayRotation));
148 eventData.SetWant(*want);
149 int32_t ret = PublishEvents(eventData);
150 if (ret != PUBLISH_SUCCESS) {
151 TLOGE(WmsLogTag::DMS, "PublishEvents failed");
152 return;
153 }
154 TLOGI(WmsLogTag::DMS, "end of publish display rotation event");
155 }
156
PublishSmartNotificationEvent(const std::string & faultDesc,const std::string & faultSuggest)157 void ScreenSessionPublish::PublishSmartNotificationEvent(const std::string& faultDesc, const std::string& faultSuggest)
158 {
159 EventFwk::CommonEventData eventData;
160 auto want = cesWantMap_[COMMON_EVENT_SMART_NOTIFICATION];
161 if (want == nullptr) {
162 TLOGE(WmsLogTag::DMS, "want is nullptr");
163 return;
164 }
165 eventData.SetWant(*want);
166 std::string eventDataStr = "{\"faultDescription\":\"" + faultDesc + "\"," +
167 "\"faultSuggestion\":\"" + faultSuggest + "\"}";
168 eventData.SetData(eventDataStr);
169 int32_t ret = PublishEvents(eventData);
170 if (ret != PUBLISH_SUCCESS) {
171 TLOGE(WmsLogTag::DMS, "PublishEvents failed");
172 return;
173 }
174 TLOGI(WmsLogTag::DMS, "end event, send massage is %{public}s", eventDataStr.c_str());
175 }
176
RegisterLowTempSubscriber()177 bool ScreenSessionPublish::RegisterLowTempSubscriber()
178 {
179 if (subscriber_ != nullptr) {
180 TLOGE(WmsLogTag::DMS, "low temp is registered");
181 return false;
182 }
183 EventFwk::MatchingSkills lowTempSkills = EventFwk::MatchingSkills();
184 lowTempSkills.AddEvent(COMMON_EVENT_LOW_TEMP_WARNING);
185 EventFwk::CommonEventSubscribeInfo lowTempInfo(lowTempSkills);
186 lowTempInfo.SetPublisherUid(lOW_TEMP_UID);
187 lowTempInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
188 subscriber_ = std::make_shared<EventSubscriber>(lowTempInfo);
189 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_)) {
190 TLOGE(WmsLogTag::DMS, "subscribe common event:%{public}s failed!", COMMON_EVENT_LOW_TEMP_WARNING.c_str());
191 return false;
192 }
193 TLOGI(WmsLogTag::DMS, "subscribe common event:%{public}s success.", COMMON_EVENT_LOW_TEMP_WARNING.c_str());
194 return true;
195 }
196
UnRegisterLowTempSubscriber()197 bool ScreenSessionPublish::UnRegisterLowTempSubscriber()
198 {
199 if (subscriber_ == nullptr) {
200 TLOGE(WmsLogTag::DMS, "subscriber_ is nullptr");
201 return false;
202 }
203 EventFwk::MatchingSkills lowTempSkills = EventFwk::MatchingSkills();
204 lowTempSkills.RemoveEvent(COMMON_EVENT_LOW_TEMP_WARNING);
205 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_)) {
206 TLOGE(WmsLogTag::DMS, "unsubscribe common event:%{public}s failed!", COMMON_EVENT_LOW_TEMP_WARNING.c_str());
207 return false;
208 }
209 TLOGI(WmsLogTag::DMS, "unsubscribe common event:%{public}s success.", COMMON_EVENT_LOW_TEMP_WARNING.c_str());
210 return true;
211 }
212
RegisterUserSwitchedSubscriber()213 bool ScreenSessionPublish::RegisterUserSwitchedSubscriber()
214 {
215 if (userSwitchedSubscriber_ != nullptr) {
216 TLOGE(WmsLogTag::DMS, "user switched is registered");
217 return false;
218 }
219 EventFwk::MatchingSkills userSwitchedSkills = EventFwk::MatchingSkills();
220 userSwitchedSkills.AddEvent(COMMON_EVENT_USER_SWITCHED);
221 EventFwk::CommonEventSubscribeInfo userSwitchedInfo(userSwitchedSkills);
222 userSwitchedInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
223 userSwitchedSubscriber_ = std::make_shared<EventSubscriber>(userSwitchedInfo);
224 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(userSwitchedSubscriber_)) {
225 TLOGE(WmsLogTag::DMS, "subscribe common event:%{public}s failed!", COMMON_EVENT_USER_SWITCHED.c_str());
226 return false;
227 }
228 TLOGI(WmsLogTag::DMS, "subscribe common event:%{public}s success.", COMMON_EVENT_USER_SWITCHED.c_str());
229 return true;
230 }
231
UnRegisterUserSwitchedSubscriber()232 bool ScreenSessionPublish::UnRegisterUserSwitchedSubscriber()
233 {
234 if (userSwitchedSubscriber_ == nullptr) {
235 TLOGE(WmsLogTag::DMS, "userSwitchedSubscriber_ is nullptr");
236 return false;
237 }
238 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(userSwitchedSubscriber_)) {
239 TLOGE(WmsLogTag::DMS, "unsubscribe common event:%{public}s failed!", COMMON_EVENT_USER_SWITCHED.c_str());
240 return false;
241 }
242 TLOGI(WmsLogTag::DMS, "unsubscribe common event:%{public}s success.", COMMON_EVENT_USER_SWITCHED.c_str());
243 return true;
244 }
245
OnReceiveEvent(const EventFwk::CommonEventData & data)246 void ScreenSessionPublish::EventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data)
247 {
248 std::string action = data.GetWant().GetAction();
249 if (action == COMMON_EVENT_LOW_TEMP_WARNING) {
250 int32_t lowTemp = data.GetWant().GetIntParam("lowTempWarning", RECEIVE_FAILURE);
251 ScreenSessionManager::GetInstance().SetLowTemp(static_cast<LowTempMode>(lowTemp));
252 TLOGI(WmsLogTag::DMS, "receive common event:%{public}s sucess, lowTempWarning is:%{public}d",
253 COMMON_EVENT_LOW_TEMP_WARNING.c_str(), lowTemp);
254 } else if (action == COMMON_EVENT_USER_SWITCHED) {
255 TLOGI(WmsLogTag::DMS, "receive common event:%{public}s sucess", COMMON_EVENT_USER_SWITCHED.c_str());
256 ScreenSessionManager::GetInstance().NotifyCastWhenSwitchScbNode();
257 }
258 }
259 } // namespace OHOS::Rosen