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 constexpr int32_t PUBLISH_SUCCESS = 0;
28 constexpr int32_t PUBLISH_FAILURE = -1;
29 constexpr int32_t TRANS_CODE_CAST_PLUGGED_CHANGED = 0;
30 constexpr int32_t TRANS_CODE_ROTATION_CHANGED = 1005;
31
32 std::map<std::string, sptr<EventFwk::Want>> ScreenSessionPublish::cesWantMap_ = {
33 {COMMON_EVENT_CAST_PLUGGED_CHANGED, nullptr},
34 {COMMON_EVENT_DISPLAY_ROTATION_CHANGED, nullptr}
35 };
36
GetInstance()37 ScreenSessionPublish &ScreenSessionPublish::GetInstance()
38 {
39 static ScreenSessionPublish screenSessionPublish;
40 return screenSessionPublish;
41 }
42
InitPublishEvents()43 void ScreenSessionPublish::InitPublishEvents()
44 {
45 if (publishInfo_ != nullptr) {
46 TLOGE(WmsLogTag::DMS, "ScreenSessionPublish has been initialized");
47 return;
48 }
49 publishInfo_ = new (std::nothrow) EventFwk::CommonEventPublishInfo();
50 if (publishInfo_ == nullptr) {
51 TLOGE(WmsLogTag::DMS, "publishInfo new failed");
52 return;
53 }
54 publishInfo_->SetOrdered(false);
55 for (auto &[event, want] : cesWantMap_) {
56 want = new (std::nothrow) EventFwk::Want();
57 if (want == nullptr) {
58 TLOGE(WmsLogTag::DMS, "common event: %{publish}s new want failed", event.c_str());
59 continue;
60 }
61 want->SetAction(event);
62 }
63 }
64
PublishEvents(const EventFwk::CommonEventData & eventData,std::string bundleName)65 int32_t ScreenSessionPublish::PublishEvents(
66 const EventFwk::CommonEventData &eventData, std::string bundleName)
67 {
68 if (publishInfo_ == nullptr) {
69 TLOGE(WmsLogTag::DMS, "publishInfo is nullptr");
70 return PUBLISH_FAILURE;
71 }
72 if (bundleName != "") {
73 publishInfo_->SetBundleName(bundleName);
74 }
75 bool ret = EventFwk::CommonEventManager::PublishCommonEvent(eventData, *publishInfo_, nullptr);
76 if (!ret) {
77 TLOGE(WmsLogTag::DMS, "PublishCommonEvent failed");
78 return PUBLISH_FAILURE;
79 }
80 TLOGI(WmsLogTag::DMS, "PublishCommonEvent succeed");
81 return PUBLISH_SUCCESS;
82 }
83
PublishCastPluggedEvent(const bool & isEnable)84 void ScreenSessionPublish::PublishCastPluggedEvent(const bool &isEnable)
85 {
86 TLOGI(WmsLogTag::DMS, "start to publish cast plugged event");
87 EventFwk::CommonEventData eventData;
88 eventData.SetCode(TRANS_CODE_CAST_PLUGGED_CHANGED);
89 if (isEnable) {
90 eventData.SetData(CAST_PLUG_IN_FLAG_DATA);
91 } else {
92 eventData.SetData(CAST_PLUG_OUT_FLAG_DATA);
93 }
94 auto want = cesWantMap_[COMMON_EVENT_CAST_PLUGGED_CHANGED];
95 if (want == nullptr) {
96 TLOGE(WmsLogTag::DMS, "want is nullptr");
97 return;
98 }
99 eventData.SetWant(*want);
100 int32_t ret = PublishEvents(eventData);
101 if (ret != PUBLISH_SUCCESS) {
102 TLOGE(WmsLogTag::DMS, "PublishEvents failed");
103 return;
104 }
105 TLOGI(WmsLogTag::DMS, "end of publish cast plugged event");
106 }
107
PublishCastPlugInEvent()108 void ScreenSessionPublish::PublishCastPlugInEvent()
109 {
110 TLOGI(WmsLogTag::DMS, "start to publish cast plug in event");
111 PublishCastPluggedEvent(true);
112 }
113
PublishCastPlugOutEvent()114 void ScreenSessionPublish::PublishCastPlugOutEvent()
115 {
116 TLOGI(WmsLogTag::DMS, "start to publish cast plug out event");
117 PublishCastPluggedEvent(false);
118 }
119
PublishDisplayRotationEvent(const ScreenId & screenId,const Rotation & displayRotation)120 void ScreenSessionPublish::PublishDisplayRotationEvent(
121 const ScreenId &screenId, const Rotation &displayRotation)
122 {
123 TLOGI(WmsLogTag::DMS,
124 "start to publish display rotation event, screenId: %{public}d, displayRotation: %{public}d",
125 static_cast<int32_t>(screenId), static_cast<int32_t>(displayRotation));
126 EventFwk::CommonEventData eventData;
127 eventData.SetCode(TRANS_CODE_ROTATION_CHANGED);
128 auto want = cesWantMap_[COMMON_EVENT_DISPLAY_ROTATION_CHANGED];
129 if (want == nullptr) {
130 TLOGE(WmsLogTag::DMS, "want is nullptr");
131 return;
132 }
133 want->SetParam("screenid", static_cast<int32_t>(screenId));
134 want->SetParam("rotation", static_cast<int32_t>(displayRotation));
135 eventData.SetWant(*want);
136 int32_t ret = PublishEvents(eventData);
137 if (ret != PUBLISH_SUCCESS) {
138 TLOGE(WmsLogTag::DMS, "PublishEvents failed");
139 return;
140 }
141 TLOGI(WmsLogTag::DMS, "end of publish display rotation event");
142 }
143 } // namespace OHOS::Rosen