1 /*
2 * Copyright (c) 2022 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 "pasteboard_dialog.h"
17 #include "ability_connect_callback_stub.h"
18 #include "iservice_registry.h"
19 #include "pasteboard_hilog.h"
20 #include "system_ability_definition.h"
21
22 namespace OHOS::MiscServices {
23 using namespace OHOS::AAFwk;
24 class DialogConnection : public AAFwk::AbilityConnectionStub {
25 public:
DialogConnection(PasteBoardDialog::Cancel cancel)26 explicit DialogConnection(PasteBoardDialog::Cancel cancel) : cancel_(std::move(cancel))
27 {
28 }
29 DialogConnection(const DialogConnection &) = delete;
30 DialogConnection &operator=(const DialogConnection &) = delete;
31 DialogConnection(DialogConnection &&) = delete;
32 DialogConnection &operator=(DialogConnection &&) = delete;
33 void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject,
34 int32_t resultCode) override;
35 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode) override;
36
37 private:
38 PasteBoardDialog::Cancel cancel_;
39 };
40
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)41 void DialogConnection::OnAbilityConnectDone(const AppExecFwk::ElementName &element,
42 const sptr<IRemoteObject> &remoteObject, int32_t resultCode)
43 {
44 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "dialog ability connected");
45 }
46
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)47 void DialogConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode)
48 {
49 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "dialog ability disconnect");
50 if (cancel_ != nullptr) {
51 cancel_();
52 }
53 }
54
GetInstance()55 PasteBoardDialog &PasteBoardDialog::GetInstance()
56 {
57 static PasteBoardDialog instance;
58 return instance;
59 }
60
ShowDialog(const MessageInfo & message,const Cancel & cancel)61 int32_t PasteBoardDialog::ShowDialog(const MessageInfo &message, const Cancel &cancel)
62 {
63 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "begin, app:%{public}s, device:%{public}s", message.appName.c_str(),
64 message.deviceType.c_str());
65 auto abilityManager = GetAbilityManagerService();
66 if (abilityManager == nullptr) {
67 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get ability manager failed");
68 return -1;
69 }
70 Want want;
71 want.SetAction("");
72 want.SetElementName(PASTEBOARD_DIALOG_APP, PASTEBOARD_DIALOG_ABILITY);
73 want.SetParam("appName", message.appName);
74 want.SetParam("deviceType", message.deviceType);
75
76 std::lock_guard<std::mutex> lock(connectionLock_);
77 connection_ = new DialogConnection(cancel);
78 int32_t result = abilityManager->ConnectAbility(want, connection_, nullptr);
79 if (result != 0) {
80 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "start pasteboard dialog failed, result:%{public}d", result);
81 return -1;
82 }
83 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "start pasteboard dialog success.");
84 return 0;
85 }
86
CancelDialog()87 void PasteBoardDialog::CancelDialog()
88 {
89 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "begin");
90 auto abilityManager = GetAbilityManagerService();
91 if (abilityManager == nullptr) {
92 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get ability manager failed");
93 return;
94 }
95 std::lock_guard<std::mutex> lock(connectionLock_);
96 int result = abilityManager->DisconnectAbility(connection_);
97 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "disconnect dialog ability:%{public}d", result);
98 }
99
GetAbilityManagerService()100 sptr<IAbilityManager> PasteBoardDialog::GetAbilityManagerService()
101 {
102 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "begin");
103 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
104 if (systemAbilityManager == nullptr) {
105 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "failed to get samgr");
106 return nullptr;
107 }
108
109 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
110 if (!remoteObject) {
111 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "failed to get ability manager service");
112 return nullptr;
113 }
114 return iface_cast<IAbilityManager>(remoteObject);
115 }
116 } // namespace OHOS::MiscServices
117