• 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 <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 
ShowProgress(const ProgressMessageInfo & message)109 int32_t PasteBoardDialog::ShowProgress(const ProgressMessageInfo &message)
110 {
111     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "showprogress begin");
112     auto abilityManager = GetAbilityManagerService();
113     if (abilityManager == nullptr) {
114         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get ability manager failed");
115         return static_cast<int32_t>(PasteboardError::OBTAIN_SERVER_SA_ERROR);
116     }
117 
118     Want want;
119     want.SetElementName(PASTEBOARD_DIALOG_APP, PASTEBOARD_PROGRESS_ABILITY);
120     want.SetAction(PASTEBOARD_PROGRESS_ABILITY);
121     want.SetParam("promptText", message.promptText);
122     want.SetParam("remoteDeviceName", message.remoteDeviceName);
123     want.SetParam("progressKey", message.progressKey);
124     want.SetParam("isRemote", message.isRemote);
125     want.SetParam("windowId", message.windowId);
126     if (message.clientCallback != nullptr) {
127         want.SetParam("ipcCallback", message.clientCallback);
128     }
129     if (message.callerToken != nullptr) {
130         want.SetParam("tokenKey", message.callerToken);
131     }
132     int32_t result = IN_PROCESS_CALL(abilityManager->StartAbility(want));
133     if (result != 0) {
134         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "start pasteboard progress failed, result:%{public}d", result);
135         return static_cast<int32_t>(PasteboardError::PROGRESS_START_ERROR);
136     }
137     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "start pasteboard progress success.");
138     return static_cast<int32_t>(PasteboardError::E_OK);
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