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_EVENT_H
17 #define BUNDLE_ACTIVE_EVENT_H
18 
19 #include <cstdint>
20 #include <iosfwd>
21 #include <memory>
22 #include "parcel.h"
23 
24 namespace OHOS {
25 namespace DeviceUsageStats {
26 class BundleActiveEvent : public Parcelable {
27 public:
28     // external events
29     static const int32_t ABILITY_FOREGROUND = 2; // onForeground() called, ability is in front.
30     static const int32_t ABILITY_BACKGROUND = 3; // onBackground() called, ability is in background.
31     static const int32_t ABILITY_STOP = 4; // onStop() called, ability is destroyed.
32     static const int32_t LONG_TIME_TASK_STARTTED = 5;
33     static const int32_t LONG_TIME_TASK_ENDED = 6;
34     static const int32_t SYSTEM_INTERACTIVE = 7;
35     static const int32_t USER_INTERACTIVE = 8;
36     // internal events
37     static const int32_t DEFAULT_EVENT_ID = 0;
38     static const int32_t DEFAULT_UID_ID = -1;
39     static const int32_t END_OF_THE_DAY = 9;
40     static const int32_t SHUTDOWN = 10;
41     static const int32_t STARTUP = 11;
42     static const int32_t FLUSH = 12;
43     static const int32_t SCREEN_INTERACTIVE = 13;
44     static const int32_t SCREEN_NON_INTERACTIVE = 14;
45     static const int32_t FORM_IS_CLICKED = 15;
46     static const int32_t FORM_IS_REMOVED = 16;
47     static const int32_t KEYGUARD_SHOWN = 17;
48     static const int32_t KEYGUARD_HIDDEN = 18;
49     static const int32_t NOTIFICATION_SEEN = 19;
50     static const int32_t SYSTEM_LOCK = 20;
51     static const int32_t SYSTEM_UNLOCK = 21;
52     static const int32_t SYSTEM_SLEEP = 22;
53     static const int32_t SYSTEM_WAKEUP = 23;
54     inline static const std::string DEVICE_EVENT_PACKAGE_NAME = "openharmony";
55     std::string bundleName_;
56     std::string continuousTaskAbilityName_;
57     std::string abilityName_;
58     std::string abilityId_;
59     std::string moduleName_;
60     std::string formName_;
61     int32_t formDimension_;
62     int64_t formId_;
63     int64_t timeStamp_;
64     int32_t eventId_;
65     int32_t uid_;
66 
67 public:
68     /*
69     * function: BundleActiveEvent, default constructor.
70     */
71     BundleActiveEvent();
72     /*
73     * function: BundleActiveEvent, copy constructor.
74     * parameters: orig
75     */
76     BundleActiveEvent(const BundleActiveEvent& orig);
77     /*
78     * function: BundleActiveEvent, constructor using event Id, time stamp.
79     * parameters: eventId, timeStamp
80     */
81     BundleActiveEvent(int32_t eventId, int64_t timeStamp);
82     /*
83     * function: BundleActiveEvent, constructor using event Id, time stamp.
84     * parameters: eventId, bundleName.
85     */
86     BundleActiveEvent(const int32_t eventId, const std::string bundleName, const int32_t uid = DEFAULT_UID_ID);
87     /*
88     * function: BundleActiveEvent, constructor of continuous task event.
89     * parameters: bundleName, continuousTaskAbilityName
90     */
91     BundleActiveEvent(const std::string bundleName, const std::string continuousTaskAbilityName,
92         const int32_t uid = DEFAULT_UID_ID);
93     /*
94     * function: BundleActiveEvent, constructor of app ability event.
95     * parameters: bundleName, abilityName, abilityId
96     */
97     BundleActiveEvent(const std::string bundleName, const std::string abilityName, const std::string abilityId,
98         const std::string moduleName, const int32_t uid = DEFAULT_UID_ID);
99     /*
100     * function: BundleActiveEvent, constructor of form event.
101     * parameters: bundleName, moduleName, formName, formDimension, formId, eventId
102     */
103     BundleActiveEvent(const std::string bundleName, const std::string moduleName,
104         const std::string formName, const int32_t formDimension, const int64_t formId, const int32_t eventId,
105         const int32_t uid = DEFAULT_UID_ID);
106     void PrintEvent(const bool debug) const;
107     /*
108     * function: operator=, override operator =.
109     * parameters: orig
110     * return: a BundleActiveEvent object same as orig.
111     */
112     BundleActiveEvent& operator=(const BundleActiveEvent& orig);
113     /*
114     * function: Marshalling, mashalling event object to parcel.
115     * parameters: parcel
116     * return: result of mashalling, true means successful, flase means failed.
117     */
118     virtual bool Marshalling(Parcel &parcel) const override;
119     /*
120     * function: UnMarshalling, Unmashalling event object from parcel.
121     * parameters: parcel
122     * return: point to a BundleActiveEvent.
123     */
124     static std::shared_ptr<BundleActiveEvent> UnMarshalling(Parcel &parcel);
125     /*
126     * function: ToString, change event object to string.
127     * return: string of bundlename, timestamp, eventid.
128     */
129     std::string ToString();
130     /**
131     * @brief get if the event is reported by bundle usage.
132     *
133     * @return return true if event reported by bundle usage.
134     */
135     static bool IsBundleEvent(const int32_t eventId);
136 };
137 }  // namespace DeviceUsageStats
138 }  // namespace OHOS
139 #endif  // BUNDLE_ACTIVE_EVENT_H
140 
141