• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2021-2023 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 <memory>
17 #include <cstdio>
18 #include <cstring>
19 #include <map>
20 #include <sys/stat.h>
21 #include <libxml/globals.h>
22 #include <libxml/xmlstring.h>
23 #include "ans_log_wrapper.h"
24 #include "notification_constant.h"
25 #include "notification_config_parse.h"
26 
27 namespace OHOS {
28 namespace Notification {
NotificationConfigFile()29 NotificationConfigFile::NotificationConfigFile()
30 {}
31 
NotificationConfigFile(const std::string & filePath)32 NotificationConfigFile::NotificationConfigFile(const std::string &filePath)
33 {}
34 
~NotificationConfigFile()35 NotificationConfigFile::~NotificationConfigFile()
36 {}
37 
binaryToDecimal(const char * binaryString)38 int NotificationConfigFile::binaryToDecimal(const char *binaryString)
39 {
40     int lenth = strlen(binaryString);
41     int decimal = 0;
42     int weight = 1;
43 
44     for (int i = lenth - 1; i >= 0; i--) {
45         if (binaryString[i] == '1') {
46             decimal += weight;
47         }
48         weight *= NotificationConstant::DECIMAL_BASE;
49     }
50     return decimal;
51 }
52 
getDefaultSlotFlagsMap(std::map<std::string,uint32_t> & slotFlagsMap)53 void NotificationConfigFile::getDefaultSlotFlagsMap(std::map<std::string, uint32_t> &slotFlagsMap)
54 {
55     // Each bit indicate one reminder way as follows: bit0: Ring, bit1: LockScreen(include AOD), bit2: Banner,
56     // bit3: Light, bit4: Vibration.
57     slotFlagsMap.insert(std::make_pair(NotificationConstant::SLOTTYPECCMNAMES[
58         NotificationConstant::SlotType::SOCIAL_COMMUNICATION], 0b11111));
59     slotFlagsMap.insert(std::make_pair(NotificationConstant::SLOTTYPECCMNAMES[
60         NotificationConstant::SlotType::SERVICE_REMINDER], 0b11011));
61     slotFlagsMap.insert(std::make_pair(NotificationConstant::SLOTTYPECCMNAMES[
62         NotificationConstant::SlotType::CONTENT_INFORMATION], 0b00000));
63     slotFlagsMap.insert(std::make_pair(NotificationConstant::SLOTTYPECCMNAMES[
64         NotificationConstant::SlotType::OTHER], 0b00000));
65     slotFlagsMap.insert(std::make_pair(NotificationConstant::SLOTTYPECCMNAMES[
66         NotificationConstant::SlotType::CUSTOM], 0b00000));
67     slotFlagsMap.insert(std::make_pair(NotificationConstant::SLOTTYPECCMNAMES[
68         NotificationConstant::SlotType::LIVE_VIEW], 0b11011));
69     slotFlagsMap.insert(std::make_pair(NotificationConstant::SLOTTYPECCMNAMES[
70         NotificationConstant::SlotType::CUSTOMER_SERVICE], 0b10001));
71     for (auto &iter : slotFlagsMap) {
72         ANS_LOGD("Default Got slotFlagsMap item slotType = %{public}s, slotFlags = %{public}d\n",
73             iter.first.c_str(), iter.second);
74     }
75 }
76 
parseNotificationConfigCcmFile(std::string & filePath,std::map<std::string,uint32_t> & slotFlagsMap)77 bool NotificationConfigFile::parseNotificationConfigCcmFile(
78     std::string &filePath, std::map<std::string, uint32_t> &slotFlagsMap)
79 {
80     xmlDocPtr docPtr = xmlReadFile(filePath.c_str(), nullptr, XML_PARSE_NOBLANKS);
81     if (docPtr == nullptr) {
82         ANS_LOGE("xmlReadFile return nullptr!");
83         return false;
84     }
85 
86     xmlNodePtr rootPtr = xmlDocGetRootElement(docPtr);
87     if (rootPtr == nullptr || rootPtr->name == nullptr ||
88         xmlStrcmp(rootPtr->name, reinterpret_cast<const xmlChar *>("slotTypeConfig")) != 0) {
89         ANS_LOGE("got RootElement return nullptr!");
90         xmlFreeDoc(docPtr);
91         return false;
92     }
93     for (xmlNodePtr curNodePtr = rootPtr->children; curNodePtr != nullptr; curNodePtr = curNodePtr->next) {
94         std::string subName = reinterpret_cast<const char *>(curNodePtr->name);
95         if (strcasecmp(subName.c_str(), "slotType") != 0) {
96             return false;
97         }
98         xmlNodePtr subNodePtr = curNodePtr->children;
99         std::string subNodeName = reinterpret_cast<const char *>(subNodePtr->children->content);
100         std::string reminderFlagsName = reinterpret_cast<const char *>(subNodePtr->next->name);
101         for (int i = 0; i < NotificationConstant::SLOTTYPE_MAX; i++) {
102             if (strcasecmp(subNodeName.c_str(), NotificationConstant::SLOTTYPECCMNAMES[i]) == 0 &&
103                 strcasecmp(reminderFlagsName.c_str(), "reminderFlags") == 0) {
104                 uint32_t flagsDecimal = binaryToDecimal(reinterpret_cast<const char *>(subNodePtr->
105                     next->children->content));
106                 ANS_LOGD("Ccm Got insertMap item slotType =%{public}s, slotFlags = %{public}d\n",
107                     subNodeName.c_str(), flagsDecimal);
108                 slotFlagsMap.insert(std::make_pair(subNodeName, flagsDecimal));
109             }
110         }
111     }
112     return (slotFlagsMap.size() > 0) ? true : false;
113 }
114 
getNotificationSlotFlagConfig(std::string & filePath,std::map<std::string,uint32_t> & slotFlagsMap)115 bool NotificationConfigFile::getNotificationSlotFlagConfig(
116     std::string &filePath, std::map<std::string, uint32_t> &slotFlagsMap)
117 {
118     struct stat buffer;
119     if (stat(filePath.c_str(), &buffer) != 0) {
120         getDefaultSlotFlagsMap(slotFlagsMap);
121         return true;
122     } else {
123         return parseNotificationConfigCcmFile(filePath, slotFlagsMap);
124     }
125 }
126 } // namespace Notification
127 } // namespace OHOS
128