• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 
18 #include "ability_connect_callback_stub.h"
19 #include "in_process_call_wrapper.h"
20 #include "iservice_registry.h"
21 #include "pasteboard_hilog.h"
22 #include "system_ability_definition.h"
23 
24 namespace OHOS::MiscServices {
25 using namespace OHOS::AAFwk;
26 class DialogConnection : public AAFwk::AbilityConnectionStub {
27 public:
DialogConnection(PasteBoardDialog::Cancel cancel)28     explicit DialogConnection(PasteBoardDialog::Cancel cancel) : cancel_(std::move(cancel))
29     {
30     }
31     DialogConnection(const DialogConnection &) = delete;
32     DialogConnection &operator=(const DialogConnection &) = delete;
33     DialogConnection(DialogConnection &&) = delete;
34     DialogConnection &operator=(DialogConnection &&) = delete;
35     void OnAbilityConnectDone(
36         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int32_t resultCode) override;
37     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode) override;
38 
39 private:
40     PasteBoardDialog::Cancel cancel_;
41 };
42 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)43 void DialogConnection::OnAbilityConnectDone(
44     const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int32_t resultCode)
45 {
46     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "dialog ability connected");
47 }
48 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)49 void DialogConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode)
50 {
51     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "dialog ability disconnect");
52     if (cancel_ != nullptr) {
53         cancel_();
54     }
55 }
56 
GetInstance()57 PasteBoardDialog &PasteBoardDialog::GetInstance()
58 {
59     static PasteBoardDialog instance;
60     return instance;
61 }
62 
ShowDialog(const MessageInfo & message,const Cancel & cancel)63 int32_t PasteBoardDialog::ShowDialog(const MessageInfo &message, const Cancel &cancel)
64 {
65     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "begin, app:%{public}s, device:%{public}s", message.appName.c_str(),
66         message.deviceType.c_str());
67     auto abilityManager = GetAbilityManagerService();
68     if (abilityManager == nullptr) {
69         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get ability manager failed");
70         return -1;
71     }
72     Want want;
73     want.SetAction("");
74     want.SetElementName(PASTEBOARD_DIALOG_APP, PASTEBOARD_DIALOG_ABILITY);
75     want.SetParam("appName", message.appName);
76     want.SetParam("deviceType", message.deviceType);
77 
78     std::lock_guard<std::mutex> lock(connectionLock_);
79     connection_ = new DialogConnection(cancel);
80     int32_t result = IN_PROCESS_CALL(abilityManager->ConnectAbility(want, connection_, nullptr));
81     if (result != 0) {
82         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "start pasteboard dialog failed, result:%{public}d", result);
83         return -1;
84     }
85     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "start pasteboard dialog success.");
86     return 0;
87 }
88 
ShowToast(const ToastMessageInfo & message)89 int32_t PasteBoardDialog::ShowToast(const ToastMessageInfo &message)
90 {
91     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "begin, fromApp:%{public}s, toApp:%{public}s",
92         message.fromAppName.c_str(), message.toAppName.c_str());
93     auto abilityManager = GetAbilityManagerService();
94     if (abilityManager == nullptr) {
95         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get ability manager failed");
96         return -1;
97     }
98     Want want;
99     want.SetAction("");
100     want.SetElementName(PASTEBOARD_DIALOG_APP, PASTEBOARD_TOAST_ABILITY);
101     want.SetParam("fromAppName", message.fromAppName);
102     want.SetParam("toAppName", message.toAppName);
103 
104     std::lock_guard<std::mutex> lock(toastConnectionLock_);
105     toastConnection_ = new DialogConnection(nullptr);
106     int32_t result = IN_PROCESS_CALL(abilityManager->ConnectAbility(want, toastConnection_, nullptr));
107     if (result != 0) {
108         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "start pasteboard toast failed, result:%{public}d", result);
109         return -1;
110     }
111     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "start pasteboard toast success.");
112     return 0;
113 }
114 
CancelDialog()115 void PasteBoardDialog::CancelDialog()
116 {
117     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "begin");
118     auto abilityManager = GetAbilityManagerService();
119     if (abilityManager == nullptr) {
120         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get ability manager failed");
121         return;
122     }
123     std::lock_guard<std::mutex> lock(connectionLock_);
124     int result = IN_PROCESS_CALL(abilityManager->DisconnectAbility(connection_));
125     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "disconnect dialog ability:%{public}d", result);
126 }
127 
CancelToast()128 void PasteBoardDialog::CancelToast()
129 {
130     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "begin");
131     auto abilityManager = GetAbilityManagerService();
132     if (abilityManager == nullptr) {
133         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get ability manager failed");
134         return;
135     }
136     std::lock_guard<std::mutex> lock(toastConnectionLock_);
137     int result = IN_PROCESS_CALL(abilityManager->DisconnectAbility(toastConnection_));
138     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "disconnect toast ability:%{public}d", result);
139 }
140 
GetAbilityManagerService()141 sptr<IAbilityManager> PasteBoardDialog::GetAbilityManagerService()
142 {
143     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "begin");
144     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
145     if (systemAbilityManager == nullptr) {
146         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "failed to get samgr");
147         return nullptr;
148     }
149 
150     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
151     if (!remoteObject) {
152         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "failed to get ability manager service");
153         return nullptr;
154     }
155     return iface_cast<IAbilityManager>(remoteObject);
156 }
157 } // namespace OHOS::MiscServices
158