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