1 /*
2 * Copyright (c) 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 "bundle_status_adapter.h"
17
18 #include "avsession_log.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21
22 namespace OHOS::AVSession {
BundleStatusAdapter()23 BundleStatusAdapter::BundleStatusAdapter()
24 {
25 SLOGI("construct");
26 }
27
~BundleStatusAdapter()28 BundleStatusAdapter::~BundleStatusAdapter()
29 {
30 SLOGI("destroy");
31 }
32
GetInstance()33 BundleStatusAdapter& BundleStatusAdapter::GetInstance()
34 {
35 static BundleStatusAdapter bundleStatusAdapter;
36 return bundleStatusAdapter;
37 }
38
Init()39 void BundleStatusAdapter::Init()
40 {
41 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42 if (!systemAbilityManager) {
43 SLOGI("fail to get system ability mgr");
44 return;
45 }
46
47 auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
48 if (!remoteObject) {
49 SLOGI("fail to get bundle manager proxy");
50 return;
51 }
52
53 SLOGI("get bundle manager proxy success");
54 bundleMgrProxy = iface_cast<AppExecFwk::BundleMgrProxy>(remoteObject);
55 }
56
SubscribeBundleStatusEvent(const std::string bundleName,const std::function<void (const std::string)> & callback)57 bool BundleStatusAdapter::SubscribeBundleStatusEvent(const std::string bundleName,
58 const std::function<void(const std::string)>& callback)
59 {
60 SLOGI("Bundle status adapter subscribe bundle status event, bundleName=%{public}s", bundleName.c_str());
61 auto bundleStatusCallback = [this](std::string bundleName) {
62 NotifyBundleRemoved(bundleName);
63 };
64 sptr<BundleStatusCallbackImpl> bundleStatusCallbackImpl =
65 new(std::nothrow) BundleStatusCallbackImpl(bundleStatusCallback);
66 if (bundleStatusCallbackImpl == nullptr) {
67 SLOGE("no memory");
68 return false;
69 }
70 if (bundleMgrProxy == nullptr) {
71 SLOGE("SubscribeBundleStatusEvent with proxy null!");
72 Init();
73 if (bundleMgrProxy == nullptr) {
74 SLOGE("SubscribeBundleStatusEvent with proxy null after init!");
75 return false;
76 }
77 }
78 bundleStatusCallbackImpl->SetBundleName(bundleName);
79 if (bundleMgrProxy->RegisterBundleStatusCallback(bundleStatusCallbackImpl)) {
80 bundleStatusListeners_.insert(std::pair<std::string, std::function<void(const std::string)>>(bundleName,
81 callback));
82 return true;
83 } else {
84 SLOGE("Register bundle status callback failed, bundleName=%{public}s", bundleName.c_str());
85 return false;
86 }
87 }
88
IsAudioPlayback(const std::string & bundleName,const std::string & abilityName)89 bool BundleStatusAdapter::IsAudioPlayback(const std::string& bundleName, const std::string& abilityName)
90 {
91 SLOGI("Estimate bundle audio playback status, bundleName=%{public}s", bundleName.c_str());
92 AppExecFwk::AbilityInfo abilityInfo;
93 bool flag = false;
94 if (bundleMgrProxy->GetAbilityInfo(bundleName, abilityName, abilityInfo)) {
95 flag = static_cast<int32_t>(abilityInfo.backgroundModes) == BACKGROUND_MODE_DEMAND ? true : false;
96 }
97 return flag;
98 }
99
NotifyBundleRemoved(const std::string bundleName)100 void BundleStatusAdapter::NotifyBundleRemoved(const std::string bundleName)
101 {
102 auto bundleStatusListener = bundleStatusListeners_.find(bundleName);
103 if (bundleStatusListener == bundleStatusListeners_.end()) {
104 return;
105 }
106 bundleStatusListener->second(bundleName);
107 SLOGI("erase bundle status callback, bundleName=%{public}s", bundleName.c_str());
108 bundleStatusListeners_.erase(bundleName);
109 }
110
GetBundleNameFromUid(const int32_t uid)111 std::string BundleStatusAdapter::GetBundleNameFromUid(const int32_t uid)
112 {
113 std::string bundleName {""};
114 if (bundleMgrProxy != nullptr) {
115 bundleMgrProxy->GetNameForUid(uid, bundleName);
116 }
117 return bundleName;
118 }
119
BundleStatusCallbackImpl(const std::function<void (const std::string)> & callback)120 BundleStatusCallbackImpl::BundleStatusCallbackImpl(const std::function<void(const std::string)>& callback)
121 {
122 SLOGI("Create bundle status instance");
123 callback_ = callback;
124 }
125
~BundleStatusCallbackImpl()126 BundleStatusCallbackImpl::~BundleStatusCallbackImpl()
127 {
128 SLOGI("Destroy bundle status instance");
129 }
130
OnBundleStateChanged(const uint8_t installType,const int32_t resultCode,const std::string & resultMsg,const std::string & bundleName)131 void BundleStatusCallbackImpl::OnBundleStateChanged(const uint8_t installType, const int32_t resultCode,
132 const std::string &resultMsg, const std::string &bundleName)
133 {
134 if (installType == static_cast<uint8_t>(AppExecFwk::InstallType::UNINSTALL_CALLBACK)) {
135 callback_(bundleName);
136 }
137 }
138 }