1 /* 2 * Copyright (C) 2021 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 package com.android.server.wifi; 17 18 import android.annotation.NonNull; 19 import android.annotation.Nullable; 20 import android.util.Log; 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.HashMap; 31 import java.util.Map; 32 /** 33 * This class performs serialization and parsing of XML data block that contain the map of IMSI 34 * protection exemption user approval info. 35 */ 36 public class ImsiPrivacyProtectionExemptionStoreData implements WifiConfigStore.StoreData { 37 private static final String TAG = "ImsiPrivacyProtectionExemptionStoreData"; 38 private static final String XML_TAG_SECTION_HEADER_IMSI_PROTECTION_EXEMPTION_CARRIER_MAP = 39 "ImsiPrivacyProtectionExemptionMap"; 40 private static final String XML_TAG_CARRIER_EXEMPTION_MAP = "CarrierExemptionMap"; 41 /** 42 * Interface define the data source for the carrier IMSI protection exemption map store data. 43 */ 44 public interface DataSource { 45 /** 46 * Retrieve the IMSI protection exemption map from the data source to serialize to disk. 47 * 48 * @return Map of carrier Id to if allowed. 49 */ toSerialize()50 Map<Integer, Boolean> toSerialize(); 51 /** 52 * Set the IMSI protection exemption map in the data source after serializing them from disk 53 * 54 * @param imsiProtectionExemptionMap Map of carrier Id to allowed or not. 55 */ fromDeserialized(Map<Integer, Boolean> imsiProtectionExemptionMap)56 void fromDeserialized(Map<Integer, Boolean> imsiProtectionExemptionMap); 57 /** 58 * Clear internal data structure in preparation for user switch or initial store read. 59 */ reset()60 void reset(); 61 /** 62 * Indicates whether there is new data to serialize. 63 */ hasNewDataToSerialize()64 boolean hasNewDataToSerialize(); 65 } 66 private final DataSource mDataSource; 67 /** 68 * Set the data source fot store data. 69 */ ImsiPrivacyProtectionExemptionStoreData(@onNull DataSource dataSource)70 public ImsiPrivacyProtectionExemptionStoreData(@NonNull DataSource dataSource) { 71 mDataSource = dataSource; 72 } 73 @Override serializeData(XmlSerializer out, WifiConfigStoreEncryptionUtil encryptionUtil)74 public void serializeData(XmlSerializer out, WifiConfigStoreEncryptionUtil encryptionUtil) 75 throws XmlPullParserException, IOException { 76 Map<String, Boolean> dataToSerialize = integerMapToStringMap(mDataSource.toSerialize()); 77 XmlUtil.writeNextValue(out, XML_TAG_CARRIER_EXEMPTION_MAP, dataToSerialize); 78 } 79 @Override deserializeData(XmlPullParser in, int outerTagDepth, int version, WifiConfigStoreEncryptionUtil encryptionUtil)80 public void deserializeData(XmlPullParser in, int outerTagDepth, int version, 81 WifiConfigStoreEncryptionUtil encryptionUtil) 82 throws XmlPullParserException, IOException { 83 // Ignore empty reads. 84 if (in == null) { 85 return; 86 } 87 mDataSource.fromDeserialized(parseCarrierImsiProtectionExemptionMap(in, outerTagDepth, 88 version, encryptionUtil)); 89 } parseCarrierImsiProtectionExemptionMap(XmlPullParser in, int outerTagDepth, @WifiConfigStore.Version int version, @Nullable WifiConfigStoreEncryptionUtil encryptionUtil)90 private Map<Integer, Boolean> parseCarrierImsiProtectionExemptionMap(XmlPullParser in, 91 int outerTagDepth, 92 @WifiConfigStore.Version int version, 93 @Nullable WifiConfigStoreEncryptionUtil encryptionUtil) 94 throws XmlPullParserException, IOException { 95 Map<String, Boolean> protectionExemptionMap = new HashMap<>(); 96 while (!XmlUtil.isNextSectionEnd(in, outerTagDepth)) { 97 String[] valueName = new String[1]; 98 Object value = XmlUtil.readCurrentValue(in, valueName); 99 if (valueName[0] == null) { 100 throw new XmlPullParserException("Missing value name"); 101 } 102 switch (valueName[0]) { 103 case XML_TAG_CARRIER_EXEMPTION_MAP: 104 if (value instanceof Map) { 105 protectionExemptionMap = (Map<String, Boolean>) value; 106 } 107 break; 108 default: 109 Log.w(TAG, "Unknown tag under " 110 + XML_TAG_SECTION_HEADER_IMSI_PROTECTION_EXEMPTION_CARRIER_MAP 111 + ": " + valueName[0]); 112 break; 113 } 114 } 115 return stringMapToIntegerMap(protectionExemptionMap); 116 } integerMapToStringMap(Map<Integer, Boolean> input)117 private Map<String, Boolean> integerMapToStringMap(Map<Integer, Boolean> input) { 118 Map<String, Boolean> output = new HashMap<>(); 119 if (input == null) { 120 return output; 121 } 122 for (Map.Entry<Integer, Boolean> entry : input.entrySet()) { 123 output.put(Integer.toString(entry.getKey()), entry.getValue()); 124 } 125 return output; 126 } stringMapToIntegerMap(Map<String, Boolean> input)127 private Map<Integer, Boolean> stringMapToIntegerMap(Map<String, Boolean> input) { 128 Map<Integer, Boolean> output = new HashMap<>(); 129 if (input == null) { 130 return output; 131 } 132 for (Map.Entry<String, Boolean> entry : input.entrySet()) { 133 try { 134 output.put(Integer.valueOf(entry.getKey()), entry.getValue()); 135 } catch (NumberFormatException e) { 136 Log.e(TAG, "Failed to Integer convert: " + entry.getKey()); 137 } 138 } 139 return output; 140 } 141 @Override resetData()142 public void resetData() { 143 mDataSource.reset(); 144 } 145 @Override hasNewDataToSerialize()146 public boolean hasNewDataToSerialize() { 147 return mDataSource.hasNewDataToSerialize(); 148 } 149 @Override getName()150 public String getName() { 151 return XML_TAG_SECTION_HEADER_IMSI_PROTECTION_EXEMPTION_CARRIER_MAP; 152 } 153 @Override getStoreFileId()154 public int getStoreFileId() { 155 // User general store. 156 return WifiConfigStore.STORE_FILE_USER_GENERAL; 157 } 158 } 159