SsidSetStoreData.java revision b280c399d7be7d04a245c5fc2d2897f0ddc9cc0a
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wifi;
18
19import android.text.TextUtils;
20
21import com.android.server.wifi.util.XmlUtil;
22
23import org.xmlpull.v1.XmlPullParser;
24import org.xmlpull.v1.XmlPullParserException;
25import org.xmlpull.v1.XmlSerializer;
26
27import java.io.IOException;
28import java.util.HashSet;
29import java.util.Set;
30
31/**
32 * Store data for network notifiers.
33 *
34 * Below are the current configuration data for each respective store file:
35 *
36 * Share Store (system wide configurations)
37 * - No data
38 *
39 * User Store (user specific configurations)
40 * - Set of blacklisted SSIDs
41 */
42public class SsidSetStoreData implements WifiConfigStore.StoreData {
43    private static final String XML_TAG_SECTION_HEADER_SUFFIX = "ConfigData";
44    private static final String XML_TAG_SSID_SET = "SSIDSet";
45
46    private final String mTagName;
47    private final DataSource mDataSource;
48
49    /**
50     * Interface define the data source for the notifier store data.
51     */
52    public interface DataSource {
53        /**
54         * Retrieve the SSID set from the data source.
55         *
56         * @return Set of SSIDs
57         */
58        Set<String> getSsids();
59
60        /**
61         * Update the SSID set in the data source.
62         *
63         * @param ssidSet The set of SSIDs
64         */
65        void setSsids(Set<String> ssidSet);
66    }
67
68    /**
69     * Creates the SSID Set store data.
70     *
71     * @param name Identifier of the SSID set.
72     * @param dataSource The DataSource that implements the update and retrieval of the SSID set.
73     */
74    SsidSetStoreData(String name, DataSource dataSource) {
75        mTagName = name + XML_TAG_SECTION_HEADER_SUFFIX;
76        mDataSource = dataSource;
77    }
78
79    @Override
80    public void serializeData(XmlSerializer out, boolean shared)
81            throws XmlPullParserException, IOException {
82        if (shared) {
83            throw new XmlPullParserException("Share data not supported");
84        }
85        Set<String> ssidSet = mDataSource.getSsids();
86        if (ssidSet != null && !ssidSet.isEmpty()) {
87            XmlUtil.writeNextValue(out, XML_TAG_SSID_SET, mDataSource.getSsids());
88        }
89    }
90
91    @Override
92    public void deserializeData(XmlPullParser in, int outerTagDepth, boolean shared)
93            throws XmlPullParserException, IOException {
94        if (shared) {
95            throw new XmlPullParserException("Share data not supported");
96        }
97
98        while (!XmlUtil.isNextSectionEnd(in, outerTagDepth)) {
99            String[] valueName = new String[1];
100            Object value = XmlUtil.readCurrentValue(in, valueName);
101            if (TextUtils.isEmpty(valueName[0])) {
102                throw new XmlPullParserException("Missing value name");
103            }
104            switch (valueName[0]) {
105                case XML_TAG_SSID_SET:
106                    mDataSource.setSsids((Set<String>) value);
107                    break;
108                default:
109                    throw new XmlPullParserException("Unknown tag under "
110                            + mTagName + ": " + valueName[0]);
111            }
112        }
113    }
114
115    @Override
116    public void resetData(boolean shared) {
117        if (!shared) {
118            mDataSource.setSsids(new HashSet<>());
119        }
120    }
121
122    @Override
123    public String getName() {
124        return mTagName;
125    }
126
127    @Override
128    public boolean supportShareData() {
129        return false;
130    }
131}
132