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_form_record.h"
17 
18 namespace OHOS {
19 namespace DeviceUsageStats {
BundleActiveFormRecord()20 BundleActiveFormRecord::BundleActiveFormRecord()
21 {
22     formName_ = "";
23     formDimension_ = 0;
24     formId_ = 0;
25     formLastUsedTime_ = -1;
26     count_ = 0;
27     userId_ = -1;
28     uid_ = -1;
29 }
30 
BundleActiveFormRecord(const std::string formName,const int32_t formDimension,const int64_t formId,const int64_t timeStamp,const int32_t userId,const int32_t uid)31 BundleActiveFormRecord::BundleActiveFormRecord(const std::string formName, const int32_t formDimension,
32     const int64_t formId, const int64_t timeStamp, const int32_t userId, const int32_t uid)
33 {
34     formName_ = formName;
35     formDimension_ = formDimension;
36     formId_ = formId;
37     formLastUsedTime_ = timeStamp;
38     count_ = 1;
39     userId_ = userId;
40     uid_ = uid;
41 }
42 
BundleActiveFormRecord(const BundleActiveFormRecord & orig)43 BundleActiveFormRecord::BundleActiveFormRecord(const BundleActiveFormRecord& orig)
44 {
45     formName_ = orig.formName_;
46     formDimension_ = orig.formDimension_;
47     formId_ = orig.formId_;
48     formLastUsedTime_ = orig.formLastUsedTime_;
49     count_ = orig.count_;
50     userId_ = orig.userId_;
51     uid_ = orig.uid_;
52 }
53 
UpdateFormRecord(const int64_t timeStamp)54 void BundleActiveFormRecord::UpdateFormRecord(const int64_t timeStamp)
55 {
56     if (formLastUsedTime_ < timeStamp) {
57         formLastUsedTime_ = timeStamp;
58         count_++;
59     }
60 }
61 
Marshalling(Parcel & parcel) const62 bool BundleActiveFormRecord::Marshalling(Parcel &parcel) const
63 {
64     if (parcel.WriteString(formName_) &&
65         parcel.WriteInt32(formDimension_) &&
66         parcel.WriteInt64(formId_) &&
67         parcel.WriteInt64(formLastUsedTime_) &&
68         parcel.WriteInt32(count_)) {
69         return true;
70     }
71     return false;
72 }
73 
UnMarshalling(Parcel & parcel)74 std::shared_ptr<BundleActiveFormRecord> BundleActiveFormRecord::UnMarshalling(Parcel &parcel)
75 {
76     std::shared_ptr<BundleActiveFormRecord> result = std::make_shared<BundleActiveFormRecord>();
77     result->formName_ = parcel.ReadString();
78     result->formDimension_ = parcel.ReadInt32();
79     result->formId_ = parcel.ReadInt64();
80     result->formLastUsedTime_ = parcel.ReadInt64();
81     result->count_ = parcel.ReadInt32();
82     return result;
83 }
84 
cmp(const BundleActiveFormRecord & formRecordA,const BundleActiveFormRecord & formRecordB)85 bool BundleActiveFormRecord::cmp(const BundleActiveFormRecord& formRecordA,
86     const BundleActiveFormRecord& formRecordB)
87 {
88     return formRecordA.count_ > formRecordB.count_;
89 }
90 
ToString()91 std::string BundleActiveFormRecord::ToString()
92 {
93     return "form name is " + this->formName_ +
94         ", form dimension is " + std::to_string(this->formDimension_) +
95         ", form id is " + std::to_string(this->formId_) +
96         ", uid id is " + std::to_string(this->uid_) +
97         ", last used time stamp is" + std::to_string(this->formLastUsedTime_) +
98         ", touch count is " + std::to_string(this->count_) + "\n";
99 }
100 }  // namespace DeviceUsageStats
101 }  // namespace OHOS
102 
103