1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ims.rcs.uce.presence.pidfparser.capabilities;
18 
19 import android.text.TextUtils;
20 
21 import com.android.ims.rcs.uce.presence.pidfparser.ElementBase;
22 
23 import org.xmlpull.v1.XmlPullParser;
24 import org.xmlpull.v1.XmlPullParserException;
25 import org.xmlpull.v1.XmlSerializer;
26 
27 import java.io.IOException;
28 
29 /**
30  * The "audio" element of the Capabilities namespace.
31  */
32 public class Audio extends ElementBase {
33     /** The name of this element */
34     public static final String ELEMENT_NAME = "audio";
35 
36     private boolean mSupported;
37 
Audio()38     public Audio() {
39     }
40 
Audio(boolean supported)41     public Audio(boolean supported) {
42         mSupported = supported;
43     }
44 
45     @Override
initNamespace()46     protected String initNamespace() {
47         return CapsConstant.NAMESPACE;
48     }
49 
50     @Override
initElementName()51     protected String initElementName() {
52         return ELEMENT_NAME;
53     }
54 
isAudioSupported()55     public boolean isAudioSupported() {
56         return mSupported;
57     }
58 
59     @Override
serialize(XmlSerializer serializer)60     public void serialize(XmlSerializer serializer) throws IOException {
61         String namespace = getNamespace();
62         String elementName = getElementName();
63         serializer.startTag(namespace, elementName);
64         serializer.text(String.valueOf(isAudioSupported()));
65         serializer.endTag(namespace, elementName);
66     }
67 
68     @Override
parse(XmlPullParser parser)69     public void parse(XmlPullParser parser) throws IOException, XmlPullParserException {
70         String namespace = parser.getNamespace();
71         String name = parser.getName();
72 
73         if (!verifyParsingElement(namespace, name)) {
74             throw new XmlPullParserException("Incorrect element: " + namespace + ", " + name);
75         }
76 
77         // Move to the next event to get the value.
78         int eventType = parser.next();
79 
80         // Get the value if the event type is text.
81         if (eventType == XmlPullParser.TEXT) {
82             String isSupported = parser.getText();
83             if (!TextUtils.isEmpty(isSupported)) {
84                 mSupported = Boolean.parseBoolean(isSupported);
85             }
86         }
87 
88         // Move to the end tag.
89         moveToElementEndTag(parser, eventType);
90     }
91 }
92