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

Commit 947f76d7 authored by Amin Shaikh's avatar Amin Shaikh
Browse files

Remove BluetoothEventManager#setReceiverHandler

Make BluetoothEventManager fields final and remove the method to
unregister receivers unnecessarily (introduced in b/35415158).

Bug: 113122466
Test: runtest systemui && make RunSettingsLibRoboTests
Change-Id: I8f34ab1a809638959ab4096a829c72b3d1ac3a23
parent 2943823a
Loading
Loading
Loading
Loading
+5 −19
Original line number Diff line number Diff line
@@ -53,22 +53,23 @@ public class BluetoothEventManager {
    private final BroadcastReceiver mBroadcastReceiver = new BluetoothBroadcastReceiver();
    private final BroadcastReceiver mProfileBroadcastReceiver = new BluetoothBroadcastReceiver();
    private final Collection<BluetoothCallback> mCallbacks = new ArrayList<>();

    private android.os.Handler mReceiverHandler;
    private Context mContext;
    private final android.os.Handler mReceiverHandler;
    private final Context mContext;

    interface Handler {
        void onReceive(Context context, Intent intent, BluetoothDevice device);
    }

    BluetoothEventManager(LocalBluetoothAdapter adapter,
            CachedBluetoothDeviceManager deviceManager, Context context) {
            CachedBluetoothDeviceManager deviceManager, Context context,
            android.os.Handler handler) {
        mLocalAdapter = adapter;
        mDeviceManager = deviceManager;
        mAdapterIntentFilter = new IntentFilter();
        mProfileIntentFilter = new IntentFilter();
        mHandlerMap = new HashMap<>();
        mContext = context;
        mReceiverHandler = handler;

        // Bluetooth on/off broadcasts
        addHandler(BluetoothAdapter.ACTION_STATE_CHANGED, new AdapterStateChangedHandler());
@@ -104,15 +105,6 @@ public class BluetoothEventManager {
                new AudioModeChangedHandler());

        mContext.registerReceiver(mBroadcastReceiver, mAdapterIntentFilter, null, mReceiverHandler);
        mContext.registerReceiver(mProfileBroadcastReceiver, mProfileIntentFilter, null, mReceiverHandler);
    }

    public void setReceiverHandler(android.os.Handler handler) {
        mContext.unregisterReceiver(mBroadcastReceiver);
        mContext.unregisterReceiver(mProfileBroadcastReceiver);
        mReceiverHandler = handler;
        mContext.registerReceiver(mBroadcastReceiver, mAdapterIntentFilter, null, mReceiverHandler);
        registerProfileIntentReceiver();
    }

    /** Register to start receiving callbacks for Bluetooth events. */
@@ -233,12 +225,6 @@ public class BluetoothEventManager {
                BluetoothDevice device) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                    BluetoothAdapter.ERROR);
            // Reregister Profile Broadcast Receiver as part of TURN OFF
            if (state == BluetoothAdapter.STATE_OFF)
            {
                context.unregisterReceiver(mProfileBroadcastReceiver);
                registerProfileIntentReceiver();
            }
            // update local profiles and get paired devices
            mLocalAdapter.setBluetoothStateInt(state);
            // send callback to update UI and possibly start scanning
+17 −4
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settingslib.bluetooth;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import java.lang.ref.WeakReference;
@@ -57,7 +58,7 @@ public class LocalBluetoothManager {
            }
            // This will be around as long as this process is
            Context appContext = context.getApplicationContext();
            sInstance = new LocalBluetoothManager(adapter, appContext);
            sInstance = new LocalBluetoothManager(adapter, appContext, null);
            if (onInitCallback != null) {
                onInitCallback.onBluetoothManagerInitialized(appContext, sInstance);
            }
