summaryrefslogtreecommitdiffstats
path: root/service/java/com/android/server/wifi/NetworkSuggestionStoreData.java
blob: 9627a9daaaf7639806b70a487c279cb427d9dae9 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * Copyright (C) 2018 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.net.wifi.WifiConfiguration;
import android.net.wifi.WifiEnterpriseConfig;
import android.net.wifi.WifiNetworkSuggestion;
import android.os.Process;
import android.util.Log;
import android.util.Pair;

import com.android.internal.util.XmlUtils;
import com.android.server.wifi.WifiNetworkSuggestionsManager.ExtendedWifiNetworkSuggestion;
import com.android.server.wifi.WifiNetworkSuggestionsManager.PerAppInfo;
import com.android.server.wifi.util.XmlUtil;
import com.android.server.wifi.util.XmlUtil.WifiConfigurationXmlUtil;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * This class performs serialization and parsing of XML data block that contain the list of WiFi
 * network suggestions.
 */
public class NetworkSuggestionStoreData implements WifiConfigStore.StoreData {
    private static final String TAG = "NetworkSuggestionStoreData";

    private static final String XML_TAG_SECTION_HEADER_NETWORK_SUGGESTION_MAP =
            "NetworkSuggestionMap";
    private static final String XML_TAG_SECTION_HEADER_NETWORK_SUGGESTION_PER_APP =
            "NetworkSuggestionPerApp";
    private static final String XML_TAG_SECTION_HEADER_NETWORK_SUGGESTION = "NetworkSuggestion";
    private static final String XML_TAG_SECTION_HEADER_WIFI_CONFIGURATION = "WifiConfiguration";
    private static final String XML_TAG_SECTION_HEADER_WIFI_ENTERPRISE_CONFIGURATION =
            "WifiEnterpriseConfiguration";
    private static final String XML_TAG_IS_APP_INTERACTION_REQUIRED = "IsAppInteractionRequired";
    private static final String XML_TAG_IS_USER_INTERACTION_REQUIRED = "IsUserInteractionRequired";
    private static final String XML_TAG_SUGGESTOR_UID = "SuggestorUid";
    private static final String XML_TAG_SUGGESTOR_PACKAGE_NAME = "SuggestorPackageName";
    private static final String XML_TAG_SUGGESTOR_HAS_USER_APPROVED = "SuggestorHasUserApproved";
    private static final String XML_TAG_SUGGESTOR_MAX_SIZE = "SuggestorMaxSize";

    /**
     * Interface define the data source for the network suggestions store data.
     */
    public interface DataSource {
        /**
         * Retrieve the network suggestion list from the data source to serialize them to disk.
         *
         * @return Map of package name to {@link PerAppInfo}
         */
        Map<String, PerAppInfo> toSerialize();

        /**
         * Set the network suggestions list in the data source after serializing them from disk.
         *
         * @param networkSuggestions Map of package name to {@link PerAppInfo}
         */
        void fromDeserialized(Map<String, PerAppInfo> networkSuggestions);

        /**
         * Clear internal data structure in preparation for user switch or initial store read.
         */
        void reset();

        /**
         * Indicates whether there is new data to serialize.
         */
        boolean hasNewDataToSerialize();
    }

    private final DataSource mDataSource;

    public NetworkSuggestionStoreData(DataSource dataSource) {
        mDataSource = dataSource;
    }

    @Override
    public void serializeData(XmlSerializer out)
            throws XmlPullParserException, IOException {
        serializeNetworkSuggestionsMap(out, mDataSource.toSerialize());
    }

    @Override
    public void deserializeData(XmlPullParser in, int outerTagDepth)
            throws XmlPullParserException, IOException {
        // Ignore empty reads.
        if (in == null) {
            return;
        }
        mDataSource.fromDeserialized(parseNetworkSuggestionsMap(in, outerTagDepth));
    }

    @Override
    public void resetData() {
        mDataSource.reset();
    }

