• 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 
ShowToast(const ToastMessageInfo & message)63 int32_t PasteBoardDialog::ShowToast(const ToastMessageInfo &message)
64 {
65     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "begin, fromApp:%{public}s, toApp:%{public}s",
66         message.fromAppName.c_str(), message.toAppName.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_TOAST_ABILITY);
75     want.SetParam("fromAppName", message.fromAppName);
76     want.SetParam("toAppName", message.toAppName);
77 
78     std::lock_guard<std::mutex> lock(connectionLock_);
79     connection_ = new DialogConnection(nullptr);
80     int32_t result = IN_PROCESS_CALL(abilityManager->ConnectAbility(want, connection_, nullptr));
81     if (result != 0) {
82         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "start pasteboard toast failed, result:%{public}d", result);
83         return -1;
84     }
85     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "start pasteboard toast success.");
86     return 0;
87 }
88 
CancelToast()89 void PasteBoardDialog::CancelToast()
90 {
91     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "begin");
92     auto abilityManager = GetAbilityManagerService();
93     if (abilityManager == nullptr) {
94         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get ability manager failed");
95         return;
96     }
97     std::lock_guard<std::mutex> lock(connectionLock_);
98     int result = IN_PROCESS_CALL(abilityManager->DisconnectAbility(connection_));
99     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "disconnect toast ability:%{public}d", result);
100 }
101 
GetAbilityManagerService()102 sptr<IAbilityManager> PasteBoardDialog::GetAbilityManagerService()
103 {
104     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "begin");
105     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
106     if (systemAbilityManager == nullptr) {
107         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "failed to get samgr");
108         return nullptr;
109     }
110 
111     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
112     if (!remoteObject) {
113         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "failed to get ability manager service");
114         return nullptr;
115     }
116     return iface_cast<IAbilityManager>(remoteObject);
117 }
118 } // namespace OHOS::MiscServices
119