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 
21 import com.android.server.wifi.util.WifiConfigStoreEncryptionUtil;
22 import com.android.server.wifi.util.XmlUtil;
23 
24 import org.xmlpull.v1.XmlPullParser;
25 import org.xmlpull.v1.XmlPullParserException;
26 import org.xmlpull.v1.XmlSerializer;
27 
28 import java.io.IOException;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Set;
32 
33 /**
34  * This class performs serialization and parsing of XML data block that contain the list of
35  * deleted ephemeral SSIDs (XML block data inside <DeletedEphemeralSSIDList> tag).
36  */
37 public class DeletedEphemeralSsidsStoreData implements WifiConfigStore.StoreData {
38     private static final String XML_TAG_SECTION_HEADER_DELETED_EPHEMERAL_SSID_LIST =
39             "DeletedEphemeralSSIDList";
40     private static final String XML_TAG_SSID_LIST = "SSIDList";
41 
42     private final Clock mClock;
43     private Map<String, Long> mSsidToTimeMap;
44 
DeletedEphemeralSsidsStoreData(Clock clock)45     DeletedEphemeralSsidsStoreData(Clock clock) {
46         mClock = clock;
47     }
48 
49     @Override
serializeData(XmlSerializer out, @Nullable WifiConfigStoreEncryptionUtil encryptionUtil)50     public void serializeData(XmlSerializer out,
51             @Nullable WifiConfigStoreEncryptionUtil encryptionUtil)
52             throws XmlPullParserException, IOException {
53         if (mSsidToTimeMap != null) {
54             XmlUtil.writeNextValue(out, XML_TAG_SSID_LIST, mSsidToTimeMap);
55         }
56     }
57 
58     @Override
deserializeData(XmlPullParser in, int outerTagDepth, @WifiConfigStore.Version int version, @Nullable WifiConfigStoreEncryptionUtil encryptionUtil)59     public void deserializeData(XmlPullParser in, int outerTagDepth,
60             @WifiConfigStore.Version int version,
61             @Nullable WifiConfigStoreEncryptionUtil encryptionUtil)
62             throws XmlPullParserException, IOException {
63         // Ignore empty reads.
64         if (in == null) {
65             return;
66         }
67         while (!XmlUtil.isNextSectionEnd(in, outerTagDepth)) {
68             String[] valueName = new String[1];
69             Object value = XmlUtil.readCurrentValue(in, valueName);
70             if (valueName[0] == null) {
71                 throw new XmlPullParserException("Missing value name");
72             }
73             switch (valueName[0]) {
74                 case XML_TAG_SSID_LIST:
75                     // Backwards compatibility, this used to be a set.
76                     if (value instanceof Set) {
77                         mSsidToTimeMap = new HashMap<>();
78                         for (String ssid : (Set<String>) value) {
79                             // Mark the deleted time as bootup time for existing entries from
80                             // previous releases.
81                             mSsidToTimeMap.put(ssid, mClock.getWallClockMillis());
82                         }
83                     } else if (value instanceof Map) {
84                         mSsidToTimeMap = (Map<String, Long>) value;
85                     }
86                     break;
87                 default:
88                     throw new XmlPullParserException("Unknown tag under "
89                             + XML_TAG_SECTION_HEADER_DELETED_EPHEMERAL_SSID_LIST
90                             + ": " + valueName[0]);
91             }
92         }
93     }
94 
95     @Override
resetData()96     public void resetData() {
97         mSsidToTimeMap = null;
98     }
99 
100     @Override
hasNewDataToSerialize()101     public boolean hasNewDataToSerialize() {
102         // always persist.
103         return true;
104     }
105 
106     @Override
getName()107     public String getName() {
108         return XML_TAG_SECTION_HEADER_DELETED_EPHEMERAL_SSID_LIST;
109     }
110 
111     @Override
getStoreFileId()112     public @WifiConfigStore.StoreFileId int getStoreFileId() {
113         // Shared general store.
114         return WifiConfigStore.STORE_FILE_USER_GENERAL;
115     }
116 
117     /**
118      * An empty map will be returned for null SSID list.
119      *
120      * @return Map of SSIDs
121      */
getSsidToTimeMap()122     public Map<String, Long> getSsidToTimeMap() {
123         if (mSsidToTimeMap == null) {
124             return new HashMap<String, Long>();
125         }
126         return mSsidToTimeMap;
127     }
128 
setSsidToTimeMap(Map<String, Long> ssidMap)129     public void setSsidToTimeMap(Map<String, Long> ssidMap) {
130         mSsidToTimeMap = ssidMap;
131     }
132 }
133 
134