• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "launcher_service.h"
17 
18 #include "bundle_mgr_proxy.h"
19 #include "common_event_subscribe_info.h"
20 #include "common_event_support.h"
21 #include "matching_skills.h"
22 #include "operation_builder.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> LauncherService::bundleMgr_ = nullptr;
27 std::mutex LauncherService::bundleMgrMutex_;
28 const char* EMPTY_STRING = "";
29 
LauncherService()30 LauncherService::LauncherService()
31 {
32     init();
33 }
34 
init()35 void LauncherService::init()
36 {
37     EventFwk::MatchingSkills matchingSkills;
38     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
39     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
40     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
41     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
42     bundleMonitor_ = std::make_shared<BundleMonitor>(subscribeInfo);
43 }
44 
GetBundleMgr()45 OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> LauncherService::GetBundleMgr()
46 {
47     if (bundleMgr_ == nullptr) {
48         std::lock_guard<std::mutex> lock(bundleMgrMutex_);
49         if (bundleMgr_ == nullptr) {
50             auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
51             if (systemAbilityManager == nullptr) {
52                 APP_LOGE("GetBundleMgr GetSystemAbilityManager is null");
53                 return nullptr;
54             }
55             auto bundleMgrSa = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
56             if (bundleMgrSa == nullptr) {
57                 APP_LOGE("GetBundleMgr GetSystemAbility is null");
58                 return nullptr;
59             }
60             auto bundleMgr = OHOS::iface_cast<IBundleMgr>(bundleMgrSa);
61             if (bundleMgr == nullptr) {
62                 APP_LOGE("GetBundleMgr iface_cast get null");
63             }
64             bundleMgr_ = bundleMgr;
65         }
66     }
67     return bundleMgr_;
68 }
69 
RegisterCallback(const sptr<IBundleStatusCallback> & callback)70 bool LauncherService::RegisterCallback(const sptr<IBundleStatusCallback> &callback)
71 {
72     APP_LOGI("RegisterCallback called");
73     if (bundleMonitor_ == nullptr) {
74         APP_LOGE("failed to register callback, bundleMonitor is null");
75         return false;
76     }
77 
78     // check permission
79     auto iBundleMgr = GetBundleMgr();
80     if (iBundleMgr == nullptr) {
81         APP_LOGE("can not get iBundleMgr");
82         return false;
83     }
84     if (!iBundleMgr->VerifySystemApi(Constants::INVALID_API_VERSION)) {
85         APP_LOGE("non-system app calling system api");
86         return false;
87     }
88     if (!iBundleMgr->VerifyCallingPermission(Constants::LISTEN_BUNDLE_CHANGE)) {
89         APP_LOGE("register bundle status callback failed due to lack of permission");
90         return false;
91     }
92     return bundleMonitor_->Subscribe(callback);
93 }
94 
UnRegisterCallback()95 bool LauncherService::UnRegisterCallback()
96 {
97     APP_LOGI("UnRegisterCallback called");
98     if (bundleMonitor_ == nullptr) {
99         APP_LOGE("failed to unregister callback, bundleMonitor is null");
100         return false;
101     }
102 
103     // check permission
104     auto iBundleMgr = GetBundleMgr();
105     if (iBundleMgr == nullptr) {
106         APP_LOGE("can not get iBundleMgr");
107         return false;
108     }
109     if (!iBundleMgr->VerifySystemApi(Constants::INVALID_API_VERSION)) {
110         APP_LOGE("non-system app calling system api");
111         return false;
112     }
113     if (!iBundleMgr->VerifyCallingPermission(Constants::LISTEN_BUNDLE_CHANGE)) {
114         APP_LOGE("register bundle status callback failed due to lack of permission");
115         return false;
116     }
117     return bundleMonitor_->UnSubscribe();
118 }
119 
GetAbilityList(const std::string & bundleName,const int userId,std::vector<LauncherAbilityInfo> & launcherAbilityInfos)120 bool LauncherService::GetAbilityList(
121     const std::string &bundleName, const int userId, std::vector<LauncherAbilityInfo> &launcherAbilityInfos)
122 {
123     APP_LOGD("GetAbilityList called");
124     auto iBundleMgr = GetBundleMgr();
125     if ((iBundleMgr == nullptr) || (bundleName.empty())) {
126         APP_LOGE("can not get iBundleMgr");
127         return false;
128     }
129     if (!iBundleMgr->VerifySystemApi(Constants::INVALID_API_VERSION)) {
130         APP_LOGE("non-system app calling system api");
131         return false;
132     }
133     std::vector<std::string> entities;
134     entities.push_back(Want::ENTITY_HOME);
135     Want want;
136     want.SetAction(Want::ACTION_HOME);
137     want.AddEntity(Want::ENTITY_HOME);
138     ElementName elementName;
139     elementName.SetBundleName(bundleName);
140     want.SetElement(elementName);
141     std::vector<AbilityInfo> abilityInfos;
142     if (!iBundleMgr->QueryAllAbilityInfos(want, userId, abilityInfos)) {
143         APP_LOGE("Query ability info failed");
144         return false;
145     }
146 
147     for (auto &ability : abilityInfos) {
148         if (ability.bundleName != bundleName || !ability.enabled) {
149             continue;
150         }
151 
152         LauncherAbilityInfo info;
153         info.applicationInfo = ability.applicationInfo;
154         info.labelId = ability.labelId;
155         info.iconId = ability.iconId;
156         ElementName abilityElementName;
157         abilityElementName.SetBundleName(ability.bundleName);
158         abilityElementName.SetModuleName(ability.moduleName);
159         abilityElementName.SetAbilityName(ability.name);
160         abilityElementName.SetDeviceID(ability.deviceId);
161         info.elementName = abilityElementName;
162         info.userId = userId;
163         info.installTime = ability.installTime;
164         launcherAbilityInfos.emplace_back(info);
165     }
166 
167     return true;
168 }
169 
GetAllLauncherAbilityInfos(int32_t userId,std::vector<LauncherAbilityInfo> & launcherAbilityInfos)170 bool LauncherService::GetAllLauncherAbilityInfos(int32_t userId, std::vector<LauncherAbilityInfo> &launcherAbilityInfos)
171 {
172     auto iBundleMgr = GetBundleMgr();
173     if (iBundleMgr == nullptr) {
174         APP_LOGE("can not get iBundleMgr");
175         return false;
176     }
177     if (!iBundleMgr->VerifySystemApi(Constants::INVALID_API_VERSION)) {
178         APP_LOGE("non-system app calling system api");
179         return false;
180     }
181     Want want;
182     want.SetAction(Want::ACTION_HOME);
183     want.AddEntity(Want::ENTITY_HOME);
184 
185     std::vector<AbilityInfo> abilityInfos;
186     if (!iBundleMgr->QueryAllAbilityInfos(want, userId, abilityInfos)) {
187         APP_LOGE("Query ability info failed");
188         return false;
189     }
190 
191     for (const auto& ability : abilityInfos) {
192         if (ability.applicationInfo.isLauncherApp || !ability.enabled) {
193             continue;
194         }
195 
196         if (ability.applicationInfo.hideDesktopIcon) {
197             APP_LOGD("Bundle(%{public}s) hide desktop icon", ability.bundleName.c_str());
198             continue;
199         }
200 
201         LauncherAbilityInfo info;
202         info.installTime = ability.installTime;
203         info.applicationInfo = ability.applicationInfo;
204         info.labelId = ability.labelId;
205         info.iconId = ability.iconId;
206         info.userId = userId;
207         ElementName elementName;
208         elementName.SetBundleName(ability.bundleName);
209         elementName.SetModuleName(ability.moduleName);
210         elementName.SetAbilityName(ability.name);
211         elementName.SetDeviceID(ability.deviceId);
212         info.elementName = elementName;
213         launcherAbilityInfos.emplace_back(info);
214     }
215     return true;
216 }
217 
GetShortcutInfos(const std::string & bundleName,std::vector<ShortcutInfo> & shortcutInfos)218 bool LauncherService::GetShortcutInfos(
219     const std::string &bundleName, std::vector<ShortcutInfo> &shortcutInfos)
220 {
221     APP_LOGI("GetShortcutInfos called");
222     if (bundleName.empty()) {
223         APP_LOGE("bundleName is empty");
224         return false;
225     }
226     auto iBundleMgr = GetBundleMgr();
227     if (iBundleMgr == nullptr) {
228         APP_LOGE("can not get iBundleMgr");
229         return false;
230     }
231     if (!iBundleMgr->VerifySystemApi(Constants::INVALID_API_VERSION)) {
232         APP_LOGE("non-system app calling system api");
233         return false;
234     }
235 
236     std::vector<ShortcutInfo> infos;
237     if (!iBundleMgr->GetShortcutInfos(bundleName, infos)) {
238         APP_LOGE("Get shortcut infos failed");
239         return false;
240     }
241     if (infos.size() == 0) {
242         APP_LOGE("ShortcutInfo is not exist in system");
243         return false;
244     }
245 
246     for (ShortcutInfo shortcutInfo : infos) {
247         if (bundleName == shortcutInfo.bundleName) {
248             shortcutInfos.emplace_back(shortcutInfo);
249         }
250     }
251     return true;
252 }
253 
InitWant(Want & want,const std::string & bundleName)254 void LauncherService::InitWant(Want &want, const std::string &bundleName)
255 {
256     want.SetAction(Want::ACTION_HOME);
257     want.AddEntity(Want::ENTITY_HOME);
258     if (!bundleName.empty()) {
259         ElementName elementName;
260         elementName.SetBundleName(bundleName);
261         want.SetElement(elementName);
262     }
263 }
264 
ConvertAbilityToLauncherAbility(const AbilityInfo & ability,LauncherAbilityInfo & launcherAbility,const int32_t userId)265 void LauncherService::ConvertAbilityToLauncherAbility(const AbilityInfo &ability, LauncherAbilityInfo &launcherAbility,
266     const int32_t userId)
267 {
268     launcherAbility.applicationInfo = ability.applicationInfo;
269     launcherAbility.labelId = ability.labelId;
270     launcherAbility.iconId = ability.iconId;
271     ElementName elementName;
272     elementName.SetBundleName(ability.bundleName);
273     elementName.SetModuleName(ability.moduleName);
274     elementName.SetAbilityName(ability.name);
275     elementName.SetDeviceID(ability.deviceId);
276     launcherAbility.elementName = elementName;
277     launcherAbility.userId = userId;
278     launcherAbility.installTime = ability.installTime;
279 }
280 
GetLauncherAbilityByBundleName(const std::string & bundleName,const int32_t userId,std::vector<LauncherAbilityInfo> & launcherAbilityInfos)281 ErrCode LauncherService::GetLauncherAbilityByBundleName(const std::string &bundleName, const int32_t userId,
282     std::vector<LauncherAbilityInfo> &launcherAbilityInfos)
283 {
284     APP_LOGD("GetLauncherAbilityByBundleName called");
285     if (bundleName.empty()) {
286         APP_LOGE("no bundleName %{public}s found", bundleName.c_str());
287         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
288     }
289     auto iBundleMgr = GetBundleMgr();
290     if (iBundleMgr == nullptr) {
291         APP_LOGE("can not get iBundleMgr");
292         return ERR_APPEXECFWK_SERVICE_NOT_READY;
293     }
294 
295     Want want;
296     InitWant(want, bundleName);
297     std::vector<AbilityInfo> abilityInfos;
298     ErrCode err = iBundleMgr->QueryLauncherAbilityInfos(want, userId, abilityInfos);
299     if (err != ERR_OK) {
300         APP_LOGE("QueryLauncherAbilityInfos failed");
301         return err;
302     }
303     for (const auto &ability : abilityInfos) {
304         if (ability.bundleName != bundleName || !ability.enabled) {
305             continue;
306         }
307         LauncherAbilityInfo info;
308         ConvertAbilityToLauncherAbility(ability, info, userId);
309         launcherAbilityInfos.push_back(info);
310     }
311     return ERR_OK;
312 }
313 
GetAllLauncherAbility(const int32_t userId,std::vector<LauncherAbilityInfo> & launcherAbilityInfos)314 ErrCode LauncherService::GetAllLauncherAbility(const int32_t userId,
315     std::vector<LauncherAbilityInfo> &launcherAbilityInfos)
316 {
317     APP_LOGD("GetAllLauncherAbility called");
318     auto iBundleMgr = GetBundleMgr();
319     if (iBundleMgr == nullptr) {
320         APP_LOGE("can not get iBundleMgr");
321         return ERR_APPEXECFWK_SERVICE_NOT_READY;
322     }
323 
324     Want want;
325     InitWant(want, EMPTY_STRING);
326     std::vector<AbilityInfo> abilityInfos;
327     ErrCode err = iBundleMgr->QueryLauncherAbilityInfos(want, userId, abilityInfos);
328     if (err != ERR_OK) {
329         APP_LOGE("QueryLauncherAbilityInfos failed");
330         return err;
331     }
332     for (const auto &ability : abilityInfos) {
333         if (!ability.enabled || ability.applicationInfo.hideDesktopIcon) {
334             continue;
335         }
336         LauncherAbilityInfo info;
337         ConvertAbilityToLauncherAbility(ability, info, userId);
338         launcherAbilityInfos.push_back(info);
339     }
340     return ERR_OK;
341 }
342 
GetShortcutInfoV9(const std::string & bundleName,std::vector<ShortcutInfo> & shortcutInfos)343 ErrCode LauncherService::GetShortcutInfoV9(
344     const std::string &bundleName, std::vector<ShortcutInfo> &shortcutInfos)
345 {
346     auto iBundleMgr = GetBundleMgr();
347     if (iBundleMgr == nullptr) {
348         APP_LOGE("can not get iBundleMgr");
349         return ERR_APPEXECFWK_SERVICE_NOT_READY;
350     }
351     std::vector<ShortcutInfo> infos;
352     ErrCode errCode = iBundleMgr->GetShortcutInfoV9(bundleName, infos);
353     if (errCode != ERR_OK) {
354         return errCode;
355     }
356     if (infos.empty()) {
357         APP_LOGI("ShortcutInfo is not exist for this bundle");
358         return ERR_OK;
359     }
360 
361     for (ShortcutInfo shortcutInfo : infos) {
362         if (bundleName == shortcutInfo.bundleName) {
363             shortcutInfos.emplace_back(shortcutInfo);
364         }
365     }
366     return ERR_OK;
367 }
368 
369 }  // namespace AppExecFwk
370 }  // namespace OHOS