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

Commit c559673d authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Stop using broadcast intent of BluetoothAdapter.ACTION_STATE_CHANGED"...

Merge "Stop using broadcast intent of BluetoothAdapter.ACTION_STATE_CHANGED" into main am: ad67bb6f

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/2691486



Change-Id: I31ecea0500f6008bfc56c3068ed9c6abf4f43f92
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents e8599202 ad67bb6f
Loading
Loading
Loading
Loading
+7 −35
Original line number Diff line number Diff line
@@ -26,10 +26,6 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHearingAid;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothSinkAudioPolicy;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioDeviceCallback;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;
@@ -101,7 +97,7 @@ import java.util.Set;
 *      will have no impact. E.g., music will continue streaming over the
 *      active Bluetooth device.
 */
public class ActiveDeviceManager {
public class ActiveDeviceManager implements AdapterService.BluetoothStateCallback {
    private static final String TAG = "ActiveDeviceManager";
    private static final boolean DBG = true;
    @VisibleForTesting
@@ -144,25 +140,10 @@ public class ActiveDeviceManager {
    private BluetoothDevice mClassicDeviceToBeActivated = null;
    private BluetoothDevice mClassicDeviceNotToBeActivated = null;

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
        public void onReceive(Context context, Intent intent) {
            if (DBG) {
                Log.d(TAG, "Received intent: action=" + intent.getAction() + ", extras="
                        + intent.getExtras());
            }
            String action = intent.getAction();
            if (action == null) {
                Log.e(TAG, "Received intent with null action");
                return;
            }

            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                int currentState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
                mHandler.post(() -> handleAdapterStateChanged(currentState));
    public void onBluetoothStateChange(int prevState, int newState) {
        mHandler.post(() -> handleAdapterStateChanged(newState));
    }
        }
    };

