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 #include "bundle_active_event.h"
17 #include "bundle_active_log.h"
18
19 namespace OHOS {
20 namespace DeviceUsageStats {
BundleActiveEvent()21 BundleActiveEvent::BundleActiveEvent()
22 {
23 bundleName_ = "";
24 continuousTaskAbilityName_ = "";
25 abilityName_ = "";
26 abilityId_ = "";
27 moduleName_ = "";
28 formName_ = "";
29 formId_ = 0;
30 formDimension_ = 0;
31 timeStamp_ = 0;
32 eventId_ = DEFAULT_EVENT_ID;
33 }
34
BundleActiveEvent(const BundleActiveEvent & orig)35 BundleActiveEvent::BundleActiveEvent (const BundleActiveEvent& orig)
36 {
37 bundleName_ = orig.bundleName_;
38 continuousTaskAbilityName_ = orig.continuousTaskAbilityName_;
39 abilityName_ = orig.abilityName_;
40 abilityId_ = orig.abilityId_;
41 moduleName_ = orig.moduleName_;
42 formName_ = orig.formName_;
43 formId_ = orig.formId_;
44 formDimension_ = orig.formDimension_;
45 timeStamp_ = orig.timeStamp_;
46 eventId_ = orig.eventId_;
47 }
48
BundleActiveEvent(int32_t eventId,int64_t timeStamp)49 BundleActiveEvent::BundleActiveEvent(int32_t eventId, int64_t timeStamp)
50 {
51 bundleName_.clear();
52 continuousTaskAbilityName_.clear();
53 abilityName_.clear();
54 abilityId_.clear();
55 moduleName_.clear();
56 formName_.clear();
57 formDimension_ = 0;
58 formId_ = 0;
59 timeStamp_ = timeStamp;
60 eventId_ = eventId;
61 }
62
BundleActiveEvent(const int32_t eventId,const std::string bundleName)63 BundleActiveEvent::BundleActiveEvent(const int32_t eventId, const std::string bundleName)
64 {
65 bundleName_ = bundleName;
66 continuousTaskAbilityName_.clear();
67 abilityName_.clear();
68 abilityId_.clear();
69 moduleName_.clear();
70 formName_.clear();
71 formDimension_ = 0;
72 formId_ = 0;
73 timeStamp_ = 0;
74 eventId_ = eventId;
75 }
76
BundleActiveEvent(const std::string bundleName,const std::string continuousTaskAbilityName)77 BundleActiveEvent::BundleActiveEvent(const std::string bundleName, const std::string continuousTaskAbilityName)
78 {
79 bundleName_ = bundleName;
80 continuousTaskAbilityName_ = continuousTaskAbilityName;
81 abilityName_.clear();
82 abilityId_.clear();
83 moduleName_.clear();
84 formName_.clear();
85 formDimension_ = 0;
86 formId_ = 0;
87 timeStamp_ = 0;
88 eventId_ = DEFAULT_EVENT_ID;
89 }
90
BundleActiveEvent(const std::string bundleName,const std::string abilityName,const std::string abilityId,const std::string moduleName)91 BundleActiveEvent::BundleActiveEvent(const std::string bundleName, const std::string abilityName,
92 const std::string abilityId, const std::string moduleName)
93 {
94 bundleName_ = bundleName;
95 continuousTaskAbilityName_.clear();
96 abilityName_ = abilityName;
97 abilityId_ = abilityId;
98 moduleName_ = moduleName;
99 formName_.clear();
100 formDimension_ = 0;
101 formId_ = 0;
102 timeStamp_ = 0;
103 eventId_ = DEFAULT_EVENT_ID;
104 }
105
BundleActiveEvent(const std::string bundleName,const std::string moduleName,const std::string formName,const int32_t formDimension,const int64_t formId,const int32_t eventId)106 BundleActiveEvent::BundleActiveEvent(const std::string bundleName, const std::string moduleName,
107 const std::string formName, const int32_t formDimension,
108 const int64_t formId, const int32_t eventId)
109 {
110 bundleName_ = bundleName;
111 continuousTaskAbilityName_.clear();
112 abilityName_.clear();
113 abilityId_.clear();
114 moduleName_ = moduleName;
115 formName_ = formName;
116 formDimension_ = formDimension;
117 formId_ = formId;
118 timeStamp_ = 0;
119 eventId_ = eventId;
120 }
121
operator =(const BundleActiveEvent & orig)122 BundleActiveEvent& BundleActiveEvent::operator=(const BundleActiveEvent& orig)
123 {
124 bundleName_ = orig.bundleName_;
125 continuousTaskAbilityName_ = orig.continuousTaskAbilityName_;
126 abilityName_ = orig.abilityName_;
127 abilityId_ = orig.abilityId_;
128 moduleName_ = orig.moduleName_;
129 formName_ = orig.formName_;
130 formDimension_ = orig.formDimension_;
131 formId_ = orig.formId_;
132 timeStamp_ = orig.timeStamp_;
133 eventId_ = orig.eventId_;
134 return *this;
135 }
136
PrintEvent(const bool debug) const137 void BundleActiveEvent::PrintEvent(const bool debug) const
138 {
139 if (!debug) {
140 return;
141 }
142 BUNDLE_ACTIVE_LOGI("bundle name is %{public}s, ability name is %{public}s, continue task ability is %{public}s, "
143 "module name is %{public}s, "
144 "form name is %{public}s, form dimension is %{public}d, form id is %{public}lld, event id is %{public}d",
145 bundleName_.c_str(), abilityName_.c_str(), continuousTaskAbilityName_.c_str(), moduleName_.c_str(),
146 formName_.c_str(), formDimension_, (long long)formId_, eventId_);
147 }
148
Marshalling(Parcel & parcel) const149 bool BundleActiveEvent::Marshalling(Parcel &parcel) const
150 {
151 if (parcel.WriteString(bundleName_) &&
152 parcel.WriteString(continuousTaskAbilityName_) &&
153 parcel.WriteString(abilityName_) &&
154 parcel.WriteString(abilityId_) &&
155 parcel.WriteString(moduleName_) &&
156 parcel.WriteString(formName_) &&
157 parcel.WriteInt32(formDimension_) &&
158 parcel.WriteInt64(formId_) &&
159 parcel.WriteInt64(timeStamp_) &&
160 parcel.WriteInt32(eventId_)) {
161 return true;
162 }
163 return false;
164 }
165
UnMarshalling(Parcel & parcel)166 std::shared_ptr<BundleActiveEvent> BundleActiveEvent::UnMarshalling(Parcel &parcel)
167 {
168 std::shared_ptr<BundleActiveEvent> result = std::make_shared<BundleActiveEvent>();
169 result->bundleName_ = parcel.ReadString();
170 result->continuousTaskAbilityName_ = parcel.ReadString();
171 result->abilityName_ = parcel.ReadString();
172 result->abilityId_ = parcel.ReadString();
173 result->moduleName_ = parcel.ReadString();
174 result->formName_ = parcel.ReadString();
175 result->formDimension_ = parcel.ReadInt32();
176 result->formId_ = parcel.ReadInt64();
177 result->timeStamp_ = parcel.ReadInt64();
178 result->eventId_ = parcel.ReadInt32();
179 return result;
180 }
181
ToString()182 std::string BundleActiveEvent::ToString()
183 {
184 return "bundle name is " + this->bundleName_ + ", event is " +
185 std::to_string(this->eventId_) + ", timestamp is " + std::to_string(this->timeStamp_) + "\n";
186 }
187
IsBundleEvent(const int32_t eventId)188 bool BundleActiveEvent::IsBundleEvent(const int32_t eventId)
189 {
190 if (eventId == ABILITY_BACKGROUND ||
191 eventId == ABILITY_FOREGROUND ||
192 eventId == ABILITY_STOP ||
193 eventId == LONG_TIME_TASK_STARTTED ||
194 eventId == LONG_TIME_TASK_ENDED ||
195 eventId == END_OF_THE_DAY) {
196 return true;
197 }
198 return false;
199 }
200 } // namespace DeviceUsageStats
201 } // namespace OHOS
202
203