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

Commit 2592031a authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes I8e8bc355,I0da53058,I7a61024d,Iea22294f into main

* changes:
  Framework: Fix & enforce AndroidFrameworkEfficientParcelable
  Framework: Fix & enforce AndroidFrameworkEfficientStrings
  Framework: Fix & enforce some errorprone
  Framework: Fix & enforce AndroidHideInComments
parents fc966368 664e2abe
Loading
Loading
Loading
Loading
+2 −107
Original line number Diff line number Diff line
@@ -1323,114 +1323,9 @@ public final class Utils {
    }

    /**
     * Simple alternative to {@link String#format} which purposefully supports only a small handful
     * of substitutions to improve execution speed. Benchmarking reveals this optimized alternative
     * performs 6.5x faster for a typical format string.
     *
     * <p>Below is a summary of the limited grammar supported by this method; if you need advanced
     * features, please continue using {@link String#format}.
     *
     * <ul>
     *   <li>{@code %b} for {@code boolean}
     *   <li>{@code %c} for {@code char}
     *   <li>{@code %d} for {@code int} or {@code long}
     *   <li>{@code %f} for {@code float} or {@code double}
     *   <li>{@code %s} for {@code String}
     *   <li>{@code %x} for hex representation of {@code int} or {@code long} or {@code byte}
     *   <li>{@code %%} for literal {@code %}
     *   <li>{@code %04d} style grammar to specify the argument width, such as {@code %04d} to
     *       prefix an {@code int} with zeros or {@code %10b} to prefix a {@code boolean} with
     *       spaces
     * </ul>
     *
     * <p>(copied from framework/base/core/java/android/text/TextUtils.java)
     *
     * <p>See {@code android.text.TextUtils.formatSimple}
     *
     * @throws IllegalArgumentException if the format string or arguments don't match the supported
     *     grammar described above.
     * @hide
     * @see android.bluetooth.BluetoothUtils.formatSimple
     */
    public static @NonNull String formatSimple(@NonNull String format, Object... args) {
        final StringBuilder sb = new StringBuilder(format);
        int j = 0;
        for (int i = 0; i < sb.length(); ) {
            if (sb.charAt(i) == '%') {
                char code = sb.charAt(i + 1);

                // Decode any argument width request
                char prefixChar = '\0';
                int prefixLen = 0;
                int consume = 2;
                while ('0' <= code && code <= '9') {
                    if (prefixChar == '\0') {
                        prefixChar = (code == '0') ? '0' : ' ';
                    }
                    prefixLen *= 10;
                    prefixLen += Character.digit(code, 10);
                    consume += 1;
                    code = sb.charAt(i + consume - 1);
                }

                final String repl;
                switch (code) {
                    case 'b' -> {
                        if (j == args.length) {
                            throw new IllegalArgumentException("Too few arguments");
                        }
                        final Object arg = args[j++];
                        if (arg instanceof Boolean) {
                            repl = Boolean.toString((boolean) arg);
                        } else {
                            repl = Boolean.toString(arg != null);
                        }
                    }
                    case 'c', 'd', 'f', 's' -> {
                        if (j == args.length) {
                            throw new IllegalArgumentException("Too few arguments");
                        }
                        final Object arg = args[j++];
                        repl = String.valueOf(arg);
                    }
                    case 'x' -> {
                        if (j == args.length) {
                            throw new IllegalArgumentException("Too few arguments");
                        }
                        final Object arg = args[j++];
                        if (arg instanceof Integer) {
                            repl = Integer.toHexString((int) arg);
                        } else if (arg instanceof Long) {
                            repl = Long.toHexString((long) arg);
                        } else if (arg instanceof Byte) {
                            repl = Integer.toHexString(Byte.toUnsignedInt((byte) arg));
                        } else {
                            throw new IllegalArgumentException(
                                    "Unsupported hex type " + arg.getClass());
                        }
                    }
                    case '%' -> {
                        repl = "%";
                    }
                    default -> {
                        throw new IllegalArgumentException("Unsupported format code " + code);
                    }
                }

                sb.replace(i, i + consume, repl);

                // Apply any argument width request
                final int prefixInsert = (prefixChar == '0' && repl.charAt(0) == '-') ? 1 : 0;
                for (int k = repl.length(); k < prefixLen; k++) {
                    sb.insert(i + prefixInsert, prefixChar);
                }
                i += Math.max(repl.length(), prefixLen);
            } else {
                i++;
            }
        }
        if (j != args.length) {
            throw new IllegalArgumentException("Too many arguments");
        }
        return sb.toString();
        return android.bluetooth.BluetoothUtils.formatSimple(format, args);
    }
}
+15 −1
Original line number Diff line number Diff line
@@ -95,7 +95,13 @@ java_sdk_library {
        javacflags: [
            "-Xep:InlineMeSuggester:OFF", // The @InlineMe annotation is not available

            "-Xep:AndroidFrameworkCompatChange:ERROR",
            "-Xep:AndroidFrameworkEfficientParcelable:ERROR",
            "-Xep:AndroidFrameworkEfficientStrings:ERROR",
            "-Xep:AndroidFrameworkRequiresPermission:ERROR",
            "-Xep:AndroidFrameworkRethrowFromSystem:ERROR",
            "-Xep:AndroidFrameworkTargetSdk:ERROR",
            "-Xep:AndroidHideInComments:ERROR",
            "-Xep:BadImport:ERROR",
            "-Xep:CatchFail:ERROR",
            "-Xep:ClassCanBeStatic:ERROR",
@@ -115,8 +121,16 @@ java_sdk_library {
            "-Xep:StringCharset:ERROR",
            "-Xep:UnnecessaryAssignment:ERROR",
            "-Xep:UnnecessaryAsync:ERROR",
            "-Xep:UnusedVariable:ERROR",

            "-XepExcludedPaths:.*/srcjars/.*", // Exclude generated files
            // After fixing this errorprone, we decided to not merge the change.
            // It is not very readable and the benefits are minimal when looking
            // at the size of the maps used in the Bluetooth application.
            // See https://r.android.com/3200511
            "-Xep:AndroidFrameworkEfficientCollections:OFF",

            // Exclude generated files
            "-XepExcludedPaths:.*/srcjars/.*",
        ],
        enabled: true,
    },
+11 −9
Original line number Diff line number Diff line
@@ -3320,18 +3320,16 @@ public final class BluetoothAdapter {
                }
            case BluetoothStatusCodes.RFCOMM_LISTENER_OPERATION_FAILED_DIFFERENT_APP:
                throw new IllegalStateException(
                        String.format(
                                "RFCOMM listener for UUID %s was not registered by this app",
                                uuid));
                        "RFCOMM listener for UUID " + uuid + " was not registered by this app");
            case BluetoothStatusCodes.RFCOMM_LISTENER_NO_SOCKET_AVAILABLE:
                return null;
            default:
                Log.e(
                        TAG,
                        String.format(
                                "Unexpected result: (%d), from the adapter service while retrieving"
                                        + " an rfcomm socket",
                                socketInfo.status));
                        "Unexpected result: ("
                                + socketInfo.status
                                + "), from the adapter service"
                                + " while retrieving an rfcomm socket");
                return null;
        }
    }