    /**
     * Called when A2DP connection state changed by A2dpService
@@ -835,12 +816,8 @@ public class ActiveDeviceManager {
        mHandlerThread.start();
        mHandler = new Handler(mHandlerThread.getLooper());

        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        mAdapterService.registerReceiver(mReceiver, filter, Context.RECEIVER_EXPORTED);

        mAudioManager.registerAudioDeviceCallback(mAudioManagerAudioDeviceCallback, mHandler);
        mAdapterService.registerBluetoothStateCallback((command) -> mHandler.post(command), this);
    }

    void cleanup() {
@@ -849,7 +826,7 @@ public class ActiveDeviceManager {
        }

        mAudioManager.unregisterAudioDeviceCallback(mAudioManagerAudioDeviceCallback);
        mAdapterService.unregisterReceiver(mReceiver);
        mAdapterService.unregisterBluetoothStateCallback(this);
        if (mHandlerThread != null) {
            mHandlerThread.quit();
            mHandlerThread = null;
@@ -1191,11 +1168,6 @@ public class ActiveDeviceManager {
        }
    }

    @VisibleForTesting
    BroadcastReceiver getBroadcastReceiver() {
        return mReceiver;
    }

    @VisibleForTesting
    BluetoothDevice getA2dpActiveDevice() {
        return mA2dpActiveDevice;
+51 −15
Original line number Diff line number Diff line
@@ -107,6 +107,7 @@ import android.provider.DeviceConfig;
import android.provider.Settings;
import android.sysprop.BluetoothProperties;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Base64;
import android.util.Log;
import android.util.SparseArray;
@@ -335,7 +336,8 @@ public class AdapterService extends Service {
    private final Map<Integer, PendingAudioProfilePreferenceRequest>
            mCsipGroupsPendingAudioProfileChanges = new HashMap<>();
    // Only BluetoothManagerService should be registered
    private RemoteCallbackList<IBluetoothCallback> mCallbacks;
    private RemoteCallbackList<IBluetoothCallback> mRemoteCallbacks;
    private final Map<BluetoothStateCallback, Executor> mLocalCallbacks = new ArrayMap<>();
    private int mCurrentRequestId;
    private boolean mQuietmode = false;
    private HashMap<String, CallerInfo> mBondAttemptCallerInfo = new HashMap<>();
@@ -652,7 +654,7 @@ public class AdapterService extends Service {
                new RemoteCallbackList<IBluetoothPreferredAudioProfilesCallback>();
        mBluetoothQualityReportReadyCallbacks =
                new RemoteCallbackList<IBluetoothQualityReportReadyCallback>();
        mCallbacks = new RemoteCallbackList<IBluetoothCallback>();
        mRemoteCallbacks = new RemoteCallbackList<IBluetoothCallback>();
        // Load the name and address
        getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDADDR);
        getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDNAME);
@@ -1049,8 +1051,15 @@ public class AdapterService extends Service {
    void updateAdapterState(int prevState, int newState) {
        mAdapterProperties.setState(newState);
        invalidateBluetoothGetStateCache();
        if (mCallbacks != null) {
            int n = mCallbacks.beginBroadcast();

        synchronized (mLocalCallbacks) {
            for (Map.Entry<BluetoothStateCallback, Executor> e : mLocalCallbacks.entrySet()) {
                e.getValue().execute(() -> e.getKey().onBluetoothStateChange(prevState, newState));
            }
        }

        if (mRemoteCallbacks != null) {
            int n = mRemoteCallbacks.beginBroadcast();
            debugLog(
                    "updateAdapterState() - Broadcasting state "
                            + BluetoothAdapter.nameForState(newState)
@@ -1059,12 +1068,14 @@ public class AdapterService extends Service {
                            + " receivers.");
            for (int i = 0; i < n; i++) {
                try {
                    mCallbacks.getBroadcastItem(i).onBluetoothStateChange(prevState, newState);
                    mRemoteCallbacks
                            .getBroadcastItem(i)
                            .onBluetoothStateChange(prevState, newState);
                } catch (RemoteException e) {
                    debugLog("updateAdapterState() - Callback #" + i + " failed (" + e + ")");
                }
            }
            mCallbacks.finishBroadcast();
            mRemoteCallbacks.finishBroadcast();
        }

        // Turn the Adapter all the way off if we are disabling and the snoop log setting changed.
@@ -1360,8 +1371,8 @@ public class AdapterService extends Service {
            mBluetoothQualityReportReadyCallbacks.kill();
        }

        if (mCallbacks != null) {
            mCallbacks.kill();
        if (mRemoteCallbacks != null) {
            mRemoteCallbacks.kill();
        }
    }

@@ -4096,7 +4107,7 @@ public class AdapterService extends Service {

            enforceBluetoothPrivilegedPermission(service);

            service.registerCallback(callback);
            service.registerRemoteCallback(callback);
        }

        @Override
@@ -4121,7 +4132,7 @@ public class AdapterService extends Service {
        private void unregisterCallback(IBluetoothCallback callback, AttributionSource source) {
            AdapterService service = getService();
            if (service == null
                    || service.mCallbacks == null
                    || service.mRemoteCallbacks == null
                    || !callerIsSystemOrActiveOrManagedUser(service, TAG, "unregisterCallback")
                    || !Utils.checkConnectPermissionForDataDelivery(service, source, TAG)) {
                return;
@@ -4129,7 +4140,7 @@ public class AdapterService extends Service {

            enforceBluetoothPrivilegedPermission(service);

            service.unregisterCallback(callback);
            service.unregisterRemoteCallback(callback);
        }

        @Override
@@ -6723,14 +6734,28 @@ public class AdapterService extends Service {
        return mAdapterProperties.isA2dpOffloadEnabled();
    }

    /** Register a bluetooth state callback */
    public void registerBluetoothStateCallback(Executor executor, BluetoothStateCallback callback) {
        synchronized (mLocalCallbacks) {
            mLocalCallbacks.put(callback, executor);
        }
    }

    /** Unregister a bluetooth state callback */
    public void unregisterBluetoothStateCallback(BluetoothStateCallback callback) {
        synchronized (mLocalCallbacks) {
            mLocalCallbacks.remove(callback);
        }
    }

