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

Commit fa6fb000 authored by William Escande's avatar William Escande
Browse files

ErrorProne: Enforce & fix StringCharset

See https://errorprone.info/bugpattern/StringCharset

Bug: 344658662
Test: m BluetoothInstrumentationTests
Flag: Exempt Build and test only
Change-Id: I33cd96234d625f4e78fdafb172a85aa813acec66
parent 9852a94e
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -332,6 +332,7 @@ android_app {
            "-Xep:NonApiType:ERROR",
            "-Xep:NonCanonicalType:ERROR",
            "-Xep:ReturnAtTheEndOfVoidFunction:ERROR",
            "-Xep:StringCharset:ERROR",
            "-Xep:UnusedMethod:ERROR",
            "-Xep:UnusedVariable:ERROR",
            "-XepExcludedPaths:.*/srcjars/.*", // Exclude generated files
+1 −3
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@ package com.android.bluetooth;

import com.android.bluetooth.map.BluetoothMapUtils;

import java.io.UnsupportedEncodingException;
import java.util.Objects;

/**
@@ -40,11 +39,10 @@ public class SignedLongLong implements Comparable<SignedLongLong> {
     *
     * @param value the hex-string
     * @return the created object
     * @throws UnsupportedEncodingException if "US-ASCII" charset is not supported,
     * @throws NullPointerException if the string {@code value} is null,
     * @throws NumberFormatException if the string {@code value} contains invalid characters.
     */
    public static SignedLongLong fromString(String value) throws UnsupportedEncodingException {
    public static SignedLongLong fromString(String value) {
        String lsbStr, msbStr;
        long msb = 0;

+3 −7
Original line number Diff line number Diff line
@@ -27,11 +27,11 @@ import org.xmlpull.v1.XmlSerializer;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * Contains the metadata that describes either (1) the desired size of a image to be downloaded or
 * (2) the extact size of an image to be uploaded.
 * (2) the exact size of an image to be uploaded.
 *
 * <p>When using this to assert the size of an image to download/pull, it's best to derive this
 * specific descriptor from any of the available BipImageFormat options returned from a
@@ -245,11 +245,7 @@ public class BipImageDescriptor {
     */
    public byte[] serialize() {
        String s = toString();
        try {
            return s != null ? s.getBytes("UTF-8") : null;
        } catch (UnsupportedEncodingException e) {
            return null;
        }
        return s != null ? s.getBytes(StandardCharsets.UTF_8) : null;
    }

    @Override
+11 −27
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ import org.xmlpull.v1.XmlSerializer;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@@ -198,7 +198,7 @@ public class BipImageProperties {
                                    new BipAttachmentFormat(
                                            contentType, charset, name, size, created, modified));
                        } else {
                            warn("Unrecognized tag in x-bt/img-properties object: " + tag);
                            Log.w(TAG, "Unrecognized tag in x-bt/img-properties object: " + tag);
                        }
                        break;
                    case XmlPullParser.END_TAG:
@@ -208,9 +208,9 @@ public class BipImageProperties {
            }
            return;
        } catch (XmlPullParserException e) {
            error("XML parser error when parsing XML", e);
            Log.e(TAG, "XML parser error when parsing XML", e);
        } catch (IOException e) {
            error("I/O error when parsing XML", e);
            Log.e(TAG, "I/O error when parsing XML", e);
        }
        throw new ParseException("Failed to parse image-properties from stream");
    }
@@ -317,7 +317,7 @@ public class BipImageProperties {
                BipPixel pixel = format.getPixel();
                int size = format.getSize();
                if (encoding == null || pixel == null) {
                    error("Native format " + format.toString() + " is invalid.");
                    Log.e(TAG, "Native format " + format.toString() + " is invalid.");
                    continue;
                }
                xmlMsgElement.startTag(null, "native");
@@ -335,7 +335,7 @@ public class BipImageProperties {
                int maxSize = format.getMaxSize();
                BipTransformation trans = format.getTransformation();
                if (encoding == null || pixel == null) {
                    error("Variant format " + format.toString() + " is invalid.");
                    Log.e(TAG, "Variant format " + format.toString() + " is invalid.");
                    continue;
                }
                xmlMsgElement.startTag(null, "variant");
@@ -358,7 +358,7 @@ public class BipImageProperties {
                BipDateTime created = format.getCreatedDate();
                BipDateTime modified = format.getModifiedDate();
                if (contentType == null || name == null) {
                    error("Attachment format " + format.toString() + " is invalid.");
                    Log.e(TAG, "Attachment format " + format.toString() + " is invalid.");
                    continue;
                }
                xmlMsgElement.startTag(null, "attachment");
@@ -383,11 +383,11 @@ public class BipImageProperties {
            xmlMsgElement.endDocument();
            return writer.toString();
        } catch (IllegalArgumentException e) {
            error("Falied to serialize ImageProperties", e);
            Log.e(TAG, "Failed to serialize ImageProperties", e);
        } catch (IllegalStateException e) {
            error("Falied to serialize ImageProperties", e);
            Log.e(TAG, "Failed to serialize ImageProperties", e);
        } catch (IOException e) {
            error("Falied to serialize ImageProperties", e);
            Log.e(TAG, "Failed to serialize ImageProperties", e);
        }
        return null;
    }
@@ -402,11 +402,7 @@ public class BipImageProperties {
    public byte[] serialize() {
        if (!isValid()) return null;
        String s = toString();
        try {
            return s != null ? s.getBytes("UTF-8") : null;
        } catch (UnsupportedEncodingException e) {
            return null;
        }
        return s != null ? s.getBytes(StandardCharsets.UTF_8) : null;
    }

    /**
@@ -419,16 +415,4 @@ public class BipImageProperties {
    public boolean isValid() {
        return sVersion.equals(mVersion) && mImageHandle != null && mHasThumbnailFormat;
    }

    private static void warn(String msg) {
        Log.w(TAG, msg);
    }

    private static void error(String msg) {
        Log.e(TAG, msg);
    }

    private static void error(String msg, Throwable e) {
        Log.e(TAG, msg, e);
    }
}
+5 −9
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ import com.android.bluetooth.flags.Flags;
import com.android.bluetooth.hfp.HeadsetHalConstants;
import com.android.internal.annotations.VisibleForTesting;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
@@ -667,14 +667,10 @@ public class RemoteDevices {

        public void setModelName(String modelName) {
            mModelName = modelName;
            try {
            mAdapterService.setMetadata(
                    this.mDevice,
                    BluetoothDevice.METADATA_MODEL_NAME,
                        mModelName.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException uee) {
                Log.e(TAG, "setModelName: UTF-8 not supported?!?"); // this should not happen
            }
                    mModelName.getBytes(StandardCharsets.UTF_8));
        }

        /**
Loading