• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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_SERVICE_H
17 #define BUNDLE_ACTIVE_SERVICE_H
18 
19 #include "singleton.h"
20 
21 #include "app_mgr_interface.h"
22 #include "ibundle_active_service.h"
23 #include "bundle_active_service_stub.h"
24 #include "bundle_active_core.h"
25 #include "bundle_active_report_handler.h"
26 #ifdef DEVICE_USAGES_STATISTICS_POWERMANGER_ENABLE
27 #include "shutdown/async_shutdown_callback_stub.h"
28 #include "bundle_active_power_state_callback_service.h"
29 #endif
30 #include "bundle_active_app_state_observer.h"
31 #include "bundle_active_continuous_task_observer.h"
32 #include "bundle_active_account_helper.h"
33 #include "bundle_active_high_frequency_period.h"
34 #include "file_ex.h"
35 #include "string_ex.h"
36 #include "system_ability.h"
37 #include "ffrt.h"
38 
39 namespace OHOS {
40 namespace DeviceUsageStats {
41 class BundleActiveService : public SystemAbility, public BundleActiveServiceStub,
42     public std::enable_shared_from_this<BundleActiveService> {
43     DISALLOW_COPY_AND_MOVE(BundleActiveService);
44     DECLARE_SYSTEM_ABILITY(BundleActiveService);
45     DECLARE_DELAYED_SINGLETON(BundleActiveService);
46 public:
47     using IBundleMgr = OHOS::AppExecFwk::IBundleMgr;
48     using BundleInfo = OHOS::AppExecFwk::BundleInfo;
49     using BundleFlag = OHOS::AppExecFwk::BundleFlag;
50     using HapModuleInfo = OHOS::AppExecFwk::HapModuleInfo;
51     using AbilityInfo = OHOS::AppExecFwk::AbilityInfo;
52     using ApplicationInfo = OHOS::AppExecFwk::ApplicationInfo;
53     using ApplicationFlag = OHOS::AppExecFwk::ApplicationFlag;
54     using AbilityType = OHOS::AppExecFwk::AbilityType;
55 
56     /**
57      * @brief ReportEvent, used to report event.
58      *
59      * @param event .
60      * @param userId .
61      * @return errCode.
62      */
63     ErrCode ReportEvent(const BundleActiveEvent& event, int32_t userId) override;
64 
65     /**
66      * @brief IsBundleIdle, used to check whether specific bundle is idle.
67      *
68      * @param isBundleIdle The result of IsBundleIdle.
69      * @param bundleName .
70      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
71      * @return errCode.
72      */
73     ErrCode IsBundleIdle(bool& isBundleIdle, const std::string& bundleName, int32_t userId) override;
74 
75         /**
76      * @brief IsBundleUsePeriod, used to check whether specific bundle is use period.
77      *
78      * @param IsUsePeriod The result of IsBundleIdle.
79 
80      * @param bundleName the target of application's bundle name.
81      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
82      * @return errCode.
83      */
84     ErrCode IsBundleUsePeriod(bool& IsUsePeriod, const std::string& bundleName, int32_t userId) override;
85 
86     /**
87      * @brief QueryBundleStatsInfoByInterval, query all bundle usage statistics in specific time span for calling user.
88      *
89      * @param packageStats .
90      * @param intervalType .
91      * @param beginTime .
92      * @param endTime .
93      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
94      * @return errCode.
95      */
96     ErrCode QueryBundleStatsInfoByInterval(std::vector<BundleActivePackageStats>& packageStats,
97         const int32_t intervalType, const int64_t beginTime, const int64_t endTime, int32_t userId) override;
98 
99     /**
100      * @brief QueryBundleEvents, query all events in specific time span for calling user.
101      *
102      * @param bundleActiveEvents .
103      * @param beginTime .
104      * @param endTime .
105      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
106      * @return errCode.
107      */
108     ErrCode QueryBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvents, const int64_t beginTime,
109         const int64_t endTime, int32_t userId) override;
110 
111     /**
112      * @brief SetAppGroup, set specific bundle of specific user to a priority group.
113      *
114      * @param bundleName .
115      * @param newGroup .
116      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
117      * @return errCode.
118      */
119     ErrCode SetAppGroup(const std::string& bundleName, int32_t newGroup, int32_t userId) override;
120 
121     /**
122      * @brief QueryBundleStatsInfos, query bundle usage statistics in specific time span for calling bundle.
123      *
124      * @param bundleActivePackageStats, The result of QueryBundleStatsInfos.
125      * @param intervalType .
126      * @param beginTime .
127      * @param endTime .
128      * @return errCode.
129      */
130     ErrCode QueryBundleStatsInfos(std::vector<BundleActivePackageStats>& bundleActivePackageStats,
131         const int32_t intervalType, const int64_t beginTime, const int64_t endTime) override;
132 
133     /**
134      * @brief QueryCurrentBundleEvents, query bundle usage statistics in specific time span for calling bundle.
135      *
136      * @param bundleActiveEvents the std::vector<BundleActiveEvent>, as the result of QueryCurrentBundleEvents.
137      * @param beginTime .
138      * @param endTime .
139      * @return errCode.
140      */
141     ErrCode QueryCurrentBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvents,
142         const int64_t beginTime, const int64_t endTime) override;
143 
144     /**
145      * @brief QueryAppGroup, query appGroup by bundleName and userId.
146      *
147      * @param appGroup as the result of QueryAppGroup.
148      * @param bundleName .
149      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
150      * @return errCode.
151      */
152     ErrCode QueryAppGroup(int32_t& appGroup, const std::string& bundleName, int32_t userId) override;
153 
154     /**
155      * @brief QueryModuleUsageRecords, query all from usage statistics in specific time span for calling user.
156      *
157      * @param maxNum .
158      * @param results .
159      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
160      * @return errCode.
161      */
162     ErrCode QueryModuleUsageRecords(int32_t maxNum, std::vector<BundleActiveModuleRecord>& results,
163         int32_t userId = -1) override;
164 
165     /**
166      * @brief QueryDeviceEventStats, query all from event stats in specific time span for calling user.
167      *
168      * @param beginTime .
169      * @param endTime .
170      * @param eventStats .
171      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
172      * @return errCode.
173      */
174     ErrCode QueryDeviceEventStats(int64_t beginTime, int64_t endTime,
175         std::vector<BundleActiveEventStats>& eventStats, int32_t userId) override;
176 
177     /**
178      * @brief QueryNotificationEventStats, query all app notification number in specific time span for calling user.
179      *
180      * @param beginTime .
181      * @param endTime .
182      * @param eventStats .
183      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
184      * @return errCode.
185      */
186     ErrCode QueryNotificationEventStats(int64_t beginTime, int64_t endTime,
187         std::vector<BundleActiveEventStats>& eventStats, int32_t userId) override;
188 
189     /**
190      * @brief QueryHighFrequencyPeriodBundle, query the high-frequency usage period of the app in the past week.
191      *
192      * @param appFreqHours .
193      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
194      * @return errCode.
195      */
196     ErrCode QueryHighFrequencyPeriodBundle(
197         std::vector<BundleActiveHighFrequencyPeriod>& appFreqHours, int32_t userId) override;
198 
199     /**
200      * @brief QueryBundleTodayLatestUsedTime, query the latest use time of the application on the current day.
201      *
202      * @param latestUsedTime .
203      * @param bundleName .
204      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
205      * @return errCode.
206      */
207     ErrCode QueryBundleTodayLatestUsedTime(
208         int64_t& latestUsedTime, const std::string& bundleName, int32_t userId) override;
209 
210     /**
211      * @brief BundleActiveService, default constructor.
212      *
213      * @param systemAbilityId .
214      * @param runOnCreate .
215      */
216     BundleActiveService(const int32_t systemAbilityId, bool runOnCreate);
217 
218     /**
219      * @brief RegisterAppGroupCallBack, observe bundle group change event.
220      *
221      * @param observer .
222      * @return errCode.
223      */
224     int32_t RegisterAppGroupCallBack(const sptr<IAppGroupCallback> &observer) override;
225 
226     /**
227      * @brief UnRegisterAppGroupCallBack, unobserve bundle group change event.
228      *
229      * @param observer .
230      * @return errCode.
231      */
232     int32_t UnRegisterAppGroupCallBack(const sptr<IAppGroupCallback> &observer) override;
233 
234     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
235 
236 protected:
237     /**
238      * @brief The OnStart callback.
239      */
240     void OnStart() override;
241     /**
242      * @brief The OnStop callback.
243      */
244     void OnStop() override;
245 
246 private:
247     std::shared_ptr<BundleActiveCore> bundleActiveCore_;
248 
249     sptr<BundleActiveAppStateObserver> appStateObserver_;
250 #ifdef BGTASKMGR_ENABLE
251     std::shared_ptr<BundleActiveContinuousTaskObserver> continuousTaskObserver_;
252 #endif
253 #ifdef DEVICE_USAGES_STATISTICS_POWERMANGER_ENABLE
254     sptr<PowerMgr::IAsyncShutdownCallback> shutdownCallback_;
255     sptr<PowerMgr::IPowerStateCallback> powerStateCallback_;
256 #endif
257     bool ready_ {false};
258     int32_t ConvertIntervalType(const int32_t intervalType);
259     void InitNecessaryState();
260     void InitService();
261     ErrCode CheckBundleIsSystemAppAndHasPermission(const int32_t uid,
262         OHOS::Security::AccessToken::AccessTokenID tokenId);
263     ErrCode CheckSystemAppOrNativePermission(const int32_t uid, OHOS::Security::AccessToken::AccessTokenID tokenId);
264     ErrCode CheckNativePermission(OHOS::Security::AccessToken::AccessTokenID tokenId);
265     void InitAppStateSubscriber();
266     void InitContinuousSubscriber();
267     bool SubscribeAppState();
268     bool SubscribeContinuousTask();
269     OHOS::sptr<OHOS::AppExecFwk::IAppMgr> GetAppManagerInstance();
270     void QueryModuleRecordInfos(BundleActiveModuleRecord& moduleRecord);
271     void SerModuleProperties(const HapModuleInfo& hapModuleInfo,
272     const ApplicationInfo& appInfo, const AbilityInfo& abilityInfo, BundleActiveModuleRecord& moduleRecord);
273     void DumpUsage(std::string &result);
274     bool AllowDump();
275     int32_t ShellDump(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
276     int32_t DumpEvents(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
277     int32_t DumpPackageUsage(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
278     int32_t DumpModuleUsage(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
279     int32_t DumpHighFreqHourUsage(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
280     int32_t GetNameAndIndexForUid(int32_t uid);
281     std::vector<BundleActivePackageStats> MergePackageStats(
282         const std::vector<BundleActivePackageStats>& packageStats);
283     void MergeSamePackageStats(BundleActivePackageStats &left, const BundleActivePackageStats &right);
284 };
285 }  // namespace DeviceUsageStats
286 }  // namespace OHOS
287 #endif  // BUNDLE_ACTIVE_SERVICE_H
288 
289