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_module_record.h"
17
18 namespace OHOS {
19 namespace DeviceUsageStats {
20 namespace {
21 static const int32_t MAX_FORM_NUM = 1000;
22 }
AddOrUpdateOneFormRecord(const std::string formName,const int32_t formDimension,const int64_t formId,const int64_t timeStamp,const int32_t uid)23 void BundleActiveModuleRecord::AddOrUpdateOneFormRecord(const std::string formName, const int32_t formDimension,
24 const int64_t formId, const int64_t timeStamp, const int32_t uid)
25 {
26 BundleActiveFormRecord newFormRecord(formName, formDimension, formId, timeStamp, userId_, uid);
27 for (auto& formRecord : formRecords_) {
28 if (formRecord == newFormRecord) {
29 formRecord.UpdateFormRecord(timeStamp);
30 return;
31 }
32 }
33 formRecords_.emplace_back(newFormRecord);
34 }
35
RemoveOneFormRecord(const std::string formName,const int32_t formDimension,const int64_t formId)36 void BundleActiveModuleRecord::RemoveOneFormRecord(const std::string formName, const int32_t formDimension,
37 const int64_t formId)
38 {
39 for (auto it = formRecords_.begin(); it != formRecords_.end(); it++) {
40 if (*it == formId) {
41 formRecords_.erase(it);
42 return;
43 }
44 }
45 }
46
UpdateModuleRecord(int64_t timeStamp)47 void BundleActiveModuleRecord::UpdateModuleRecord(int64_t timeStamp)
48 {
49 if (lastModuleUsedTime_ < timeStamp) {
50 lastModuleUsedTime_ = timeStamp;
51 launchedCount_++;
52 }
53 }
54
BundleActiveModuleRecord()55 BundleActiveModuleRecord::BundleActiveModuleRecord()
56 {
57 deviceId_ = "";
58 bundleName_ = ""; // in database
59 moduleName_ = ""; // in database
60 abilityName_ = "";
61 appLabelId_ = 0;
62 labelId_ = 0;
63 descriptionId_ = 0;
64 abilityLableId_ = 0;
65 abilityDescriptionId_ = 0;
66 abilityIconId_ = 0;
67 launchedCount_ = 0;
68 lastModuleUsedTime_ = -1;
69 removed_ = false;
70 installFreeSupported_ = false;
71 isNewAdded_ = false;
72 userId_ = -1;
73 uid_ = 0;
74 }
75
Marshalling(Parcel & parcel) const76 bool BundleActiveModuleRecord::Marshalling(Parcel &parcel) const
77 {
78 if (parcel.WriteString(deviceId_) &&
79 parcel.WriteString(bundleName_) &&
80 parcel.WriteString(moduleName_) &&
81 parcel.WriteString(abilityName_) &&
82 parcel.WriteUint32(appLabelId_) &&
83 parcel.WriteUint32(descriptionId_) &&
84 parcel.WriteUint32(abilityLableId_) &&
85 parcel.WriteUint32(abilityDescriptionId_) &&
86 parcel.WriteUint32(abilityIconId_) &&
87 parcel.WriteInt32(launchedCount_) &&
88 parcel.WriteInt64(lastModuleUsedTime_) &&
89 parcel.WriteUint32(formRecords_.size()) &&
90 parcel.WriteInt32(uid_)
91 ) {
92 for (auto formRecord : formRecords_) {
93 formRecord.Marshalling(parcel);
94 }
95 return true;
96 }
97 return false;
98 }
99
Unmarshalling(Parcel & parcel)100 BundleActiveModuleRecord *BundleActiveModuleRecord::Unmarshalling(Parcel &parcel)
101 {
102 BundleActiveModuleRecord *result = new (std::nothrow) BundleActiveModuleRecord();
103 if (result == nullptr) {
104 return nullptr;
105 }
106 result->deviceId_ = parcel.ReadString();
107 result->bundleName_ = parcel.ReadString();
108 result->moduleName_ = parcel.ReadString();
109 result->abilityName_ = parcel.ReadString();
110 result->appLabelId_ = parcel.ReadUint32();
111 result->descriptionId_ = parcel.ReadUint32();
112 result->abilityLableId_ = parcel.ReadUint32();
113 result->abilityDescriptionId_ = parcel.ReadUint32();
114 result->abilityIconId_ = parcel.ReadUint32();
115 result->launchedCount_ = parcel.ReadInt32();
116 result->lastModuleUsedTime_ = parcel.ReadInt64();
117 uint32_t size = parcel.ReadUint32();
118 result->uid_ = parcel.ReadInt32();
119 if (size > MAX_FORM_NUM) {
120 delete result;
121 return nullptr;
122 }
123
124 for (uint32_t i = 0; i < size; i++) {
125 std::unique_ptr<BundleActiveFormRecord> tmp(BundleActiveFormRecord::Unmarshalling(parcel));
126 if (!tmp) {
127 continue;
128 }
129 result->formRecords_.emplace_back(*tmp);
130 }
131 return result;
132 }
133
cmp(const BundleActiveModuleRecord & moduleRecordA,const BundleActiveModuleRecord & moduleRecordB)134 bool BundleActiveModuleRecord::cmp(const BundleActiveModuleRecord& moduleRecordA,
135 const BundleActiveModuleRecord& moduleRecordB)
136 {
137 return moduleRecordA.lastModuleUsedTime_ > moduleRecordB.lastModuleUsedTime_;
138 }
139
ToString()140 std::string BundleActiveModuleRecord::ToString()
141 {
142 return "bundle name is " + this->bundleName_ +
143 ", module name is " + this->moduleName_ +
144 ", uid is " + std::to_string(this->uid_) +
145 ", last used time stamp is " + std::to_string(this->lastModuleUsedTime_) +
146 ", module is used for " + std::to_string(this->launchedCount_) + " times\n";
147 }
148 } // namespace DeviceUsageStats
149 } // namespace OHOS
150
151