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