diff options
author | 2024-10-17 19:20:08 +0000 | |
---|---|---|
committer | 2024-10-17 19:20:08 +0000 | |
commit | 674fff48d64159b567d0ed2ab8d3f272df08cf8c (patch) | |
tree | 2c65e62c688a27164e8b3deceb6674fa348d89cb | |
parent | d949946d7299f088e0fa682ba3e4c5c7befb5e73 (diff) | |
parent | 1cb695c659fe54720c6a79cbc5a320c625000b60 (diff) |
Merge "MapbMessage: Apply pattern matching" into main
-rw-r--r-- | android/app/src/com/android/bluetooth/map/BluetoothMapbMessage.java | 46 | ||||
-rw-r--r-- | android/app/src/com/android/bluetooth/map/BluetoothMapbMessageMime.java | 28 |
2 files changed, 42 insertions, 32 deletions
diff --git a/android/app/src/com/android/bluetooth/map/BluetoothMapbMessage.java b/android/app/src/com/android/bluetooth/map/BluetoothMapbMessage.java index 5af521c49c..7b7d879d0a 100644 --- a/android/app/src/com/android/bluetooth/map/BluetoothMapbMessage.java +++ b/android/app/src/com/android/bluetooth/map/BluetoothMapbMessage.java @@ -32,11 +32,15 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +import java.util.regex.Pattern; // Next tag value for ContentProfileErrorReportUtils.report(): 10 public abstract class BluetoothMapbMessage { - protected static final String TAG = "BluetoothMapbMessage"; + protected static final String TAG = BluetoothMapbMessage.class.getSimpleName(); + + private static final Pattern UNESCAPE_COLON = Pattern.compile("[^\\\\]:"); + protected static final Pattern COLON = Pattern.compile(":"); private String mVersionString = "VERSION:1.0"; @@ -263,34 +267,34 @@ public abstract class BluetoothMapbMessage { while (!line.contains("END:VCARD")) { line = line.trim(); if (line.startsWith("N:")) { - parts = line.split("[^\\\\]:"); // Split on "un-escaped" ':' + parts = UNESCAPE_COLON.split(line); if (parts.length == 2) { name = parts[1]; } else { name = ""; } } else if (line.startsWith("FN:")) { - parts = line.split("[^\\\\]:"); // Split on "un-escaped" ':' + parts = UNESCAPE_COLON.split(line); if (parts.length == 2) { formattedName = parts[1]; } else { formattedName = ""; } } else if (line.startsWith("TEL:")) { - parts = line.split("[^\\\\]:"); // Split on "un-escaped" ':' + parts = UNESCAPE_COLON.split(line); if (parts.length == 2) { - String[] subParts = parts[1].split("[^\\\\];"); + String[] subParts = UNESCAPE_COLON.split(parts[1]); if (phoneNumbers == null) { - phoneNumbers = new ArrayList<String>(1); + phoneNumbers = new ArrayList<>(1); } // only keep actual phone number phoneNumbers.add(subParts[subParts.length - 1]); } // Empty phone number - ignore } else if (line.startsWith("EMAIL:")) { - parts = line.split("[^\\\\]:"); // Split on "un-escaped" : + parts = UNESCAPE_COLON.split(line); if (parts.length == 2) { - String[] subParts = parts[1].split("[^\\\\];"); + String[] subParts = UNESCAPE_COLON.split(parts[1]); if (emailAddresses == null) { emailAddresses = new ArrayList<String>(1); } @@ -299,9 +303,9 @@ public abstract class BluetoothMapbMessage { } // Empty email address entry - ignore } else if (line.startsWith("X-BT-UCI:")) { - parts = line.split("[^\\\\]:"); // Split on "un-escaped" : + parts = UNESCAPE_COLON.split(line); if (parts.length == 2) { - String[] subParts = parts[1].split("[^\\\\];"); + String[] subParts = UNESCAPE_COLON.split(parts[1]); if (btUcis == null) { btUcis = new ArrayList<String>(1); } @@ -309,9 +313,9 @@ public abstract class BluetoothMapbMessage { } // Empty UCIentry - ignore } else if (line.startsWith("X-BT-UID:")) { - parts = line.split("[^\\\\]:"); // Split on "un-escaped" : + parts = UNESCAPE_COLON.split(line); if (parts.length == 2) { - String[] subParts = parts[1].split("[^\\\\];"); + String[] subParts = UNESCAPE_COLON.split(parts[1]); if (btUids == null) { btUids = new ArrayList<String>(1); } @@ -514,7 +518,7 @@ public abstract class BluetoothMapbMessage { // Parse the properties - which end with either a VCARD or a BENV while (!line.contains("BEGIN:VCARD") && !line.contains("BEGIN:BENV")) { if (line.contains("STATUS")) { - String[] arg = line.split(":"); + String[] arg = COLON.split(line); if (arg != null && arg.length == 2) { if (arg[1].trim().equals("READ")) { status = true; @@ -528,7 +532,7 @@ public abstract class BluetoothMapbMessage { } } if (line.contains("EXTENDEDDATA")) { - String[] arg = line.split(":"); + String[] arg = COLON.split(line); if (arg != null && arg.length == 2) { String value = arg[1].trim(); // FIXME what should we do with this @@ -536,7 +540,7 @@ public abstract class BluetoothMapbMessage { } } if (line.contains("TYPE")) { - String[] arg = line.split(":"); + String[] arg = COLON.split(line); if (arg != null && arg.length == 2) { String value = arg[1].trim(); /* Will throw IllegalArgumentException if value is wrong */ @@ -569,7 +573,7 @@ public abstract class BluetoothMapbMessage { } } if (line.contains("FOLDER")) { - String[] arg = line.split(":"); + String[] arg = COLON.split(line); if (arg != null && arg.length == 2) { folder = arg[1].trim(); } @@ -652,7 +656,7 @@ public abstract class BluetoothMapbMessage { parseMsgInit(); while (!line.contains("END:")) { if (line.contains("PARTID:")) { - String[] arg = line.split(":"); + String[] arg = COLON.split(line); if (arg != null && arg.length == 2) { try { Long unusedId = Long.parseLong(arg[1].trim()); @@ -669,7 +673,7 @@ public abstract class BluetoothMapbMessage { throw new IllegalArgumentException("Missing value for 'PARTID': " + line); } } else if (line.contains("ENCODING:")) { - String[] arg = line.split(":"); + String[] arg = COLON.split(line); if (arg != null && arg.length == 2) { mEncoding = arg[1].trim(); // If needed validation will be done when the value is used @@ -677,7 +681,7 @@ public abstract class BluetoothMapbMessage { throw new IllegalArgumentException("Missing value for 'ENCODING': " + line); } } else if (line.contains("CHARSET:")) { - String[] arg = line.split(":"); + String[] arg = COLON.split(line); if (arg != null && arg.length == 2) { mCharset = arg[1].trim(); // If needed validation will be done when the value is used @@ -685,7 +689,7 @@ public abstract class BluetoothMapbMessage { throw new IllegalArgumentException("Missing value for 'CHARSET': " + line); } } else if (line.contains("LANGUAGE:")) { - String[] arg = line.split(":"); + String[] arg = COLON.split(line); if (arg != null && arg.length == 2) { String unusedLanguage = arg[1].trim(); // If needed validation will be done when the value is used @@ -693,7 +697,7 @@ public abstract class BluetoothMapbMessage { throw new IllegalArgumentException("Missing value for 'LANGUAGE': " + line); } } else if (line.contains("LENGTH:")) { - String[] arg = line.split(":"); + String[] arg = COLON.split(line); if (arg != null && arg.length == 2) { try { mBMsgLength = Integer.parseInt(arg[1].trim()); diff --git a/android/app/src/com/android/bluetooth/map/BluetoothMapbMessageMime.java b/android/app/src/com/android/bluetooth/map/BluetoothMapbMessageMime.java index 546fcb8fc7..6e5204576a 100644 --- a/android/app/src/com/android/bluetooth/map/BluetoothMapbMessageMime.java +++ b/android/app/src/com/android/bluetooth/map/BluetoothMapbMessageMime.java @@ -37,9 +37,15 @@ import java.util.Date; import java.util.List; import java.util.Locale; import java.util.UUID; +import java.util.regex.Pattern; // Next tag value for ContentProfileErrorReportUtils.report(): 8 public class BluetoothMapbMessageMime extends BluetoothMapbMessage { + private static final Pattern NEW_LINE = Pattern.compile("\r\n"); + private static final Pattern TWO_NEW_LINE = Pattern.compile("\r\n\r\n"); + private static final Pattern SEMI_COLON = Pattern.compile(";"); + private static final Pattern BOUNDARY_PATTERN = Pattern.compile("boundary[\\s]*="); + private static final Pattern CHARSET_PATTERN = Pattern.compile("charset[\\s]*="); public static class MimePart { public long mId = INVALID_VALUE; /* The _id from the content provider, can be used to @@ -559,7 +565,7 @@ public class BluetoothMapbMessageMime extends BluetoothMapbMessage { * headers were found. */ private String parseMimeHeaders(String hdrPart) { - String[] headers = hdrPart.split("\r\n"); + String[] headers = NEW_LINE.split(hdrPart); Log.d(TAG, "Header count=" + headers.length); String header; @@ -574,7 +580,7 @@ public class BluetoothMapbMessageMime extends BluetoothMapbMessage { if (header.trim().isEmpty()) { continue; } - String[] headerParts = header.split(":", 2); + String[] headerParts = COLON.split(header, 2); if (headerParts.length != 2) { // We treat the remaining content as plain text. StringBuilder remaining = new StringBuilder(); @@ -622,12 +628,12 @@ public class BluetoothMapbMessageMime extends BluetoothMapbMessage { } else if (headerType.contains("MIME-VERSION")) { /* The mime version is not needed */ } else if (headerType.contains("CONTENT-TYPE")) { - String[] contentTypeParts = headerValue.split(";"); + String[] contentTypeParts = SEMI_COLON.split(headerValue); mContentType = contentTypeParts[0]; // Extract the boundary if it exists for (int j = 1, n = contentTypeParts.length; j < n; j++) { if (contentTypeParts[j].contains("boundary")) { - mBoundary = contentTypeParts[j].split("boundary[\\s]*=", 2)[1].trim(); + mBoundary = BOUNDARY_PATTERN.split(contentTypeParts[j], 2)[1].trim(); // removing quotes from boundary string if ((mBoundary.charAt(0) == '\"') && (mBoundary.charAt(mBoundary.length() - 1) == '\"')) { @@ -635,7 +641,7 @@ public class BluetoothMapbMessageMime extends BluetoothMapbMessage { } Log.d(TAG, "Boundary tag=" + mBoundary); } else if (contentTypeParts[j].contains("charset")) { - mCharset = contentTypeParts[j].split("charset[\\s]*=", 2)[1].trim(); + mCharset = CHARSET_PATTERN.split(contentTypeParts[j], 2)[1].trim(); } } } else if (headerType.contains("CONTENT-TRANSFER-ENCODING")) { @@ -653,12 +659,12 @@ public class BluetoothMapbMessageMime extends BluetoothMapbMessage { } private void parseMimePart(String partStr) { - String[] parts = partStr.split("\r\n\r\n", 2); // Split the header from the body + String[] parts = TWO_NEW_LINE.split(partStr, 2); // Split the header from the body MimePart newPart = addMimePart(); String partEncoding = mMyEncoding; /* Use the overall encoding as default */ String body; - String[] headers = parts[0].split("\r\n"); + String[] headers = NEW_LINE.split(parts[0]); Log.d(TAG, "parseMimePart: headers count=" + headers.length); if (parts.length != 2) { @@ -672,7 +678,7 @@ public class BluetoothMapbMessageMime extends BluetoothMapbMessage { continue; } - String[] headerParts = header.split(":", 2); + String[] headerParts = COLON.split(header, 2); if (headerParts.length != 2) { Log.w(TAG, "part-Header not formatted correctly: "); ContentProfileErrorReportUtils.report( @@ -687,13 +693,13 @@ public class BluetoothMapbMessageMime extends BluetoothMapbMessage { String headerType = Ascii.toUpperCase(headerParts[0]); String headerValue = headerParts[1].trim(); if (headerType.contains("CONTENT-TYPE")) { - String[] contentTypeParts = headerValue.split(";"); + String[] contentTypeParts = SEMI_COLON.split(headerValue); newPart.mContentType = contentTypeParts[0]; // Extract the boundary if it exists for (int j = 1, n = contentTypeParts.length; j < n; j++) { String value = Ascii.toLowerCase(contentTypeParts[j]); if (value.contains("charset")) { - newPart.mCharsetName = value.split("charset[\\s]*=", 2)[1].trim(); + newPart.mCharsetName = CHARSET_PATTERN.split(value, 2)[1].trim(); } } } else if (headerType.contains("CONTENT-LOCATION")) { @@ -772,7 +778,7 @@ public class BluetoothMapbMessageMime extends BluetoothMapbMessage { String messageBody = null; message = message.replaceAll("\\r\\n[ \\\t]+", ""); // Unfold - messageParts = message.split("\r\n\r\n", 2); // Split the header from the body + messageParts = TWO_NEW_LINE.split(message, 2); // Split the header from the body if (messageParts.length != 2) { // Handle entire message as plain text messageBody = message; |