1 /*
2  * Copyright (c) 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 #ifndef BUNDLE_ACTIVE_PACKAGE_STATS_H
17 #define BUNDLE_ACTIVE_PACKAGE_STATS_H
18 
19 #include <cstdint>
20 #include <iosfwd>
21 #include <map>
22 #include <memory>
23 #include "parcel.h"
24 
25 namespace OHOS {
26 namespace DeviceUsageStats {
27 class BundleActivePackageStats : public Parcelable {
28 public:
29     std::string bundleName_;
30     int64_t beginTimeStamp_; // start time of counting
31     int64_t endTimeStamp_; // stop time of counting
32     int64_t lastTimeUsed_; // the timestamp of last launch
33     int64_t totalInFrontTime_; // the total time of using the bundle
34     int64_t lastContiniousTaskUsed_;
35     int64_t totalContiniousTaskUsedTime_;
36     int32_t startCount_;
37     int32_t bundleStartedCount_;
38     int32_t lastEvent_;
39     int32_t userId_;
40     int32_t uid_;
41     // key is abilityId, value is the last event of this ability. Restore all abilities' last event of bundle.
42     std::map<std::string, int32_t> abilities_;
43     // key is name of continuous task, value is last event of this last continuous task.
44     std::map<std::string, int32_t> longTimeTasks_;
45     /*
46     * function: BundleActivePackageStats, default constructor.
47     */
48     BundleActivePackageStats();
49     /*
50     * function: ~BundleActivePackageStats, default destructor.
51     */
~BundleActivePackageStats()52     ~BundleActivePackageStats() {}
53     /*
54     * function: BundleActivePackageStats, copy constructor.
55     * parameters: orig
56     */
57     BundleActivePackageStats(const BundleActivePackageStats& orig);
58     /*
59     * function: Update, update one bundle statistics.
60     * parameters: longTimeTaskName timeStamp eventId abilityId
61     */
62     void Update(const std::string& longTimeTaskName, const int64_t timeStamp, const int32_t eventId,
63         const std::string& abilityId, const int32_t uid);
64     /*
65     * function: IncrementTimeUsed, increase bundle's use time.
66     * parameters: timeStamp
67     */
68     void IncrementTimeUsed(const int64_t timeStamp);
69     /*
70     * function: IncrementServiceTimeUsed, increase bundle's continuous task use time.
71     * parameters: timeStamp
72     */
73     void IncrementServiceTimeUsed(const int64_t timeStamp);
74     /*
75     * function: IncrementBundleLaunchedCount, increase bundle's launched times by 1.
76     */
77     void IncrementBundleLaunchedCount();
78     /*
79     * function: Marshalling, mashalling BundleActivePackageStats object to parcel.
80     * parameters: parcel
81     * return: result of mashalling, true means successful, flase means failed.
82     */
83     virtual bool Marshalling(Parcel &parcel) const override;
84     /*
85     * function: UnMarshalling, Unmashalling BundleActivePackageStats object from parcel.
86     * parameters: parcel
87     * return: point to a BundleActivePackageStats.
88     */
89     static std::shared_ptr<BundleActivePackageStats> UnMarshalling(Parcel &parcel);
90     /*
91     * function: ToString, change module record object to string.
92     * return: string of bundle name, last used time, total front time, last continuous task used time,
93     * total continuous task time.
94     */
95     std::string ToString();
96 
97 private:
98     bool HasFrontAbility();
99     bool AnyLongTimeTaskStarted();
100     void UpdateAbility(const int64_t timeStamp, const int32_t eventId, const std::string& abilityId);
101     void UpdateLongTimeTask(const std::string& longTimeTaskName, const int64_t timeStamp, const int32_t eventId);
102 };
103 }  // namespace DeviceUsageStats
104 }  // namespace OHOS
105 #endif  // BUNDLE_ACTIVE_PACKAGE_STATS_H
106 
107