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