Add error handle for device that not support Bluetooth
- Add LocalBluetoothManager null check for device that not support Bluetooth - Add test to verify when LocalBluetoothManager is null will not crash Bug: 110712414 Test: make -j42 RunSettingsRoboTests Change-Id: Ib506a0206cfcfdfec60bdfcf9a1944338a7ab729
This commit is contained in:
@@ -19,6 +19,7 @@ import android.bluetooth.BluetoothDevice;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.os.SystemProperties;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.connecteddevice.DevicePreferenceCallback;
|
||||
@@ -52,11 +53,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;
|
||||
|
||||
@@ -85,6 +87,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);
|
||||
@@ -95,6 +101,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);
|
||||
|
@@ -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);
|
||||
}
|
||||
|
@@ -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();
|
||||
}
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
@@ -178,4 +178,22 @@ public class DiscoverableFooterPreferenceControllerTest {
|
||||
}
|
||||
return registeredBroadcastReceivers;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onResume_localBluetoothManagerNull_shouldNotCrash() {
|
||||
mDiscoverableFooterPreferenceController.mLocalManager = null;
|
||||
mDiscoverableFooterPreferenceController.init(mFooterPreferenceMixin, mPreference, null);
|
||||
|
||||
// Shouldn't crash
|
||||
mDiscoverableFooterPreferenceController.onResume();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPause_localBluetoothManagerNull_shouldNotCrash() {
|
||||
mDiscoverableFooterPreferenceController.mLocalManager = null;
|
||||
mDiscoverableFooterPreferenceController.init(mFooterPreferenceMixin, mPreference, null);
|
||||
|
||||
// Shouldn't crash
|
||||
mDiscoverableFooterPreferenceController.onPause();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user