@@ -66,13 +67,25 @@ public class LocalBluetoothManager {
        return sInstance;
    }

    private LocalBluetoothManager(LocalBluetoothAdapter adapter, Context context) {
    /**
     * Returns a new instance of {@link LocalBluetoothManager} or null if Bluetooth is not
     * supported for this hardware. This instance should be globally cached by the caller.
     */
    public static LocalBluetoothManager create(Context context, Handler handler) {
        LocalBluetoothAdapter adapter = LocalBluetoothAdapter.getInstance();
        if (adapter == null) {
            return null;
        }
        return new LocalBluetoothManager(adapter, context.getApplicationContext(), handler);
    }

    private LocalBluetoothManager(
            LocalBluetoothAdapter adapter, Context context, Handler handler) {
        mContext = context;
        mLocalAdapter = adapter;

        mCachedDeviceManager = new CachedBluetoothDeviceManager(context, this);
        mEventManager = new BluetoothEventManager(mLocalAdapter,
                mCachedDeviceManager, context);
                mCachedDeviceManager, context, handler);
        mProfileManager = new LocalBluetoothProfileManager(context,
                mLocalAdapter, mCachedDeviceManager, mEventManager);

+1 −1
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ public class BluetoothEventManagerTest {
        mContext = RuntimeEnvironment.application;

        mBluetoothEventManager = new BluetoothEventManager(mLocalAdapter,
                mCachedDeviceManager, mContext);
                mCachedDeviceManager, mContext, null);
    }

    /**
+7 −19
Original line number Diff line number Diff line
@@ -76,7 +76,7 @@ public class LocalBluetoothProfileManagerTest {
        mContext = spy(RuntimeEnvironment.application);
        mLocalBluetoothAdapter = LocalBluetoothAdapter.getInstance();
        mEventManager = spy(new BluetoothEventManager(mLocalBluetoothAdapter, mDeviceManager,
                mContext));
                mContext, null));
        mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
        when(mDeviceManager.findDevice(mDevice)).thenReturn(mCachedBluetoothDevice);
        mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter,
@@ -151,9 +151,7 @@ public class LocalBluetoothProfileManagerTest {
        mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                new int[] {BluetoothProfile.A2DP}));
        mProfileManager.updateLocalProfiles();
        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
        // LocalBluetoothProfileManager created.
        mEventManager.setReceiverHandler(null);

        mIntent = new Intent(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
        mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
        mIntent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_CONNECTING);
@@ -174,9 +172,7 @@ public class LocalBluetoothProfileManagerTest {
        mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                new int[] {BluetoothProfile.HEADSET}));
        mProfileManager.updateLocalProfiles();
        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
        // LocalBluetoothProfileManager created.
        mEventManager.setReceiverHandler(null);

        mIntent = new Intent(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
        mIntent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_CONNECTING);
@@ -197,9 +193,7 @@ public class LocalBluetoothProfileManagerTest {
        mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                new int[] {BluetoothProfile.HEARING_AID}));
        mProfileManager.updateLocalProfiles();
        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
        // LocalBluetoothProfileManager created.
        mEventManager.setReceiverHandler(null);

        mIntent = new Intent(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
        mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
        mIntent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_CONNECTING);
@@ -220,9 +214,7 @@ public class LocalBluetoothProfileManagerTest {
        mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                new int[] {BluetoothProfile.PAN}));
        mProfileManager.updateLocalProfiles();
        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
        // LocalBluetoothProfileManager created.
        mEventManager.setReceiverHandler(null);

        mIntent = new Intent(BluetoothPan.ACTION_CONNECTION_STATE_CHANGED);
        mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
        mIntent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_CONNECTING);
@@ -243,9 +235,7 @@ public class LocalBluetoothProfileManagerTest {
    () {
        mShadowBluetoothAdapter.setSupportedProfiles(null);
        mProfileManager.updateLocalProfiles();
        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
        // LocalBluetoothProfileManager created.
        mEventManager.setReceiverHandler(null);

        mIntent = new Intent(BluetoothPan.ACTION_CONNECTION_STATE_CHANGED);
        mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
        mIntent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_CONNECTING);
@@ -265,9 +255,7 @@ public class LocalBluetoothProfileManagerTest {
        mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                new int[] {BluetoothProfile.PAN}));
        mProfileManager.updateLocalProfiles();
        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
        // LocalBluetoothProfileManager created.
        mEventManager.setReceiverHandler(null);

        mIntent = new Intent(BluetoothPan.ACTION_CONNECTION_STATE_CHANGED);
        mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
        mIntent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_CONNECTING);
+6 −1
Original line number Diff line number Diff line
@@ -132,6 +132,10 @@ public class Dependency extends SystemUI {
     * Key for getting a background Looper for background work.
     */
    public static final DependencyKey<Looper> BG_LOOPER = new DependencyKey<>("background_looper");
    /**
     * Key for getting a background Handler for background work.
     */
    public static final DependencyKey<Handler> BG_HANDLER = new DependencyKey<>("background_handler");
    /**
     * Key for getting a Handler for receiving time tick broadcasts on.
     */
@@ -166,6 +170,7 @@ public class Dependency extends SystemUI {
            thread.start();
            return thread.getLooper();
        });
        mProviders.put(BG_HANDLER, () -> new Handler(getDependency(BG_LOOPER)));
        mProviders.put(MAIN_HANDLER, () -> new Handler(Looper.getMainLooper()));
        mProviders.put(ActivityStarter.class, () -> new ActivityStarterDelegate());
        mProviders.put(ActivityStarterDelegate.class, () ->
@@ -288,7 +293,7 @@ public class Dependency extends SystemUI {
                new PluginDependencyProvider(get(PluginManager.class)));

        mProviders.put(LocalBluetoothManager.class, () ->
                LocalBluetoothManager.getInstance(mContext, null));
                LocalBluetoothManager.create(mContext, getDependency(BG_HANDLER)));

        mProviders.put(VolumeDialogController.class, () ->
                new VolumeDialogControllerImpl(mContext));
Loading