XmlUtil.java revision 241605aebc6a8f55624026e8b72246bceb1c2ac2
12b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius/*
22b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius * Copyright (C) 2016 The Android Open Source Project
32b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius *
42b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius * Licensed under the Apache License, Version 2.0 (the "License");
52b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius * you may not use this file except in compliance with the License.
62b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius * You may obtain a copy of the License at
72b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius *
82b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius *      http://www.apache.org/licenses/LICENSE-2.0
92b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius *
102b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius * Unless required by applicable law or agreed to in writing, software
112b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius * distributed under the License is distributed on an "AS IS" BASIS,
122b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius * See the License for the specific language governing permissions and
142b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius * limitations under the License.
152b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius */
162b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius
172b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Piuspackage com.android.server.wifi.util;
182b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius
19e33a4bb414892435c016486585c26022cafdab68Roshan Piusimport android.net.IpConfiguration;
20e33a4bb414892435c016486585c26022cafdab68Roshan Piusimport android.net.IpConfiguration.IpAssignment;
21e33a4bb414892435c016486585c26022cafdab68Roshan Piusimport android.net.IpConfiguration.ProxySettings;
22e33a4bb414892435c016486585c26022cafdab68Roshan Piusimport android.net.LinkAddress;
23e33a4bb414892435c016486585c26022cafdab68Roshan Piusimport android.net.NetworkUtils;
24e33a4bb414892435c016486585c26022cafdab68Roshan Piusimport android.net.ProxyInfo;
25e33a4bb414892435c016486585c26022cafdab68Roshan Piusimport android.net.RouteInfo;
26e33a4bb414892435c016486585c26022cafdab68Roshan Piusimport android.net.StaticIpConfiguration;
27e33a4bb414892435c016486585c26022cafdab68Roshan Piusimport android.net.wifi.WifiConfiguration;
285d3609b1931180c37d7292619146ad7d33df9a21Roshan Piusimport android.net.wifi.WifiConfiguration.NetworkSelectionStatus;
29642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Piusimport android.net.wifi.WifiEnterpriseConfig;
30e33a4bb414892435c016486585c26022cafdab68Roshan Piusimport android.util.Log;
312fafcc56fda54b1adf8b6743beaac59dbb84dfecRoshan Piusimport android.util.Pair;
32e33a4bb414892435c016486585c26022cafdab68Roshan Pius
332b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Piusimport com.android.internal.util.XmlUtils;
342b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius
352b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Piusimport org.xmlpull.v1.XmlPullParser;
362b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Piusimport org.xmlpull.v1.XmlPullParserException;
372b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Piusimport org.xmlpull.v1.XmlSerializer;
382b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius
392b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Piusimport java.io.IOException;
40e33a4bb414892435c016486585c26022cafdab68Roshan Piusimport java.net.Inet4Address;
41e33a4bb414892435c016486585c26022cafdab68Roshan Piusimport java.net.InetAddress;
420e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Piusimport java.util.Arrays;
43e33a4bb414892435c016486585c26022cafdab68Roshan Piusimport java.util.BitSet;
44030c5debfefddf0512cd53fec48b269c08d9972eRoshan Piusimport java.util.HashMap;
452b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius
462b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius/**
472b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius * Utils for manipulating XML data. This is essentially a wrapper over XmlUtils provided by core.
482b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius * The utility provides methods to write/parse section headers and write/parse values.
492b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius * This utility is designed for formatting the XML into the following format:
502b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius * <Document Header>
512b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius *  <Section 1 Header>
522b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius *   <Value 1>
532b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius *   <Value 2>
542b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius *   ...
552b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius *   <Sub Section 1 Header>
562b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius *    <Value 1>
572b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius *    <Value 2>
582b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius *    ...
592b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius *   </Sub Section 1 Header>
602b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius *  </Section 1 Header>
612b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius * </Document Header>
622b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius */
632b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Piuspublic class XmlUtil {
642b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    private static final String TAG = "WifiXmlUtil";
652b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius
662b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    /**
67e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * Ensure that the XML stream is at a start tag or the end of document.
68e33a4bb414892435c016486585c26022cafdab68Roshan Pius     *
69052b948a8b8a009486e35cb56dbd7bb9516e8626Roshan Pius     * @throws XmlPullParserException if parsing errors occur.
702b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     */
71e33a4bb414892435c016486585c26022cafdab68Roshan Pius    private static void gotoStartTag(XmlPullParser in)
722b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius            throws XmlPullParserException, IOException {
73e33a4bb414892435c016486585c26022cafdab68Roshan Pius        int type = in.getEventType();
74e33a4bb414892435c016486585c26022cafdab68Roshan Pius        while (type != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
75e33a4bb414892435c016486585c26022cafdab68Roshan Pius            type = in.next();
76e33a4bb414892435c016486585c26022cafdab68Roshan Pius        }
77e33a4bb414892435c016486585c26022cafdab68Roshan Pius    }
78e33a4bb414892435c016486585c26022cafdab68Roshan Pius
79e33a4bb414892435c016486585c26022cafdab68Roshan Pius    /**
80e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * Ensure that the XML stream is at an end tag or the end of document.
81e33a4bb414892435c016486585c26022cafdab68Roshan Pius     *
82052b948a8b8a009486e35cb56dbd7bb9516e8626Roshan Pius     * @throws XmlPullParserException if parsing errors occur.
83e33a4bb414892435c016486585c26022cafdab68Roshan Pius     */
84e33a4bb414892435c016486585c26022cafdab68Roshan Pius    private static void gotoEndTag(XmlPullParser in)
85e33a4bb414892435c016486585c26022cafdab68Roshan Pius            throws XmlPullParserException, IOException {
86e33a4bb414892435c016486585c26022cafdab68Roshan Pius        int type = in.getEventType();
87e33a4bb414892435c016486585c26022cafdab68Roshan Pius        while (type != XmlPullParser.END_TAG && type != XmlPullParser.END_DOCUMENT) {
88e33a4bb414892435c016486585c26022cafdab68Roshan Pius            type = in.next();
89e33a4bb414892435c016486585c26022cafdab68Roshan Pius        }
902b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    }
912b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius
922b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    /**
932b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * Start processing the XML stream at the document header.
942b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     *
952b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @param in         XmlPullParser instance pointing to the XML stream.
962b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @param headerName expected name for the start tag.
97052b948a8b8a009486e35cb56dbd7bb9516e8626Roshan Pius     * @throws XmlPullParserException if parsing errors occur.
982b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     */
992b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    public static void gotoDocumentStart(XmlPullParser in, String headerName)
1002b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius            throws XmlPullParserException, IOException {
1012b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius        XmlUtils.beginDocument(in, headerName);
1022b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    }
1032b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius
1042b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    /**
105c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * Move the XML stream to the next section header or indicate if there are no more sections.
106c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * The provided outerDepth is used to find sub sections within that depth.
107c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     *
108c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * Use this to move across sections if the ordering of sections are variable. The returned name
109c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * can be used to decide what section is next.
1102b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     *
1112b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @param in         XmlPullParser instance pointing to the XML stream.
112c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * @param headerName An array of one string, used to return the name of the next section.
1132b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @param outerDepth Find section within this depth.
114c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * @return {@code true} if a next section is found, {@code false} if there are no more sections.
115052b948a8b8a009486e35cb56dbd7bb9516e8626Roshan Pius     * @throws XmlPullParserException if parsing errors occur.
1162b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     */
117c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius    public static boolean gotoNextSectionOrEnd(
118c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius            XmlPullParser in, String[] headerName, int outerDepth)
119c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius            throws XmlPullParserException, IOException {
120c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius        if (XmlUtils.nextElementWithin(in, outerDepth)) {
121c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius            headerName[0] = in.getName();
122c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius            return true;
123c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius        }
124c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius        return false;
125c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius    }
126c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius
127c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius    /**
128c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * Move the XML stream to the next section header or indicate if there are no more sections.
129c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * If a section, exists ensure that the name matches the provided name.
130c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * The provided outerDepth is used to find sub sections within that depth.
131c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     *
132c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * Use this to move across repeated sections until the end.
133c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     *
134c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * @param in           XmlPullParser instance pointing to the XML stream.
135c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * @param expectedName expected name for the section header.
136c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * @param outerDepth   Find section within this depth.
137c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * @return {@code true} if a next section is found, {@code false} if there are no more sections.
138c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * @throws XmlPullParserException if the section header name does not match |expectedName|,
139c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     *                                or if parsing errors occur.
140c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     */
141c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius    public static boolean gotoNextSectionWithNameOrEnd(
142c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius            XmlPullParser in, String expectedName, int outerDepth)
1432b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius            throws XmlPullParserException, IOException {
144c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius        String[] headerName = new String[1];
145c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius        if (gotoNextSectionOrEnd(in, headerName, outerDepth)) {
146c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius            if (headerName[0].equals(expectedName)) {
1472b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius                return true;
1482b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius            }
149c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius            throw new XmlPullParserException(
150c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius                    "Next section name does not match expected name: " + expectedName);
1512b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius        }
1522b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius        return false;
1532b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    }
1542b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius
1552b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    /**
156c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * Move the XML stream to the next section header and ensure that the name matches the provided
157c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * name.
158c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * The provided outerDepth is used to find sub sections within that depth.
159c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     *
160c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * Use this to move across sections if the ordering of sections are fixed.
161c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     *
162c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * @param in           XmlPullParser instance pointing to the XML stream.
163c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * @param expectedName expected name for the section header.
164c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * @param outerDepth   Find section within this depth.
165c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     * @throws XmlPullParserException if the section header name does not match |expectedName|,
166c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     *                                there are no more sections or if parsing errors occur.
167c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius     */
168c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius    public static void gotoNextSectionWithName(
169c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius            XmlPullParser in, String expectedName, int outerDepth)
170c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius            throws XmlPullParserException, IOException {
171c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius        if (!gotoNextSectionWithNameOrEnd(in, expectedName, outerDepth)) {
172c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius            throw new XmlPullParserException("Section not found. Expected: " + expectedName);
173c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius        }
174c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius    }
175c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius
176c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius    /**
177e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * Checks if the stream is at the end of a section of values. This moves the stream to next tag
178e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * and checks if it finds an end tag at the specified depth.
179e33a4bb414892435c016486585c26022cafdab68Roshan Pius     *
180e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * @param in           XmlPullParser instance pointing to the XML stream.
181e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * @param sectionDepth depth of the start tag of this section. Used to match the end tag.
182e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * @return {@code true} if a end tag at the provided depth is found, {@code false} otherwise
183052b948a8b8a009486e35cb56dbd7bb9516e8626Roshan Pius     * @throws XmlPullParserException if parsing errors occur.
184e33a4bb414892435c016486585c26022cafdab68Roshan Pius     */
185e33a4bb414892435c016486585c26022cafdab68Roshan Pius    public static boolean isNextSectionEnd(XmlPullParser in, int sectionDepth)
186e33a4bb414892435c016486585c26022cafdab68Roshan Pius            throws XmlPullParserException, IOException {
187c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius        return !XmlUtils.nextElementWithin(in, sectionDepth);
188e33a4bb414892435c016486585c26022cafdab68Roshan Pius    }
189e33a4bb414892435c016486585c26022cafdab68Roshan Pius
190e33a4bb414892435c016486585c26022cafdab68Roshan Pius    /**
191e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * Read the current value in the XML stream using core XmlUtils and stores the retrieved
192e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * value name in the string provided. This method reads the value contained in current start
193e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * tag.
194e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * Note: Because there could be genuine null values being read from the XML, this method raises
195e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * an exception to indicate errors.
196e33a4bb414892435c016486585c26022cafdab68Roshan Pius     *
197e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * @param in        XmlPullParser instance pointing to the XML stream.
198e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * @param valueName An array of one string, used to return the name attribute
199e33a4bb414892435c016486585c26022cafdab68Roshan Pius     *                  of the value's tag.
200e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * @return value retrieved from the XML stream.
201052b948a8b8a009486e35cb56dbd7bb9516e8626Roshan Pius     * @throws XmlPullParserException if parsing errors occur.
202e33a4bb414892435c016486585c26022cafdab68Roshan Pius     */
203e33a4bb414892435c016486585c26022cafdab68Roshan Pius    public static Object readCurrentValue(XmlPullParser in, String[] valueName)
204e33a4bb414892435c016486585c26022cafdab68Roshan Pius            throws XmlPullParserException, IOException {
205e33a4bb414892435c016486585c26022cafdab68Roshan Pius        Object value = XmlUtils.readValueXml(in, valueName);
206e33a4bb414892435c016486585c26022cafdab68Roshan Pius        // XmlUtils.readValue does not always move the stream to the end of the tag. So, move
207e33a4bb414892435c016486585c26022cafdab68Roshan Pius        // it to the end tag before returning from here.
208e33a4bb414892435c016486585c26022cafdab68Roshan Pius        gotoEndTag(in);
209e33a4bb414892435c016486585c26022cafdab68Roshan Pius        return value;
210e33a4bb414892435c016486585c26022cafdab68Roshan Pius    }
211e33a4bb414892435c016486585c26022cafdab68Roshan Pius
212e33a4bb414892435c016486585c26022cafdab68Roshan Pius    /**
2132b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * Read the next value in the XML stream using core XmlUtils and ensure that it matches the
214e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * provided name. This method moves the stream to the next start tag and reads the value
215e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * contained in it.
2162b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * Note: Because there could be genuine null values being read from the XML, this method raises
2172b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * an exception to indicate errors.
2182b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     *
2192b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @param in XmlPullParser instance pointing to the XML stream.
2202b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @return value retrieved from the XML stream.
221052b948a8b8a009486e35cb56dbd7bb9516e8626Roshan Pius     * @throws XmlPullParserException if the value read does not match |expectedName|,
222052b948a8b8a009486e35cb56dbd7bb9516e8626Roshan Pius     *                                or if parsing errors occur.
2232b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     */
224e33a4bb414892435c016486585c26022cafdab68Roshan Pius    public static Object readNextValueWithName(XmlPullParser in, String expectedName)
2252b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius            throws XmlPullParserException, IOException {
226e33a4bb414892435c016486585c26022cafdab68Roshan Pius        String[] valueName = new String[1];
227e33a4bb414892435c016486585c26022cafdab68Roshan Pius        XmlUtils.nextElement(in);
228e33a4bb414892435c016486585c26022cafdab68Roshan Pius        Object value = readCurrentValue(in, valueName);
229e33a4bb414892435c016486585c26022cafdab68Roshan Pius        if (valueName[0].equals(expectedName)) {
2302b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius            return value;
2312b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius        }
2322b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius        throw new XmlPullParserException(
233e33a4bb414892435c016486585c26022cafdab68Roshan Pius                "Value not found. Expected: " + expectedName + ", but got: " + valueName[0]);
2342b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    }
2352b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius
2362b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    /**
2372b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * Write the XML document start with the provided document header name.
2382b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     *
2392b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @param out        XmlSerializer instance pointing to the XML stream.
2402b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @param headerName name for the start tag.
2412b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     */
2422b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    public static void writeDocumentStart(XmlSerializer out, String headerName)
243e33a4bb414892435c016486585c26022cafdab68Roshan Pius            throws IOException {
2442b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius        out.startDocument(null, true);
2452b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius        out.startTag(null, headerName);
2462b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    }
2472b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius
2482b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    /**
2492b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * Write the XML document end with the provided document header name.
2502b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     *
2512b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @param out        XmlSerializer instance pointing to the XML stream.
2522b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @param headerName name for the end tag.
2532b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     */
2542b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    public static void writeDocumentEnd(XmlSerializer out, String headerName)
255e33a4bb414892435c016486585c26022cafdab68Roshan Pius            throws IOException {
2562b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius        out.endTag(null, headerName);
2572b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius        out.endDocument();
2582b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    }
2592b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius
2602b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    /**
2612b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * Write a section start header tag with the provided section name.
2622b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     *
2632b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @param out        XmlSerializer instance pointing to the XML stream.
2642b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @param headerName name for the start tag.
2652b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     */
2662b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    public static void writeNextSectionStart(XmlSerializer out, String headerName)
267e33a4bb414892435c016486585c26022cafdab68Roshan Pius            throws IOException {
2682b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius        out.startTag(null, headerName);
2692b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    }
2702b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius
2712b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    /**
2722b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * Write a section end header tag with the provided section name.
2732b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     *
2742b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @param out        XmlSerializer instance pointing to the XML stream.
2752b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @param headerName name for the end tag.
2762b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     */
2772b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    public static void writeNextSectionEnd(XmlSerializer out, String headerName)
278e33a4bb414892435c016486585c26022cafdab68Roshan Pius            throws IOException {
2792b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius        out.endTag(null, headerName);
2802b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    }
2812b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius
2822b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    /**
2832b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * Write the value with the provided name in the XML stream using core XmlUtils.
2842b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     *
2852b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @param out   XmlSerializer instance pointing to the XML stream.
2862b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @param name  name of the value.
2872b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     * @param value value to be written.
2882b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius     */
2892b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    public static void writeNextValue(XmlSerializer out, String name, Object value)
2902b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius            throws XmlPullParserException, IOException {
2912b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius        XmlUtils.writeValueXml(value, name, out);
2922b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius    }
293e33a4bb414892435c016486585c26022cafdab68Roshan Pius
294e33a4bb414892435c016486585c26022cafdab68Roshan Pius    /**
2955d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius     * Utility class to serialize and deseriaize {@link WifiConfiguration} object to XML &
2965d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius     * vice versa.
2975d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius     * This is used by both {@link com.android.server.wifi.WifiConfigStore} &
2985d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius     * {@link com.android.server.wifi.WifiBackupRestore} modules.
299e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * The |writeConfigurationToXml| has 2 versions, one for backup and one for config store.
300e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * There is only 1 version of |parseXmlToConfiguration| for both backup & config store.
301e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * The parse method is written so that any element added/deleted in future revisions can
302e33a4bb414892435c016486585c26022cafdab68Roshan Pius     * be easily handled.
303e33a4bb414892435c016486585c26022cafdab68Roshan Pius     */
304e33a4bb414892435c016486585c26022cafdab68Roshan Pius    public static class WifiConfigurationXmlUtil {
305e33a4bb414892435c016486585c26022cafdab68Roshan Pius        /**
306e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * List of XML tags corresponding to WifiConfiguration object elements.
307e33a4bb414892435c016486585c26022cafdab68Roshan Pius         */
308e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_SSID = "SSID";
309e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_BSSID = "BSSID";
310e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_CONFIG_KEY = "ConfigKey";
311e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_PRE_SHARED_KEY = "PreSharedKey";
312e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_WEP_KEYS = "WEPKeys";
313e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_WEP_TX_KEY_INDEX = "WEPTxKeyIndex";
314e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_HIDDEN_SSID = "HiddenSSID";
315241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius        public static final String XML_TAG_REQUIRE_PMF = "RequirePMF";
316e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_ALLOWED_KEY_MGMT = "AllowedKeyMgmt";
317e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_ALLOWED_PROTOCOLS = "AllowedProtocols";
318e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_ALLOWED_AUTH_ALGOS = "AllowedAuthAlgos";
319241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius        public static final String XML_TAG_ALLOWED_GROUP_CIPHERS = "AllowedGroupCiphers";
320241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius        public static final String XML_TAG_ALLOWED_PAIRWISE_CIPHERS = "AllowedPairwiseCiphers";
321e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_SHARED = "Shared";
322030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius        public static final String XML_TAG_FQDN = "FQDN";
323030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius        public static final String XML_TAG_PROVIDER_FRIENDLY_NAME = "ProviderFriendlyName";
324030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius        public static final String XML_TAG_LINKED_NETWORKS_LIST = "LinkedNetworksList";
325030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius        public static final String XML_TAG_DEFAULT_GW_MAC_ADDRESS = "DefaultGwMacAddress";
326030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius        public static final String XML_TAG_VALIDATED_INTERNET_ACCESS = "ValidatedInternetAccess";
327030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius        public static final String XML_TAG_NO_INTERNET_ACCESS_EXPECTED = "NoInternetAccessExpected";
328030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius        public static final String XML_TAG_USER_APPROVED = "UserApproved";
329030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius        public static final String XML_TAG_METERED_HINT = "MeteredHint";
330030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius        public static final String XML_TAG_USE_EXTERNAL_SCORES = "UseExternalScores";
331030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius        public static final String XML_TAG_NUM_ASSOCIATION = "NumAssociation";
332e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_CREATOR_UID = "CreatorUid";
3332fafcc56fda54b1adf8b6743beaac59dbb84dfecRoshan Pius        public static final String XML_TAG_CREATOR_NAME = "CreatorName";
334030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius        public static final String XML_TAG_CREATION_TIME = "CreationTime";
335030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius        public static final String XML_TAG_LAST_UPDATE_UID = "LastUpdateUid";
336030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius        public static final String XML_TAG_LAST_UPDATE_NAME = "LastUpdateName";
337030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius        public static final String XML_TAG_LAST_CONNECT_UID = "LastConnectUid";
338e33a4bb414892435c016486585c26022cafdab68Roshan Pius
339e33a4bb414892435c016486585c26022cafdab68Roshan Pius        /**
340e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * Write WepKeys to the XML stream.
341e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * WepKeys array is intialized in WifiConfiguration constructor, but all of the elements
34206a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius         * are set to null. User may chose to set any one of the key elements in WifiConfiguration.
34306a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius         * XmlUtils serialization doesn't handle this array of nulls well .
34406a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius         * So, write empty strings if some of the keys are not initialized and null if all of
34506a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius         * the elements are empty.
346e33a4bb414892435c016486585c26022cafdab68Roshan Pius         */
347e33a4bb414892435c016486585c26022cafdab68Roshan Pius        private static void writeWepKeysToXml(XmlSerializer out, String[] wepKeys)
348e33a4bb414892435c016486585c26022cafdab68Roshan Pius                throws XmlPullParserException, IOException {
34906a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius            String[] wepKeysToWrite = new String[wepKeys.length];
35006a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius            boolean hasWepKey = false;
35106a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius            for (int i = 0; i < wepKeys.length; i++) {
35206a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius                if (wepKeys[i] == null) {
35306a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius                    wepKeysToWrite[i] = new String();
35406a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius                } else {
35506a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius                    wepKeysToWrite[i] = wepKeys[i];
35606a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius                    hasWepKey = true;
35706a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius                }
35806a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius            }
35906a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius            if (hasWepKey) {
36006a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius                XmlUtil.writeNextValue(out, XML_TAG_WEP_KEYS, wepKeysToWrite);
361e33a4bb414892435c016486585c26022cafdab68Roshan Pius            } else {
362e33a4bb414892435c016486585c26022cafdab68Roshan Pius                XmlUtil.writeNextValue(out, XML_TAG_WEP_KEYS, null);
363e33a4bb414892435c016486585c26022cafdab68Roshan Pius            }
364e33a4bb414892435c016486585c26022cafdab68Roshan Pius        }
365e33a4bb414892435c016486585c26022cafdab68Roshan Pius
366e33a4bb414892435c016486585c26022cafdab68Roshan Pius        /**
367e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * Write the Configuration data elements that are common for backup & config store to the
368e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * XML stream.
369e33a4bb414892435c016486585c26022cafdab68Roshan Pius         *
370e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * @param out           XmlSerializer instance pointing to the XML stream.
371e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * @param configuration WifiConfiguration object to be serialized.
372e33a4bb414892435c016486585c26022cafdab68Roshan Pius         */
373c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius        public static void writeCommonElementsToXml(
374c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius                XmlSerializer out, WifiConfiguration configuration)
375e33a4bb414892435c016486585c26022cafdab68Roshan Pius                throws XmlPullParserException, IOException {
376e33a4bb414892435c016486585c26022cafdab68Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_CONFIG_KEY, configuration.configKey());
377e33a4bb414892435c016486585c26022cafdab68Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_SSID, configuration.SSID);
378e33a4bb414892435c016486585c26022cafdab68Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_BSSID, configuration.BSSID);
379e33a4bb414892435c016486585c26022cafdab68Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_PRE_SHARED_KEY, configuration.preSharedKey);
380e33a4bb414892435c016486585c26022cafdab68Roshan Pius            writeWepKeysToXml(out, configuration.wepKeys);
381e33a4bb414892435c016486585c26022cafdab68Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_WEP_TX_KEY_INDEX, configuration.wepTxKeyIndex);
382e33a4bb414892435c016486585c26022cafdab68Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_HIDDEN_SSID, configuration.hiddenSSID);
383241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_REQUIRE_PMF, configuration.requirePMF);
384e33a4bb414892435c016486585c26022cafdab68Roshan Pius            XmlUtil.writeNextValue(
385e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    out, XML_TAG_ALLOWED_KEY_MGMT,
386e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    configuration.allowedKeyManagement.toByteArray());
387e33a4bb414892435c016486585c26022cafdab68Roshan Pius            XmlUtil.writeNextValue(
388e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    out, XML_TAG_ALLOWED_PROTOCOLS,
389e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    configuration.allowedProtocols.toByteArray());
390e33a4bb414892435c016486585c26022cafdab68Roshan Pius            XmlUtil.writeNextValue(
391e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    out, XML_TAG_ALLOWED_AUTH_ALGOS,
392e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    configuration.allowedAuthAlgorithms.toByteArray());
393241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius            XmlUtil.writeNextValue(
394241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius                    out, XML_TAG_ALLOWED_GROUP_CIPHERS,
395241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius                    configuration.allowedGroupCiphers.toByteArray());
396241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius            XmlUtil.writeNextValue(
397241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius                    out, XML_TAG_ALLOWED_PAIRWISE_CIPHERS,
398241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius                    configuration.allowedPairwiseCiphers.toByteArray());
399e33a4bb414892435c016486585c26022cafdab68Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_SHARED, configuration.shared);
400e33a4bb414892435c016486585c26022cafdab68Roshan Pius        }
401e33a4bb414892435c016486585c26022cafdab68Roshan Pius
402e33a4bb414892435c016486585c26022cafdab68Roshan Pius        /**
403e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * Write the Configuration data elements for backup from the provided Configuration to the
404e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * XML stream.
405e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * Note: This is a subset of the elements serialized for config store.
406e33a4bb414892435c016486585c26022cafdab68Roshan Pius         *
407e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * @param out           XmlSerializer instance pointing to the XML stream.
408e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * @param configuration WifiConfiguration object to be serialized.
409e33a4bb414892435c016486585c26022cafdab68Roshan Pius         */
410c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius        public static void writeToXmlForBackup(XmlSerializer out, WifiConfiguration configuration)
411e33a4bb414892435c016486585c26022cafdab68Roshan Pius                throws XmlPullParserException, IOException {
412c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius            writeCommonElementsToXml(out, configuration);
413e33a4bb414892435c016486585c26022cafdab68Roshan Pius        }
414e33a4bb414892435c016486585c26022cafdab68Roshan Pius
415e33a4bb414892435c016486585c26022cafdab68Roshan Pius        /**
416e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * Write the Configuration data elements for config store from the provided Configuration
417e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * to the XML stream.
418642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         *
419642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         * @param out           XmlSerializer instance pointing to the XML stream.
420642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         * @param configuration WifiConfiguration object to be serialized.
421e33a4bb414892435c016486585c26022cafdab68Roshan Pius         */
422c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius        public static void writeToXmlForConfigStore(
423c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius                XmlSerializer out, WifiConfiguration configuration)
424e33a4bb414892435c016486585c26022cafdab68Roshan Pius                throws XmlPullParserException, IOException {
425c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius            writeCommonElementsToXml(out, configuration);
426030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius            XmlUtil.writeNextValue(out, XML_TAG_FQDN, configuration.FQDN);
427030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius            XmlUtil.writeNextValue(
428030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    out, XML_TAG_PROVIDER_FRIENDLY_NAME, configuration.providerFriendlyName);
429030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius            XmlUtil.writeNextValue(
430030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    out, XML_TAG_LINKED_NETWORKS_LIST, configuration.linkedConfigurations);
431030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius            XmlUtil.writeNextValue(
432030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    out, XML_TAG_DEFAULT_GW_MAC_ADDRESS, configuration.defaultGwMacAddress);
433030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius            XmlUtil.writeNextValue(
434030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    out, XML_TAG_VALIDATED_INTERNET_ACCESS, configuration.validatedInternetAccess);
435030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius            XmlUtil.writeNextValue(
436030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    out, XML_TAG_NO_INTERNET_ACCESS_EXPECTED,
437030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    configuration.noInternetAccessExpected);
438030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius            XmlUtil.writeNextValue(out, XML_TAG_USER_APPROVED, configuration.userApproved);
439030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius            XmlUtil.writeNextValue(out, XML_TAG_METERED_HINT, configuration.meteredHint);
440030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius            XmlUtil.writeNextValue(
441030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    out, XML_TAG_USE_EXTERNAL_SCORES, configuration.useExternalScores);
442030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius            XmlUtil.writeNextValue(out, XML_TAG_NUM_ASSOCIATION, configuration.numAssociation);
4432fafcc56fda54b1adf8b6743beaac59dbb84dfecRoshan Pius            XmlUtil.writeNextValue(out, XML_TAG_CREATOR_UID, configuration.creatorUid);
4442fafcc56fda54b1adf8b6743beaac59dbb84dfecRoshan Pius            XmlUtil.writeNextValue(out, XML_TAG_CREATOR_NAME, configuration.creatorName);
445030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius            XmlUtil.writeNextValue(out, XML_TAG_CREATION_TIME, configuration.creationTime);
446030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius            XmlUtil.writeNextValue(out, XML_TAG_LAST_UPDATE_UID, configuration.lastUpdateUid);
447030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius            XmlUtil.writeNextValue(out, XML_TAG_LAST_UPDATE_NAME, configuration.lastUpdateName);
448030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius            XmlUtil.writeNextValue(out, XML_TAG_LAST_CONNECT_UID, configuration.lastConnectUid);
449e33a4bb414892435c016486585c26022cafdab68Roshan Pius        }
450e33a4bb414892435c016486585c26022cafdab68Roshan Pius
451e33a4bb414892435c016486585c26022cafdab68Roshan Pius        /**
45206a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius         * Populate wepKeys array elements only if they were non-empty in the backup data.
4532fafcc56fda54b1adf8b6743beaac59dbb84dfecRoshan Pius         *
45406a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius         * @throws XmlPullParserException if parsing errors occur.
455e33a4bb414892435c016486585c26022cafdab68Roshan Pius         */
456e33a4bb414892435c016486585c26022cafdab68Roshan Pius        private static void populateWepKeysFromXmlValue(Object value, String[] wepKeys)
457e33a4bb414892435c016486585c26022cafdab68Roshan Pius                throws XmlPullParserException, IOException {
458e33a4bb414892435c016486585c26022cafdab68Roshan Pius            String[] wepKeysInData = (String[]) value;
45906a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius            if (wepKeysInData == null) {
46006a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius                return;
46106a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius            }
46206a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius            if (wepKeysInData.length != wepKeys.length) {
46306a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius                throw new XmlPullParserException(
46406a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius                        "Invalid Wep Keys length: " + wepKeysInData.length);
46506a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius            }
46606a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius            for (int i = 0; i < wepKeys.length; i++) {
46706a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius                if (wepKeysInData[i].isEmpty()) {
46806a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius                    wepKeys[i] = null;
46906a2281303248446bacc87a00ab66ea1fdf0392dRoshan Pius                } else {
470e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    wepKeys[i] = wepKeysInData[i];
471e33a4bb414892435c016486585c26022cafdab68Roshan Pius                }
472e33a4bb414892435c016486585c26022cafdab68Roshan Pius            }
473e33a4bb414892435c016486585c26022cafdab68Roshan Pius        }
474e33a4bb414892435c016486585c26022cafdab68Roshan Pius
475e33a4bb414892435c016486585c26022cafdab68Roshan Pius        /**
4765d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius         * Parses the configuration data elements from the provided XML stream to a
4775d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius         * WifiConfiguration object.
478e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * Note: This is used for parsing both backup data and config store data. Looping through
479e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * the tags make it easy to add or remove elements in the future versions if needed.
480e33a4bb414892435c016486585c26022cafdab68Roshan Pius         *
481e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * @param in            XmlPullParser instance pointing to the XML stream.
482e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * @param outerTagDepth depth of the outer tag in the XML document.
4832fafcc56fda54b1adf8b6743beaac59dbb84dfecRoshan Pius         * @return Pair<Config key, WifiConfiguration object> if parsing is successful,
4842fafcc56fda54b1adf8b6743beaac59dbb84dfecRoshan Pius         * null otherwise.
485e33a4bb414892435c016486585c26022cafdab68Roshan Pius         */
486c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius        public static Pair<String, WifiConfiguration> parseFromXml(
4872fafcc56fda54b1adf8b6743beaac59dbb84dfecRoshan Pius                XmlPullParser in, int outerTagDepth)
488e33a4bb414892435c016486585c26022cafdab68Roshan Pius                throws XmlPullParserException, IOException {
489e33a4bb414892435c016486585c26022cafdab68Roshan Pius            WifiConfiguration configuration = new WifiConfiguration();
490e33a4bb414892435c016486585c26022cafdab68Roshan Pius            String configKeyInData = null;
491e33a4bb414892435c016486585c26022cafdab68Roshan Pius
492e33a4bb414892435c016486585c26022cafdab68Roshan Pius            // Loop through and parse out all the elements from the stream within this section.
493e33a4bb414892435c016486585c26022cafdab68Roshan Pius            while (!XmlUtil.isNextSectionEnd(in, outerTagDepth)) {
494e33a4bb414892435c016486585c26022cafdab68Roshan Pius                String[] valueName = new String[1];
495e33a4bb414892435c016486585c26022cafdab68Roshan Pius                Object value = XmlUtil.readCurrentValue(in, valueName);
496e33a4bb414892435c016486585c26022cafdab68Roshan Pius                if (valueName[0] == null) {
497c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius                    throw new XmlPullParserException("Missing value name");
498e33a4bb414892435c016486585c26022cafdab68Roshan Pius                }
499e33a4bb414892435c016486585c26022cafdab68Roshan Pius                switch (valueName[0]) {
500e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    case XML_TAG_CONFIG_KEY:
501e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        configKeyInData = (String) value;
502e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        break;
503e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    case XML_TAG_SSID:
504e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        configuration.SSID = (String) value;
505e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        break;
506e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    case XML_TAG_BSSID:
507e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        configuration.BSSID = (String) value;
508e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        break;
509e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    case XML_TAG_PRE_SHARED_KEY:
510e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        configuration.preSharedKey = (String) value;
511e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        break;
512e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    case XML_TAG_WEP_KEYS:
513e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        populateWepKeysFromXmlValue(value, configuration.wepKeys);
514e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        break;
515e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    case XML_TAG_WEP_TX_KEY_INDEX:
516e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        configuration.wepTxKeyIndex = (int) value;
517e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        break;
518e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    case XML_TAG_HIDDEN_SSID:
519e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        configuration.hiddenSSID = (boolean) value;
520e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        break;
521241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius                    case XML_TAG_REQUIRE_PMF:
522241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius                        configuration.requirePMF = (boolean) value;
523241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius                        break;
524e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    case XML_TAG_ALLOWED_KEY_MGMT:
525e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        byte[] allowedKeyMgmt = (byte[]) value;
526e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        configuration.allowedKeyManagement = BitSet.valueOf(allowedKeyMgmt);
527e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        break;
528e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    case XML_TAG_ALLOWED_PROTOCOLS:
529e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        byte[] allowedProtocols = (byte[]) value;
530e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        configuration.allowedProtocols = BitSet.valueOf(allowedProtocols);
531e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        break;
532e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    case XML_TAG_ALLOWED_AUTH_ALGOS:
533e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        byte[] allowedAuthAlgorithms = (byte[]) value;
534e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        configuration.allowedAuthAlgorithms = BitSet.valueOf(allowedAuthAlgorithms);
535e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        break;
536241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius                    case XML_TAG_ALLOWED_GROUP_CIPHERS:
537241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius                        byte[] allowedGroupCiphers = (byte[]) value;
538241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius                        configuration.allowedGroupCiphers = BitSet.valueOf(allowedGroupCiphers);
539241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius                        break;
540241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius                    case XML_TAG_ALLOWED_PAIRWISE_CIPHERS:
541241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius                        byte[] allowedPairwiseCiphers = (byte[]) value;
542241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius                        configuration.allowedPairwiseCiphers =
543241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius                                BitSet.valueOf(allowedPairwiseCiphers);
544241605aebc6a8f55624026e8b72246bceb1c2ac2Roshan Pius                        break;
545e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    case XML_TAG_SHARED:
546e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        configuration.shared = (boolean) value;
547e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        break;
548030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    case XML_TAG_FQDN:
549030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        configuration.FQDN = (String) value;
550030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        break;
551030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    case XML_TAG_PROVIDER_FRIENDLY_NAME:
552030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        configuration.providerFriendlyName = (String) value;
553030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        break;
554030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    case XML_TAG_LINKED_NETWORKS_LIST:
555030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        configuration.linkedConfigurations = (HashMap<String, Integer>) value;
556030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        break;
557030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    case XML_TAG_DEFAULT_GW_MAC_ADDRESS:
558030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        configuration.defaultGwMacAddress = (String) value;
559030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        break;
560030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    case XML_TAG_VALIDATED_INTERNET_ACCESS:
561030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        configuration.validatedInternetAccess = (boolean) value;
562030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        break;
563030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    case XML_TAG_NO_INTERNET_ACCESS_EXPECTED:
564030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        configuration.noInternetAccessExpected = (boolean) value;
565030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        break;
566030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    case XML_TAG_USER_APPROVED:
567030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        configuration.userApproved = (int) value;
568030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        break;
569030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    case XML_TAG_METERED_HINT:
570030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        configuration.meteredHint = (boolean) value;
571030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        break;
572030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    case XML_TAG_USE_EXTERNAL_SCORES:
573030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        configuration.useExternalScores = (boolean) value;
574030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        break;
575030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    case XML_TAG_NUM_ASSOCIATION:
576030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        configuration.numAssociation = (int) value;
577030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        break;
578e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    case XML_TAG_CREATOR_UID:
579e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        configuration.creatorUid = (int) value;
580e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        break;
5812fafcc56fda54b1adf8b6743beaac59dbb84dfecRoshan Pius                    case XML_TAG_CREATOR_NAME:
5822fafcc56fda54b1adf8b6743beaac59dbb84dfecRoshan Pius                        configuration.creatorName = (String) value;
5832fafcc56fda54b1adf8b6743beaac59dbb84dfecRoshan Pius                        break;
584030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    case XML_TAG_CREATION_TIME:
585030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        configuration.creationTime = (String) value;
586030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        break;
587030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    case XML_TAG_LAST_UPDATE_UID:
588030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        configuration.lastUpdateUid = (int) value;
589030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        break;
590030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    case XML_TAG_LAST_UPDATE_NAME:
591030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        configuration.lastUpdateName = (String) value;
592030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        break;
593030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                    case XML_TAG_LAST_CONNECT_UID:
594030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        configuration.lastConnectUid = (int) value;
595030c5debfefddf0512cd53fec48b269c08d9972eRoshan Pius                        break;
596e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    default:
597c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius                        throw new XmlPullParserException(
598c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius                                "Unknown value name found: " + valueName[0]);
599e33a4bb414892435c016486585c26022cafdab68Roshan Pius                }
600e33a4bb414892435c016486585c26022cafdab68Roshan Pius            }
6012fafcc56fda54b1adf8b6743beaac59dbb84dfecRoshan Pius            return Pair.create(configKeyInData, configuration);
602e33a4bb414892435c016486585c26022cafdab68Roshan Pius        }
603e33a4bb414892435c016486585c26022cafdab68Roshan Pius    }
604e33a4bb414892435c016486585c26022cafdab68Roshan Pius
605e33a4bb414892435c016486585c26022cafdab68Roshan Pius    /**
6065d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius     * Utility class to serialize and deseriaize {@link IpConfiguration} object to XML & vice versa.
6075d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius     * This is used by both {@link com.android.server.wifi.WifiConfigStore} &
6085d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius     * {@link com.android.server.wifi.WifiBackupRestore} modules.
609e33a4bb414892435c016486585c26022cafdab68Roshan Pius     */
610e33a4bb414892435c016486585c26022cafdab68Roshan Pius    public static class IpConfigurationXmlUtil {
611e33a4bb414892435c016486585c26022cafdab68Roshan Pius
612e33a4bb414892435c016486585c26022cafdab68Roshan Pius        /**
613e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * List of XML tags corresponding to IpConfiguration object elements.
614e33a4bb414892435c016486585c26022cafdab68Roshan Pius         */
615e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_IP_ASSIGNMENT = "IpAssignment";
616e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_LINK_ADDRESS = "LinkAddress";
617e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_LINK_PREFIX_LENGTH = "LinkPrefixLength";
618e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_GATEWAY_ADDRESS = "GatewayAddress";
619e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_DNS_SERVER_ADDRESSES = "DNSServers";
620e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_PROXY_SETTINGS = "ProxySettings";
621e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_PROXY_HOST = "ProxyHost";
622e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_PROXY_PORT = "ProxyPort";
623e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_PROXY_PAC_FILE = "ProxyPac";
624e33a4bb414892435c016486585c26022cafdab68Roshan Pius        public static final String XML_TAG_PROXY_EXCLUSION_LIST = "ProxyExclusionList";
625e33a4bb414892435c016486585c26022cafdab68Roshan Pius
626e33a4bb414892435c016486585c26022cafdab68Roshan Pius        /**
627e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * Write the static IP configuration data elements to XML stream.
628e33a4bb414892435c016486585c26022cafdab68Roshan Pius         */
629c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius        private static void writeStaticIpConfigurationToXml(
630c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius                XmlSerializer out, StaticIpConfiguration staticIpConfiguration)
631e33a4bb414892435c016486585c26022cafdab68Roshan Pius                throws XmlPullParserException, IOException {
632e33a4bb414892435c016486585c26022cafdab68Roshan Pius            if (staticIpConfiguration.ipAddress != null) {
633e33a4bb414892435c016486585c26022cafdab68Roshan Pius                XmlUtil.writeNextValue(
634e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        out, XML_TAG_LINK_ADDRESS,
635e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        staticIpConfiguration.ipAddress.getAddress().getHostAddress());
636e33a4bb414892435c016486585c26022cafdab68Roshan Pius                XmlUtil.writeNextValue(
637e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        out, XML_TAG_LINK_PREFIX_LENGTH,
638e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        staticIpConfiguration.ipAddress.getPrefixLength());
639e33a4bb414892435c016486585c26022cafdab68Roshan Pius            } else {
640e33a4bb414892435c016486585c26022cafdab68Roshan Pius                XmlUtil.writeNextValue(
641e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        out, XML_TAG_LINK_ADDRESS, null);
642e33a4bb414892435c016486585c26022cafdab68Roshan Pius                XmlUtil.writeNextValue(
643e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        out, XML_TAG_LINK_PREFIX_LENGTH, null);
644e33a4bb414892435c016486585c26022cafdab68Roshan Pius            }
645e33a4bb414892435c016486585c26022cafdab68Roshan Pius            if (staticIpConfiguration.gateway != null) {
646e33a4bb414892435c016486585c26022cafdab68Roshan Pius                XmlUtil.writeNextValue(
647e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        out, XML_TAG_GATEWAY_ADDRESS,
648e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        staticIpConfiguration.gateway.getHostAddress());
649e33a4bb414892435c016486585c26022cafdab68Roshan Pius            } else {
650e33a4bb414892435c016486585c26022cafdab68Roshan Pius                XmlUtil.writeNextValue(
651e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        out, XML_TAG_GATEWAY_ADDRESS, null);
652e33a4bb414892435c016486585c26022cafdab68Roshan Pius
653e33a4bb414892435c016486585c26022cafdab68Roshan Pius            }
654e33a4bb414892435c016486585c26022cafdab68Roshan Pius            if (staticIpConfiguration.dnsServers != null) {
655e33a4bb414892435c016486585c26022cafdab68Roshan Pius                // Create a string array of DNS server addresses
656e33a4bb414892435c016486585c26022cafdab68Roshan Pius                String[] dnsServers = new String[staticIpConfiguration.dnsServers.size()];
657e33a4bb414892435c016486585c26022cafdab68Roshan Pius                int dnsServerIdx = 0;
658e33a4bb414892435c016486585c26022cafdab68Roshan Pius                for (InetAddress inetAddr : staticIpConfiguration.dnsServers) {
659e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    dnsServers[dnsServerIdx++] = inetAddr.getHostAddress();
660e33a4bb414892435c016486585c26022cafdab68Roshan Pius                }
661e33a4bb414892435c016486585c26022cafdab68Roshan Pius                XmlUtil.writeNextValue(
662e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        out, XML_TAG_DNS_SERVER_ADDRESSES, dnsServers);
663e33a4bb414892435c016486585c26022cafdab68Roshan Pius            } else {
664e33a4bb414892435c016486585c26022cafdab68Roshan Pius                XmlUtil.writeNextValue(
665e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        out, XML_TAG_DNS_SERVER_ADDRESSES, null);
666e33a4bb414892435c016486585c26022cafdab68Roshan Pius            }
667e33a4bb414892435c016486585c26022cafdab68Roshan Pius        }
668e33a4bb414892435c016486585c26022cafdab68Roshan Pius
669e33a4bb414892435c016486585c26022cafdab68Roshan Pius        /**
670e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * Write the IP configuration data elements from the provided Configuration to the XML
671e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * stream.
672642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         *
673642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         * @param out             XmlSerializer instance pointing to the XML stream.
674642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         * @param ipConfiguration IpConfiguration object to be serialized.
675e33a4bb414892435c016486585c26022cafdab68Roshan Pius         */
676c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius        public static void writeToXml(XmlSerializer out, IpConfiguration ipConfiguration)
677e33a4bb414892435c016486585c26022cafdab68Roshan Pius                throws XmlPullParserException, IOException {
678e33a4bb414892435c016486585c26022cafdab68Roshan Pius            // Write IP assignment settings
679e33a4bb414892435c016486585c26022cafdab68Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_IP_ASSIGNMENT,
680e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    ipConfiguration.ipAssignment.toString());
681e33a4bb414892435c016486585c26022cafdab68Roshan Pius            switch (ipConfiguration.ipAssignment) {
682e33a4bb414892435c016486585c26022cafdab68Roshan Pius                case STATIC:
683e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    writeStaticIpConfigurationToXml(
684e33a4bb414892435c016486585c26022cafdab68Roshan Pius                            out, ipConfiguration.getStaticIpConfiguration());
685e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    break;
686e33a4bb414892435c016486585c26022cafdab68Roshan Pius                default:
687e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    break;
688e33a4bb414892435c016486585c26022cafdab68Roshan Pius            }
689e33a4bb414892435c016486585c26022cafdab68Roshan Pius
690e33a4bb414892435c016486585c26022cafdab68Roshan Pius            // Write proxy settings
691e33a4bb414892435c016486585c26022cafdab68Roshan Pius            XmlUtil.writeNextValue(
692e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    out, XML_TAG_PROXY_SETTINGS,
693e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    ipConfiguration.proxySettings.toString());
694e33a4bb414892435c016486585c26022cafdab68Roshan Pius            switch (ipConfiguration.proxySettings) {
695e33a4bb414892435c016486585c26022cafdab68Roshan Pius                case STATIC:
696e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    XmlUtil.writeNextValue(
697e33a4bb414892435c016486585c26022cafdab68Roshan Pius                            out, XML_TAG_PROXY_HOST,
698e33a4bb414892435c016486585c26022cafdab68Roshan Pius                            ipConfiguration.httpProxy.getHost());
699e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    XmlUtil.writeNextValue(
700e33a4bb414892435c016486585c26022cafdab68Roshan Pius                            out, XML_TAG_PROXY_PORT,
701e33a4bb414892435c016486585c26022cafdab68Roshan Pius                            ipConfiguration.httpProxy.getPort());
702e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    XmlUtil.writeNextValue(
703e33a4bb414892435c016486585c26022cafdab68Roshan Pius                            out, XML_TAG_PROXY_EXCLUSION_LIST,
704e33a4bb414892435c016486585c26022cafdab68Roshan Pius                            ipConfiguration.httpProxy.getExclusionListAsString());
705e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    break;
706e33a4bb414892435c016486585c26022cafdab68Roshan Pius                case PAC:
707e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    XmlUtil.writeNextValue(
708e33a4bb414892435c016486585c26022cafdab68Roshan Pius                            out, XML_TAG_PROXY_PAC_FILE,
709e33a4bb414892435c016486585c26022cafdab68Roshan Pius                            ipConfiguration.httpProxy.getPacFileUrl().toString());
710e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    break;
711e33a4bb414892435c016486585c26022cafdab68Roshan Pius                default:
712e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    break;
713e33a4bb414892435c016486585c26022cafdab68Roshan Pius            }
714e33a4bb414892435c016486585c26022cafdab68Roshan Pius        }
715e33a4bb414892435c016486585c26022cafdab68Roshan Pius
716e33a4bb414892435c016486585c26022cafdab68Roshan Pius        /**
717e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * Parse out the static IP configuration from the XML stream.
718e33a4bb414892435c016486585c26022cafdab68Roshan Pius         */
719e33a4bb414892435c016486585c26022cafdab68Roshan Pius        private static StaticIpConfiguration parseStaticIpConfigurationFromXml(XmlPullParser in)
720e33a4bb414892435c016486585c26022cafdab68Roshan Pius                throws XmlPullParserException, IOException {
721e33a4bb414892435c016486585c26022cafdab68Roshan Pius            StaticIpConfiguration staticIpConfiguration = new StaticIpConfiguration();
722e33a4bb414892435c016486585c26022cafdab68Roshan Pius
723e33a4bb414892435c016486585c26022cafdab68Roshan Pius            String linkAddressString =
724e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    (String) XmlUtil.readNextValueWithName(in, XML_TAG_LINK_ADDRESS);
725e33a4bb414892435c016486585c26022cafdab68Roshan Pius            Integer linkPrefixLength =
726e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    (Integer) XmlUtil.readNextValueWithName(in, XML_TAG_LINK_PREFIX_LENGTH);
727e33a4bb414892435c016486585c26022cafdab68Roshan Pius            if (linkAddressString != null && linkPrefixLength != null) {
728e33a4bb414892435c016486585c26022cafdab68Roshan Pius                LinkAddress linkAddress = new LinkAddress(
729e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        NetworkUtils.numericToInetAddress(linkAddressString),
730e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        linkPrefixLength);
731e33a4bb414892435c016486585c26022cafdab68Roshan Pius                if (linkAddress.getAddress() instanceof Inet4Address) {
732e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    staticIpConfiguration.ipAddress = linkAddress;
733e33a4bb414892435c016486585c26022cafdab68Roshan Pius                } else {
734e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    Log.w(TAG, "Non-IPv4 address: " + linkAddress);
735e33a4bb414892435c016486585c26022cafdab68Roshan Pius                }
736e33a4bb414892435c016486585c26022cafdab68Roshan Pius            }
737e33a4bb414892435c016486585c26022cafdab68Roshan Pius            String gatewayAddressString =
738e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    (String) XmlUtil.readNextValueWithName(in, XML_TAG_GATEWAY_ADDRESS);
739e33a4bb414892435c016486585c26022cafdab68Roshan Pius            if (gatewayAddressString != null) {
740e33a4bb414892435c016486585c26022cafdab68Roshan Pius                LinkAddress dest = null;
741e33a4bb414892435c016486585c26022cafdab68Roshan Pius                InetAddress gateway =
742e33a4bb414892435c016486585c26022cafdab68Roshan Pius                        NetworkUtils.numericToInetAddress(gatewayAddressString);
743e33a4bb414892435c016486585c26022cafdab68Roshan Pius                RouteInfo route = new RouteInfo(dest, gateway);
744e33a4bb414892435c016486585c26022cafdab68Roshan Pius                if (route.isIPv4Default()) {
745e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    staticIpConfiguration.gateway = gateway;
746e33a4bb414892435c016486585c26022cafdab68Roshan Pius                } else {
747e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    Log.w(TAG, "Non-IPv4 default route: " + route);
748e33a4bb414892435c016486585c26022cafdab68Roshan Pius                }
749e33a4bb414892435c016486585c26022cafdab68Roshan Pius            }
750e33a4bb414892435c016486585c26022cafdab68Roshan Pius            String[] dnsServerAddressesString =
751e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    (String[]) XmlUtil.readNextValueWithName(in, XML_TAG_DNS_SERVER_ADDRESSES);
752e33a4bb414892435c016486585c26022cafdab68Roshan Pius            if (dnsServerAddressesString != null) {
753e33a4bb414892435c016486585c26022cafdab68Roshan Pius                for (String dnsServerAddressString : dnsServerAddressesString) {
754e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    InetAddress dnsServerAddress =
755e33a4bb414892435c016486585c26022cafdab68Roshan Pius                            NetworkUtils.numericToInetAddress(dnsServerAddressString);
756e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    staticIpConfiguration.dnsServers.add(dnsServerAddress);
757e33a4bb414892435c016486585c26022cafdab68Roshan Pius                }
758e33a4bb414892435c016486585c26022cafdab68Roshan Pius            }
759e33a4bb414892435c016486585c26022cafdab68Roshan Pius            return staticIpConfiguration;
760e33a4bb414892435c016486585c26022cafdab68Roshan Pius        }
761e33a4bb414892435c016486585c26022cafdab68Roshan Pius
762e33a4bb414892435c016486585c26022cafdab68Roshan Pius        /**
7635d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius         * Parses the IP configuration data elements from the provided XML stream to an
7645d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius         * IpConfiguration object.
765e33a4bb414892435c016486585c26022cafdab68Roshan Pius         *
766e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * @param in            XmlPullParser instance pointing to the XML stream.
767e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * @param outerTagDepth depth of the outer tag in the XML document.
768e33a4bb414892435c016486585c26022cafdab68Roshan Pius         * @return IpConfiguration object if parsing is successful, null otherwise.
769e33a4bb414892435c016486585c26022cafdab68Roshan Pius         */
770c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius        public static IpConfiguration parseFromXml(XmlPullParser in, int outerTagDepth)
771e33a4bb414892435c016486585c26022cafdab68Roshan Pius                throws XmlPullParserException, IOException {
772e33a4bb414892435c016486585c26022cafdab68Roshan Pius            IpConfiguration ipConfiguration = new IpConfiguration();
773e33a4bb414892435c016486585c26022cafdab68Roshan Pius
774e33a4bb414892435c016486585c26022cafdab68Roshan Pius            // Parse out the IP assignment info first.
775e33a4bb414892435c016486585c26022cafdab68Roshan Pius            String ipAssignmentString =
776e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    (String) XmlUtil.readNextValueWithName(in, XML_TAG_IP_ASSIGNMENT);
777e33a4bb414892435c016486585c26022cafdab68Roshan Pius            IpAssignment ipAssignment = IpAssignment.valueOf(ipAssignmentString);
778e33a4bb414892435c016486585c26022cafdab68Roshan Pius            ipConfiguration.setIpAssignment(ipAssignment);
779e33a4bb414892435c016486585c26022cafdab68Roshan Pius            switch (ipAssignment) {
780e33a4bb414892435c016486585c26022cafdab68Roshan Pius                case STATIC:
781e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    ipConfiguration.setStaticIpConfiguration(parseStaticIpConfigurationFromXml(in));
782e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    break;
783e33a4bb414892435c016486585c26022cafdab68Roshan Pius                case DHCP:
784e33a4bb414892435c016486585c26022cafdab68Roshan Pius                case UNASSIGNED:
785e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    break;
786e33a4bb414892435c016486585c26022cafdab68Roshan Pius                default:
787c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius                    throw new XmlPullParserException("Unknown ip assignment type: " + ipAssignment);
788e33a4bb414892435c016486585c26022cafdab68Roshan Pius            }
789e33a4bb414892435c016486585c26022cafdab68Roshan Pius
790e33a4bb414892435c016486585c26022cafdab68Roshan Pius            // Parse out the proxy settings next.
791e33a4bb414892435c016486585c26022cafdab68Roshan Pius            String proxySettingsString =
792e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    (String) XmlUtil.readNextValueWithName(in, XML_TAG_PROXY_SETTINGS);
793e33a4bb414892435c016486585c26022cafdab68Roshan Pius            ProxySettings proxySettings = ProxySettings.valueOf(proxySettingsString);
794e33a4bb414892435c016486585c26022cafdab68Roshan Pius            ipConfiguration.setProxySettings(proxySettings);
795e33a4bb414892435c016486585c26022cafdab68Roshan Pius            switch (proxySettings) {
796e33a4bb414892435c016486585c26022cafdab68Roshan Pius                case STATIC:
797e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    String proxyHost =
798e33a4bb414892435c016486585c26022cafdab68Roshan Pius                            (String) XmlUtil.readNextValueWithName(in, XML_TAG_PROXY_HOST);
799e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    int proxyPort =
800e33a4bb414892435c016486585c26022cafdab68Roshan Pius                            (int) XmlUtil.readNextValueWithName(in, XML_TAG_PROXY_PORT);
801e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    String proxyExclusionList =
802e33a4bb414892435c016486585c26022cafdab68Roshan Pius                            (String) XmlUtil.readNextValueWithName(
803e33a4bb414892435c016486585c26022cafdab68Roshan Pius                                    in, XML_TAG_PROXY_EXCLUSION_LIST);
804e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    ipConfiguration.setHttpProxy(
805e33a4bb414892435c016486585c26022cafdab68Roshan Pius                            new ProxyInfo(proxyHost, proxyPort, proxyExclusionList));
806e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    break;
807e33a4bb414892435c016486585c26022cafdab68Roshan Pius                case PAC:
808e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    String proxyPacFile =
809e33a4bb414892435c016486585c26022cafdab68Roshan Pius                            (String) XmlUtil.readNextValueWithName(in, XML_TAG_PROXY_PAC_FILE);
810e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    ipConfiguration.setHttpProxy(new ProxyInfo(proxyPacFile));
811e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    break;
812e33a4bb414892435c016486585c26022cafdab68Roshan Pius                case NONE:
813e33a4bb414892435c016486585c26022cafdab68Roshan Pius                case UNASSIGNED:
814e33a4bb414892435c016486585c26022cafdab68Roshan Pius                    break;
815e33a4bb414892435c016486585c26022cafdab68Roshan Pius                default:
816c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius                    throw new XmlPullParserException(
817c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius                            "Unknown proxy settings type: " + proxySettings);
818e33a4bb414892435c016486585c26022cafdab68Roshan Pius            }
819e33a4bb414892435c016486585c26022cafdab68Roshan Pius            return ipConfiguration;
820e33a4bb414892435c016486585c26022cafdab68Roshan Pius        }
821e33a4bb414892435c016486585c26022cafdab68Roshan Pius    }
8225d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius
8235d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius    /**
8245d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius     * Utility class to serialize and deseriaize {@link NetworkSelectionStatus} object to XML &
8255d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius     * vice versa. This is used by {@link com.android.server.wifi.WifiConfigStore} module.
8265d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius     */
8275d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius    public static class NetworkSelectionStatusXmlUtil {
8285d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius
8295d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius        /**
8305d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius         * List of XML tags corresponding to NetworkSelectionStatus object elements.
8315d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius         */
8325d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius        public static final String XML_TAG_SELECTION_STATUS = "SelectionStatus";
8335d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius        public static final String XML_TAG_DISABLE_REASON = "DisableReason";
8345d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius        public static final String XML_TAG_CONNECT_CHOICE = "ConnectChoice";
8355d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius        public static final String XML_TAG_CONNECT_CHOICE_TIMESTAMP = "ConnectChoiceTimeStamp";
8365d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius        public static final String XML_TAG_HAS_EVER_CONNECTED = "HasEverConnected";
8375d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius
8385d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius        /**
8395d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius         * Write the NetworkSelectionStatus data elements from the provided status to the XML
8405d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius         * stream.
841642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         *
8420e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius         * @param out             XmlSerializer instance pointing to the XML stream.
8430e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius         * @param selectionStatus NetworkSelectionStatus object to be serialized.
8445d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius         */
8450e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius        public static void writeToXml(XmlSerializer out, NetworkSelectionStatus selectionStatus)
8465d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                throws XmlPullParserException, IOException {
8475d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius            XmlUtil.writeNextValue(
8480e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                    out, XML_TAG_SELECTION_STATUS, selectionStatus.getNetworkStatusString());
8490e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            XmlUtil.writeNextValue(
8500e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                    out, XML_TAG_DISABLE_REASON, selectionStatus.getNetworkDisableReasonString());
8510e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_CONNECT_CHOICE, selectionStatus.getConnectChoice());
8525d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius            XmlUtil.writeNextValue(
8530e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                    out, XML_TAG_CONNECT_CHOICE_TIMESTAMP,
8540e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                    selectionStatus.getConnectChoiceTimestamp());
8550e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            XmlUtil.writeNextValue(
8560e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                    out, XML_TAG_HAS_EVER_CONNECTED, selectionStatus.getHasEverConnected());
8575d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius        }
8585d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius
8595d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius        /**
8605d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius         * Parses the NetworkSelectionStatus data elements from the provided XML stream to a
8615d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius         * NetworkSelectionStatus object.
8625d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius         *
8635d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius         * @param in            XmlPullParser instance pointing to the XML stream.
8645d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius         * @param outerTagDepth depth of the outer tag in the XML document.
8655d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius         * @return NetworkSelectionStatus object if parsing is successful, null otherwise.
8665d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius         */
867c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius        public static NetworkSelectionStatus parseFromXml(XmlPullParser in, int outerTagDepth)
8685d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                throws XmlPullParserException, IOException {
8690e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            NetworkSelectionStatus selectionStatus = new NetworkSelectionStatus();
8700e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            String statusString = "";
8710e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            String disableReasonString = "";
8725d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius
8735d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius            // Loop through and parse out all the elements from the stream within this section.
8745d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius            while (!XmlUtil.isNextSectionEnd(in, outerTagDepth)) {
8755d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                String[] valueName = new String[1];
8765d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                Object value = XmlUtil.readCurrentValue(in, valueName);
8775d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                if (valueName[0] == null) {
878c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius                    throw new XmlPullParserException("Missing value name");
8795d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                }
8805d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                switch (valueName[0]) {
8815d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                    case XML_TAG_SELECTION_STATUS:
8820e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                        statusString = (String) value;
8835d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                        break;
8845d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                    case XML_TAG_DISABLE_REASON:
8850e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                        disableReasonString = (String) value;
8865d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                        break;
8875d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                    case XML_TAG_CONNECT_CHOICE:
8880e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                        selectionStatus.setConnectChoice((String) value);
8895d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                        break;
8905d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                    case XML_TAG_CONNECT_CHOICE_TIMESTAMP:
8910e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                        selectionStatus.setConnectChoiceTimestamp((long) value);
8925d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                        break;
8935d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                    case XML_TAG_HAS_EVER_CONNECTED:
8940e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                        selectionStatus.setHasEverConnected((boolean) value);
8955d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                        break;
8965d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                    default:
897c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius                        throw new XmlPullParserException(
898c00a9331ab2a51babc3c3acd69f44be3d341c4b9Roshan Pius                                "Unknown value name found: " + valueName[0]);
8995d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius                }
9005d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius            }
9010e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            // Now figure out the network selection status codes from |selectionStatusString| &
9020e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            // |disableReasonString|.
9030e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            int status =
9040e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                    Arrays.asList(NetworkSelectionStatus.QUALITY_NETWORK_SELECTION_STATUS)
9050e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                            .indexOf(statusString);
9060e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            int disableReason =
9070e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                    Arrays.asList(NetworkSelectionStatus.QUALITY_NETWORK_SELECTION_DISABLE_REASON)
9080e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                            .indexOf(disableReasonString);
9090e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius
9100e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            // If either of the above codes are invalid or if the network was temporarily disabled
9110e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            // (blacklisted), restore the status as enabled. We don't want to persist blacklists
9120e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            // across reboots.
9130e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            if (status == -1 || disableReason == -1 ||
9140e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                    status == NetworkSelectionStatus.NETWORK_SELECTION_TEMPORARY_DISABLED) {
9150e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                status = NetworkSelectionStatus.NETWORK_SELECTION_ENABLED;
9160e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius                disableReason = NetworkSelectionStatus.NETWORK_SELECTION_ENABLE;
9170e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            }
9180e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            selectionStatus.setNetworkSelectionStatus(status);
9190e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            selectionStatus.setNetworkSelectionDisableReason(disableReason);
9200e2540a1c1ba3c541a229b039a90789f93c41ee7Roshan Pius            return selectionStatus;
9215d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius        }
9225d3609b1931180c37d7292619146ad7d33df9a21Roshan Pius    }
923642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius
924642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius    /**
925642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius     * Utility class to serialize and deseriaize {@link WifiEnterpriseConfig} object to XML &
926642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius     * vice versa. This is used by {@link com.android.server.wifi.WifiConfigStore} module.
927642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius     */
928642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius    public static class WifiEnterpriseConfigXmlUtil {
929642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius
930642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        /**
931642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         * List of XML tags corresponding to WifiEnterpriseConfig object elements.
932642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         */
933642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        public static final String XML_TAG_IDENTITY = "Identity";
934642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        public static final String XML_TAG_ANON_IDENTITY = "AnonIdentity";
935642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        public static final String XML_TAG_PASSWORD = "Password";
936642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        public static final String XML_TAG_CLIENT_CERT = "ClientCert";
937642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        public static final String XML_TAG_CA_CERT = "CaCert";
938642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        public static final String XML_TAG_SUBJECT_MATCH = "SubjectMatch";
939642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        public static final String XML_TAG_ENGINE = "Engine";
940642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        public static final String XML_TAG_ENGINE_ID = "EngineId";
941642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        public static final String XML_TAG_PRIVATE_KEY_ID = "PrivateKeyId";
942642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        public static final String XML_TAG_ALT_SUBJECT_MATCH = "AltSubjectMatch";
943642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        public static final String XML_TAG_DOM_SUFFIX_MATCH = "DomSuffixMatch";
944642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        public static final String XML_TAG_CA_PATH = "CaPath";
945642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        public static final String XML_TAG_EAP_METHOD = "EapMethod";
946642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        public static final String XML_TAG_PHASE2_METHOD = "Phase2Method";
947642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius
948642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        /**
949642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         * Write the WifiEnterpriseConfig data elements from the provided config to the XML
950642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         * stream.
951642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         *
952642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         * @param out              XmlSerializer instance pointing to the XML stream.
953642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         * @param enterpriseConfig WifiEnterpriseConfig object to be serialized.
954642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         */
955642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        public static void writeToXml(XmlSerializer out, WifiEnterpriseConfig enterpriseConfig)
956642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                throws XmlPullParserException, IOException {
957642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_IDENTITY,
958d83ef7d8c4948afc328d8ef0e746b32c195f271eRoshan Pius                    enterpriseConfig.getFieldValue(WifiEnterpriseConfig.IDENTITY_KEY));
959642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_ANON_IDENTITY,
960d83ef7d8c4948afc328d8ef0e746b32c195f271eRoshan Pius                    enterpriseConfig.getFieldValue(WifiEnterpriseConfig.ANON_IDENTITY_KEY));
961642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_PASSWORD,
962d83ef7d8c4948afc328d8ef0e746b32c195f271eRoshan Pius                    enterpriseConfig.getFieldValue(WifiEnterpriseConfig.PASSWORD_KEY));
963642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_CLIENT_CERT,
964d83ef7d8c4948afc328d8ef0e746b32c195f271eRoshan Pius                    enterpriseConfig.getFieldValue(WifiEnterpriseConfig.CLIENT_CERT_KEY));
965642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_CA_CERT,
966d83ef7d8c4948afc328d8ef0e746b32c195f271eRoshan Pius                    enterpriseConfig.getFieldValue(WifiEnterpriseConfig.CA_CERT_KEY));
967642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_SUBJECT_MATCH,
968d83ef7d8c4948afc328d8ef0e746b32c195f271eRoshan Pius                    enterpriseConfig.getFieldValue(WifiEnterpriseConfig.SUBJECT_MATCH_KEY));
969642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_ENGINE,
970d83ef7d8c4948afc328d8ef0e746b32c195f271eRoshan Pius                    enterpriseConfig.getFieldValue(WifiEnterpriseConfig.ENGINE_KEY));
971642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_ENGINE_ID,
972d83ef7d8c4948afc328d8ef0e746b32c195f271eRoshan Pius                    enterpriseConfig.getFieldValue(WifiEnterpriseConfig.ENGINE_ID_KEY));
973642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_PRIVATE_KEY_ID,
974d83ef7d8c4948afc328d8ef0e746b32c195f271eRoshan Pius                    enterpriseConfig.getFieldValue(WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY));
975642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_ALT_SUBJECT_MATCH,
976d83ef7d8c4948afc328d8ef0e746b32c195f271eRoshan Pius                    enterpriseConfig.getFieldValue(WifiEnterpriseConfig.ALTSUBJECT_MATCH_KEY));
977642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_DOM_SUFFIX_MATCH,
978d83ef7d8c4948afc328d8ef0e746b32c195f271eRoshan Pius                    enterpriseConfig.getFieldValue(WifiEnterpriseConfig.DOM_SUFFIX_MATCH_KEY));
979642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_CA_PATH,
980d83ef7d8c4948afc328d8ef0e746b32c195f271eRoshan Pius                    enterpriseConfig.getFieldValue(WifiEnterpriseConfig.CA_PATH_KEY));
981642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_EAP_METHOD, enterpriseConfig.getEapMethod());
982642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            XmlUtil.writeNextValue(out, XML_TAG_PHASE2_METHOD, enterpriseConfig.getPhase2Method());
983642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        }
984642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius
985642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        /**
986642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         * Parses the data elements from the provided XML stream to a WifiEnterpriseConfig object.
987642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         *
988642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         * @param in            XmlPullParser instance pointing to the XML stream.
989642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         * @param outerTagDepth depth of the outer tag in the XML document.
990642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         * @return WifiEnterpriseConfig object if parsing is successful, null otherwise.
991642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius         */
992642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        public static WifiEnterpriseConfig parseFromXml(XmlPullParser in, int outerTagDepth)
993642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                throws XmlPullParserException, IOException {
994642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig();
995642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius
996642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            // Loop through and parse out all the elements from the stream within this section.
997642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            while (!XmlUtil.isNextSectionEnd(in, outerTagDepth)) {
998642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                String[] valueName = new String[1];
999642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                Object value = XmlUtil.readCurrentValue(in, valueName);
1000642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                if (valueName[0] == null) {
1001642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                    throw new XmlPullParserException("Missing value name");
1002642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                }
1003642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                switch (valueName[0]) {
1004642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                    case XML_TAG_IDENTITY:
1005642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        enterpriseConfig.setFieldValue(
1006642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                                WifiEnterpriseConfig.IDENTITY_KEY, (String) value);
1007642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        break;
1008642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                    case XML_TAG_ANON_IDENTITY:
1009642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        enterpriseConfig.setFieldValue(
1010642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                                WifiEnterpriseConfig.ANON_IDENTITY_KEY, (String) value);
1011642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        break;
1012642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                    case XML_TAG_PASSWORD:
1013642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        enterpriseConfig.setFieldValue(
1014642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                                WifiEnterpriseConfig.PASSWORD_KEY, (String) value);
1015642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        break;
1016642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                    case XML_TAG_CLIENT_CERT:
1017642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        enterpriseConfig.setFieldValue(
1018642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                                WifiEnterpriseConfig.CLIENT_CERT_KEY, (String) value);
1019642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        break;
1020642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                    case XML_TAG_CA_CERT:
1021642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        enterpriseConfig.setFieldValue(
1022642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                                WifiEnterpriseConfig.CA_CERT_KEY, (String) value);
1023642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        break;
1024642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                    case XML_TAG_SUBJECT_MATCH:
1025642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        enterpriseConfig.setFieldValue(
1026642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                                WifiEnterpriseConfig.SUBJECT_MATCH_KEY, (String) value);
1027642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        break;
1028642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                    case XML_TAG_ENGINE:
1029642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        enterpriseConfig.setFieldValue(
1030642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                                WifiEnterpriseConfig.ENGINE_KEY, (String) value);
1031642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        break;
1032642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                    case XML_TAG_ENGINE_ID:
1033642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        enterpriseConfig.setFieldValue(
1034642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                                WifiEnterpriseConfig.ENGINE_ID_KEY, (String) value);
1035642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        break;
1036642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                    case XML_TAG_PRIVATE_KEY_ID:
1037642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        enterpriseConfig.setFieldValue(
1038642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                                WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY, (String) value);
1039642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        break;
1040642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                    case XML_TAG_ALT_SUBJECT_MATCH:
1041642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        enterpriseConfig.setFieldValue(
1042642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                                WifiEnterpriseConfig.ALTSUBJECT_MATCH_KEY, (String) value);
1043642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        break;
1044642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                    case XML_TAG_DOM_SUFFIX_MATCH:
1045642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        enterpriseConfig.setFieldValue(
1046642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                                WifiEnterpriseConfig.DOM_SUFFIX_MATCH_KEY, (String) value);
1047642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        break;
1048642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                    case XML_TAG_CA_PATH:
1049642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        enterpriseConfig.setFieldValue(
1050642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                                WifiEnterpriseConfig.CA_PATH_KEY, (String) value);
1051642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        break;
1052642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                    case XML_TAG_EAP_METHOD:
1053642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        enterpriseConfig.setEapMethod((int) value);
1054642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        break;
1055642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                    case XML_TAG_PHASE2_METHOD:
1056642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        enterpriseConfig.setPhase2Method((int) value);
1057642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        break;
1058642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                    default:
1059642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                        throw new XmlPullParserException(
1060642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                                "Unknown value name found: " + valueName[0]);
1061642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius                }
1062642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            }
1063642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius            return enterpriseConfig;
1064642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius        }
1065642b0bb43ed856bac0503d3169d67026de2c1b02Roshan Pius    }
10662b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius}
10672b7e486db94455a4a4c0124b5ba5e57c837ef10eRoshan Pius
1068