    @Override
    public boolean hasNewDataToSerialize() {
        return mDataSource.hasNewDataToSerialize();
    }

    @Override
    public String getName() {
        return XML_TAG_SECTION_HEADER_NETWORK_SUGGESTION_MAP;
    }

    @Override
    public @WifiConfigStore.StoreFileId int getStoreFileId() {
        return WifiConfigStore.STORE_FILE_USER_NETWORK_SUGGESTIONS;
    }

    /**
     * Serialize the map of package name to network suggestions to an output stream in XML format.
     *
     * @throws XmlPullParserException
     * @throws IOException
     */
    private void serializeNetworkSuggestionsMap(
            XmlSerializer out, final Map<String, PerAppInfo> networkSuggestionsMap)
            throws XmlPullParserException, IOException {
        if (networkSuggestionsMap == null) {
            return;
        }
        for (Entry<String, PerAppInfo> entry : networkSuggestionsMap.entrySet()) {
            String packageName = entry.getKey();
            boolean hasUserApproved = entry.getValue().hasUserApproved;
            int maxSize = entry.getValue().maxSize;
            Set<ExtendedWifiNetworkSuggestion> networkSuggestions =
                    entry.getValue().extNetworkSuggestions;
            XmlUtil.writeNextSectionStart(out, XML_TAG_SECTION_HEADER_NETWORK_SUGGESTION_PER_APP);
            XmlUtil.writeNextValue(out, XML_TAG_SUGGESTOR_PACKAGE_NAME, packageName);
            XmlUtil.writeNextValue(out, XML_TAG_SUGGESTOR_HAS_USER_APPROVED, hasUserApproved);
            XmlUtil.writeNextValue(out, XML_TAG_SUGGESTOR_MAX_SIZE, maxSize);
            serializeExtNetworkSuggestions(out, networkSuggestions);
            XmlUtil.writeNextSectionEnd(out, XML_TAG_SECTION_HEADER_NETWORK_SUGGESTION_PER_APP);
        }
    }

    /**
     * Serialize the set of network suggestions to an output stream in XML format.
     *
     * @throws XmlPullParserException
     * @throws IOException
     */
    private void serializeExtNetworkSuggestions(
            XmlSerializer out, final Set<ExtendedWifiNetworkSuggestion> extNetworkSuggestions)
            throws XmlPullParserException, IOException {
        for (ExtendedWifiNetworkSuggestion extNetworkSuggestion : extNetworkSuggestions) {
            serializeNetworkSuggestion(out, extNetworkSuggestion.wns);
        }
    }

    /**
     * Serialize a {@link ExtendedWifiNetworkSuggestion} to an output stream in XML format.
     *
     * @throws XmlPullParserException
     * @throws IOException
     */
    private void serializeNetworkSuggestion(XmlSerializer out,
                                            final WifiNetworkSuggestion suggestion)
            throws XmlPullParserException, IOException {
        XmlUtil.writeNextSectionStart(out, XML_TAG_SECTION_HEADER_NETWORK_SUGGESTION);

        // Serialize WifiConfiguration.
        XmlUtil.writeNextSectionStart(out, XML_TAG_SECTION_HEADER_WIFI_CONFIGURATION);
        WifiConfigurationXmlUtil.writeToXmlForConfigStore(out, suggestion.wifiConfiguration);
        XmlUtil.writeNextSectionEnd(out, XML_TAG_SECTION_HEADER_WIFI_CONFIGURATION);
        // Serialize enterprise configuration for enterprise networks.
        if (suggestion.wifiConfiguration.enterpriseConfig != null
                && suggestion.wifiConfiguration.enterpriseConfig.getEapMethod()
                != WifiEnterpriseConfig.Eap.NONE) {
            XmlUtil.writeNextSectionStart(
                    out, XML_TAG_SECTION_HEADER_WIFI_ENTERPRISE_CONFIGURATION);
            XmlUtil.WifiEnterpriseConfigXmlUtil.writeToXml(
                    out, suggestion.wifiConfiguration.enterpriseConfig);
            XmlUtil.writeNextSectionEnd(out, XML_TAG_SECTION_HEADER_WIFI_ENTERPRISE_CONFIGURATION);
        }

        // Serialize other fields
        XmlUtil.writeNextValue(out, XML_TAG_IS_APP_INTERACTION_REQUIRED,
                suggestion.isAppInteractionRequired);
        XmlUtil.writeNextValue(out, XML_TAG_IS_USER_INTERACTION_REQUIRED,
                suggestion.isUserInteractionRequired);
        XmlUtil.writeNextValue(out, XML_TAG_SUGGESTOR_UID, suggestion.suggestorUid);
        XmlUtil.writeNextValue(out, XML_TAG_SUGGESTOR_PACKAGE_NAME,
                suggestion.suggestorPackageName);

        XmlUtil.writeNextSectionEnd(out, XML_TAG_SECTION_HEADER_NETWORK_SUGGESTION);
    }

