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

Commit 818a3830 authored by Hyundo Moon's avatar Hyundo Moon Committed by Android (Google) Code Review
Browse files

Merge changes from topic "cherrypicker-L12400000957513984:N30500001315227278" into tm-qpr-dev

* changes:
  Add tests for nested classes in BluetoothPbapVcardManager
  Pull out an inner class HandlerForStringBuffer
parents db3feabe cc657928
Loading
Loading
Loading
Loading
+6 −64
Original line number Diff line number Diff line
@@ -38,8 +38,6 @@ import com.android.vcard.VCardBuilder;
import com.android.vcard.VCardConfig;
import com.android.vcard.VCardUtils;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@@ -386,7 +384,7 @@ public class BluetoothPbapSimVcardManager {
            composer = new BluetoothPbapSimVcardManager(context);
            buffer = new HandlerForStringBuffer(op, ownerVCard);

            if (!composer.init(SIM_URI, null, null, null) || !buffer.onInit(context)) {
            if (!composer.init(SIM_URI, null, null, null) || !buffer.init()) {
                return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
            }
            composer.moveToPosition(startPoint -1, false);
@@ -402,75 +400,19 @@ public class BluetoothPbapSimVcardManager {
                            + composer.getErrorReason() + ", count:" + count);
                    return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
                }
                buffer.onEntryCreated(vcard);
                buffer.writeVCard(vcard);
            }
        } finally {
            if (composer != null) {
                composer.terminate();
            }
            if (buffer != null) {
                buffer.onTerminate();
                buffer.terminate();
            }
        }
        return ResponseCodes.OBEX_HTTP_OK;
    }

    /**
     * Handler to emit vCards to PCE.
     */
    public static class HandlerForStringBuffer {
        private Operation operation;

        private OutputStream outputStream;

        private String phoneOwnVCard = null;

        public HandlerForStringBuffer(Operation op, String ownerVCard) {
            operation = op;
            if (ownerVCard != null) {
                phoneOwnVCard = ownerVCard;
                if (V) Log.v(TAG, "phoneOwnVCard \n " + phoneOwnVCard);
            }
        }

        private boolean write(String vCard) {
            try {
                if (vCard != null) {
                    outputStream.write(vCard.getBytes());
                    return true;
                }
            } catch (IOException e) {
                Log.e(TAG, "write outputstrem failed" + e.toString());
            }
            return false;
        }

        public boolean onInit(Context context) {
            try {
                outputStream = operation.openOutputStream();
                if (phoneOwnVCard != null) {
                    return write(phoneOwnVCard);
                }
                return true;
            } catch (IOException e) {
                Log.e(TAG, "open outputstrem failed" + e.toString());
            }
            return false;
        }

        public boolean onEntryCreated(String vcard) {
            return write(vcard);
        }

        public void onTerminate() {
            if (!BluetoothPbapObexServer.closeStream(outputStream, operation)) {
                if (V) Log.v(TAG, "CloseStream failed!");
            } else {
                if (V) Log.v(TAG, "CloseStream ok!");
            }
        }
    }

    public static final int composeAndSendSIMPhonebookOneVcard(Context context, Operation op,
            final int offset, final boolean vcardType21, String ownerVCard,
            int orderByWhat) {
@@ -484,7 +426,7 @@ public class BluetoothPbapSimVcardManager {
        try {
            composer = new BluetoothPbapSimVcardManager(context);
            buffer = new HandlerForStringBuffer(op, ownerVCard);
            if (!composer.init(SIM_URI, null, null, null) || !buffer.onInit(context)) {
            if (!composer.init(SIM_URI, null, null, null) || !buffer.init()) {
                return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
            }
            if (orderByWhat == BluetoothPbapObexServer.ORDER_BY_INDEXED) {
@@ -502,13 +444,13 @@ public class BluetoothPbapSimVcardManager {
                            + composer.getErrorReason());
                return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
            }
            buffer.onEntryCreated(vcard);
            buffer.writeVCard(vcard);
        } finally {
            if (composer != null) {
                composer.terminate();
            }
            if (buffer != null) {
                buffer.onTerminate();
                buffer.terminate();
            }
        }

+88 −147
Original line number Diff line number Diff line
@@ -62,8 +62,6 @@ import com.android.vcard.VCardComposer;
import com.android.vcard.VCardConfig;
import com.android.vcard.VCardPhoneNumberTranslationCallback;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
@@ -317,7 +315,7 @@ public class BluetoothPbapVcardManager {

    final ArrayList<String> getSelectedPhonebookNameList(final int orderByWhat,
            final boolean vcardType21, int needSendBody, int pbSize, byte[] selector,
            String vcardselectorop) {
            String vCardSelectorOperator) {
        ArrayList<String> nameList = new ArrayList<String>();
        PropertySelector vcardselector = new PropertySelector(selector);
        VCardComposer composer = null;
@@ -391,13 +389,13 @@ public class BluetoothPbapVcardManager {
                        Log.v(TAG, "Checking selected bits in the vcard composer" + vcard);
                    }

                    if (!vcardselector.checkVCardSelector(vcard, vcardselectorop)) {
                    if (!vcardselector.checkVCardSelector(vcard, vCardSelectorOperator)) {
                        Log.e(TAG, "vcard selector check fail");
                        vcard = null;
                        pbSize--;
                        continue;
                    } else {
                        String name = vcardselector.getName(vcard);
                        String name = getNameFromVCard(vcard);
                        if (TextUtils.isEmpty(name)) {
                            name = mContext.getString(android.R.string.unknownName);
                        }
@@ -659,14 +657,14 @@ public class BluetoothPbapVcardManager {
    /**
     * Filter contact cursor by certain condition.
     */
    private static final class ContactCursorFilter {
    static final class ContactCursorFilter {
        /**
         *
         * @param contactCursor
         * @param offset
         * @return a cursor containing contact id of {@code offset} contact.
         */
        public static Cursor filterByOffset(Cursor contactCursor, int offset) {
        static Cursor filterByOffset(Cursor contactCursor, int offset) {
            return filterByRange(contactCursor, offset, offset);
        }

@@ -676,9 +674,9 @@ public class BluetoothPbapVcardManager {
         * @param startPoint
         * @param endPoint
         * @return a cursor containing contact ids of {@code startPoint}th to {@code endPoint}th
         * contact.
         * contact. (i.e. [startPoint, endPoint], both points should be greater than 0)
         */
        public static Cursor filterByRange(Cursor contactCursor, int startPoint, int endPoint) {
        static Cursor filterByRange(Cursor contactCursor, int startPoint, int endPoint) {
            final int contactIdColumn = contactCursor.getColumnIndex(Data.CONTACT_ID);
            long previousContactId = -1;
            // As startPoint, endOffset index starts from 1 to n, we set
@@ -752,7 +750,7 @@ public class BluetoothPbapVcardManager {
            });
            buffer = new HandlerForStringBuffer(op, ownerVCard);
            Log.v(TAG, "contactIdCursor size: " + contactIdCursor.getCount());
            if (!composer.init(contactIdCursor) || !buffer.onInit(mContext)) {
            if (!composer.init(contactIdCursor) || !buffer.init()) {
                return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
            }
            int idColumn = contactIdCursor.getColumnIndex(Data.CONTACT_ID);
@@ -789,7 +787,7 @@ public class BluetoothPbapVcardManager {
                    Log.v(TAG, "vCard after cleanup: " + vcard);
                }

                if (!buffer.onEntryCreated(vcard)) {
                if (!buffer.writeVCard(vcard)) {
                    // onEntryCreate() already emits error.
                    return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
                }
@@ -799,7 +797,7 @@ public class BluetoothPbapVcardManager {
                composer.terminate();
            }
            if (buffer != null) {
                buffer.onTerminate();
                buffer.terminate();
            }
        }

@@ -857,7 +855,7 @@ public class BluetoothPbapVcardManager {
            });
            buffer = new HandlerForStringBuffer(op, ownerVCard);
            Log.v(TAG, "contactIdCursor size: " + contactIdCursor.getCount());
            if (!composer.init(contactIdCursor) || !buffer.onInit(mContext)) {
            if (!composer.init(contactIdCursor) || !buffer.init()) {
                return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
            }
            int idColumn = contactIdCursor.getColumnIndex(Data.CONTACT_ID);
@@ -904,7 +902,7 @@ public class BluetoothPbapVcardManager {
                        Log.v(TAG, "vCard after cleanup: " + vcard);
                    }

                    if (!buffer.onEntryCreated(vcard)) {
                    if (!buffer.writeVCard(vcard)) {
                        // onEntryCreate() already emits error.
                        return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
                    }
@@ -919,7 +917,7 @@ public class BluetoothPbapVcardManager {
                composer.terminate();
            }
            if (buffer != null) {
                buffer.onTerminate();
                buffer.terminate();
            }
        }

@@ -949,7 +947,7 @@ public class BluetoothPbapVcardManager {
            composer = new BluetoothPbapCallLogComposer(mContext);
            buffer = new HandlerForStringBuffer(op, ownerVCard);
            if (!composer.init(CallLog.Calls.CONTENT_URI, selection, null, CALLLOG_SORT_ORDER)
                    || !buffer.onInit(mContext)) {
                    || !buffer.init()) {
                return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
            }

@@ -982,7 +980,7 @@ public class BluetoothPbapVcardManager {
                            Log.v(TAG, "Vcard Entry:");
                            Log.v(TAG, vcard);
                        }
                        buffer.onEntryCreated(vcard);
                        buffer.writeVCard(vcard);
                    }
                } else {
                    if (vcard == null) {
@@ -994,7 +992,7 @@ public class BluetoothPbapVcardManager {
                        Log.v(TAG, "Vcard Entry:");
                        Log.v(TAG, vcard);
                    }
                    buffer.onEntryCreated(vcard);
                    buffer.writeVCard(vcard);
                }
            }
            if (needSendBody != NEED_SEND_BODY && vCardSelct) {
@@ -1005,7 +1003,7 @@ public class BluetoothPbapVcardManager {
                composer.terminate();
            }
            if (buffer != null) {
                buffer.onTerminate();
                buffer.terminate();
            }
        }

@@ -1050,72 +1048,6 @@ public class BluetoothPbapVcardManager {
        return stripedVCard;
    }

    // TODO: Merge this class with BluetoothPbapSimVcardManager.HandlerForStringBuffer
    /**
     * Handler to emit vCards to PCE.
     */
    public class HandlerForStringBuffer {
        private Operation mOperation;

        private OutputStream mOutputStream;

        private String mPhoneOwnVCard = null;

        public HandlerForStringBuffer(Operation op, String ownerVCard) {
            mOperation = op;
            if (ownerVCard != null) {
                mPhoneOwnVCard = ownerVCard;
                if (V) {
                    Log.v(TAG, "phone own number vcard:");
                }
                if (V) {
                    Log.v(TAG, mPhoneOwnVCard);
                }
            }
        }

        private boolean write(String vCard) {
            try {
                if (vCard != null) {
                    mOutputStream.write(vCard.getBytes());
                    return true;
                }
            } catch (IOException e) {
                Log.e(TAG, "write outputstrem failed" + e.toString());
            }
            return false;
        }

        public boolean onInit(Context context) {
            try {
                mOutputStream = mOperation.openOutputStream();
                if (mPhoneOwnVCard != null) {
                    return write(mPhoneOwnVCard);
                }
                return true;
            } catch (IOException e) {
                Log.e(TAG, "open outputstrem failed" + e.toString());
            }
            return false;
        }

        public boolean onEntryCreated(String vcard) {
            return write(vcard);
        }

        public void onTerminate() {
            if (!BluetoothPbapObexServer.closeStream(mOutputStream, mOperation)) {
                if (V) {
                    Log.v(TAG, "CloseStream failed!");
                }
            } else {
                if (V) {
                    Log.v(TAG, "CloseStream ok!");
                }
            }
        }
    }

    public static class VCardFilter {
        private enum FilterBit {
            //       bit  property                  onlyCheckV21  excludeForV21
@@ -1158,7 +1090,7 @@ public class BluetoothPbapVcardManager {
            if (vCardType21 && bit.excludeForV21) {
                return false;
            }
            if (mFilter == null || offset >= mFilter.length) {
            if (mFilter == null || offset > mFilter.length) {
                return true;
            }
            return ((mFilter[mFilter.length - offset] >> bitPos) & 0x01) != 0;
@@ -1215,7 +1147,8 @@ public class BluetoothPbapVcardManager {
        }
    }

    private static class PropertySelector {
    @VisibleForTesting
    static class PropertySelector {
        private enum PropertyMask {
            //               bit    property
            VERSION(0, "VERSION"),
@@ -1234,12 +1167,12 @@ public class BluetoothPbapVcardManager {
            NICKNAME(23, "NICKNAME"),
            DATETIME(28, "DATETIME");

            public final int pos;
            public final String prop;
            public final int mBitPosition;
            public final String mProperty;

            PropertyMask(int pos, String prop) {
                this.pos = pos;
                this.prop = prop;
            PropertyMask(int bitPosition, String property) {
                this.mBitPosition = bitPosition;
                this.mProperty = property;
            }
        }

@@ -1250,71 +1183,51 @@ public class BluetoothPbapVcardManager {
            this.mSelector = selector;
        }

        private boolean checkbit(int attrBit, byte[] selector) {
            int selectorlen = selector.length;
            if (((selector[selectorlen - 1 - ((int) attrBit / 8)] >> (attrBit % 8)) & 0x01) == 0) {
                return false;
            }
            return true;
        }
        boolean checkVCardSelector(String vCard, String vCardSelectorOperator) {
            Log.d(TAG, "vCardSelectorOperator=" + vCardSelectorOperator);

        private boolean checkprop(String vcard, String prop) {
            String[] lines = vcard.split(SEPARATOR);
            boolean isPresent = false;
            for (String line : lines) {
                if (!Character.isWhitespace(line.charAt(0)) && !line.startsWith("=")) {
                    String currentProp = line.split("[;:]")[0];
                    if (prop.equals(currentProp)) {
                        Log.d(TAG, "bit.prop.equals current prop :" + prop);
                        isPresent = true;
                        return isPresent;
                    }
                }
            }
            final boolean checkAtLeastOnePropertyExists = vCardSelectorOperator.equals("0");
            final boolean checkAllPropertiesExist = vCardSelectorOperator.equals("1");

            return isPresent;
        }
            boolean result = true;

        private boolean checkVCardSelector(String vcard, String vcardselectorop) {
            boolean selectedIn = true;
            if (checkAtLeastOnePropertyExists) {
                for (PropertyMask mask : PropertyMask.values()) {
                    if (!checkBit(mask.mBitPosition, mSelector)) {
                        continue;
                    }
                    Log.d(TAG, "checking for prop :" + mask.mProperty);

            for (PropertyMask bit : PropertyMask.values()) {
                if (checkbit(bit.pos, mSelector)) {
                    Log.d(TAG, "checking for prop :" + bit.prop);
                    if (vcardselectorop.equals("0")) {
                        if (checkprop(vcard, bit.prop)) {
                            Log.d(TAG, "bit.prop.equals current prop :" + bit.prop);
                            selectedIn = true;
                            break;
                    if (doesVCardHaveProperty(vCard, mask.mProperty)) {
                        Log.d(TAG, "mask.prop.equals current prop :" + mask.mProperty);
                        return true;
                    } else {
                            selectedIn = false;
                        result = false;
                    }
                    } else if (vcardselectorop.equals("1")) {
                        if (!checkprop(vcard, bit.prop)) {
                            Log.d(TAG, "bit.prop.notequals current prop" + bit.prop);
                            selectedIn = false;
                            return selectedIn;
                        } else {
                            selectedIn = true;
                }
            } else if (checkAllPropertiesExist) {
                for (PropertyMask mask : PropertyMask.values()) {
                    if (!checkBit(mask.mBitPosition, mSelector)) {
                        continue;
                    }
                    Log.d(TAG, "checking for prop :" + mask.mProperty);

                    if (!doesVCardHaveProperty(vCard, mask.mProperty)) {
                        Log.d(TAG, "mask.prop.notequals current prop" + mask.mProperty);
                        return false;
                    }
                }
            return selectedIn;
            }

        private String getName(String vcard) {
            String[] lines = vcard.split(SEPARATOR);
            String name = "";
            for (String line : lines) {
                if (!Character.isWhitespace(line.charAt(0)) && !line.startsWith("=")) {
                    if (line.startsWith("N:")) {
                        name = line.substring(line.lastIndexOf(':'), line.length());
                    }
            return result;
        }

        private boolean checkBit(int attrBit, byte[] selector) {
            int offset = (attrBit / 8) + 1;
            if (mSelector == null || offset > mSelector.length) {
                return false;
            }
            Log.d(TAG, "returning name: " + name);
            return name;
            return ((selector[mSelector.length - offset] >> (attrBit % 8)) & 0x01) != 0;
        }
    }

@@ -1375,4 +1288,32 @@ public class BluetoothPbapVcardManager {
            }
        }
    }

    @VisibleForTesting
    static String getNameFromVCard(String vCard) {
        String[] lines = vCard.split(PropertySelector.SEPARATOR);
        String name = "";
        for (String line : lines) {
            if (!Character.isWhitespace(line.charAt(0)) && !line.startsWith("=")) {
                if (line.startsWith("N:")) {
                    name = line.substring(line.lastIndexOf(':') + 1);
                }
            }
        }
        Log.d(TAG, "returning name: " + name);
        return name;
    }

    private static boolean doesVCardHaveProperty(String vCard, String property) {
        String[] lines = vCard.split(PropertySelector.SEPARATOR);
        for (String line : lines) {
            if (!Character.isWhitespace(line.charAt(0)) && !line.startsWith("=")) {
                String currentProperty = line.split("[;:]")[0];
                if (property.equals(currentProperty)) {
                    return true;
                }
            }
        }
        return false;
    }
}
+80 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 The Android Open Source 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 com.android.bluetooth.pbap;

import android.util.Log;

import com.android.obex.Operation;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Handler to emit vCards to PCE.
 */
public class HandlerForStringBuffer {
    private static final String TAG = "HandlerForStringBuffer";

    private final Operation mOperation;
    private final String mOwnerVCard;

    private OutputStream mOutputStream;

    public HandlerForStringBuffer(Operation op, String ownerVCard) {
        mOperation = op;
        mOwnerVCard = ownerVCard;
        if (BluetoothPbapService.VERBOSE) {
            Log.v(TAG, "ownerVCard \n " + mOwnerVCard);
        }
    }

    public boolean init() {
        try {
            mOutputStream = mOperation.openOutputStream();
            if (mOwnerVCard != null) {
                return writeVCard(mOwnerVCard);
            }
            return true;
        } catch (IOException e) {
            Log.e(TAG, "openOutputStream failed", e);
        }
        return false;
    }

    public boolean writeVCard(String vCard) {
        try {
            if (vCard != null) {
                mOutputStream.write(vCard.getBytes());
                return true;
            }
        } catch (IOException e) {
            Log.e(TAG, "write failed", e);
        }
        return false;
    }

    public void terminate() {
        boolean result = BluetoothPbapObexServer.closeStream(mOutputStream, mOperation);
        if (BluetoothPbapService.VERBOSE) {
            if (result) {
                Log.v(TAG, "closeStream succeeded!");
            } else {
                Log.v(TAG, "closeStream failed!");
            }
        }
    }
}
+216 −0

File added.

Preview size limit exceeded, changes collapsed.

+10 −0
Original line number Diff line number Diff line
@@ -333,4 +333,14 @@ public class BluetoothPbapVcardManagerTest {

        assertThat(mManager.stripTelephoneNumber(vCard)).isEqualTo(expectedResult);
    }

    @Test
    public void getNameFromVCard() {
        final String separator = System.getProperty("line.separator");
        String vCard = "N:Test Name" + separator
                + "FN:Test Full Name" + separator
                + "EMAIL:android@android.com:" + separator;

        assertThat(BluetoothPbapVcardManager.getNameFromVCard(vCard)).isEqualTo("Test Name");
    }
}
Loading