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

Commit 0a87c513 authored by William Escande's avatar William Escande
Browse files

Utils: remove obsolete & not used method

Bug: 311772251
Flag: Exempt refactor - dead code removal and single use method inline
Test: atest BluetoothInstrumentationTests
Change-Id: I95363f5230f6368d6f33e745a0580eb52a019cca
parent 6da0d41e
Loading
Loading
Loading
Loading
+4 −90
Original line number Diff line number Diff line
@@ -78,8 +78,6 @@ import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@@ -118,26 +116,8 @@ public final class Utils {
    /** Thread pool to handle background and outgoing blocking task */
    public static final ExecutorService BackgroundExecutor = Executors.newSingleThreadExecutor();

    /*
     * Special character
     *
     * (See "What is a phone number?" doc)
     * 'p' --- GSM pause character, same as comma
     * 'n' --- GSM wild character
     * 'w' --- GSM wait character
     */
    public static final char PAUSE = ',';
    public static final char WAIT = ';';
    public static final String PAIRING_UI_PROPERTY = "bluetooth.pairing_ui_package.name";

    private static boolean isPause(char c) {
        return c == 'p' || c == 'P';
    }

    private static boolean isToneWait(char c) {
        return c == 'w' || c == 'W';
    }

    /**
     * Check if dual mode audio is enabled. This is set via the system property
     * persist.bluetooth.enable_dual_mode_audio.
@@ -322,12 +302,6 @@ public final class Utils {
        return byteArrayToLong(valueBuf, 0);
    }

    public static short byteArrayToShort(byte[] valueBuf) {
        ByteBuffer converter = ByteBuffer.wrap(valueBuf);
        converter.order(ByteOrder.nativeOrder());
        return converter.getShort();
    }

    public static int byteArrayToInt(byte[] valueBuf, int offset) {
        ByteBuffer converter = ByteBuffer.wrap(valueBuf);
        converter.order(ByteOrder.nativeOrder());
@@ -422,66 +396,6 @@ public final class Utils {
        return puuids;
    }

    public static String debugGetAdapterStateString(int state) {
        switch (state) {
            case BluetoothAdapter.STATE_OFF:
                return "STATE_OFF";
            case BluetoothAdapter.STATE_ON:
                return "STATE_ON";
            case BluetoothAdapter.STATE_TURNING_ON:
                return "STATE_TURNING_ON";
            case BluetoothAdapter.STATE_TURNING_OFF:
                return "STATE_TURNING_OFF";
            default:
                return "UNKNOWN";
        }
    }

    public static String ellipsize(String s) {
        // Only ellipsize release builds
        if (!Build.TYPE.equals("user")) {
            return s;
        }
        if (s == null) {
            return null;
        }
        if (s.length() < 3) {
            return s;
        }
        return s.charAt(0) + "⋯" + s.charAt(s.length() - 1);
    }

    public static void copyStream(InputStream is, OutputStream os, int bufferSize)
            throws IOException {
        if (is != null && os != null) {
            byte[] buffer = new byte[bufferSize];
            int bytesRead = 0;
            while ((bytesRead = is.read(buffer)) >= 0) {
                os.write(buffer, 0, bytesRead);
            }
        }
    }

    public static void safeCloseStream(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (Throwable t) {
                Log.d(TAG, "Error closing stream", t);
            }
        }
    }

    public static void safeCloseStream(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (Throwable t) {
                Log.d(TAG, "Error closing stream", t);
            }
        }
    }

    static int sSystemUiUid = USER_HANDLE_NULL.getIdentifier();

    public static void setSystemUiUid(int uid) {
@@ -1162,10 +1076,10 @@ public final class Utils {
        for (int i = 0; i < len; i++) {
            char c = phoneNumber.charAt(i);

            if (isPause(c)) {
                c = PAUSE;
            } else if (isToneWait(c)) {
                c = WAIT;
            if (c == 'p' || c == 'P') {
                c = ',';
            } else if (c == 'w' || c == 'W') {
                c = ';';
            }
            ret.append(c);
        }
+0 −68
Original line number Diff line number Diff line
@@ -63,13 +63,6 @@ public class UtilsTest {

    @Rule public Expect expect = Expect.create();

    @Test
    public void byteArrayToShort() {
        byte[] valueBuf = new byte[] {0x01, 0x02};
        short s = Utils.byteArrayToShort(valueBuf);
        assertThat(s).isEqualTo(0x0201);
    }

    @Test
    public void byteArrayToLong() {
        byte[] valueBuf =
@@ -204,67 +197,6 @@ public class UtilsTest {
        Utils.checkCallerIsSystemOrActiveUser(tag);
    }

    @Test
    public void testCopyStream() throws Exception {
        byte[] data = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};
        ByteArrayInputStream in = new ByteArrayInputStream(data);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int bufferSize = 4;

        Utils.copyStream(in, out, bufferSize);

        assertThat(out.toByteArray()).isEqualTo(data);
    }

    @Test
    public void debugGetAdapterStateString() {
        assertThat(Utils.debugGetAdapterStateString(BluetoothAdapter.STATE_OFF))
                .isEqualTo("STATE_OFF");
        assertThat(Utils.debugGetAdapterStateString(BluetoothAdapter.STATE_ON))
                .isEqualTo("STATE_ON");
        assertThat(Utils.debugGetAdapterStateString(BluetoothAdapter.STATE_TURNING_ON))
                .isEqualTo("STATE_TURNING_ON");
        assertThat(Utils.debugGetAdapterStateString(BluetoothAdapter.STATE_TURNING_OFF))
                .isEqualTo("STATE_TURNING_OFF");
        assertThat(Utils.debugGetAdapterStateString(-124)).isEqualTo("UNKNOWN");
    }

    @Test
    public void ellipsize() {
        if (!Build.TYPE.equals("user")) {
            // Only ellipsize release builds
            String input = "a_long_string";
            assertThat(Utils.ellipsize(input)).isEqualTo(input);
            return;
        }

        assertThat(Utils.ellipsize("ab")).isEqualTo("ab");
        assertThat(Utils.ellipsize("abc")).isEqualTo("a⋯c");
        assertThat(Utils.ellipsize(null)).isEqualTo(null);
    }

    @Test
    public void safeCloseStream_inputStream_doesNotCrash() throws Exception {
        InputStream is = mock(InputStream.class);
        Utils.safeCloseStream(is);
        verify(is).close();

        Mockito.clearInvocations(is);
        doThrow(new IOException()).when(is).close();
        Utils.safeCloseStream(is);
    }

    @Test
    public void safeCloseStream_outputStream_doesNotCrash() throws Exception {
        OutputStream os = mock(OutputStream.class);
        Utils.safeCloseStream(os);
        verify(os).close();

        Mockito.clearInvocations(os);
        doThrow(new IOException()).when(os).close();
        Utils.safeCloseStream(os);
    }

    @Test
    public void truncateUtf8_toZeroLength_isEmpty() {
        assertThat(Utils.truncateStringForUtf8Storage("abc", 0)).isEmpty();