@@ -3589,7 +3587,11 @@ public final class BluetoothAdapter {
     *     BluetoothProfile#HEARING_AID} or {@link BluetoothProfile#GATT_SERVER}.
     * @return true on success, false on error
     */
    @SuppressLint({"AndroidFrameworkRequiresPermission", "AndroidFrameworkBluetoothPermission"})
    @SuppressLint({
        "AndroidFrameworkRequiresPermission",
        "AndroidFrameworkBluetoothPermission",
        "AndroidFrameworkCompatChange"
    })
    public boolean getProfileProxy(
            Context context, BluetoothProfile.ServiceListener listener, int profile) {
        if (context == null || listener == null) {
@@ -3617,7 +3619,7 @@ public final class BluetoothAdapter {
        // Preserve legacy compatibility where apps were depending on
        // registerStateChangeCallback() performing a permissions check which
        // has been relaxed in modern platform versions
        if (context.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.R
        if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.S
                && context.checkSelfPermission(BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Need BLUETOOTH permission");
        }
+5 −3
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.bluetooth;

import static android.bluetooth.BluetoothUtils.formatSimple;

import android.annotation.FlaggedApi;
import android.annotation.IntDef;
import android.annotation.NonNull;
@@ -450,14 +452,14 @@ public final class BluetoothCodecConfig implements Parcelable {
        return ("{codecName:" + codecName)
                + (",mCodecType:" + codecType)
                + (",mCodecPriority:" + mCodecPriority)
                + (",mSampleRate:" + String.format("0x%x", mSampleRate) + "(" + sampleRateStr + ")")
                + (",mSampleRate:" + formatSimple("0x%x", mSampleRate) + "(" + sampleRateStr + ")")
                + (",mBitsPerSample:"
                        + String.format("0x%x", mBitsPerSample)
                        + formatSimple("0x%x", mBitsPerSample)
                        + "("
                        + bitsPerSampleStr
                        + ")")
                + (",mChannelMode:"
                        + String.format("0x%x", mChannelMode)
                        + formatSimple("0x%x", mChannelMode)
                        + "("
                        + channelModeStr
                        + ")")
+1 −1
Original line number Diff line number Diff line
@@ -160,7 +160,7 @@ public final class BluetoothCodecType implements Parcelable {
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeInt(mNativeCodecType);
        dest.writeLong(mCodecId);
        dest.writeString(mCodecName);
        BluetoothUtils.writeStringToParcel(dest, mCodecName);
    }

    /**
Loading