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

Commit 64c21f12 authored by William Escande's avatar William Escande
Browse files

24Q4: Removal of leaudio_call_start_scan_directly

Bug: 348562830
Fix: 348562830
Flag: com.android.bluetooth.flags.leaudio_call_start_scan_directly
Test: m . | Tested by soaking and QA and flag release
Change-Id: Ifcadb97d381bc401f50a2e243e8bb0ac5f14782a
parent 685ffd2d
Loading
Loading
Loading
Loading
+16 −130
Original line number Diff line number Diff line
@@ -221,10 +221,8 @@ public class LeAudioService extends ProfileService {
            new RemoteCallbackList<>();

    BluetoothLeScanner mAudioServersScanner;
    /* When mScanCallback is not null, it means scan is started. */
    ScanCallback mScanCallback;

    private final AudioServerScanCallback2 mScanCallback2 = new AudioServerScanCallback2();
    private final AudioServerScanCallback mScanCallback = new AudioServerScanCallback();

    public LeAudioService(Context ctx) {
        this(ctx, LeAudioNativeInterface.getInstance());
@@ -641,7 +639,7 @@ public class LeAudioService extends ProfileService {
            mTmapStarted = false;
        }

        stopAudioServersBackgroundScan();
        mScanCallback.stopBackgroundScan();
        mAudioServersScanner = null;

        // Don't wait for async call with INACTIVE group status, clean active
@@ -1956,7 +1954,7 @@ public class LeAudioService extends ProfileService {
        return true;
    }

    private class AudioServerScanCallback2 extends IScannerCallback.Stub {
    private class AudioServerScanCallback extends IScannerCallback.Stub {
        // See BluetoothLeScanner.BleScanCallbackWrapper.mScannerId
        int mScannerId = 0;

@@ -2055,44 +2053,6 @@ public class LeAudioService extends ProfileService {
        public void onScanManagerErrorCallback(int errorCode) {}
    }

    private class AudioServerScanCallback extends ScanCallback {
        int mMaxScanRetries = 10;
        int mScanRetries = 0;

        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            /* Filter is set in the way, that there will be no results found.
             * We just need a scanner to be running for the APCF filtering defined in native
             */
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            /* Filter is set in the way, that there will be no results found.
             * We just need a scanner to be running for the APCF filtering defined in native
             */
        }

        @Override
        public void onScanFailed(int errorCode) {
            Log.w(TAG, "Scan failed err: " + errorCode + " scan retries: " + mScanRetries);
            switch (errorCode) {
                case SCAN_FAILED_INTERNAL_ERROR:
                case SCAN_FAILED_APPLICATION_REGISTRATION_FAILED:
                    if (mScanRetries < mMaxScanRetries) {
                        mScanRetries++;
                        Log.w(TAG, "Failed to start. Let's retry");
                        mHandler.post(() -> startAudioServersBackgroundScan(/* retry= */ true));
                    }
                    break;
                default:
                    /* Indicate scan is no running */
                    mScanCallback = null;
                    break;
            }
        }
    }

    @VisibleForTesting
    boolean handleAudioDeviceAdded(
            BluetoothDevice device, int type, boolean isSink, boolean isSource) {
@@ -3101,87 +3061,6 @@ public class LeAudioService extends ProfileService {
        }
    }

    @SuppressLint("AndroidFrameworkRequiresPermission") // TODO: b/348562830 - Remove with flag
    void stopAudioServersBackgroundScan() {
        Log.d(TAG, "stopAudioServersBackgroundScan");

        if (Flags.leaudioCallStartScanDirectly()) {
            mScanCallback2.stopBackgroundScan();
            return;
        }

        if (mAudioServersScanner == null || mScanCallback == null) {
            Log.d(TAG, "stopAudioServersBackgroundScan: already stopped");
            return;
        }

        try {
            mAudioServersScanner.stopScan(mScanCallback);
        } catch (IllegalStateException e) {
            Log.e(TAG, "Fail to stop scanner, consider it stopped", e);
        }

        /* Callback is the indicator for scanning being enabled */
        mScanCallback = null;
    }

    @SuppressLint("AndroidFrameworkRequiresPermission") // TODO: b/348562830 - Remove with flag
    void startAudioServersBackgroundScan(boolean retry) {
        Log.d(TAG, "startAudioServersBackgroundScan, retry: " + retry);

        if (!isScannerNeeded()) {
            return;
        }

        if (Flags.leaudioCallStartScanDirectly()) {
            mScanCallback2.startBackgroundScan();
            return;
        }

        if (mAudioServersScanner == null) {
            mAudioServersScanner = BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();
            if (mAudioServersScanner == null) {
                Log.e(TAG, "startAudioServersBackgroundScan: Could not get scanner");
                return;
            }
        }

        if (!retry) {
            if (mScanCallback != null) {
                Log.d(TAG, "startAudioServersBackgroundScan: Scanning already enabled");
                return;
            }
            mScanCallback = new AudioServerScanCallback();
        }

        /* Filter we are building here will not match to anything.
         * Eventually we should be able to start scan from native when
         * b/276350722 is done
         */
        byte[] serviceData = new byte[] {0x11};

        ArrayList filterList = new ArrayList<ScanFilter>();
        ScanFilter filter =
                new ScanFilter.Builder()
                        .setServiceData(BluetoothUuid.LE_AUDIO, serviceData)
                        .build();
        filterList.add(filter);

        ScanSettings settings =
                new ScanSettings.Builder()
                        .setLegacy(false)
                        .setScanMode(ScanSettings.SCAN_MODE_BALANCED)
                        .setPhy(BluetoothDevice.PHY_LE_1M)
                        .build();

        try {
            mAudioServersScanner.startScan(filterList, settings, mScanCallback);
        } catch (IllegalStateException e) {
            Log.e(TAG, "Fail to start scanner, consider it stopped", e);
            mScanCallback = null;
        }
    }

    void transitionFromBroadcastToUnicast() {
        if (mUnicastGroupIdDeactivatedForBroadcastTransition == LE_AUDIO_GROUP_ID_INVALID) {
            Log.d(TAG, "No deactivated group due for broadcast transmission");
@@ -3411,7 +3290,10 @@ public class LeAudioService extends ProfileService {
                        case LeAudioStackEvent.CONNECTION_STATE_DISCONNECTING:
                        case LeAudioStackEvent.CONNECTION_STATE_DISCONNECTED:
                            deviceDescriptor.mAclConnected = false;
                            startAudioServersBackgroundScan(/* retry= */ false);

                            if (isScannerNeeded()) {
                                mScanCallback.startBackgroundScan();
                            }

                            boolean disconnectDueToUnbond =
                                    (BluetoothDevice.BOND_NONE
@@ -4143,7 +4025,7 @@ public class LeAudioService extends ProfileService {
            }
            mDeviceDescriptors.remove(device);
            if (!isScannerNeeded()) {
                stopAudioServersBackgroundScan();
                mScanCallback.stopBackgroundScan();
            }
        } finally {
            mGroupWriteLock.unlock();
@@ -4185,7 +4067,7 @@ public class LeAudioService extends ProfileService {
        }

        if (!isScannerNeeded()) {
            stopAudioServersBackgroundScan();
            mScanCallback.stopBackgroundScan();
        }

        /* Set by default earliest connected device */
@@ -4221,7 +4103,7 @@ public class LeAudioService extends ProfileService {
        }

        if (!isScannerNeeded()) {
            stopAudioServersBackgroundScan();
            mScanCallback.stopBackgroundScan();
        }

        mGroupReadLock.lock();
@@ -4754,7 +4636,9 @@ public class LeAudioService extends ProfileService {
            mGroupReadLock.unlock();
        }

        startAudioServersBackgroundScan(/* retry= */ false);
        if (isScannerNeeded()) {
            mScanCallback.startBackgroundScan();
        }
    }

    @VisibleForTesting
@@ -4866,7 +4750,9 @@ public class LeAudioService extends ProfileService {

        if (mBluetoothEnabled) {
            setAuthorizationForRelatedProfiles(device, true);
            startAudioServersBackgroundScan(/* retry= */ false);
            if (isScannerNeeded()) {
                mScanCallback.startBackgroundScan();
            }
        }
    }

+0 −10
Original line number Diff line number Diff line
@@ -203,16 +203,6 @@ flag {
    }
}

flag {
    name: "leaudio_call_start_scan_directly"
    namespace: "bluetooth"
    description: "Calling the framework is working only when we are expecting IPC. This will call the underlying implementation directly"
    bug: "348562830"
    metadata {
        purpose: PURPOSE_BUGFIX
    }
}

flag {
    name: "leaudio_no_context_validate_streaming_request"
    namespace: "bluetooth"