    /**
     * Parse a map of package name to network suggestions from an input stream in XML format.
     *
     * @throws XmlPullParserException
     * @throws IOException
     */
    private Map<String, PerAppInfo> parseNetworkSuggestionsMap(XmlPullParser in, int outerTagDepth)
            throws XmlPullParserException, IOException {
        Map<String, PerAppInfo> networkSuggestionsMap = new HashMap<>();
        while (XmlUtil.gotoNextSectionWithNameOrEnd(
                in, XML_TAG_SECTION_HEADER_NETWORK_SUGGESTION_PER_APP, outerTagDepth)) {
            // Try/catch only runtime exceptions (like illegal args), any XML/IO exceptions are
            // fatal and should abort the entire loading process.
            try {
                String packageName =
                        (String) XmlUtil.readNextValueWithName(in, XML_TAG_SUGGESTOR_PACKAGE_NAME);
                boolean hasUserApproved = (boolean) XmlUtil.readNextValueWithName(in,
                        XML_TAG_SUGGESTOR_HAS_USER_APPROVED);
                int maxSize = (int) XmlUtil.readNextValueWithName(in, XML_TAG_SUGGESTOR_MAX_SIZE);
                PerAppInfo perAppInfo = new PerAppInfo(packageName);
                Set<ExtendedWifiNetworkSuggestion> extNetworkSuggestions =
                        parseExtNetworkSuggestions(in, outerTagDepth + 1, perAppInfo);
                perAppInfo.hasUserApproved = hasUserApproved;
                perAppInfo.maxSize = maxSize;
                perAppInfo.extNetworkSuggestions.addAll(extNetworkSuggestions);
                networkSuggestionsMap.put(packageName, perAppInfo);
            } catch (RuntimeException e) {
                // Failed to parse this network, skip it.
                Log.e(TAG, "Failed to parse network suggestion. Skipping...", e);
            }
        }
        return networkSuggestionsMap;
    }

    /**
     * Parse a set of network suggestions from an input stream in XML format.
     *
     * @throws XmlPullParserException
     * @throws IOException
     */
    private Set<ExtendedWifiNetworkSuggestion> parseExtNetworkSuggestions(
            XmlPullParser in, int outerTagDepth, PerAppInfo perAppInfo)
            throws XmlPullParserException, IOException {
        Set<ExtendedWifiNetworkSuggestion> extNetworkSuggestions = new HashSet<>();
        while (XmlUtil.gotoNextSectionWithNameOrEnd(
                in, XML_TAG_SECTION_HEADER_NETWORK_SUGGESTION, outerTagDepth)) {
            // Try/catch only runtime exceptions (like illegal args), any XML/IO exceptions are
            // fatal and should abort the entire loading process.
            try {
                WifiNetworkSuggestion networkSuggestion =
                        parseNetworkSuggestion(in, outerTagDepth + 1);
                extNetworkSuggestions.add(ExtendedWifiNetworkSuggestion.fromWns(
                        networkSuggestion, perAppInfo));
            } catch (RuntimeException e) {
                // Failed to parse this network, skip it.
                Log.e(TAG, "Failed to parse network suggestion. Skipping...", e);
            }
        }
        return extNetworkSuggestions;
    }

