diff options
author | 2025-03-11 13:31:07 -0700 | |
---|---|---|
committer | 2025-03-11 13:31:07 -0700 | |
commit | 97adc120086ea731632865a1d79d736ad32bef8c (patch) | |
tree | f99660b7877b6e48b217c89ea518d2168bd67ecd | |
parent | 47c2d7d626c53b15a861ab3aa0e2c49c22dc847a (diff) | |
parent | 04f7ee7bc23c1084b6831067a52241846eb9924c (diff) |
Merge "Optimize and clean up Utils.java" into main
3 files changed, 31 insertions, 49 deletions
diff --git a/android/app/src/com/android/bluetooth/Utils.java b/android/app/src/com/android/bluetooth/Utils.java index 1f8f182b67..ce23339f0b 100644 --- a/android/app/src/com/android/bluetooth/Utils.java +++ b/android/app/src/com/android/bluetooth/Utils.java @@ -100,6 +100,14 @@ public final class Utils { public static final String TAG_PREFIX_BLUETOOTH = "Bluetooth"; private static final String TAG = TAG_PREFIX_BLUETOOTH + Utils.class.getSimpleName(); + public static final int BD_ADDR_LEN = 6; // bytes + private static final int BD_UUID_LEN = 16; // bytes + + /** Thread pool to handle background and outgoing blocking task */ + public static final ExecutorService BackgroundExecutor = Executors.newSingleThreadExecutor(); + + public static final String PAIRING_UI_PROPERTY = "bluetooth.pairing_ui_package.name"; + private static final int MICROS_PER_UNIT = 625; private static final String PTS_TEST_MODE_PROPERTY = "persist.bluetooth.pts"; @@ -115,13 +123,22 @@ public final class Utils { private static final String KEY_TEMP_ALLOW_LIST_DURATION_MS = "temp_allow_list_duration_ms"; private static final long DEFAULT_TEMP_ALLOW_LIST_DURATION_MS = 20_000; - static final int BD_ADDR_LEN = 6; // bytes - static final int BD_UUID_LEN = 16; // bytes + private static int sSystemUiUid = USER_HANDLE_NULL.getIdentifier(); + private static int sForegroundUserId = USER_HANDLE_NULL.getIdentifier(); - /** Thread pool to handle background and outgoing blocking task */ - public static final ExecutorService BackgroundExecutor = Executors.newSingleThreadExecutor(); + private Utils() {} - public static final String PAIRING_UI_PROPERTY = "bluetooth.pairing_ui_package.name"; + public static void setSystemUiUid(int uid) { + sSystemUiUid = uid; + } + + public static int getForegroundUserId() { + return sForegroundUserId; + } + + public static void setForegroundUserId(int userId) { + sForegroundUserId = userId; + } /** * Check if dual mode audio is enabled. This is set via the system property @@ -347,17 +364,6 @@ public final class Utils { return converter.getLong(offset); } - public static String byteArrayToString(byte[] valueBuf) { - StringBuilder sb = new StringBuilder(); - for (int idx = 0; idx < valueBuf.length; idx++) { - if (idx != 0) { - sb.append(" "); - } - sb.append(formatSimple("%02x", valueBuf[idx])); - } - return sb.toString(); - } - /** * A parser to transfer a byte array to a UTF8 string * @@ -429,22 +435,6 @@ public final class Utils { return puuids; } - static int sSystemUiUid = USER_HANDLE_NULL.getIdentifier(); - - public static void setSystemUiUid(int uid) { - Utils.sSystemUiUid = uid; - } - - static int sForegroundUserId = USER_HANDLE_NULL.getIdentifier(); - - public static int getForegroundUserId() { - return Utils.sForegroundUserId; - } - - public static void setForegroundUserId(int userId) { - Utils.sForegroundUserId = userId; - } - /** * Enforces that a Companion Device Manager (CDM) association exists between the calling * application and the Bluetooth Device. @@ -750,7 +740,7 @@ public final class Utils { || (UserHandle.getAppId(Process.SYSTEM_UID) == UserHandle.getAppId(callingUid)); } - public static boolean checkCallerIsSystemOrActiveUser(String tag) { + static boolean checkCallerIsSystemOrActiveUser(String tag) { final boolean res = checkCallerIsSystemOrActiveUser(); if (!res) { Log.w(TAG, tag + " - Not allowed for non-active user and non-system user"); @@ -758,10 +748,6 @@ public final class Utils { return res; } - public static boolean callerIsSystemOrActiveUser(String tag, String method) { - return checkCallerIsSystemOrActiveUser(tag + "." + method + "()"); - } - /** * Checks if the caller to the method is system server. * @@ -1189,13 +1175,15 @@ public final class Utils { return "NO SUBSCRIPTION"; } - if (BigInteger.valueOf(cccValue).testBit(0) && BigInteger.valueOf(cccValue).testBit(1)) { + final boolean isBit0Set = BigInteger.valueOf(cccValue).testBit(0); + final boolean isBit1Set = BigInteger.valueOf(cccValue).testBit(1); + if (isBit0Set && isBit1Set) { return "NOTIFICATION|INDICATION"; } - if (BigInteger.valueOf(cccValue).testBit(0)) { + if (isBit0Set) { return "NOTIFICATION"; } - if (BigInteger.valueOf(cccValue).testBit(1)) { + if (isBit1Set) { return "INDICATION"; } return ""; diff --git a/android/app/src/com/android/bluetooth/btservice/AdapterProperties.java b/android/app/src/com/android/bluetooth/btservice/AdapterProperties.java index 9a5b54540d..bd483f5925 100644 --- a/android/app/src/com/android/bluetooth/btservice/AdapterProperties.java +++ b/android/app/src/com/android/bluetooth/btservice/AdapterProperties.java @@ -24,6 +24,8 @@ import static android.bluetooth.BluetoothProfile.STATE_CONNECTING; import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED; import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTING; +import static com.android.bluetooth.Utils.BD_ADDR_LEN; + import android.annotation.NonNull; import android.app.BroadcastOptions; import android.bluetooth.BluetoothA2dp; @@ -79,7 +81,6 @@ class AdapterProperties { private static final long DEFAULT_DISCOVERY_TIMEOUT_MS = 12800; @VisibleForTesting static final int BLUETOOTH_NAME_MAX_LENGTH_BYTES = 248; - private static final int BD_ADDR_LEN = 6; // in bytes private static final int SYSTEM_CONNECTION_LATENCY_METRIC = 65536; private volatile String mName; diff --git a/android/app/tests/unit/src/com/android/bluetooth/UtilsTest.java b/android/app/tests/unit/src/com/android/bluetooth/UtilsTest.java index ebada2d9a4..4d949cb785 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/UtilsTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/UtilsTest.java @@ -47,7 +47,7 @@ import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; import java.util.UUID; -/** Test for Utils.java */ +/** Test cases for {@link Utils}. */ @SmallTest @RunWith(AndroidJUnit4.class) public class UtilsTest { @@ -66,13 +66,6 @@ public class UtilsTest { } @Test - public void byteArrayToString() { - byte[] valueBuf = new byte[] {0x01, 0x02}; - String str = Utils.byteArrayToString(valueBuf); - assertThat(str).isEqualTo("01 02"); - } - - @Test public void uuidsToByteArray() { ParcelUuid[] uuids = new ParcelUuid[] { |