lineage-sdk: Move to schema parser

Change-Id: Id79656a4abed0d94e2af81f1935b663164ccc8d8
This commit is contained in:
Luca Stefani
2021-03-13 17:36:20 +01:00
parent 402a3860f4
commit 97c2e135f1
3 changed files with 22 additions and 172 deletions

View File

@@ -1,98 +0,0 @@
/*
* Copyright (C) 2017 The Android Open Source Project
* Copyright (C) 2017-2021 The LineageOS 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 org.lineageos.lib.phone;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.ArrayList;
public class SensitivePhoneNumber {
private static final String LOG_TAG = "SensitivePhoneNumber";
private static final String ns = null;
private String mNetworkNumeric;
private ArrayList<SensitivePhoneNumberInfo> mPhoneNumberInfos;
public SensitivePhoneNumber(String networkNumeric, ArrayList<SensitivePhoneNumberInfo> infos) {
mNetworkNumeric = networkNumeric;
mPhoneNumberInfos = infos;
}
public String getNetworkNumeric() {
return mNetworkNumeric;
}
public ArrayList<SensitivePhoneNumberInfo> getPhoneNumberInfos() {
return mPhoneNumberInfos;
}
public void setNetworkNumeric(String networkNumeric) {
mNetworkNumeric = networkNumeric;
}
public void setPhoneNumberInfos(ArrayList<SensitivePhoneNumberInfo> infos) {
mPhoneNumberInfos = infos;
}
public void addPhoneNumberInfo(SensitivePhoneNumberInfo info) {
mPhoneNumberInfos.add(info);
}
public static SensitivePhoneNumber readSensitivePhoneNumbers(XmlPullParser parser)
throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "sensitivePN");
String network = parser.getAttributeValue(null, "network");
ArrayList<SensitivePhoneNumberInfo> infos = readPhoneNumberInfo(parser);
return new SensitivePhoneNumber(network, infos);
}
private static ArrayList<SensitivePhoneNumberInfo> readPhoneNumberInfo(XmlPullParser parser)
throws XmlPullParserException, IOException {
ArrayList<SensitivePhoneNumberInfo> numberInfos = new ArrayList<>();
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
parser.require(XmlPullParser.START_TAG, ns, "item");
SensitivePhoneNumberInfo item = parseItem(parser);
numberInfos.add(item);
parser.require(XmlPullParser.END_TAG, ns, "item");
}
return numberInfos;
}
private static SensitivePhoneNumberInfo parseItem(XmlPullParser parser)
throws XmlPullParserException, IOException {
SensitivePhoneNumberInfo item = new SensitivePhoneNumberInfo();
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String tag = parser.getName();
item.set(tag, parser.nextText());
parser.require(XmlPullParser.END_TAG, ns, tag);
}
return item;
}
}

View File

@@ -1,34 +0,0 @@
/*
* Copyright (C) 2019-2021 The LineageOS 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 org.lineageos.lib.phone;
import java.util.HashMap;
public class SensitivePhoneNumberInfo {
private HashMap<String, String> mHashmap = new HashMap<String, String>();
public SensitivePhoneNumberInfo() { }
public void set(String key, String value) {
mHashmap.put(key, value);
}
public String get(String key) {
return mHashmap.getOrDefault(key, "");
}
}

View File

@@ -32,18 +32,24 @@ import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;
import org.xmlpull.v1.XmlPullParser;
import org.lineageos.lib.phone.spn.Item;
import org.lineageos.lib.phone.spn.SensitivePN;
import org.lineageos.lib.phone.spn.SensitivePNS;
import org.lineageos.lib.phone.spn.XmlParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import javax.xml.datatype.DatatypeConfigurationException;
public class SensitivePhoneNumbers {
private final String LOG_TAG = this.getClass().getSimpleName();
@@ -53,8 +59,7 @@ public class SensitivePhoneNumbers {
private static SensitivePhoneNumbers sInstance = null;
private static boolean sNumbersLoaded;
private HashMap<String, ArrayList<SensitivePhoneNumberInfo>> mSensitiveNumbersMap =
new HashMap<>();
private HashMap<String, ArrayList<Item>> mSensitiveNumbersMap = new HashMap<>();
private SensitivePhoneNumbers() { }
@@ -70,57 +75,34 @@ public class SensitivePhoneNumbers {
return;
}
FileReader sensiblePNReader;
File sensiblePNFile = new File(Environment.getRootDirectory(),
SENSIBLE_PHONENUMBERS_FILE_PATH);
FileInputStream sensiblePNInputStream;
try {
sensiblePNReader = new FileReader(sensiblePNFile);
sensiblePNInputStream = new FileInputStream(sensiblePNFile);
} catch (FileNotFoundException e) {
Log.w(LOG_TAG, "Can not open " + sensiblePNFile.getAbsolutePath());
return;
}
try {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(sensiblePNReader);
parser.nextTag();
readSensitivePNS(parser);
sensiblePNReader.close();
} catch (IOException | XmlPullParserException e) {
for (SensitivePN sensitivePN : new XmlParser().read(sensiblePNInputStream).getSensitivePN()) {
String[] mccs = sensitivePN.getNetwork().split(",");
for (String mcc : mccs) {
mSensitiveNumbersMap.put(mcc, new ArrayList(sensitivePN.getItem()));
}
}
} catch (DatatypeConfigurationException | IOException | XmlPullParserException e) {
Log.w(LOG_TAG, "Exception in spn-conf parser", e);
}
sNumbersLoaded = true;
}
private void readSensitivePNS(XmlPullParser parser)
throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "sensitivePNS");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (!"sensitivePN".equals(name)) {
break;
}
SensitivePhoneNumber sensitivePN = SensitivePhoneNumber
.readSensitivePhoneNumbers(parser);
String[] mccs = sensitivePN.getNetworkNumeric().split(",");
ArrayList<SensitivePhoneNumberInfo> sensitive_nums = sensitivePN.getPhoneNumberInfos();
for (String mcc : mccs) {
mSensitiveNumbersMap.put(mcc, sensitive_nums);
}
}
}
public ArrayList<SensitivePhoneNumberInfo> getSensitivePnInfosForMcc(String mcc) {
public ArrayList<Item> getSensitivePnInfosForMcc(String mcc) {
loadSensiblePhoneNumbers();
return mSensitiveNumbersMap.getOrDefault(mcc, new ArrayList<SensitivePhoneNumberInfo>());
return mSensitiveNumbersMap.getOrDefault(mcc, new ArrayList<Item>());
}
public boolean isSensitiveNumber(Context context, String numberToCheck, int subId) {
@@ -172,8 +154,8 @@ public class SensitivePhoneNumbers {
private boolean isSensitiveNumber(String numberToCheck, String mcc) {
if (mSensitiveNumbersMap.containsKey(mcc)) {
for (SensitivePhoneNumberInfo info : mSensitiveNumbersMap.get(mcc)) {
if (PhoneNumberUtils.compare(numberToCheck, info.get("number"))) {
for (Item item : mSensitiveNumbersMap.get(mcc)) {
if (PhoneNumberUtils.compare(numberToCheck, item.getNumber())) {
return true;
}
}