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