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