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