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