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 "connection/screen_cast_connection.h"
17
18 #include "window_manager_hilog.h"
19
20 namespace OHOS::Rosen {
21
22 static const std::map<int32_t, std::pair<std::string, std::string>> PARAM_FLAG_MAP = {
23 {0, {"requestReason", "onPlugOut"}},
24 {1, {"requestReason", "onPlugIn"}}
25 };
26
GetInstance()27 ScreenCastConnection &ScreenCastConnection::GetInstance()
28 {
29 static ScreenCastConnection screenCastConnection;
30 return screenCastConnection;
31 }
32
CastConnectExtension(const int32_t & paramFlag)33 bool ScreenCastConnection::CastConnectExtension(const int32_t ¶mFlag)
34 {
35 if (bundleName_.empty() || abilityName_.empty()) {
36 TLOGE(WmsLogTag::DMS, "screen cast bundleName or abilityName is empty");
37 return false;
38 }
39 TLOGI(WmsLogTag::DMS, "bundleName: %{public}s, abilityName: %{public}s",
40 bundleName_.c_str(), abilityName_.c_str());
41 if (abilityConnection_ != nullptr) {
42 TLOGI(WmsLogTag::DMS, "screen cast already connected");
43 return true;
44 }
45 abilityConnection_ = std::make_unique<ScreenSessionAbilityConnection>();
46 if (abilityConnection_ == nullptr) {
47 TLOGE(WmsLogTag::DMS, "connection is nullptr");
48 return false;
49 }
50 std::vector<std::pair<std::string, std::string>> params;
51 auto iter = PARAM_FLAG_MAP.find(paramFlag);
52 if (iter == PARAM_FLAG_MAP.end()) {
53 TLOGE(WmsLogTag::DMS, "The paramFlag does not exist!");
54 return false;
55 }
56 params.push_back(iter->second);
57 bool ret = abilityConnection_->ScreenSessionConnectExtension(bundleName_, abilityName_, params);
58 if (!ret) {
59 TLOGE(WmsLogTag::DMS, "ScreenSessionConnectExtension failed");
60 return false;
61 }
62 TLOGI(WmsLogTag::DMS, "CastConnectExtension succeed");
63 return true;
64 }
65
CastDisconnectExtension()66 void ScreenCastConnection::CastDisconnectExtension()
67 {
68 if (abilityConnection_ == nullptr) {
69 TLOGE(WmsLogTag::DMS, "ability connect failed");
70 return;
71 }
72 abilityConnection_->ScreenSessionDisconnectExtension();
73 abilityConnection_ = nullptr;
74 TLOGI(WmsLogTag::DMS, "CastDisconnectExtension exit");
75 }
76
SetBundleName(const std::string & bundleName)77 void ScreenCastConnection::SetBundleName(const std::string &bundleName)
78 {
79 bundleName_ = bundleName;
80 }
81
SetAbilityName(const std::string & abilityName)82 void ScreenCastConnection::SetAbilityName(const std::string &abilityName)
83 {
84 abilityName_ = abilityName;
85 }
86
GetBundleName() const87 std::string ScreenCastConnection::GetBundleName() const
88 {
89 return bundleName_;
90 }
91
GetAbilityName() const92 std::string ScreenCastConnection::GetAbilityName() const
93 {
94 return abilityName_;
95 }
96
IsConnectedSync()97 bool ScreenCastConnection::IsConnectedSync()
98 {
99 if (abilityConnection_ == nullptr) {
100 TLOGE(WmsLogTag::DMS, "ability connection is nullptr");
101 return false;
102 }
103 return abilityConnection_->IsConnectedSync();
104 }
105
SendMessageToCastService(const int32_t & transCode,MessageParcel & data,MessageParcel & reply)106 int32_t ScreenCastConnection::SendMessageToCastService(const int32_t &transCode, MessageParcel &data,
107 MessageParcel &reply)
108 {
109 if (abilityConnection_ == nullptr) {
110 TLOGE(WmsLogTag::DMS, "ability connection is nullptr");
111 return -1;
112 }
113 return abilityConnection_->SendMessage(transCode, data, reply);
114 }
115 } // namespace OHOS::Rosen
116