1 /*
2  * Copyright (c) 2021-2022 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 #ifndef AUDIO_FOCUS_PARSER_H
16 #define AUDIO_FOCUS_PARSER_H
17 
18 #include <map>
19 #include <string>
20 #include <libxml/parser.h>
21 #include <libxml/tree.h>
22 #include "audio_errors.h"
23 #include "audio_info.h"
24 #include "audio_policy_log.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 #ifdef USE_CONFIG_POLICY
29 static constexpr char AUDIO_FOCUS_CONFIG_FILE[] = "etc/audio/audio_interrupt_policy_config.xml";
30 #else
31 static constexpr char AUDIO_FOCUS_CONFIG_FILE[] = "system/etc/audio/audio_interrupt_policy_config.xml";
32 #endif
33 
34 class AudioFocusParser {
35 public:
36     AudioFocusParser();
37     virtual ~AudioFocusParser();
38     int32_t LoadConfig(std::map<std::pair<AudioFocusType, AudioFocusType>, AudioFocusEntry> &focusMap);
39 
40 private:
41     static std::map<std::string, AudioFocusType> audioFocusMap;
42     static std::map<std::string, InterruptHint> actionMap;
43     static std::map<std::string, ActionTarget> targetMap;
44     static std::map<std::string, InterruptForceType> forceMap;
45 
46     void LoadDefaultConfig(std::map<std::pair<AudioFocusType, AudioFocusType>, AudioFocusEntry> &focusMap);
47     void ParseFocusChildrenMap(xmlNode *node, const std::string &curStream,
48         std::map<std::pair<AudioFocusType, AudioFocusType>, AudioFocusEntry> &focusMap);
49     void ParseFocusMap(xmlNode *node, const std::string &curStream, std::map<std::pair<AudioFocusType, AudioFocusType>,
50         AudioFocusEntry> &focusMap);
51     void ParseStreams(xmlNode *node, std::map<std::pair<AudioFocusType, AudioFocusType>,
52         AudioFocusEntry> &focusMap);
53     void AddAllowedFocusEntry(xmlNode *currNode, const std::string &curStream,
54         std::map<std::pair<AudioFocusType, AudioFocusType>, AudioFocusEntry> &focusMap);
55     void ParseAllowedStreams(xmlNode *node, const std::string &curStream,
56         std::map<std::pair<AudioFocusType, AudioFocusType>, AudioFocusEntry> &focusMap);
57     void AddRejectedFocusEntry(xmlNode *currNode, const std::string &curStream,
58         std::map<std::pair<AudioFocusType, AudioFocusType>, AudioFocusEntry> &focusMap);
59     void ParseRejectedStreams(xmlNode *node, const std::string &curStream,
60         std::map<std::pair<AudioFocusType, AudioFocusType>, AudioFocusEntry> &focusMap);
61     void WriteConfigErrorEvent();
62 };
63 } // namespace AudioStandard
64 } // namespace OHOS
65 #endif // AUDIO_FOCUS_PARSER_H
66