• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "module_app_gallery/app_gallery_service_connection.h"
17 
18 #include <condition_variable>
19 #include <chrono>
20 
21 #include "module_external/bms_adapter.h"
22 #include "filemgmt_libhilog.h"
23 
24 namespace OHOS::FileManagement::Backup {
25 const int32_t CONNECT_TIME = 3;
26 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)27 void AppGalleryConnection::OnAbilityConnectDone(const AppExecFwk::ElementName &element,
28     const sptr<IRemoteObject> &remoteObject, int resultCode)
29 {
30     std::string uri = element.GetURI();
31     HILOGI("OnAbilityConnectDone, uri = %{public}s", uri.c_str());
32     appRemoteObj_ = remoteObject;
33     conditionVal_.notify_one();
34 }
35 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)36 void AppGalleryConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
37 {
38     std::string uri = element.GetURI();
39     HILOGI("OnAbilityDisconnectDone, uri = %{public}s", uri.c_str());
40     appRemoteObj_ = nullptr;
41 }
42 
ConnectExtAbility(std::string abilityName)43 bool AppGalleryConnection::ConnectExtAbility(std::string abilityName)
44 {
45     HILOGI("ConnectExtAbility called, userId = %{public}d", userId);
46     std::lock_guard<std::mutex> autoLock(appRemoteObjLock_);
47     if (appRemoteObj_ != nullptr) {
48         return true;
49     }
50 
51     auto appGalleryBundleName = BundleMgrAdapter::GetAppGalleryBundleName();
52     if (appGalleryBundleName.empty()) {
53         HILOGE("ConnectExtAbility GetAppGalleryBundleName failed, userId = %{public}d", userId);
54         return false;
55     }
56 
57     Want want;
58     want.SetElementName(appGalleryBundleName.c_str(), abilityName);
59     auto ret = AbilityManagerClient::GetInstance()->ConnectAbility(want, this, userId);
60 
61     std::unique_lock<std::mutex> uniqueLock(connectMutex);
62     conditionVal_.wait_for(uniqueLock, std::chrono::seconds(CONNECT_TIME));
63     if (ret != IAppGalleryService::ERR_OK || appRemoteObj_ == nullptr) {
64         HILOGE("ConnectExtAbility failed, ret=%{public}d, userId = %{public}d", ret, userId);
65         appRemoteObj_ = nullptr;
66         return false;
67     }
68     HILOGI("ConnectExtAbility success, ret=%{public}d, userId = %{public}d", ret, userId);
69     return true;
70 }
71 
GetRemoteObj()72 sptr<IRemoteObject> AppGalleryConnection::GetRemoteObj()
73 {
74     return appRemoteObj_;
75 }
76 } // namespace OHOS::FileManagement::Backup