• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "notification_progress.h"
17 
18 #include <cstdint>
19 #include <string>             // for basic_string, operator+, basic_string<>...
20 #include <memory>             // for shared_ptr, shared_ptr<>::element_type
21 
22 
23 #include "ans_log_wrapper.h"
24 #include "nlohmann/json.hpp"  // for json, basic_json<>::object_t, basic_json
25 #include "parcel.h"           // for Parcel
26 
27 namespace OHOS {
28 namespace Notification {
29 
GetMaxValue() const30 int32_t NotificationProgress::GetMaxValue() const
31 {
32     return maxValue_;
33 }
34 
SetMaxValue(int32_t maxValue)35 void NotificationProgress::SetMaxValue(int32_t maxValue)
36 {
37     maxValue_ = maxValue;
38 }
39 
GetCurrentValue() const40 int32_t NotificationProgress::GetCurrentValue() const
41 {
42     return currentValue_;
43 }
44 
SetCurrentValue(int32_t curValue)45 void NotificationProgress::SetCurrentValue(int32_t curValue)
46 {
47     currentValue_ = curValue;
48 }
49 
GetIsPercentage() const50 bool NotificationProgress::GetIsPercentage() const
51 {
52     return isPercentage_;
53 }
54 
SetIsPercentage(bool isPercentage)55 void NotificationProgress::SetIsPercentage(bool isPercentage)
56 {
57     isPercentage_ = isPercentage;
58 }
59 
60 
Dump()61 std::string NotificationProgress::Dump()
62 {
63     return "Progress{ "
64             "maxValue = " + std::to_string(maxValue_) +
65             ", currentValue = " + std::to_string(currentValue_) +
66             ", isPercentage = " + std::to_string(isPercentage_) +
67             " }";
68 }
69 
ToJson(nlohmann::json & jsonObject) const70 bool NotificationProgress::ToJson(nlohmann::json &jsonObject) const
71 {
72     jsonObject["maxValue"] = maxValue_;
73     jsonObject["currentValue"] = currentValue_;
74     jsonObject["isPercentage"] = isPercentage_;
75 
76     return true;
77 }
78 
FromJson(const nlohmann::json & jsonObject)79 NotificationProgress *NotificationProgress::FromJson(const nlohmann::json &jsonObject)
80 {
81     if (jsonObject.is_null() or !jsonObject.is_object()) {
82         ANS_LOGE("Invalid JSON object");
83         return nullptr;
84     }
85 
86     NotificationProgress *progress = new (std::nothrow) NotificationProgress();
87     if (progress == nullptr) {
88         ANS_LOGE("Failed to create capsule instance");
89         return nullptr;
90     }
91 
92     const auto &jsonEnd = jsonObject.cend();
93     if (jsonObject.find("maxValue") != jsonEnd && jsonObject.at("maxValue").is_number_integer()) {
94         progress->maxValue_ = jsonObject.at("maxValue").get<int32_t>();
95     }
96 
97     if (jsonObject.find("currentValue") != jsonEnd && jsonObject.at("currentValue").is_number_integer()) {
98         progress->currentValue_ = jsonObject.at("currentValue").get<int32_t>();
99     }
100 
101     if (jsonObject.find("isPercentage") != jsonEnd && jsonObject.at("isPercentage").is_boolean()) {
102         progress->isPercentage_ = jsonObject.at("isPercentage_").get<bool>();
103     }
104 
105     return progress;
106 }
107 
Marshalling(Parcel & parcel) const108 bool NotificationProgress::Marshalling(Parcel &parcel) const
109 {
110     if (!parcel.WriteInt32(maxValue_)) {
111         ANS_LOGE("Failed to write maxValue");
112         return false;
113     }
114 
115     if (!parcel.WriteInt32(currentValue_)) {
116         ANS_LOGE("Failed to write currentValue");
117         return false;
118     }
119 
120     if (!parcel.WriteBool(isPercentage_)) {
121         ANS_LOGE("Failed to write isPercentage");
122         return false;
123     }
124     return true;
125 }
126 
ReadFromParcel(Parcel & parcel)127 bool NotificationProgress::ReadFromParcel(Parcel &parcel)
128 {
129     maxValue_ = parcel.ReadInt32();
130     currentValue_ = parcel.ReadInt32();
131     isPercentage_ = parcel.ReadBool();
132 
133     return true;
134 }
135 
Unmarshalling(Parcel & parcel)136 NotificationProgress *NotificationProgress::Unmarshalling(Parcel &parcel)
137 {
138     NotificationProgress *progress = new (std::nothrow) NotificationProgress();
139 
140     if (progress && !progress->ReadFromParcel(parcel)) {
141         delete progress;
142         progress = nullptr;
143     }
144 
145     return progress;
146 }
147 }  // namespace Notification
148 }  // namespace OHOS
149