Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9ada0f5e authored by William Escande's avatar William Escande
Browse files

PbapVcard: Apply pattern matching

str.split(regex, n) is the same as Pattern.compile(regex).split(str, n).
Except that the second can be compiled once why the first need to be
recompile everytime it is invoked

Bug: 371471052
Flag: Exempt refactor
Test: atest BluetoothInstrumentationTests
Change-Id: I0363ef188571db5e1c3a8745a377fde06d8894ff
parent bb69ca6c
Loading
Loading
Loading
Loading
+18 −15
Original line number Original line Diff line number Diff line
@@ -53,10 +53,11 @@ import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Collections;
import java.util.List;
import java.util.List;
import java.util.regex.Pattern;


// Next tag value for ContentProfileErrorReportUtils.report(): 22
// Next tag value for ContentProfileErrorReportUtils.report(): 22
public class BluetoothPbapVcardManager {
public class BluetoothPbapVcardManager {
    private static final String TAG = "BluetoothPbapVcardManager";
    private static final String TAG = BluetoothPbapVcardManager.class.getSimpleName();


    private ContentResolver mResolver;
    private ContentResolver mResolver;


@@ -87,6 +88,11 @@ public class BluetoothPbapVcardManager {


    private static final int NEED_SEND_BODY = -1;
    private static final int NEED_SEND_BODY = -1;


    private static final String SEPARATOR = System.getProperty("line.separator");
    private static final Pattern SEPARATOR_PATTERN = Pattern.compile(SEPARATOR);
    private static final Pattern PROPERTY_PATTERN = Pattern.compile("[;:]");
    private static final Pattern ATTRIBUTE_PATTERN = Pattern.compile(":");

    public BluetoothPbapVcardManager(final Context context) {
    public BluetoothPbapVcardManager(final Context context) {
        mContext = context;
        mContext = context;
        mResolver = mContext.getContentResolver();
        mResolver = mContext.getContentResolver();
@@ -1100,7 +1106,7 @@ public class BluetoothPbapVcardManager {
            byte[] filter,
            byte[] filter,
            byte[] selector,
            byte[] selector,
            String vcardselectorop,
            String vcardselectorop,
            boolean vCardSelct) {
            boolean vCardSelect) {
        long timestamp = System.currentTimeMillis();
        long timestamp = System.currentTimeMillis();


        HandlerForStringBuffer buffer = null;
        HandlerForStringBuffer buffer = null;
@@ -1121,7 +1127,7 @@ public class BluetoothPbapVcardManager {
                    break;
                    break;
                }
                }
                String vcard = composer.createOneEntry(vcardType21);
                String vcard = composer.createOneEntry(vcardType21);
                if (vCardSelct) {
                if (vCardSelect) {
                    if (!vcardselector.checkVCardSelector(vcard, vcardselectorop)) {
                    if (!vcardselector.checkVCardSelector(vcard, vcardselectorop)) {
                        Log.e(TAG, "Checking vcard selector for call log");
                        Log.e(TAG, "Checking vcard selector for call log");
                        ContentProfileErrorReportUtils.report(
                        ContentProfileErrorReportUtils.report(
@@ -1176,7 +1182,7 @@ public class BluetoothPbapVcardManager {
                    buffer.writeVCard(vcard);
                    buffer.writeVCard(vcard);
                }
                }
            }
            }
            if (needSendBody != NEED_SEND_BODY && vCardSelct) {
            if (needSendBody != NEED_SEND_BODY && vCardSelect) {
                return pbSize;
                return pbSize;
            }
            }
        } finally {
        } finally {
@@ -1194,12 +1200,11 @@ public class BluetoothPbapVcardManager {
    }
    }


    public String stripTelephoneNumber(String vCard) {
    public String stripTelephoneNumber(String vCard) {
        String separator = System.getProperty("line.separator");
        String[] attr = SEPARATOR_PATTERN.split(vCard);
        String[] attr = vCard.split(separator);
        String stripedVCard = "";
        String stripedVCard = "";
        for (int i = 0; i < attr.length; i++) {
        for (int i = 0; i < attr.length; i++) {
            if (attr[i].startsWith("TEL")) {
            if (attr[i].startsWith("TEL")) {
                String[] vTagAndTel = attr[i].split(":", 2);
                String[] vTagAndTel = ATTRIBUTE_PATTERN.split(attr[i], 2);
                int telLenBefore = vTagAndTel[1].length();
                int telLenBefore = vTagAndTel[1].length();
                // Remove '-', '(', ')' or ' ' from TEL number
                // Remove '-', '(', ')' or ' ' from TEL number
                vTagAndTel[1] =
                vTagAndTel[1] =
@@ -1217,7 +1222,7 @@ public class BluetoothPbapVcardManager {


        for (int i = 0; i < attr.length; i++) {
        for (int i = 0; i < attr.length; i++) {
            if (!attr[i].isEmpty()) {
            if (!attr[i].isEmpty()) {
                stripedVCard = stripedVCard.concat(attr[i] + separator);
                stripedVCard = stripedVCard.concat(attr[i] + SEPARATOR);
            }
            }
        }
        }
        Log.v(TAG, "vCard with stripped telephone no.: " + stripedVCard);
        Log.v(TAG, "vCard with stripped telephone no.: " + stripedVCard);
@@ -1253,7 +1258,6 @@ public class BluetoothPbapVcardManager {
            }
            }
        }
        }


        private static final String SEPARATOR = System.getProperty("line.separator");
        private final byte[] mFilter;
        private final byte[] mFilter;


        // This function returns true if the attributes needs to be included in the filtered vcard.
        // This function returns true if the attributes needs to be included in the filtered vcard.
@@ -1284,7 +1288,7 @@ public class BluetoothPbapVcardManager {
            if (mFilter == null) {
            if (mFilter == null) {
                return vCard;
                return vCard;
            }
            }
            String[] lines = vCard.split(SEPARATOR);
            String[] lines = SEPARATOR_PATTERN.split(vCard);
            StringBuilder filteredVCard = new StringBuilder();
            StringBuilder filteredVCard = new StringBuilder();
            boolean filteredIn = false;
            boolean filteredIn = false;


@@ -1292,7 +1296,7 @@ public class BluetoothPbapVcardManager {
                // Check whether the current property is changing (ignoring multi-line properties)
                // Check whether the current property is changing (ignoring multi-line properties)
                // and determine if the current property is filtered in.
                // and determine if the current property is filtered in.
                if (!Character.isWhitespace(line.charAt(0)) && !line.startsWith("=")) {
                if (!Character.isWhitespace(line.charAt(0)) && !line.startsWith("=")) {
                    String currentProp = line.split("[;:]")[0];
                    String currentProp = PROPERTY_PATTERN.split(line, 2)[0];
                    filteredIn = true;
                    filteredIn = true;


                    for (FilterBit bit : FilterBit.values()) {
                    for (FilterBit bit : FilterBit.values()) {
@@ -1352,7 +1356,6 @@ public class BluetoothPbapVcardManager {
            }
            }
        }
        }


        private static final String SEPARATOR = System.getProperty("line.separator");
        private final byte[] mSelector;
        private final byte[] mSelector;


        PropertySelector(byte[] selector) {
        PropertySelector(byte[] selector) {
@@ -1463,7 +1466,7 @@ public class BluetoothPbapVcardManager {


    @VisibleForTesting
    @VisibleForTesting
    static String getNameFromVCard(String vCard) {
    static String getNameFromVCard(String vCard) {
        String[] lines = vCard.split(PropertySelector.SEPARATOR);
        String[] lines = SEPARATOR_PATTERN.split(vCard);
        String name = "";
        String name = "";
        for (String line : lines) {
        for (String line : lines) {
            if (!Character.isWhitespace(line.charAt(0)) && !line.startsWith("=")) {
            if (!Character.isWhitespace(line.charAt(0)) && !line.startsWith("=")) {
@@ -1477,10 +1480,10 @@ public class BluetoothPbapVcardManager {
    }
    }


    private static boolean doesVCardHaveProperty(String vCard, String property) {
    private static boolean doesVCardHaveProperty(String vCard, String property) {
        String[] lines = vCard.split(PropertySelector.SEPARATOR);
        String[] lines = SEPARATOR_PATTERN.split(vCard);
        for (String line : lines) {
        for (String line : lines) {
            if (!Character.isWhitespace(line.charAt(0)) && !line.startsWith("=")) {
            if (!Character.isWhitespace(line.charAt(0)) && !line.startsWith("=")) {
                String currentProperty = line.split("[;:]")[0];
                String currentProperty = PROPERTY_PATTERN.split(line)[0];
                if (property.equals(currentProperty)) {
                if (property.equals(currentProperty)) {
                    return true;
                    return true;
                }
                }