1 /*
2  * Copyright (c) 2021 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 #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_LAUNCHER_SERVICE_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_LAUNCHER_SERVICE_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "appexecfwk_errors.h"
23 #include "bundle_mgr_interface.h"
24 #include "securec.h"
25 #include "system_ability_definition.h"
26 #include "if_system_ability_manager.h"
27 #include "iservice_registry.h"
28 #include "bundle_monitor.h"
29 #include "bundle_status_callback_interface.h"
30 #include "launcher_ability_info.h"
31 #include "launcher_shortcut_info.h"
32 #include "ability_info.h"
33 #include "application_info.h"
34 #include "want.h"
35 #include "app_log_wrapper.h"
36 
37 namespace OHOS {
38 namespace AppExecFwk {
39 class LauncherService : public virtual RefBase {
40 public:
41     using Want = OHOS::AAFwk::Want;
42 
43     LauncherService();
44     virtual ~LauncherService();
45 
46     /**
47      * @brief Registers a callback method for monitoring bundle installation, uninstallation, and update events.
48      * @param callback Indicates the callback method to be called.
49      * @return Returns true if this function is successfully called; returns false otherwise.
50      */
51     virtual bool RegisterCallback(const sptr<IBundleStatusCallback> &callback);
52     /**
53      * @brief Unregisters the callback method for monitoring bundle installation, uninstallation, and update events.
54      * @return Returns true if this function is successfully called; returns false otherwise.
55      */
56     virtual bool UnRegisterCallback();
57     /**
58      * @brief Obtains launcher abilities based on the bundle name and user id.
59      * @param bundleName Indicates the bundle name.
60      * @param userId Indicates the user ID.
61      * @param launcherAbilityInfos List of LauncherAbilityInfo objects if obtained;
62      * @return Returns true if this function is successfully called; returns false otherwise.
63      */
64     virtual bool GetAbilityList(
65         const std::string &bundleName, const int userId, std::vector<LauncherAbilityInfo> &launcherAbilityInfos);
66     /**
67      * @brief Obtains information about the shortcuts of the application.
68      * @param bundleName Indicates the bundle name of the application.
69      * @param launcherShortcutInfo List of LauncherShortcutInfo objects if obtained.
70      * @return Returns true if the function is successfully called; returns false otherwise.
71      */
72     virtual ErrCode GetShortcutInfos(const std::string &bundleName, std::vector<ShortcutInfo> &shortcutInfo);
73     /**
74      * @brief Obtains information about the launcher ability of all application that appears on launcher.
75      * @param userId Indicates the id for the user.
76      * @param launcherAbilityInfo Indicates the obtained LauncherAbilityInfo object.
77      * @return Returns true if the function is successfully called; returns false otherwise.
78      */
79     virtual bool GetAllLauncherAbilityInfos(int32_t userId, std::vector<LauncherAbilityInfo> &launcherAbilityInfos);
80     /**
81      * @brief Obtains information about the launcher ability of input bundleName and userId.
82      * @param bundleName Indicates the bundleName of the application.
83      * @param userId Indicates the id for the user.
84      * @param launcherAbilityInfos Indicates the obtained vector of the input bundle.
85      * @return Returns ERR code of the result.
86      */
87     ErrCode GetLauncherAbilityByBundleName(const std::string &bundleName, const int32_t userId,
88         std::vector<LauncherAbilityInfo> &launcherAbilityInfos);
89     /**
90      * @brief Obtains information about the launcher ability of input bundleName and userId.
91      * @param userId Indicates the id for the user.
92      * @param launcherAbilityInfos Indicates the obtained vector of all launcher bundle.
93      * @return Returns ERR code of the result.
94      */
95     ErrCode GetAllLauncherAbility(const int32_t userId, std::vector<LauncherAbilityInfo> &launcherAbilityInfos);
96     /**
97      * @brief Obtains information about the shortcuts of the application.
98      * @param bundleName Indicates the bundle name of the application.
99      * @param launcherShortcutInfo List of LauncherShortcutInfo objects if obtained.
100      * @return Returns err code of result.
101      */
102     ErrCode GetShortcutInfoV9(const std::string &bundleName,
103         std::vector<ShortcutInfo> &shortcutInfos, int32_t userId = Constants::UNSPECIFIED_USERID);
104 
105 private:
106     void InitWant(Want &want, const std::string &bundleName);
107     void ConvertAbilityToLauncherAbility(const AbilityInfo &ability, LauncherAbilityInfo &launcherAbility,
108         const int32_t userId);
109 
110 private:
111     void init();
112     void OnDeath();
113     static OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgr();
114 
115     std::shared_ptr<BundleMonitor> bundleMonitor_ = nullptr;
116     static OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> bundleMgr_;
117     static OHOS::sptr<IRemoteObject::DeathRecipient> deathRecipient_;
118 
119     static std::mutex bundleMgrMutex_;
120     DISALLOW_COPY_AND_MOVE(LauncherService);
121 
122     class LauncherServiceDeathRecipient : public IRemoteObject::DeathRecipient {
123         virtual void OnRemoteDied([[maybe_unused]] const wptr<IRemoteObject>& remote) override;
124     };
125 };
126 }  // namespace AppExecFwk
127 }  // namespace OHOS
128 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_LAUNCHER_SERVICE_H