1 /*
2 * Copyright (c) 2024-2024 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 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
17 #include "reminder_affected.h"
18
19 #include "distributed_device_status.h"
20 #include "notification_config_parse.h"
21 namespace OHOS {
22 namespace Notification {
FromJson(const nlohmann::json & root)23 bool ReminderAffected::FromJson(const nlohmann::json &root)
24 {
25 if (root.is_null() || !root.is_object()) {
26 ANS_LOGE("ReminderAffected fromJson failed as root is null.");
27 return false;
28 }
29
30 ValidStatus(root, status_);
31 ValidAndGetAffectedBy(root, affectedBy_);
32 if (root.find(NotificationConfigParse::CFG_KEY_REMINDER_FLAGS) != root.end() &&
33 !root[NotificationConfigParse::CFG_KEY_REMINDER_FLAGS].is_null() &&
34 root[NotificationConfigParse::CFG_KEY_REMINDER_FLAGS].is_string() &&
35 NotificationFlags::GetReminderFlagsByString(
36 root[NotificationConfigParse::CFG_KEY_REMINDER_FLAGS].get<std::string>(), reminderFlags_)) {
37 return true;
38 }
39 return false;
40 }
41
ValidAndGetAffectedBy(const nlohmann::json & root,std::vector<std::pair<std::string,std::string>> & affectedBy)42 bool ReminderAffected::ValidAndGetAffectedBy(
43 const nlohmann::json &root, std::vector<std::pair<std::string, std::string>> &affectedBy)
44 {
45 if (root.is_null() || root.find(AFFECTED_BY) == root.end()) {
46 return false;
47 }
48 nlohmann::json affectedBysJson = root[AFFECTED_BY];
49 if (!affectedBysJson.is_array() || affectedBysJson.empty()) {
50 return false;
51 }
52 for (auto affectedByJson : affectedBysJson) {
53 std::string status;
54 if (affectedByJson.is_null() || !affectedByJson.is_object()) {
55 continue;
56 }
57
58 if (affectedByJson.find(DEVICE_TYPE) == affectedByJson.end() ||
59 affectedByJson[DEVICE_TYPE].is_null() ||
60 !affectedByJson[DEVICE_TYPE].is_string() ||
61 !ValidStatus(affectedByJson, status)) {
62 continue;
63 }
64
65 if (status.size() <= 0) {
66 continue;
67 }
68 affectedBy.push_back(std::make_pair(affectedByJson[DEVICE_TYPE].get<std::string>(), status));
69 }
70 if (affectedBy.size() <= 0) {
71 return false;
72 }
73 return true;
74 }
75
ValidStatus(const nlohmann::json & root,std::string & status)76 bool ReminderAffected::ValidStatus(const nlohmann::json &root, std::string &status)
77 {
78 if (root.is_null() || root.find(STATUS) == root.end()) {
79 ANS_LOGD("ValidStatus failed as status json is empty.");
80 return false;
81 }
82 nlohmann::json statusJson = root[STATUS];
83 if (!statusJson.is_string()) {
84 ANS_LOGD("ValidStatus failed as status json is not string.");
85 return false;
86 }
87 std::string strStatus = statusJson.get<std::string>();
88 if (strStatus.size() <= 0) {
89 return true;
90 }
91 if (strStatus.size() != DistributedDeviceStatus::STATUS_SIZE) {
92 ANS_LOGD("ValidStatus failed as invalid status size.");
93 return false;
94 }
95 for (int32_t seq = 0; seq < DistributedDeviceStatus::STATUS_SIZE; seq++) {
96 if (strStatus.at(seq) != STATUS_DEFAULT &&
97 strStatus.at(seq) != STATUS_ENABLE && strStatus.at(seq) != STATUS_DISABLE) {
98 ANS_LOGD("ValidStatus failed as invalid status value.");
99 return false;
100 }
101 }
102 status = strStatus;
103 return true;
104 }
105 } // namespace Notification
106 } // namespace OHOS
107 #endif
108
109