    @VisibleForTesting
    void registerCallback(IBluetoothCallback callback) {
        mCallbacks.register(callback);
    void registerRemoteCallback(IBluetoothCallback callback) {
        mRemoteCallbacks.register(callback);
    }

    @VisibleForTesting
    void unregisterCallback(IBluetoothCallback callback) {
        mCallbacks.unregister(callback);
    void unregisterRemoteCallback(IBluetoothCallback callback) {
        mRemoteCallbacks.unregister(callback);
    }

    @VisibleForTesting
@@ -7457,6 +7482,17 @@ public class AdapterService extends Service {
        }
    }

    /** A callback that will be called when AdapterState is changed */
    public interface BluetoothStateCallback {
        /**
         * Called when the status of bluetooth adapter is changing
         *
         * @param prevState the previous Bluetooth state.
         * @param newState the new Bluetooth state.
         */
        void onBluetoothStateChange(int prevState, int newState);
    }

    /**
     * Obfuscate Bluetooth MAC address into a PII free ID string
     *
+45 −56
Original line number Diff line number Diff line
@@ -36,12 +36,10 @@ import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevicePicker;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.CharArrayBuffer;
import android.database.ContentObserver;
import android.database.Cursor;
@@ -72,11 +70,11 @@ import java.util.Date;
import java.util.Locale;

/**
 * Performs the background Bluetooth OPP transfer. It also starts thread to
 * accept incoming OPP connection.
 * Performs the background Bluetooth OPP transfer. It also starts thread to accept incoming OPP
 * connection.
 */

public class BluetoothOppService extends ProfileService implements IObexConnectionHandler {
public class BluetoothOppService extends ProfileService
        implements IObexConnectionHandler, AdapterService.BluetoothStateCallback {
    private static final boolean D = Constants.DEBUG;
    private static final boolean V = Constants.VERBOSE;

@@ -229,10 +227,6 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
        mBatches = new ArrayList();
        mBatchId = 1;

        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        registerReceiver(mBluetoothReceiver, filter);

        if (V) {
            BluetoothOppPreference preference = BluetoothOppPreference.getInstance(this);
            if (preference != null) {
@@ -265,6 +259,7 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
        }.start();

        mAdapterService = AdapterService.getAdapterService();
        mAdapterService.registerBluetoothStateCallback((command) -> mHandler.post(command), this);
        mObserver = new BluetoothShareContentObserver();
        getContentResolver().registerContentObserver(BluetoothShare.CONTENT_URI, true, mObserver);
        mNotifier = new BluetoothOppNotification(this);
@@ -283,6 +278,7 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
            Log.w(TAG, "stop() called before start()");
            return true;
        }
        mAdapterService.unregisterBluetoothStateCallback(this);
        mAdapterService.notifyActivityAttributionInfo(
                getAttributionSource(),
                AdapterService.ACTIVITY_ATTRIBUTION_NO_ACTIVE_DEVICE_ADDRESS);
@@ -545,35 +541,9 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
        }
    }

    private void unregisterReceivers() {
        try {
            if (mObserver != null) {
                getContentResolver().unregisterContentObserver(mObserver);
                mObserver = null;
            }
            unregisterReceiver(mBluetoothReceiver);
        } catch (IllegalArgumentException e) {
            Log.w(TAG, "unregisterReceivers " + e.toString());
        }
    }

    /* suppose we auto accept an incoming OPUSH connection */
    private void createServerSession(ObexTransport transport) {
        mServerSession = new BluetoothOppObexServerSession(this, transport, this);
        mServerSession.preStart();
        if (D) {
            Log.d(TAG, "Get ServerSession " + mServerSession.toString() + " for incoming connection"
                    + transport.toString());
        }
    }

    private final BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
    @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                switch (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
    public void onBluetoothStateChange(int prevState, int newState) {
        switch (newState) {
            case BluetoothAdapter.STATE_ON:
                if (V) {
                    Log.v(TAG, "Bluetooth state changed: STATE_ON");
@@ -582,24 +552,24 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
                // If this is within a sending process, continue the handle
                // logic to display device picker dialog.
                synchronized (this) {
                            if (BluetoothOppManager.getInstance(context).mSendingFlag) {
                    if (BluetoothOppManager.getInstance(this).mSendingFlag) {
                        // reset the flags
                                BluetoothOppManager.getInstance(context).mSendingFlag = false;
                        BluetoothOppManager.getInstance(this).mSendingFlag = false;

                        Intent in1 = new Intent(BluetoothDevicePicker.ACTION_LAUNCH);
                        in1.putExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
                                in1.putExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
                        in1.putExtra(
                                BluetoothDevicePicker.EXTRA_FILTER_TYPE,
                                BluetoothDevicePicker.FILTER_TYPE_TRANSFER);
                                in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE,
                                        getPackageName());
                                in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS,
                        in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE, getPackageName());
                        in1.putExtra(
                                BluetoothDevicePicker.EXTRA_LAUNCH_CLASS,
                                BluetoothOppReceiver.class.getName());

                        in1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                context.startActivity(in1);
                        startActivity(in1);
                    }
                }

                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
                if (V) {
@@ -609,8 +579,27 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
                break;
        }
    }

    private void unregisterReceivers() {
        try {
            if (mObserver != null) {
                getContentResolver().unregisterContentObserver(mObserver);
                mObserver = null;
            }
        } catch (IllegalArgumentException e) {
            Log.w(TAG, "unregisterReceivers " + e.toString());
        }
    }

    /* suppose we auto accept an incoming OPUSH connection */
    private void createServerSession(ObexTransport transport) {
        mServerSession = new BluetoothOppObexServerSession(this, transport, this);
        mServerSession.preStart();
        if (D) {
            Log.d(TAG, "Get ServerSession " + mServerSession.toString() + " for incoming connection"
                    + transport.toString());
        }
    }
    };

    private void updateFromProvider() {
        synchronized (BluetoothOppService.this) {
+19 −23
Original line number Diff line number Diff line
@@ -3,7 +3,6 @@ package com.android.bluetooth.sap;
import static android.Manifest.permission.BLUETOOTH_CONNECT;

import android.annotation.RequiresPermission;
import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
@@ -19,7 +18,6 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.os.ParcelUuid;
@@ -43,7 +41,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SapService extends ProfileService {
public class SapService extends ProfileService implements AdapterService.BluetoothStateCallback {

    private static final String SDP_SAP_SERVICE_NAME = "SIM Access";
    private static final int SDP_SAP_VERSION = 0x0102;
@@ -680,7 +678,6 @@ public class SapService extends ProfileService {
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        filter.addAction(USER_CONFIRM_TIMEOUT_ACTION);

@@ -692,6 +689,7 @@ public class SapService extends ProfileService {
        }
        mInterrupted = false;
        mAdapterService = AdapterService.getAdapterService();
        mAdapterService.registerBluetoothStateCallback(getMainExecutor(), this);
        // start RFCOMM listener
        mSessionStatusHandler.sendMessage(mSessionStatusHandler.obtainMessage(START_LISTENER));
        setSapService(this);
@@ -712,6 +710,7 @@ public class SapService extends ProfileService {
        } catch (Exception e) {
            Log.w(TAG, "Unable to unregister sap receiver", e);
        }
        mAdapterService.unregisterBluetoothStateCallback(this);
        setState(BluetoothSap.STATE_DISCONNECTED, BluetoothSap.RESULT_CANCELED);
        sendShutdownMessage();
        return true;
@@ -726,6 +725,22 @@ public class SapService extends ProfileService {
        }
    }

    @Override
    public void onBluetoothStateChange(int prevState, int newState) {
        if (newState == BluetoothAdapter.STATE_TURNING_OFF) {
            if (DEBUG) {
                Log.d(TAG, "STATE_TURNING_OFF");
            }
            sendShutdownMessage();
        } else if (newState == BluetoothAdapter.STATE_ON) {
            if (DEBUG) {
                Log.d(TAG, "STATE_ON");
            }
            // start RFCOMM listener
            mSessionStatusHandler.sendMessage(mSessionStatusHandler.obtainMessage(START_LISTENER));
        }
    }

    /**
     * Get the current instance of {@link SapService}
     *
@@ -827,25 +842,6 @@ public class SapService extends ProfileService {
                Log.v(TAG, "onReceive");
            }
            String action = intent.getAction();
            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                int state =
                        intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                if (state == BluetoothAdapter.STATE_TURNING_OFF) {
                    if (DEBUG) {
                        Log.d(TAG, "STATE_TURNING_OFF");
                    }
                    sendShutdownMessage();
                } else if (state == BluetoothAdapter.STATE_ON) {
                    if (DEBUG) {
                        Log.d(TAG, "STATE_ON");
                    }
                    // start RFCOMM listener
                    mSessionStatusHandler.sendMessage(
                            mSessionStatusHandler.obtainMessage(START_LISTENER));
                }
                return;
            }

            if (action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY)) {
                Log.v(TAG, " - Received BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY");

+2 −2
Original line number Diff line number Diff line
@@ -232,7 +232,7 @@ public class AdapterServiceRestartTest {

        mLooper.dispatchAll();

        mAdapterService.registerCallback(mIBluetoothCallback);
        mAdapterService.registerRemoteCallback(mIBluetoothCallback);

        mAdapterConfig = TestUtils.readAdapterConfig();
        assertThat(mAdapterConfig).isNotNull();
@@ -245,7 +245,7 @@ public class AdapterServiceRestartTest {
        // Restores the foregroundUserId to the ID prior to the test setup
        Utils.setForegroundUserId(mForegroundUserId);

        mAdapterService.unregisterCallback(mIBluetoothCallback);
        mAdapterService.unregisterRemoteCallback(mIBluetoothCallback);
        mAdapterService.cleanup();
    }

Loading