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

Commit 7ec80919 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add error handle for device that not support Bluetooth"

parents 87007778 34066fa2
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -54,11 +54,12 @@ public abstract class BluetoothDeviceUpdater implements BluetoothCallback,
    private static final String BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY =
            "persist.bluetooth.showdeviceswithoutnames";

    protected final LocalBluetoothManager mLocalManager;
    protected final DevicePreferenceCallback mDevicePreferenceCallback;
    protected final Map<BluetoothDevice, Preference> mPreferenceMap;
    protected Context mPrefContext;
    protected DashboardFragment mFragment;
    @VisibleForTesting
    protected LocalBluetoothManager mLocalManager;

    private final boolean mShowDeviceWithoutNames;

@@ -87,6 +88,10 @@ public abstract class BluetoothDeviceUpdater implements BluetoothCallback,
     * Register the bluetooth event callback and update the list
     */
    public void registerCallback() {
        if (mLocalManager == null) {
            Log.e(TAG, "registerCallback() Bluetooth is not supported on this device");
            return;
        }
        mLocalManager.setForegroundActivity(mFragment.getContext());
        mLocalManager.getEventManager().registerCallback(this);
        mLocalManager.getProfileManager().addServiceListener(this);
@@ -97,6 +102,10 @@ public abstract class BluetoothDeviceUpdater implements BluetoothCallback,
     * Unregister the bluetooth event callback
     */
    public void unregisterCallback() {
        if (mLocalManager == null) {
            Log.e(TAG, "unregisterCallback() Bluetooth is not supported on this device");
            return;
        }
        mLocalManager.setForegroundActivity(null);
        mLocalManager.getEventManager().unregisterCallback(this);
        mLocalManager.getProfileManager().removeServiceListener(this);
+12 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ import static com.android.settingslib.Utils.isAudioModeOngoingCall;

import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

import com.android.settings.R;
import com.android.settings.bluetooth.AvailableMediaBluetoothDeviceUpdater;
@@ -46,12 +47,14 @@ import androidx.preference.PreferenceScreen;
public class AvailableMediaDeviceGroupController extends BasePreferenceController
        implements LifecycleObserver, OnStart, OnStop, DevicePreferenceCallback, BluetoothCallback {

    private static final String TAG = "AvailableMediaDeviceGroupController";
    private static final String KEY = "available_device_list";

    @VisibleForTesting
    PreferenceGroup mPreferenceGroup;
    @VisibleForTesting
    LocalBluetoothManager mLocalBluetoothManager;
    private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
    private final LocalBluetoothManager mLocalBluetoothManager;

    public AvailableMediaDeviceGroupController(Context context) {
        super(context, KEY);
@@ -60,12 +63,20 @@ public class AvailableMediaDeviceGroupController extends BasePreferenceControlle

    @Override
    public void onStart() {
        if (mLocalBluetoothManager == null) {
            Log.e(TAG, "onStart() Bluetooth is not supported on this device");
            return;
        }
        mBluetoothDeviceUpdater.registerCallback();
        mLocalBluetoothManager.getEventManager().registerCallback(this);
    }

    @Override
    public void onStop() {
        if (mLocalBluetoothManager == null) {
            Log.e(TAG, "onStop() Bluetooth is not supported on this device");
            return;
        }
        mBluetoothDeviceUpdater.unregisterCallback();
        mLocalBluetoothManager.getEventManager().unregisterCallback(this);
    }
+8 −1
Original line number Diff line number Diff line
@@ -50,9 +50,10 @@ public class DiscoverableFooterPreferenceController extends BasePreferenceContro

    @VisibleForTesting
    BroadcastReceiver mBluetoothChangedReceiver;
    @VisibleForTesting
    LocalBluetoothManager mLocalManager;
    private FooterPreferenceMixin mFooterPreferenceMixin;
    private FooterPreference mPreference;
    private LocalBluetoothManager mLocalManager;
    private LocalBluetoothAdapter mLocalAdapter;
    private AlwaysDiscoverable mAlwaysDiscoverable;

@@ -113,6 +114,9 @@ public class DiscoverableFooterPreferenceController extends BasePreferenceContro

    @Override
    public void onResume() {
        if (mLocalManager == null) {
            return;
        }
        mContext.registerReceiver(mBluetoothChangedReceiver,
                new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
        mAlwaysDiscoverable.start();
@@ -121,6 +125,9 @@ public class DiscoverableFooterPreferenceController extends BasePreferenceContro

    @Override
    public void onPause() {
        if (mLocalManager == null) {
            return;
        }
        mContext.unregisterReceiver(mBluetoothChangedReceiver);
        mAlwaysDiscoverable.stop();
    }
+16 −0
Original line number Diff line number Diff line
@@ -167,4 +167,20 @@ public class BluetoothDeviceUpdaterTest {

        assertThat(mBluetoothDeviceUpdater.isDeviceConnected(mCachedBluetoothDevice)).isFalse();
    }

    @Test
    public void registerCallback_localBluetoothManagerNull_shouldNotCrash() {
        mBluetoothDeviceUpdater.mLocalManager = null;

        // Shouldn't crash
        mBluetoothDeviceUpdater.registerCallback();
    }

    @Test
    public void unregisterCallback_localBluetoothManagerNull_shouldNotCrash() {
        mBluetoothDeviceUpdater.mLocalManager = null;

        // Shouldn't crash
        mBluetoothDeviceUpdater.unregisterCallback();
    }
}
+16 −0
Original line number Diff line number Diff line
@@ -197,4 +197,20 @@ public class AvailableMediaDeviceGroupControllerTest {
        assertThat(mPreferenceGroup.getTitle()).isEqualTo(
                mContext.getText(R.string.connected_device_available_media_title));
    }

    @Test
    public void onStart_localBluetoothManagerNull_shouldNotCrash() {
        mAvailableMediaDeviceGroupController.mLocalBluetoothManager = null;

        // Shouldn't crash
        mAvailableMediaDeviceGroupController.onStart();
    }

    @Test
    public void onStop_localBluetoothManagerNull_shouldNotCrash() {
        mAvailableMediaDeviceGroupController.mLocalBluetoothManager = null;

        // Shouldn't crash
        mAvailableMediaDeviceGroupController.onStop();
    }
}
Loading