Loading android/app/src/com/android/bluetooth/map/BluetoothMapConvoListingElement.java +1 −2 Original line number Diff line number Diff line Loading @@ -226,8 +226,7 @@ public class BluetoothMapConvoListingElement private String getSummary() { if (mSummary != null) { try { return new String(BluetoothMapUtils.truncateUtf8StringToBytearray(mSummary, 256), "UTF-8"); return BluetoothMapUtils.truncateUtf8StringToString(mSummary, 256); } catch (UnsupportedEncodingException e) { // This cannot happen on an Android platform - UTF-8 is mandatory Log.e(TAG, "Missing UTF-8 support on platform", e); Loading android/app/src/com/android/bluetooth/map/BluetoothMapUtils.java +31 −0 Original line number Diff line number Diff line Loading @@ -22,8 +22,13 @@ import com.android.bluetooth.mapapi.BluetoothMapContract; import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CodingErrorAction; import java.nio.charset.IllegalCharsetNameException; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.BitSet; Loading Loading @@ -447,6 +452,32 @@ public class BluetoothMapUtils { return utf8Bytes; } /** * Truncate UTF-8 string encoded to desired length * @param utf8InString String to truncate * @param maxBytesLength Max length in bytes of the returned string * @return A valid truncated utf-8 string * @throws UnsupportedEncodingException */ public static String truncateUtf8StringToString(String utf8InString, int maxBytesLength) throws UnsupportedEncodingException { Charset charset = StandardCharsets.UTF_8; final byte[] utf8InBytes = utf8InString.getBytes(charset); if (utf8InBytes.length <= maxBytesLength) { return utf8InString; } // Create a buffer that wildly truncate at desired lengtht. // It may contain invalid utf-8 char. ByteBuffer truncatedString = ByteBuffer.wrap(utf8InBytes, 0, maxBytesLength); CharBuffer validUtf8Buffer = CharBuffer.allocate(maxBytesLength); // Decode From the truncatedString into a valid Utf8 CharBuffer while ignoring(discarding) // any invalid utf-8 CharsetDecoder decoder = charset.newDecoder().onMalformedInput(CodingErrorAction.IGNORE); decoder.decode(truncatedString, validUtf8Buffer, true); decoder.flush(validUtf8Buffer); return new String(validUtf8Buffer.array(), 0, validUtf8Buffer.position()); } private static final Pattern PATTERN = Pattern.compile("=\\?(.+?)\\?(.)\\?(.+?(?=\\?=))\\?="); /** Loading android/app/tests/unit/src/com/android/bluetooth/map/BluetoothMapConvoContactElementTest.java +3 −2 Original line number Diff line number Diff line Loading @@ -20,10 +20,11 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.when; import android.util.Xml; import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.SignedLongLong; import com.android.internal.util.FastXmlSerializer; import org.junit.Before; import org.junit.Test; Loading Loading @@ -124,7 +125,7 @@ public class BluetoothMapConvoContactElementTest { TEST_PRESENCE_STATUS, TEST_PRESENCE_AVAILABILITY, TEST_LAST_ACTIVITY, TEST_CHAT_STATE, TEST_PRIORITY, TEST_BT_UID); final XmlSerializer serializer = new FastXmlSerializer(); final XmlSerializer serializer = Xml.newSerializer(); final StringWriter writer = new StringWriter(); serializer.setOutput(writer); Loading android/app/tests/unit/src/com/android/bluetooth/map/BluetoothMapConvoListingElementTest.java +3 −2 Original line number Diff line number Diff line Loading @@ -18,11 +18,12 @@ package com.android.bluetooth.map; import static com.google.common.truth.Truth.assertThat; import android.util.Xml; import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.SignedLongLong; import com.android.bluetooth.map.BluetoothMapUtils.TYPE; import com.android.internal.util.FastXmlSerializer; import org.junit.Before; import org.junit.Test; Loading Loading @@ -120,7 +121,7 @@ public class BluetoothMapConvoListingElementTest { @Test public void encodeToXml_thenDecodeToInstance_returnsCorrectly() throws Exception { final XmlSerializer serializer = new FastXmlSerializer(); final XmlSerializer serializer = Xml.newSerializer(); final StringWriter writer = new StringWriter(); serializer.setOutput(writer); Loading android/app/tests/unit/src/com/android/bluetooth/map/BluetoothMapMessageListingElementTest.java +3 −2 Original line number Diff line number Diff line Loading @@ -18,10 +18,11 @@ package com.android.bluetooth.map; import static com.google.common.truth.Truth.assertThat; import android.util.Xml; import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.map.BluetoothMapUtils.TYPE; import com.android.internal.util.FastXmlSerializer; import org.junit.Before; import org.junit.Test; Loading Loading @@ -131,7 +132,7 @@ public class BluetoothMapMessageListingElementTest { public void encode() throws Exception { mMessageListingElement.setSubject(null); final XmlSerializer serializer = new FastXmlSerializer(); final XmlSerializer serializer = Xml.newSerializer(); final StringWriter writer = new StringWriter(); serializer.setOutput(writer); Loading Loading
android/app/src/com/android/bluetooth/map/BluetoothMapConvoListingElement.java +1 −2 Original line number Diff line number Diff line Loading @@ -226,8 +226,7 @@ public class BluetoothMapConvoListingElement private String getSummary() { if (mSummary != null) { try { return new String(BluetoothMapUtils.truncateUtf8StringToBytearray(mSummary, 256), "UTF-8"); return BluetoothMapUtils.truncateUtf8StringToString(mSummary, 256); } catch (UnsupportedEncodingException e) { // This cannot happen on an Android platform - UTF-8 is mandatory Log.e(TAG, "Missing UTF-8 support on platform", e); Loading
android/app/src/com/android/bluetooth/map/BluetoothMapUtils.java +31 −0 Original line number Diff line number Diff line Loading @@ -22,8 +22,13 @@ import com.android.bluetooth.mapapi.BluetoothMapContract; import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CodingErrorAction; import java.nio.charset.IllegalCharsetNameException; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.BitSet; Loading Loading @@ -447,6 +452,32 @@ public class BluetoothMapUtils { return utf8Bytes; } /** * Truncate UTF-8 string encoded to desired length * @param utf8InString String to truncate * @param maxBytesLength Max length in bytes of the returned string * @return A valid truncated utf-8 string * @throws UnsupportedEncodingException */ public static String truncateUtf8StringToString(String utf8InString, int maxBytesLength) throws UnsupportedEncodingException { Charset charset = StandardCharsets.UTF_8; final byte[] utf8InBytes = utf8InString.getBytes(charset); if (utf8InBytes.length <= maxBytesLength) { return utf8InString; } // Create a buffer that wildly truncate at desired lengtht. // It may contain invalid utf-8 char. ByteBuffer truncatedString = ByteBuffer.wrap(utf8InBytes, 0, maxBytesLength); CharBuffer validUtf8Buffer = CharBuffer.allocate(maxBytesLength); // Decode From the truncatedString into a valid Utf8 CharBuffer while ignoring(discarding) // any invalid utf-8 CharsetDecoder decoder = charset.newDecoder().onMalformedInput(CodingErrorAction.IGNORE); decoder.decode(truncatedString, validUtf8Buffer, true); decoder.flush(validUtf8Buffer); return new String(validUtf8Buffer.array(), 0, validUtf8Buffer.position()); } private static final Pattern PATTERN = Pattern.compile("=\\?(.+?)\\?(.)\\?(.+?(?=\\?=))\\?="); /** Loading
android/app/tests/unit/src/com/android/bluetooth/map/BluetoothMapConvoContactElementTest.java +3 −2 Original line number Diff line number Diff line Loading @@ -20,10 +20,11 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.when; import android.util.Xml; import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.SignedLongLong; import com.android.internal.util.FastXmlSerializer; import org.junit.Before; import org.junit.Test; Loading Loading @@ -124,7 +125,7 @@ public class BluetoothMapConvoContactElementTest { TEST_PRESENCE_STATUS, TEST_PRESENCE_AVAILABILITY, TEST_LAST_ACTIVITY, TEST_CHAT_STATE, TEST_PRIORITY, TEST_BT_UID); final XmlSerializer serializer = new FastXmlSerializer(); final XmlSerializer serializer = Xml.newSerializer(); final StringWriter writer = new StringWriter(); serializer.setOutput(writer); Loading
android/app/tests/unit/src/com/android/bluetooth/map/BluetoothMapConvoListingElementTest.java +3 −2 Original line number Diff line number Diff line Loading @@ -18,11 +18,12 @@ package com.android.bluetooth.map; import static com.google.common.truth.Truth.assertThat; import android.util.Xml; import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.SignedLongLong; import com.android.bluetooth.map.BluetoothMapUtils.TYPE; import com.android.internal.util.FastXmlSerializer; import org.junit.Before; import org.junit.Test; Loading Loading @@ -120,7 +121,7 @@ public class BluetoothMapConvoListingElementTest { @Test public void encodeToXml_thenDecodeToInstance_returnsCorrectly() throws Exception { final XmlSerializer serializer = new FastXmlSerializer(); final XmlSerializer serializer = Xml.newSerializer(); final StringWriter writer = new StringWriter(); serializer.setOutput(writer); Loading
android/app/tests/unit/src/com/android/bluetooth/map/BluetoothMapMessageListingElementTest.java +3 −2 Original line number Diff line number Diff line Loading @@ -18,10 +18,11 @@ package com.android.bluetooth.map; import static com.google.common.truth.Truth.assertThat; import android.util.Xml; import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.map.BluetoothMapUtils.TYPE; import com.android.internal.util.FastXmlSerializer; import org.junit.Before; import org.junit.Test; Loading Loading @@ -131,7 +132,7 @@ public class BluetoothMapMessageListingElementTest { public void encode() throws Exception { mMessageListingElement.setSubject(null); final XmlSerializer serializer = new FastXmlSerializer(); final XmlSerializer serializer = Xml.newSerializer(); final StringWriter writer = new StringWriter(); serializer.setOutput(writer); Loading