Fixed null pointer exception when BT is enabled from settings screen with previously bonded devices

Change-Id: Ia0cf763920fd99897994ea15445aec1dcd48853e
This commit is contained in:
fredc
2012-05-19 10:17:14 -07:00
committed by Matthew Xie
parent c7b490c7a0
commit afedeacd57
4 changed files with 38 additions and 7 deletions

View File

@@ -28,6 +28,7 @@ import android.util.Log;
import com.android.settings.R; import com.android.settings.R;
import java.util.ArrayList;
import java.util.List; import java.util.List;
final class A2dpProfile implements LocalBluetoothProfile { final class A2dpProfile implements LocalBluetoothProfile {
@@ -53,7 +54,7 @@ final class A2dpProfile implements LocalBluetoothProfile {
implements BluetoothProfile.ServiceListener { implements BluetoothProfile.ServiceListener {
public void onServiceConnected(int profile, BluetoothProfile proxy) { public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (V) Log.d(TAG,"Bluetooth service disconnected"); if (V) Log.d(TAG,"Bluetooth service connected");
mService = (BluetoothA2dp) proxy; mService = (BluetoothA2dp) proxy;
mProfileManager.setA2dpServiceUp(true); mProfileManager.setA2dpServiceUp(true);
mIsProfileReady=true; mIsProfileReady=true;
@@ -70,7 +71,6 @@ final class A2dpProfile implements LocalBluetoothProfile {
return mIsProfileReady; return mIsProfileReady;
} }
A2dpProfile(Context context, LocalBluetoothProfileManager profileManager) { A2dpProfile(Context context, LocalBluetoothProfileManager profileManager) {
mProfileManager = profileManager; mProfileManager = profileManager;
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.getProfileProxy(context, new A2dpServiceListener(), adapter.getProfileProxy(context, new A2dpServiceListener(),
@@ -86,6 +86,7 @@ final class A2dpProfile implements LocalBluetoothProfile {
} }
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {
if (mService == null) return new ArrayList<BluetoothDevice>(0);
return mService.getDevicesMatchingConnectionStates( return mService.getDevicesMatchingConnectionStates(
new int[] {BluetoothProfile.STATE_CONNECTED, new int[] {BluetoothProfile.STATE_CONNECTED,
BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_CONNECTING,
@@ -93,6 +94,7 @@ final class A2dpProfile implements LocalBluetoothProfile {
} }
public boolean connect(BluetoothDevice device) { public boolean connect(BluetoothDevice device) {
if (mService == null) return false;
List<BluetoothDevice> sinks = getConnectedDevices(); List<BluetoothDevice> sinks = getConnectedDevices();
if (sinks != null) { if (sinks != null) {
for (BluetoothDevice sink : sinks) { for (BluetoothDevice sink : sinks) {
@@ -103,6 +105,7 @@ final class A2dpProfile implements LocalBluetoothProfile {
} }
public boolean disconnect(BluetoothDevice device) { public boolean disconnect(BluetoothDevice device) {
if (mService == null) return false;
return mService.disconnect(device); return mService.disconnect(device);
} }
@@ -110,6 +113,7 @@ final class A2dpProfile implements LocalBluetoothProfile {
// as setPreferred() takes only boolean input but getPreferred() supports interger output. // as setPreferred() takes only boolean input but getPreferred() supports interger output.
// Also this need not implemented by all profiles so this has been added here. // Also this need not implemented by all profiles so this has been added here.
public void enableAutoConnect(BluetoothDevice device, boolean enable) { public void enableAutoConnect(BluetoothDevice device, boolean enable) {
if (mService == null) return;
if (enable) { if (enable) {
mService.setPriority(device, BluetoothProfile.PRIORITY_AUTO_CONNECT); mService.setPriority(device, BluetoothProfile.PRIORITY_AUTO_CONNECT);
} else { } else {
@@ -120,18 +124,24 @@ final class A2dpProfile implements LocalBluetoothProfile {
} }
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) {
return BluetoothProfile.STATE_DISCONNECTED;
}
return mService.getConnectionState(device); return mService.getConnectionState(device);
} }
public boolean isPreferred(BluetoothDevice device) { public boolean isPreferred(BluetoothDevice device) {
if (mService == null) return false;
return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF; return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
} }
public int getPreferred(BluetoothDevice device) { public int getPreferred(BluetoothDevice device) {
if (mService == null) return BluetoothProfile.PRIORITY_OFF;
return mService.getPriority(device); return mService.getPriority(device);
} }
public void setPreferred(BluetoothDevice device, boolean preferred) { public void setPreferred(BluetoothDevice device, boolean preferred) {
if (mService == null) return;
if (preferred) { if (preferred) {
if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) { if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
mService.setPriority(device, BluetoothProfile.PRIORITY_ON); mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
@@ -142,6 +152,7 @@ final class A2dpProfile implements LocalBluetoothProfile {
} }
boolean isA2dpPlaying() { boolean isA2dpPlaying() {
if (mService == null) return false;
List<BluetoothDevice> sinks = mService.getConnectedDevices(); List<BluetoothDevice> sinks = mService.getConnectedDevices();
if (!sinks.isEmpty()) { if (!sinks.isEmpty()) {
if (mService.isA2dpPlaying(sinks.get(0))) { if (mService.isA2dpPlaying(sinks.get(0))) {
@@ -164,7 +175,7 @@ final class A2dpProfile implements LocalBluetoothProfile {
} }
public int getSummaryResourceForDevice(BluetoothDevice device) { public int getSummaryResourceForDevice(BluetoothDevice device) {
int state = mService.getConnectionState(device); int state = getConnectionStatus(device);
switch (state) { switch (state) {
case BluetoothProfile.STATE_DISCONNECTED: case BluetoothProfile.STATE_DISCONNECTED:
{ {

View File

@@ -28,6 +28,7 @@ import android.util.Log;
import com.android.settings.R; import com.android.settings.R;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@@ -114,6 +115,7 @@ final class HeadsetProfile implements LocalBluetoothProfile {
} }
public boolean connect(BluetoothDevice device) { public boolean connect(BluetoothDevice device) {
if (mService == null) return false;
List<BluetoothDevice> sinks = mService.getConnectedDevices(); List<BluetoothDevice> sinks = mService.getConnectedDevices();
if (sinks != null) { if (sinks != null) {
for (BluetoothDevice sink : sinks) { for (BluetoothDevice sink : sinks) {
@@ -124,6 +126,7 @@ final class HeadsetProfile implements LocalBluetoothProfile {
} }
public boolean disconnect(BluetoothDevice device) { public boolean disconnect(BluetoothDevice device) {
if (mService == null) return false;
List<BluetoothDevice> deviceList = mService.getConnectedDevices(); List<BluetoothDevice> deviceList = mService.getConnectedDevices();
if (!deviceList.isEmpty() && deviceList.get(0).equals(device)) { if (!deviceList.isEmpty() && deviceList.get(0).equals(device)) {
// Downgrade priority as user is disconnecting the headset. // Downgrade priority as user is disconnecting the headset.
@@ -138,7 +141,6 @@ final class HeadsetProfile implements LocalBluetoothProfile {
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) return BluetoothProfile.STATE_DISCONNECTED; if (mService == null) return BluetoothProfile.STATE_DISCONNECTED;
List<BluetoothDevice> deviceList = mService.getConnectedDevices(); List<BluetoothDevice> deviceList = mService.getConnectedDevices();
return !deviceList.isEmpty() && deviceList.get(0).equals(device) return !deviceList.isEmpty() && deviceList.get(0).equals(device)
@@ -147,14 +149,17 @@ final class HeadsetProfile implements LocalBluetoothProfile {
} }
public boolean isPreferred(BluetoothDevice device) { public boolean isPreferred(BluetoothDevice device) {
if (mService == null) return false;
return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF; return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
} }
public int getPreferred(BluetoothDevice device) { public int getPreferred(BluetoothDevice device) {
if (mService == null) return BluetoothProfile.PRIORITY_OFF;
return mService.getPriority(device); return mService.getPriority(device);
} }
public void setPreferred(BluetoothDevice device, boolean preferred) { public void setPreferred(BluetoothDevice device, boolean preferred) {
if (mService == null) return;
if (preferred) { if (preferred) {
if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) { if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
mService.setPriority(device, BluetoothProfile.PRIORITY_ON); mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
@@ -165,6 +170,7 @@ final class HeadsetProfile implements LocalBluetoothProfile {
} }
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {
if (mService == null) return new ArrayList<BluetoothDevice>(0);
return mService.getDevicesMatchingConnectionStates( return mService.getDevicesMatchingConnectionStates(
new int[] {BluetoothProfile.STATE_CONNECTED, new int[] {BluetoothProfile.STATE_CONNECTED,
BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_CONNECTING,
@@ -176,6 +182,7 @@ final class HeadsetProfile implements LocalBluetoothProfile {
// as setPreferred() takes only boolean input but getPreferred() supports interger output. // as setPreferred() takes only boolean input but getPreferred() supports interger output.
// Also this need not implemented by all profiles so this has been added here. // Also this need not implemented by all profiles so this has been added here.
public void enableAutoConnect(BluetoothDevice device, boolean enable) { public void enableAutoConnect(BluetoothDevice device, boolean enable) {
if (mService == null) return;
if (enable) { if (enable) {
mService.setPriority(device, BluetoothProfile.PRIORITY_AUTO_CONNECT); mService.setPriority(device, BluetoothProfile.PRIORITY_AUTO_CONNECT);
} else { } else {
@@ -198,7 +205,7 @@ final class HeadsetProfile implements LocalBluetoothProfile {
} }
public int getSummaryResourceForDevice(BluetoothDevice device) { public int getSummaryResourceForDevice(BluetoothDevice device) {
int state = mService.getConnectionState(device); int state = getConnectionStatus(device);
switch (state) { switch (state) {
case BluetoothProfile.STATE_DISCONNECTED: case BluetoothProfile.STATE_DISCONNECTED:
return R.string.bluetooth_headset_profile_summary_use_for; return R.string.bluetooth_headset_profile_summary_use_for;

View File

@@ -77,14 +77,19 @@ final class HidProfile implements LocalBluetoothProfile {
} }
public boolean connect(BluetoothDevice device) { public boolean connect(BluetoothDevice device) {
if (mService == null) return false;
return mService.connect(device); return mService.connect(device);
} }
public boolean disconnect(BluetoothDevice device) { public boolean disconnect(BluetoothDevice device) {
if (mService == null) return false;
return mService.disconnect(device); return mService.disconnect(device);
} }
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) {
return BluetoothProfile.STATE_DISCONNECTED;
}
List<BluetoothDevice> deviceList = mService.getConnectedDevices(); List<BluetoothDevice> deviceList = mService.getConnectedDevices();
return !deviceList.isEmpty() && deviceList.get(0).equals(device) return !deviceList.isEmpty() && deviceList.get(0).equals(device)
@@ -93,14 +98,17 @@ final class HidProfile implements LocalBluetoothProfile {
} }
public boolean isPreferred(BluetoothDevice device) { public boolean isPreferred(BluetoothDevice device) {
if (mService == null) return false;
return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF; return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
} }
public int getPreferred(BluetoothDevice device) { public int getPreferred(BluetoothDevice device) {
if (mService == null) return BluetoothProfile.PRIORITY_OFF;
return mService.getPriority(device); return mService.getPriority(device);
} }
public void setPreferred(BluetoothDevice device, boolean preferred) { public void setPreferred(BluetoothDevice device, boolean preferred) {
if (mService == null) return;
if (preferred) { if (preferred) {
if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) { if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
mService.setPriority(device, BluetoothProfile.PRIORITY_ON); mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
@@ -124,7 +132,7 @@ final class HidProfile implements LocalBluetoothProfile {
} }
public int getSummaryResourceForDevice(BluetoothDevice device) { public int getSummaryResourceForDevice(BluetoothDevice device) {
int state = mService.getConnectionState(device); int state = getConnectionStatus(device);
switch (state) { switch (state) {
case BluetoothProfile.STATE_DISCONNECTED: case BluetoothProfile.STATE_DISCONNECTED:
return R.string.bluetooth_hid_profile_summary_use_for; return R.string.bluetooth_hid_profile_summary_use_for;

View File

@@ -83,6 +83,7 @@ final class PanProfile implements LocalBluetoothProfile {
} }
public boolean connect(BluetoothDevice device) { public boolean connect(BluetoothDevice device) {
if (mService == null) return false;
List<BluetoothDevice> sinks = mService.getConnectedDevices(); List<BluetoothDevice> sinks = mService.getConnectedDevices();
if (sinks != null) { if (sinks != null) {
for (BluetoothDevice sink : sinks) { for (BluetoothDevice sink : sinks) {
@@ -93,10 +94,14 @@ final class PanProfile implements LocalBluetoothProfile {
} }
public boolean disconnect(BluetoothDevice device) { public boolean disconnect(BluetoothDevice device) {
if (mService == null) return false;
return mService.disconnect(device); return mService.disconnect(device);
} }
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) {
return BluetoothProfile.STATE_DISCONNECTED;
}
return mService.getConnectionState(device); return mService.getConnectionState(device);
} }
@@ -129,7 +134,7 @@ final class PanProfile implements LocalBluetoothProfile {
} }
public int getSummaryResourceForDevice(BluetoothDevice device) { public int getSummaryResourceForDevice(BluetoothDevice device) {
int state = mService.getConnectionState(device); int state = getConnectionStatus(device);
switch (state) { switch (state) {
case BluetoothProfile.STATE_DISCONNECTED: case BluetoothProfile.STATE_DISCONNECTED:
return R.string.bluetooth_pan_profile_summary_use_for; return R.string.bluetooth_pan_profile_summary_use_for;