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 std::shared_ptr<BundleActiveModuleRecord> BundleActiveModuleRecord::UnMarshalling(Parcel &parcel)
101 {
102     std::shared_ptr<BundleActiveModuleRecord> result = std::make_shared<BundleActiveModuleRecord>();
103     result->deviceId_ = parcel.ReadString();
104     result->bundleName_ = parcel.ReadString();
105     result->moduleName_ = parcel.ReadString();
106     result->abilityName_ = parcel.ReadString();
107     result->appLabelId_ = parcel.ReadUint32();
108     result->descriptionId_ = parcel.ReadUint32();
109     result->abilityLableId_ = parcel.ReadUint32();
110     result->abilityDescriptionId_ = parcel.ReadUint32();
111     result->abilityIconId_ = parcel.ReadUint32();
112     result->launchedCount_ = parcel.ReadInt32();
113     result->lastModuleUsedTime_ = parcel.ReadInt64();
114     uint32_t size = parcel.ReadUint32();
115     result->uid_ = parcel.ReadInt32();
116     if (size > MAX_FORM_NUM) {
117         return nullptr;
118     }
119     std::shared_ptr<BundleActiveFormRecord> tmp = std::make_shared<BundleActiveFormRecord>();
120     for (uint32_t i = 0; i < size; i++) {
121         tmp = tmp->UnMarshalling(parcel);
122         if (!tmp) {
123             continue;
124         }
125         result->formRecords_.emplace_back(*tmp);
126     }
127     return result;
128 }
129 
cmp(const BundleActiveModuleRecord & moduleRecordA,const BundleActiveModuleRecord & moduleRecordB)130 bool BundleActiveModuleRecord::cmp(const BundleActiveModuleRecord& moduleRecordA,
131     const BundleActiveModuleRecord& moduleRecordB)
132 {
133     return moduleRecordA.lastModuleUsedTime_ > moduleRecordB.lastModuleUsedTime_;
134 }
135 
ToString()136 std::string BundleActiveModuleRecord::ToString()
137 {
138     return "bundle name is " + this->bundleName_ +
139         ", module name is " + this->moduleName_ +
140         ", uid is " + std::to_string(this->uid_) +
141         ", last used time stamp is " + std::to_string(this->lastModuleUsedTime_) +
142         ", module is used for " + std::to_string(this->launchedCount_) + " times\n";
143 }
144 }  // namespace DeviceUsageStats
145 }  // namespace OHOS
146 
147