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)23 void BundleActiveModuleRecord::AddOrUpdateOneFormRecord(const std::string formName, const int32_t formDimension,
24 const int64_t formId, const int64_t timeStamp)
25 {
26 BundleActiveFormRecord newFormRecord(formName, formDimension, formId, timeStamp, userId_);
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 }
74
Marshalling(Parcel & parcel) const75 bool BundleActiveModuleRecord::Marshalling(Parcel &parcel) const
76 {
77 if (parcel.WriteString(deviceId_) &&
78 parcel.WriteString(bundleName_) &&
79 parcel.WriteString(moduleName_) &&
80 parcel.WriteString(abilityName_) &&
81 parcel.WriteUint32(appLabelId_) &&
82 parcel.WriteUint32(descriptionId_) &&
83 parcel.WriteUint32(abilityLableId_) &&
84 parcel.WriteUint32(abilityDescriptionId_) &&
85 parcel.WriteUint32(abilityIconId_) &&
86 parcel.WriteInt32(launchedCount_) &&
87 parcel.WriteInt64(lastModuleUsedTime_) &&
88 parcel.WriteUint32(formRecords_.size())
89 ) {
90 for (auto formRecord : formRecords_) {
91 formRecord.Marshalling(parcel);
92 }
93 return true;
94 }
95 return false;
96 }
97
UnMarshalling(Parcel & parcel)98 std::shared_ptr<BundleActiveModuleRecord> BundleActiveModuleRecord::UnMarshalling(Parcel &parcel)
99 {
100 std::shared_ptr<BundleActiveModuleRecord> result = std::make_shared<BundleActiveModuleRecord>();
101 result->deviceId_ = parcel.ReadString();
102 result->bundleName_ = parcel.ReadString();
103 result->moduleName_ = parcel.ReadString();
104 result->abilityName_ = parcel.ReadString();
105 result->appLabelId_ = parcel.ReadUint32();
106 result->descriptionId_ = parcel.ReadUint32();
107 result->abilityLableId_ = parcel.ReadUint32();
108 result->abilityDescriptionId_ = parcel.ReadUint32();
109 result->abilityIconId_ = parcel.ReadUint32();
110 result->launchedCount_ = parcel.ReadInt32();
111 result->lastModuleUsedTime_ = parcel.ReadInt64();
112 uint32_t size = parcel.ReadUint32();
113 if (size > MAX_FORM_NUM) {
114 return nullptr;
115 }
116 std::shared_ptr<BundleActiveFormRecord> tmp = std::make_shared<BundleActiveFormRecord>();
117 for (uint32_t i = 0; i < size; i++) {
118 tmp = tmp->UnMarshalling(parcel);
119 if (!tmp) {
120 continue;
121 }
122 result->formRecords_.emplace_back(*tmp);
123 }
124 return result;
125 }
126
cmp(const BundleActiveModuleRecord & moduleRecordA,const BundleActiveModuleRecord & moduleRecordB)127 bool BundleActiveModuleRecord::cmp(const BundleActiveModuleRecord& moduleRecordA,
128 const BundleActiveModuleRecord& moduleRecordB)
129 {
130 return moduleRecordA.lastModuleUsedTime_ > moduleRecordB.lastModuleUsedTime_;
131 }
132
ToString()133 std::string BundleActiveModuleRecord::ToString()
134 {
135 return "bundle name is " + this->bundleName_ +
136 ", module name is " + this->moduleName_ +
137 ", last used time stamp is " + std::to_string(this->lastModuleUsedTime_) +
138 ", module is used for " + std::to_string(this->launchedCount_) + " times\n";
139 }
140 } // namespace DeviceUsageStats
141 } // namespace OHOS
142
143