• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_active_bundle_mgr_helper.h"
17 
18 #include "bundle_active_constant.h"
19 #include "bundle_active_log.h"
20 #include "accesstoken_kit.h"
21 #include "application_info.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "tokenid_kit.h"
25 
26 namespace OHOS {
27 namespace DeviceUsageStats {
28 using OHOS::AppExecFwk::Constants::PERMISSION_GRANTED;
29 
BundleActiveBundleMgrHelper()30 BundleActiveBundleMgrHelper::BundleActiveBundleMgrHelper()
31 {
32 }
33 
~BundleActiveBundleMgrHelper()34 BundleActiveBundleMgrHelper::~BundleActiveBundleMgrHelper()
35 {
36     launcherAppMap_.clear();
37     packageContainUidMap_.clear();
38 }
39 
GetNameForUid(int32_t uid,std::string & bundleName)40 void BundleActiveBundleMgrHelper::GetNameForUid(int32_t uid, std::string& bundleName)
41 {
42     std::lock_guard<ffrt::mutex> lock(connectionMutex_);
43     if (!Connect()) {
44         return;
45     }
46     if (!bundleMgr_) {
47         return;
48     }
49     bundleMgr_->GetNameForUid(uid, bundleName);
50     BUNDLE_ACTIVE_LOGD("get Bundle Name: %{public}s", bundleName.c_str());
51 }
52 
GetApplicationInfo(const std::string & appName,const AppExecFwk::ApplicationFlag flag,const int userId,AppExecFwk::ApplicationInfo & appInfo)53 bool BundleActiveBundleMgrHelper::GetApplicationInfo(const std::string &appName, const AppExecFwk::ApplicationFlag flag,
54     const int userId, AppExecFwk::ApplicationInfo &appInfo)
55 {
56     BUNDLE_ACTIVE_LOGD("start get application info");
57     std::lock_guard<ffrt::mutex> lock(connectionMutex_);
58 
59     if (!Connect()) {
60         return false;
61     }
62     if (!bundleMgr_) {
63         return false;
64     }
65     BUNDLE_ACTIVE_LOGI("bundleMgr is null: %{public}d ", bundleMgr_ == nullptr);
66     if (bundleMgr_ != nullptr && bundleMgr_->GetApplicationInfo(appName, flag, userId, appInfo)) {
67         return true;
68     }
69     return false;
70 }
71 
GetApplicationInfos(const AppExecFwk::ApplicationFlag flag,const int userId,std::vector<BundleActiveApplication> & bundleActiveApplicationInfos)72 bool BundleActiveBundleMgrHelper::GetApplicationInfos(const AppExecFwk::ApplicationFlag flag,
73     const int userId, std::vector<BundleActiveApplication> &bundleActiveApplicationInfos)
74 {
75     BUNDLE_ACTIVE_LOGI("start get application infos");
76     std::lock_guard<ffrt::mutex> lock(connectionMutex_);
77 
78     if (!Connect()) {
79         return false;
80     }
81     BUNDLE_ACTIVE_LOGI("bundleMgr is null: %{public}d ", bundleMgr_ == nullptr);
82     std::vector<AppExecFwk::ApplicationInfo> appInfos;
83     if (bundleMgr_ == nullptr || !bundleMgr_->GetApplicationInfos(flag, userId, appInfos)) {
84         return false;
85     }
86     for (auto appInfo : appInfos) {
87         bundleActiveApplicationInfos.emplace_back(appInfo.bundleName, appInfo.uid, appInfo.isLauncherApp);
88     }
89     return true;
90 }
91 
GetBundleInfo(const std::string & bundleName,const AppExecFwk::BundleFlag flag,AppExecFwk::BundleInfo & bundleInfo,int32_t userId)92 bool BundleActiveBundleMgrHelper::GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag,
93     AppExecFwk::BundleInfo &bundleInfo, int32_t userId)
94 {
95     std::lock_guard<ffrt::mutex> lock(connectionMutex_);
96 
97     if (!Connect()) {
98         return false;
99     }
100     if (!bundleMgr_) {
101         return false;
102     }
103     if (bundleMgr_ != nullptr && bundleMgr_->GetBundleInfo(bundleName, flag, bundleInfo, userId)) {
104         return true;
105     }
106     return false;
107 }
108 
Connect()109 bool BundleActiveBundleMgrHelper::Connect()
110 {
111     if (bundleMgr_ != nullptr) {
112         return true;
113     }
114 
115     sptr<ISystemAbilityManager> systemAbilityManager =
116         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
117     if (systemAbilityManager == nullptr) {
118         BUNDLE_ACTIVE_LOGE("get SystemAbilityManager failed");
119         return false;
120     }
121 
122     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
123     if (remoteObject == nullptr) {
124         BUNDLE_ACTIVE_LOGE("get Bundle Manager failed");
125         return false;
126     }
127 
128     bundleMgr_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
129     return bundleMgr_ ? true : false;
130 }
131 
IsLauncherApp(const std::string & bundleName,const int32_t userId)132 bool BundleActiveBundleMgrHelper::IsLauncherApp(const std::string &bundleName, const int32_t userId)
133 {
134     if (!isInitLauncherAppMap_) {
135         InitLauncherAppMap();
136     }
137     if (launcherAppMap_.find(bundleName) != launcherAppMap_.end()) {
138         BUNDLE_ACTIVE_LOGD("launcherAppMap cache, bundleName:%{public}s isLauncherApp:%{public}d",
139             bundleName.c_str(), launcherAppMap_[bundleName]);
140         return launcherAppMap_[bundleName];
141     }
142     AppExecFwk::ApplicationInfo appInfo;
143     if (!GetApplicationInfo(bundleName,
144         AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, userId, appInfo)) {
145         BUNDLE_ACTIVE_LOGE("get applicationInfo failed.");
146         return false;
147     }
148     launcherAppMap_[bundleName] = appInfo.isLauncherApp;
149     BUNDLE_ACTIVE_LOGD("insert launcherAppMap, bundleName:%{public}s isLauncherApp:%{public}d",
150         bundleName.c_str(), launcherAppMap_[bundleName]);
151     return appInfo.isLauncherApp;
152 }
153 
InitLauncherAppMap()154 void BundleActiveBundleMgrHelper::InitLauncherAppMap()
155 {
156     BUNDLE_ACTIVE_LOGI("init laucherAppMap");
157     isInitLauncherAppMap_ = true;
158     InitSystemEvent();
159     std::vector<BundleActiveApplication> appInfos;
160     if (!GetApplicationInfos(AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO,
161         AppExecFwk::Constants::ALL_USERID, appInfos)) {
162         BUNDLE_ACTIVE_LOGE("Init Launcher App Map by BMS failed");
163         return;
164     }
165     for (auto appInfo : appInfos) {
166         launcherAppMap_[appInfo.bundleName] = appInfo.isLauncherApp;
167     }
168 }
169 
InitSystemEvent()170 void BundleActiveBundleMgrHelper::InitSystemEvent()
171 {
172     launcherAppMap_[OPERATION_SYSTEM_LOCK] = false;
173     launcherAppMap_[OPERATION_SYSTEM_UNLOCK] = false;
174     launcherAppMap_[OPERATION_SYSTEM_SLEEP] = false;
175     launcherAppMap_[OPERATION_SYSTEM_WAKEUP] = false;
176 }
177 
InsertPackageUid(const std::string & bundleName,const int32_t uid)178 void BundleActiveBundleMgrHelper::InsertPackageUid(const std::string &bundleName, const int32_t uid)
179 {
180     std::lock_guard<ffrt::mutex> lock(packageContainUidMapMutex_);
181     packageContainUidMap_[bundleName].insert(uid);
182 }
183 
DeletePackageUid(const std::string & bundleName,const int32_t uid)184 void BundleActiveBundleMgrHelper::DeletePackageUid(const std::string &bundleName, const int32_t uid)
185 {
186     std::lock_guard<ffrt::mutex> lock(packageContainUidMapMutex_);
187     packageContainUidMap_[bundleName].erase(uid);
188     if (packageContainUidMap_[bundleName].size() == 0) {
189         packageContainUidMap_.erase(bundleName);
190     }
191 }
192 
DeleteMemPackage(const std::string & bundleName)193 void BundleActiveBundleMgrHelper::DeleteMemPackage(const std::string &bundleName)
194 {
195     std::lock_guard<ffrt::mutex> lock(packageContainUidMapMutex_);
196     packageContainUidMap_.erase(bundleName);
197 }
198 
GetPackageUidSet(const std::string & bundleName)199 std::set<int32_t> BundleActiveBundleMgrHelper::GetPackageUidSet(const std::string &bundleName)
200 {
201     std::lock_guard<ffrt::mutex> lock(packageContainUidMapMutex_);
202     auto iter = packageContainUidMap_.find(bundleName);
203     if (iter == packageContainUidMap_.end()) {
204         return std::set<int32_t>();
205     }
206     return iter->second;
207 }
208 
209 }  // namespace DeviceUsageStats
210 }  // namespace OHOS