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 #ifndef BUNDLE_ACTIVE_BUNDLE_MGR_HELPER_H
17 #define BUNDLE_ACTIVE_BUNDLE_MGR_HELPER_H
18 
19 #include "errors.h"
20 #include "bundle_mgr_interface.h"
21 #include "bundle_mgr_proxy.h"
22 #include "ipc_skeleton.h"
23 #include "iremote_object.h"
24 #include "singleton.h"
25 #include <unordered_set>
26 #include "ffrt.h"
27 
28 namespace OHOS {
29 namespace DeviceUsageStats {
30 class BundleActiveBundleMgrHelper : public DelayedSingleton<BundleActiveBundleMgrHelper> {
31 public:
32     /**
33      * @brief GetNameForUid by uid.
34      *
35      * @param uid .
36      * @param bundleName .
37      */
38     void GetNameForUid(int32_t uid, std::string& bundleName);
39 
40     /**
41      * @brief GetBundleInfo by bundleName、flag、userId.
42      *
43      * @param bundleName .
44      * @param flag which type is AppExecFwk::ApplicationFlag.
45      * @param bundleInfo.
46      * @param userId which type is AppExecFwk::BundleInfo.
47      */
48     bool GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag,
49         AppExecFwk::BundleInfo &bundleInfo, int32_t userId);
50 
51     /**
52      * @brief GetApplicationInfo, get bundleName by uid.
53      *
54      * @param appName .
55      * @param flag which type is AppExecFwk::ApplicationFlag.
56      * @param userId.
57      * @param appInfo which type is AppExecFwk::ApplicationInfo.
58      */
59     bool GetApplicationInfo(const std::string &appName, const AppExecFwk::ApplicationFlag flag,
60         const int userId, AppExecFwk::ApplicationInfo &appInfo);
61 
62     bool GetApplicationInfos(const AppExecFwk::ApplicationFlag flag,
63         const int userId, std::vector<AppExecFwk::ApplicationInfo> &appInfos);
64 
65     bool IsLauncherApp(const std::string &bundleName, const int32_t userId);
66 
67     void InsertPackageUid(const std::string &bundleName, const int32_t userId);
68 
69     std::set<int32_t> GetPackageUidSet(const std::string &bundleName);
70 
71     void DeletePackageUid(const std::string &bundleName, const int32_t userId);
72 
73     void DeleteMemPackage(const std::string &bundleName);
74 
75     void InitLauncherAppMap();
76 
77 private:
78     bool Connect();
79     void InitSystemEvent();
80     std::map<std::string, bool> launcherAppMap_;
81     std::map<std::string, std::set<int32_t>> packageContainUidMap_;
82     bool isInitLauncherAppMap_ = false;
83     sptr<AppExecFwk::IBundleMgr> bundleMgr_ = nullptr;
84     ffrt::mutex connectionMutex_;
85     ffrt::mutex packageContainUidMapMutex_;
86     DECLARE_DELAYED_SINGLETON(BundleActiveBundleMgrHelper);
87 };
88 }  // namespace DeviceUsageStats
89 }  // namespace OHOS
90 #endif  // BUNDLE_ACTIVE_BUNDLE_MGR_HELPER_H
91 
92