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