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 
17 package com.android.server.wifi;
18 
19 import android.annotation.Nullable;
20 import android.text.TextUtils;
21 
22 import com.android.server.wifi.util.WifiConfigStoreEncryptionUtil;
23 import com.android.server.wifi.util.XmlUtil;
24 
25 import org.xmlpull.v1.XmlPullParser;
26 import org.xmlpull.v1.XmlPullParserException;
27 import org.xmlpull.v1.XmlSerializer;
28 
29 import java.io.IOException;
30 import java.util.HashSet;
31 import java.util.Set;
32 
33 /**
34  * Store data for network notifiers.
35  *
36  * Below are the current configuration data for each respective store file:
37  *
38  * User Store (user specific configurations)
39  * - Set of blacklisted SSIDs
40  */
41 public class SsidSetStoreData implements WifiConfigStore.StoreData {
42     private static final String XML_TAG_SECTION_HEADER_SUFFIX = "ConfigData";
43     private static final String XML_TAG_SSID_SET = "SSIDSet";
44 
45     private final String mTagName;
46     private final DataSource mDataSource;
47 
48     /**
49      * Interface define the data source for the notifier store data.
50      */
51     public interface DataSource {
52         /**
53          * Retrieve the SSID set from the data source.
54          *
55          * @return Set of SSIDs
56          */
getSsids()57         Set<String> getSsids();
58 
59         /**
60          * Update the SSID set in the data source.
61          *
62          * @param ssidSet The set of SSIDs
63          */
setSsids(Set<String> ssidSet)64         void setSsids(Set<String> ssidSet);
65     }
66 
67     /**
68      * Creates the SSID Set store data.
69      *
70      * @param name Identifier of the SSID set.
71      * @param dataSource The DataSource that implements the update and retrieval of the SSID set.
72      */
SsidSetStoreData(String name, DataSource dataSource)73     SsidSetStoreData(String name, DataSource dataSource) {
74         mTagName = name + XML_TAG_SECTION_HEADER_SUFFIX;
75         mDataSource = dataSource;
76     }
77 
78     @Override
serializeData(XmlSerializer out, @Nullable WifiConfigStoreEncryptionUtil encryptionUtil)79     public void serializeData(XmlSerializer out,
80             @Nullable WifiConfigStoreEncryptionUtil encryptionUtil)
81             throws XmlPullParserException, IOException {
82         Set<String> ssidSet = mDataSource.getSsids();
83         if (ssidSet != null && !ssidSet.isEmpty()) {
84             XmlUtil.writeNextValue(out, XML_TAG_SSID_SET, mDataSource.getSsids());
85         }
86     }
87 
88     @Override
deserializeData(XmlPullParser in, int outerTagDepth, @WifiConfigStore.Version int version, @Nullable WifiConfigStoreEncryptionUtil encryptionUtil)89     public void deserializeData(XmlPullParser in, int outerTagDepth,
90             @WifiConfigStore.Version int version,
91             @Nullable WifiConfigStoreEncryptionUtil encryptionUtil)
92             throws XmlPullParserException, IOException {
93         // Ignore empty reads.
94         if (in == null) {
95             return;
96         }
97         while (!XmlUtil.isNextSectionEnd(in, outerTagDepth)) {
98             String[] valueName = new String[1];
99             Object value = XmlUtil.readCurrentValue(in, valueName);
100             if (TextUtils.isEmpty(valueName[0])) {
101                 throw new XmlPullParserException("Missing value name");
102             }
103             switch (valueName[0]) {
104                 case XML_TAG_SSID_SET:
105                     mDataSource.setSsids((Set<String>) value);
106                     break;
107                 default:
108                     throw new XmlPullParserException("Unknown tag under "
109                             + mTagName + ": " + valueName[0]);
110             }
111         }
112     }
113 
114     @Override
resetData()115     public void resetData() {
116         mDataSource.setSsids(new HashSet<>());
117     }
118 
119     @Override
hasNewDataToSerialize()120     public boolean hasNewDataToSerialize() {
121         // always persist.
122         return true;
123     }
124 
125     @Override
getName()126     public String getName() {
127         return mTagName;
128     }
129 
130     @Override
getStoreFileId()131     public @WifiConfigStore.StoreFileId int getStoreFileId() {
132         // Shared general store.
133         return WifiConfigStore.STORE_FILE_USER_GENERAL;
134     }
135 }
136