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 <thread>
17
18 #include "ability_connect_callback_stub.h"
19 #include "in_process_call_wrapper.h"
20 #include "iservice_registry.h"
21 #include "pasteboard_dialog.h"
22 #include "pasteboard_error.h"
23 #include "pasteboard_hilog.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS::MiscServices {
27 using namespace OHOS::AAFwk;
28 class DialogConnection : public AAFwk::AbilityConnectionStub {
29 public:
DialogConnection(PasteBoardDialog::Cancel cancel)30 explicit DialogConnection(PasteBoardDialog::Cancel cancel) : cancel_(std::move(cancel)) {}
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
ShowToast(const ToastMessageInfo & message)63 int32_t PasteBoardDialog::ShowToast(const ToastMessageInfo &message)
64 {
65 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "begin, app:%{public}s", message.appName.c_str());
66 auto abilityManager = GetAbilityManagerService();
67 if (abilityManager == nullptr) {
68 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get ability manager failed");
69 return static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR);
70 }
71 Want want;
72 want.SetAction("");
73 want.SetElementName(PASTEBOARD_DIALOG_APP, PASTEBOARD_TOAST_ABILITY);
74 want.SetParam("appName", message.appName);
75
76 std::lock_guard<std::mutex> lock(connectionLock_);
77 connection_ = new DialogConnection(nullptr);
78 int32_t result = IN_PROCESS_CALL(
79 abilityManager->ConnectAbility(want, iface_cast<AAFwk::IAbilityConnection>(connection_), nullptr));
80 if (result != 0) {
81 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "start pasteboard toast failed, result:%{public}d", result);
82 return static_cast<int32_t>(PasteboardError::TASK_PROCESSING);
83 }
84 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "start pasteboard toast success");
85 std::thread thread([this]() mutable {
86 std::this_thread::sleep_for(std::chrono::milliseconds(SHOW_TOAST_TIME));
87 CancelToast();
88 });
89 thread.detach();
90 return static_cast<int32_t>(PasteboardError::E_OK);
91 }
92
CancelToast()93 void PasteBoardDialog::CancelToast()
94 {
95 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "begin");
96 auto abilityManager = GetAbilityManagerService();
97 if (abilityManager == nullptr) {
98 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get ability manager failed");
99 return;
100 }
101 std::lock_guard<std::mutex> lock(connectionLock_);
102 int result = IN_PROCESS_CALL(abilityManager->DisconnectAbility(connection_));
103 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "disconnect toast ability:%{public}d", result);
104 }
105
ShowProgress(const ProgressMessageInfo & message)106 int32_t PasteBoardDialog::ShowProgress(const ProgressMessageInfo &message)
107 {
108 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "showprogress begin");
109 auto abilityManager = GetAbilityManagerService();
110 if (abilityManager == nullptr) {
111 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get ability manager failed");
112 return static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR);
113 }
114
115 Want want;
116 want.SetElementName(PASTEBOARD_DIALOG_APP, PASTEBOARD_PROGRESS_ABILITY);
117 want.SetAction(PASTEBOARD_PROGRESS_ABILITY);
118 want.SetParam("promptText", message.promptText);
119 want.SetParam("remoteDeviceName", message.remoteDeviceName);
120 want.SetParam("progressKey", message.progressKey);
121 want.SetParam("isRemote", message.isRemote);
122 want.SetParam("windowId", message.windowId);
123 if (message.clientCallback != nullptr) {
124 want.SetParam("ipcCallback", message.clientCallback);
125 }
126 if (message.callerToken != nullptr) {
127 want.SetParam("tokenKey", message.callerToken);
128 }
129 int32_t result = IN_PROCESS_CALL(abilityManager->StartAbility(want));
130 if (result != 0) {
131 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "start pasteboard progress failed, result:%{public}d", result);
132 return static_cast<int32_t>(PasteboardError::PROGRESS_START_ERROR);
133 }
134 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start pasteboard progress success.");
135 return static_cast<int32_t>(PasteboardError::E_OK);
136 }
137
GetAbilityManagerService()138 sptr<IAbilityManager> PasteBoardDialog::GetAbilityManagerService()
139 {
140 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "begin");
141 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
142 if (systemAbilityManager == nullptr) {
143 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "failed to get samgr");
144 return nullptr;
145 }
146
147 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
148 if (remoteObject == nullptr) {
149 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "failed to get ability manager service");
150 return nullptr;
151 }
152 return iface_cast<IAbilityManager>(remoteObject);
153 }
154 } // namespace OHOS::MiscServices
155