1 /*
2 * Copyright (C) 2024 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 #include "ability_manager_helper.h"
16
17
18 namespace OHOS {
19 namespace Notification {
20
AbilityManagerHelper()21 AbilityManagerHelper::AbilityManagerHelper()
22 {
23 operationQueue_ = std::make_shared<ffrt::queue>("dans_operation");
24 if (operationQueue_ == nullptr) {
25 ANS_LOGW("ffrt create failed!");
26 return;
27 }
28 ANS_LOGI("Operation service init successfully.");
29 }
30
GetInstance()31 AbilityManagerHelper& AbilityManagerHelper::GetInstance()
32 {
33 static AbilityManagerHelper abilityManagerHelper;
34 return abilityManagerHelper;
35 }
36
ConnectAbility(const std::string & eventId,const AAFwk::Want & want,const std::string & userInputKey,const std::string & userInput)37 int AbilityManagerHelper::ConnectAbility(const std::string &eventId, const AAFwk::Want &want,
38 const std::string& userInputKey, const std::string& userInput)
39 {
40 ANS_LOGI("enter, target bundle = %{public}s", want.GetBundle().c_str());
41 std::lock_guard<ffrt::mutex> lock(connectionLock_);
42 sptr<DistributedOperationConnection> connection =
43 new (std::nothrow) DistributedOperationConnection(eventId, userInputKey, userInput);
44 if (connection == nullptr) {
45 ANS_LOGI("failed to create obj!");
46 return -1;
47 }
48 int32_t result = AAFwk::AbilityManagerClient::GetInstance()->StartAbilityByCall(want, connection);
49 if (result == ERR_OK) {
50 operationConnection_[eventId] = connection;
51 }
52 ANS_LOGI("Ability manager connect call %{public}d %{public}d!", result, (int32_t)(operationConnection_.size()));
53 return result;
54 }
55
DisconnectServiceAbility(const std::string & eventId,const AppExecFwk::ElementName & element)56 void AbilityManagerHelper::DisconnectServiceAbility(const std::string &eventId, const AppExecFwk::ElementName& element)
57 {
58 ANS_LOGI("DisconnectServiceAbility %{public}s", eventId.c_str());
59 if (operationQueue_ == nullptr) {
60 ANS_LOGI("operationQueue is nullptr");
61 return;
62 }
63
64 std::function<void()> task = [this, eventId, element]() {
65 std::lock_guard<ffrt::mutex> lock(connectionLock_);
66 auto iter = operationConnection_.find(eventId);
67 if (iter == operationConnection_.end()) {
68 ANS_LOGI("failed to find connection!");
69 return;
70 }
71
72 auto ret = AAFwk::AbilityManagerClient::GetInstance()->ReleaseCall(iter->second, element);
73 operationConnection_.erase(eventId);
74 ANS_LOGI("Ability manager releas call %{public}d %{public}d!", ret, (int32_t)(operationConnection_.size()));
75 };
76 operationQueue_->submit(task);
77 }
78
79 }
80 }
81