1 /*
2 * Copyright (c) 2025 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 <sstream>
17
18 #include "bundle_active_high_frequency_period.h"
19
20 namespace OHOS {
21 namespace DeviceUsageStats {
22 namespace {
23 static const int32_t MAX_HOUR_NUM = 24;
24 }
BundleActiveHighFrequencyPeriod()25 BundleActiveHighFrequencyPeriod::BundleActiveHighFrequencyPeriod()
26 {
27 bundleName_ = "";
28 }
29
BundleActiveHighFrequencyPeriod(const std::string & bundleName,const std::vector<int32_t> & highFreqHours)30 BundleActiveHighFrequencyPeriod::BundleActiveHighFrequencyPeriod(
31 const std::string& bundleName, const std::vector<int32_t>& highFreqHours)
32 : bundleName_(bundleName), highFreqHours_(highFreqHours)
33 {
34 }
35
BundleActiveHighFrequencyPeriod(const BundleActiveHighFrequencyPeriod & orig)36 BundleActiveHighFrequencyPeriod::BundleActiveHighFrequencyPeriod(const BundleActiveHighFrequencyPeriod& orig)
37 {
38 bundleName_ = orig.bundleName_;
39 highFreqHours_ = orig.highFreqHours_;
40 }
41
Marshalling(Parcel & parcel) const42 bool BundleActiveHighFrequencyPeriod::Marshalling(Parcel &parcel) const
43 {
44 if (highFreqHours_.size() > MAX_HOUR_NUM) {
45 return false;
46 }
47 if (parcel.WriteString(bundleName_) && parcel.WriteUint32(highFreqHours_.size())) {
48 for (auto highFreqHour : highFreqHours_) {
49 if (!parcel.WriteInt32(highFreqHour)) {
50 return false;
51 }
52 }
53 return true;
54 }
55 return false;
56 }
57
Unmarshalling(Parcel & parcel)58 BundleActiveHighFrequencyPeriod *BundleActiveHighFrequencyPeriod::Unmarshalling(Parcel& parcel)
59 {
60 BundleActiveHighFrequencyPeriod *result = new (std::nothrow) BundleActiveHighFrequencyPeriod();
61 if (result == nullptr) {
62 return nullptr;
63 }
64 result->bundleName_ = parcel.ReadString();
65 uint32_t size;
66 if (!parcel.ReadUint32(size) || size > MAX_HOUR_NUM) {
67 delete result;
68 return nullptr;
69 }
70 result->highFreqHours_.clear();
71 for (uint32_t i = 0; i < size; i++) {
72 int32_t tmp;
73 if (!parcel.ReadInt32(tmp)) {
74 delete result;
75 return nullptr;
76 }
77 result->highFreqHours_.emplace_back(tmp);
78 }
79 return result;
80 }
81
ToString() const82 std::string BundleActiveHighFrequencyPeriod::ToString() const
83 {
84 std::stringstream highFreqHoursToString;
85 highFreqHoursToString << "bundle name is ";
86 highFreqHoursToString << this->bundleName_;
87 highFreqHoursToString << ", highFreqHours_ is ";
88 for (size_t i = 0; i < highFreqHours_.size(); ++i) {
89 if (i != 0) {
90 highFreqHoursToString << ", ";
91 }
92 highFreqHoursToString << highFreqHours_[i];
93 }
94 highFreqHoursToString << "\n";
95 return highFreqHoursToString.str();
96 }
97 } // namespace DeviceUsageStats
98 } // namespace OHOS
99
100