    /**
     * Parse a {@link ExtendedWifiNetworkSuggestion} from an input stream in XML format.
     *
     * @throws XmlPullParserException
     * @throws IOException
     */
    private WifiNetworkSuggestion parseNetworkSuggestion(XmlPullParser in, int outerTagDepth)
            throws XmlPullParserException, IOException {
        Pair<String, WifiConfiguration> parsedConfig = null;
        WifiEnterpriseConfig enterpriseConfig = null;
        boolean isAppInteractionRequired = false;
        boolean isUserInteractionRequired = false;
        int suggestorUid = Process.INVALID_UID;
        String suggestorPackageName = null;

        // Loop through and parse out all the elements from the stream within this section.
        while (XmlUtils.nextElementWithin(in, outerTagDepth)) {
            if (in.getAttributeValue(null, "name") != null) {
                // Value elements.
                String[] valueName = new String[1];
                Object value = XmlUtil.readCurrentValue(in, valueName);
                switch (valueName[0]) {
                    case XML_TAG_IS_APP_INTERACTION_REQUIRED:
                        isAppInteractionRequired = (boolean) value;
                        break;
                    case XML_TAG_IS_USER_INTERACTION_REQUIRED:
                        isUserInteractionRequired = (boolean) value;
                        break;
                    case XML_TAG_SUGGESTOR_UID:
                        suggestorUid = (int) value;
                        break;
                    case XML_TAG_SUGGESTOR_PACKAGE_NAME:
                        suggestorPackageName = (String) value;
                        break;
                    default:
                        throw new XmlPullParserException(
                                "Unknown value name found: " + valueName[0]);
                }
            } else {
                String tagName = in.getName();
                if (tagName == null) {
                    throw new XmlPullParserException("Unexpected null under "
                            + XML_TAG_SECTION_HEADER_NETWORK_SUGGESTION);
                }
                switch (tagName) {
                    case XML_TAG_SECTION_HEADER_WIFI_CONFIGURATION:
                        if (parsedConfig != null) {
                            throw new XmlPullParserException("Detected duplicate tag for: "
                                    + XML_TAG_SECTION_HEADER_WIFI_CONFIGURATION);
                        }
                        parsedConfig = WifiConfigurationXmlUtil.parseFromXml(
                                in, outerTagDepth + 1);
                        break;
                    case XML_TAG_SECTION_HEADER_WIFI_ENTERPRISE_CONFIGURATION:
                        if (enterpriseConfig != null) {
                            throw new XmlPullParserException("Detected duplicate tag for: "
                                    + XML_TAG_SECTION_HEADER_WIFI_ENTERPRISE_CONFIGURATION);
                        }
                        enterpriseConfig = XmlUtil.WifiEnterpriseConfigXmlUtil.parseFromXml(
                                in, outerTagDepth + 1);
                        break;
                    default:
                        throw new XmlPullParserException("Unknown tag under "
                                + XML_TAG_SECTION_HEADER_NETWORK_SUGGESTION + ": " + in.getName());
                }
            }
        }
        if (parsedConfig == null || parsedConfig.second == null) {
            throw new XmlPullParserException("XML parsing of wifi configuration failed");
        }
        if (suggestorUid == -1) {
            throw new XmlPullParserException("XML parsing of suggestor uid failed");
        }
        if (suggestorPackageName == null) {
            throw new XmlPullParserException("XML parsing of suggestor package name failed");
        }
        WifiConfiguration wifiConfiguration =  parsedConfig.second;
        if (enterpriseConfig != null) {
            wifiConfiguration.enterpriseConfig = enterpriseConfig;
        }
        return new WifiNetworkSuggestion(
                wifiConfiguration, isAppInteractionRequired, isUserInteractionRequired,
                suggestorUid, suggestorPackageName);
    }
}