Merge changes from topic "update_profile_api"

* changes:
  Use new bt api to set active device
  Update LocalBluetoothProfile api
  Update connect/disconnect Bluetooth api
This commit is contained in:
Treehugger Robot
2020-02-07 02:46:12 +00:00
committed by Gerrit Code Review
25 changed files with 303 additions and 433 deletions

View File

@@ -16,6 +16,10 @@
package com.android.settingslib.bluetooth; package com.android.settingslib.bluetooth;
import static android.bluetooth.BluetoothAdapter.ACTIVE_DEVICE_AUDIO;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
import android.bluetooth.BluetoothA2dp; import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothClass;
@@ -42,6 +46,7 @@ public class A2dpProfile implements LocalBluetoothProfile {
private boolean mIsProfileReady; private boolean mIsProfileReady;
private final CachedBluetoothDeviceManager mDeviceManager; private final CachedBluetoothDeviceManager mDeviceManager;
private final BluetoothAdapter mBluetoothAdapter;
static final ParcelUuid[] SINK_UUIDS = { static final ParcelUuid[] SINK_UUIDS = {
BluetoothUuid.A2DP_SINK, BluetoothUuid.A2DP_SINK,
@@ -96,7 +101,8 @@ public class A2dpProfile implements LocalBluetoothProfile {
mContext = context; mContext = context;
mDeviceManager = deviceManager; mDeviceManager = deviceManager;
mProfileManager = profileManager; mProfileManager = profileManager;
BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, new A2dpServiceListener(), mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.getProfileProxy(context, new A2dpServiceListener(),
BluetoothProfile.A2DP); BluetoothProfile.A2DP);
} }
@@ -147,20 +153,6 @@ public class A2dpProfile implements LocalBluetoothProfile {
return mService.getDevicesMatchingConnectionStates(states); return mService.getDevicesMatchingConnectionStates(states);
} }
public boolean connect(BluetoothDevice device) {
if (mService == null) return false;
return mService.connect(device);
}
public boolean disconnect(BluetoothDevice device) {
if (mService == null) return false;
// Downgrade priority as user is disconnecting the headset.
if (mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
}
return mService.disconnect(device);
}
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
@@ -169,8 +161,10 @@ public class A2dpProfile implements LocalBluetoothProfile {
} }
public boolean setActiveDevice(BluetoothDevice device) { public boolean setActiveDevice(BluetoothDevice device) {
if (mService == null) return false; if (mBluetoothAdapter == null) {
return mService.setActiveDevice(device); return false;
}
return mBluetoothAdapter.setActiveDevice(device, ACTIVE_DEVICE_AUDIO);
} }
public BluetoothDevice getActiveDevice() { public BluetoothDevice getActiveDevice() {
@@ -178,31 +172,37 @@ public class A2dpProfile implements LocalBluetoothProfile {
return mService.getActiveDevice(); return mService.getActiveDevice();
} }
public boolean isPreferred(BluetoothDevice device) { @Override
public boolean isEnabled(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return false; return false;
} }
return mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return mService.getConnectionPolicy(device) > CONNECTION_POLICY_FORBIDDEN;
} }
public int getPreferred(BluetoothDevice device) { @Override
public int getConnectionPolicy(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return CONNECTION_POLICY_FORBIDDEN;
} }
return mService.getConnectionPolicy(device); return mService.getConnectionPolicy(device);
} }
public void setPreferred(BluetoothDevice device, boolean preferred) { @Override
public boolean setEnabled(BluetoothDevice device, boolean enabled) {
boolean isEnabled = false;
if (mService == null) { if (mService == null) {
return; return false;
} }
if (preferred) { if (enabled) {
if (mService.getConnectionPolicy(device) < BluetoothProfile.CONNECTION_POLICY_ALLOWED) { if (mService.getConnectionPolicy(device) < CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED);
} }
} else { } else {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
} }
return isEnabled;
} }
boolean isA2dpPlaying() { boolean isA2dpPlaying() {
if (mService == null) return false; if (mService == null) return false;

View File

@@ -16,6 +16,9 @@
package com.android.settingslib.bluetooth; package com.android.settingslib.bluetooth;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
import android.bluetooth.BluetoothA2dpSink; import android.bluetooth.BluetoothA2dpSink;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothClass;
@@ -112,24 +115,6 @@ final class A2dpSinkProfile implements LocalBluetoothProfile {
BluetoothProfile.STATE_DISCONNECTING}); BluetoothProfile.STATE_DISCONNECTING});
} }
public boolean connect(BluetoothDevice device) {
if (mService == null) {
return false;
}
return mService.connect(device);
}
public boolean disconnect(BluetoothDevice device) {
if (mService == null) {
return false;
}
// Downgrade priority as user is disconnecting the headset.
if (mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
}
return mService.disconnect(device);
}
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
@@ -137,31 +122,37 @@ final class A2dpSinkProfile implements LocalBluetoothProfile {
return mService.getConnectionState(device); return mService.getConnectionState(device);
} }
public boolean isPreferred(BluetoothDevice device) { @Override
public boolean isEnabled(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return false; return false;
} }
return mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return mService.getConnectionPolicy(device) > CONNECTION_POLICY_FORBIDDEN;
} }
public int getPreferred(BluetoothDevice device) { @Override
public int getConnectionPolicy(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return CONNECTION_POLICY_FORBIDDEN;
} }
return mService.getConnectionPolicy(device); return mService.getConnectionPolicy(device);
} }
public void setPreferred(BluetoothDevice device, boolean preferred) { @Override
public boolean setEnabled(BluetoothDevice device, boolean enabled) {
boolean isEnabled = false;
if (mService == null) { if (mService == null) {
return; return false;
} }
if (preferred) { if (enabled) {
if (mService.getConnectionPolicy(device) < BluetoothProfile.CONNECTION_POLICY_ALLOWED) { if (mService.getConnectionPolicy(device) < CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED);
} }
} else { } else {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
} }
return isEnabled;
} }
boolean isAudioPlaying() { boolean isAudioPlaying() {

View File

@@ -135,7 +135,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
synchronized (mProfileLock) { synchronized (mProfileLock) {
if (newProfileState == BluetoothProfile.STATE_CONNECTED) { if (newProfileState == BluetoothProfile.STATE_CONNECTED) {
if (profile instanceof MapProfile) { if (profile instanceof MapProfile) {
profile.setPreferred(mDevice, true); profile.setEnabled(mDevice, true);
} }
if (!mProfiles.contains(profile)) { if (!mProfiles.contains(profile)) {
mRemovedProfiles.remove(profile); mRemovedProfiles.remove(profile);
@@ -148,7 +148,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
} }
} else if (profile instanceof MapProfile } else if (profile instanceof MapProfile
&& newProfileState == BluetoothProfile.STATE_DISCONNECTED) { && newProfileState == BluetoothProfile.STATE_DISCONNECTED) {
profile.setPreferred(mDevice, false); profile.setEnabled(mDevice, false);
} else if (mLocalNapRoleConnected && profile instanceof PanProfile } else if (mLocalNapRoleConnected && profile instanceof PanProfile
&& ((PanProfile) profile).isLocalRoleNap(mDevice) && ((PanProfile) profile).isLocalRoleNap(mDevice)
&& newProfileState == BluetoothProfile.STATE_DISCONNECTED) { && newProfileState == BluetoothProfile.STATE_DISCONNECTED) {
@@ -172,12 +172,12 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
PbapServerProfile PbapProfile = mProfileManager.getPbapProfile(); PbapServerProfile PbapProfile = mProfileManager.getPbapProfile();
if (PbapProfile != null && isConnectedProfile(PbapProfile)) if (PbapProfile != null && isConnectedProfile(PbapProfile))
{ {
PbapProfile.disconnect(mDevice); PbapProfile.setEnabled(mDevice, false);
} }
} }
public void disconnect(LocalBluetoothProfile profile) { public void disconnect(LocalBluetoothProfile profile) {
if (profile.disconnect(mDevice)) { if (profile.setEnabled(mDevice, false)) {
if (BluetoothUtils.D) { if (BluetoothUtils.D) {
Log.d(TAG, "Command sent successfully:DISCONNECT " + describe(profile)); Log.d(TAG, "Command sent successfully:DISCONNECT " + describe(profile));
} }
@@ -264,7 +264,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
if (!ensurePaired()) { if (!ensurePaired()) {
return; return;
} }
if (profile.connect(mDevice)) { if (profile.setEnabled(mDevice, true)) {
if (BluetoothUtils.D) { if (BluetoothUtils.D) {
Log.d(TAG, "Command sent successfully:CONNECT " + describe(profile)); Log.d(TAG, "Command sent successfully:CONNECT " + describe(profile));
} }

View File

@@ -16,6 +16,10 @@
package com.android.settingslib.bluetooth; package com.android.settingslib.bluetooth;
import static android.bluetooth.BluetoothAdapter.ACTIVE_DEVICE_PHONE_CALL;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
@@ -42,6 +46,7 @@ public class HeadsetProfile implements LocalBluetoothProfile {
private final CachedBluetoothDeviceManager mDeviceManager; private final CachedBluetoothDeviceManager mDeviceManager;
private final LocalBluetoothProfileManager mProfileManager; private final LocalBluetoothProfileManager mProfileManager;
private final BluetoothAdapter mBluetoothAdapter;
static final ParcelUuid[] UUIDS = { static final ParcelUuid[] UUIDS = {
BluetoothUuid.HSP, BluetoothUuid.HSP,
@@ -96,7 +101,8 @@ public class HeadsetProfile implements LocalBluetoothProfile {
LocalBluetoothProfileManager profileManager) { LocalBluetoothProfileManager profileManager) {
mDeviceManager = deviceManager; mDeviceManager = deviceManager;
mProfileManager = profileManager; mProfileManager = profileManager;
BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, new HeadsetServiceListener(), mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.getProfileProxy(context, new HeadsetServiceListener(),
BluetoothProfile.HEADSET); BluetoothProfile.HEADSET);
} }
@@ -108,24 +114,6 @@ public class HeadsetProfile implements LocalBluetoothProfile {
return true; return true;
} }
public boolean connect(BluetoothDevice device) {
if (mService == null) {
return false;
}
return mService.connect(device);
}
public boolean disconnect(BluetoothDevice device) {
if (mService == null) {
return false;
}
// Downgrade priority as user is disconnecting the headset.
if (mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
}
return mService.disconnect(device);
}
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
@@ -134,10 +122,10 @@ public class HeadsetProfile implements LocalBluetoothProfile {
} }
public boolean setActiveDevice(BluetoothDevice device) { public boolean setActiveDevice(BluetoothDevice device) {
if (mService == null) { if (mBluetoothAdapter == null) {
return false; return false;
} }
return mService.setActiveDevice(device); return mBluetoothAdapter.setActiveDevice(device, ACTIVE_DEVICE_PHONE_CALL);
} }
public BluetoothDevice getActiveDevice() { public BluetoothDevice getActiveDevice() {
@@ -161,31 +149,37 @@ public class HeadsetProfile implements LocalBluetoothProfile {
return mService.getAudioState(device); return mService.getAudioState(device);
} }
public boolean isPreferred(BluetoothDevice device) { @Override
public boolean isEnabled(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return false; return false;
} }
return mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return mService.getConnectionPolicy(device) > CONNECTION_POLICY_FORBIDDEN;
} }
public int getPreferred(BluetoothDevice device) { @Override
public int getConnectionPolicy(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return CONNECTION_POLICY_FORBIDDEN;
} }
return mService.getConnectionPolicy(device); return mService.getConnectionPolicy(device);
} }
public void setPreferred(BluetoothDevice device, boolean preferred) { @Override
public boolean setEnabled(BluetoothDevice device, boolean enabled) {
boolean isEnabled = false;
if (mService == null) { if (mService == null) {
return; return false;
} }
if (preferred) { if (enabled) {
if (mService.getConnectionPolicy(device) < BluetoothProfile.CONNECTION_POLICY_ALLOWED) { if (mService.getConnectionPolicy(device) < CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED);
} }
} else { } else {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
} }
return isEnabled;
} }
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {

View File

@@ -16,6 +16,10 @@
package com.android.settingslib.bluetooth; package com.android.settingslib.bluetooth;
import static android.bluetooth.BluetoothAdapter.ACTIVE_DEVICE_ALL;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
@@ -42,6 +46,7 @@ public class HearingAidProfile implements LocalBluetoothProfile {
static final String NAME = "HearingAid"; static final String NAME = "HearingAid";
private final LocalBluetoothProfileManager mProfileManager; private final LocalBluetoothProfileManager mProfileManager;
private final BluetoothAdapter mBluetoothAdapter;
// Order of this profile in device profiles list // Order of this profile in device profiles list
private static final int ORDINAL = 1; private static final int ORDINAL = 1;
@@ -94,7 +99,8 @@ public class HearingAidProfile implements LocalBluetoothProfile {
mContext = context; mContext = context;
mDeviceManager = deviceManager; mDeviceManager = deviceManager;
mProfileManager = profileManager; mProfileManager = profileManager;
BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.getProfileProxy(context,
new HearingAidServiceListener(), BluetoothProfile.HEARING_AID); new HearingAidServiceListener(), BluetoothProfile.HEARING_AID);
} }
@@ -145,20 +151,6 @@ public class HearingAidProfile implements LocalBluetoothProfile {
return mService.getDevicesMatchingConnectionStates(states); return mService.getDevicesMatchingConnectionStates(states);
} }
public boolean connect(BluetoothDevice device) {
if (mService == null) return false;
return mService.connect(device);
}
public boolean disconnect(BluetoothDevice device) {
if (mService == null) return false;
// Downgrade priority as user is disconnecting the hearing aid.
if (mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
}
return mService.disconnect(device);
}
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
@@ -167,8 +159,10 @@ public class HearingAidProfile implements LocalBluetoothProfile {
} }
public boolean setActiveDevice(BluetoothDevice device) { public boolean setActiveDevice(BluetoothDevice device) {
if (mService == null) return false; if (mBluetoothAdapter == null) {
return mService.setActiveDevice(device); return false;
}
return mBluetoothAdapter.setActiveDevice(device, ACTIVE_DEVICE_ALL);
} }
public List<BluetoothDevice> getActiveDevices() { public List<BluetoothDevice> getActiveDevices() {
@@ -176,31 +170,37 @@ public class HearingAidProfile implements LocalBluetoothProfile {
return mService.getActiveDevices(); return mService.getActiveDevices();
} }
public boolean isPreferred(BluetoothDevice device) { @Override
public boolean isEnabled(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return false; return false;
} }
return mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return mService.getConnectionPolicy(device) > CONNECTION_POLICY_FORBIDDEN;
} }
public int getPreferred(BluetoothDevice device) { @Override
public int getConnectionPolicy(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return CONNECTION_POLICY_FORBIDDEN;
} }
return mService.getConnectionPolicy(device); return mService.getConnectionPolicy(device);
} }
public void setPreferred(BluetoothDevice device, boolean preferred) { @Override
public boolean setEnabled(BluetoothDevice device, boolean enabled) {
boolean isEnabled = false;
if (mService == null) { if (mService == null) {
return; return false;
} }
if (preferred) { if (enabled) {
if (mService.getConnectionPolicy(device) < BluetoothProfile.CONNECTION_POLICY_ALLOWED) { if (mService.getConnectionPolicy(device) < CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED);
} }
} else { } else {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
} }
return isEnabled;
} }
public void setVolume(int volume) { public void setVolume(int volume) {

View File

@@ -16,6 +16,9 @@
package com.android.settingslib.bluetooth; package com.android.settingslib.bluetooth;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
@@ -121,26 +124,6 @@ final class HfpClientProfile implements LocalBluetoothProfile {
BluetoothProfile.STATE_DISCONNECTING}); BluetoothProfile.STATE_DISCONNECTING});
} }
@Override
public boolean connect(BluetoothDevice device) {
if (mService == null) {
return false;
}
return mService.connect(device);
}
@Override
public boolean disconnect(BluetoothDevice device) {
if (mService == null) {
return false;
}
// Downgrade priority as user is disconnecting the headset.
if (mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
}
return mService.disconnect(device);
}
@Override @Override
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
@@ -150,33 +133,36 @@ final class HfpClientProfile implements LocalBluetoothProfile {
} }
@Override @Override
public boolean isPreferred(BluetoothDevice device) { public boolean isEnabled(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return false; return false;
} }
return mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return mService.getConnectionPolicy(device) > CONNECTION_POLICY_FORBIDDEN;
} }
@Override @Override
public int getPreferred(BluetoothDevice device) { public int getConnectionPolicy(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return CONNECTION_POLICY_FORBIDDEN;
} }
return mService.getConnectionPolicy(device); return mService.getConnectionPolicy(device);
} }
@Override @Override
public void setPreferred(BluetoothDevice device, boolean preferred) { public boolean setEnabled(BluetoothDevice device, boolean enabled) {
boolean isEnabled = false;
if (mService == null) { if (mService == null) {
return; return false;
} }
if (preferred) { if (enabled) {
if (mService.getConnectionPolicy(device) < BluetoothProfile.CONNECTION_POLICY_ALLOWED) { if (mService.getConnectionPolicy(device) < CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED);
} }
} else { } else {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
} }
return isEnabled;
} }
@Override @Override

View File

@@ -16,6 +16,8 @@
package com.android.settingslib.bluetooth; package com.android.settingslib.bluetooth;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
@@ -101,20 +103,6 @@ public class HidDeviceProfile implements LocalBluetoothProfile {
return false; return false;
} }
@Override
public boolean connect(BluetoothDevice device) {
// Don't invoke method in service because settings is not allowed to connect this profile.
return false;
}
@Override
public boolean disconnect(BluetoothDevice device) {
if (mService == null) {
return false;
}
return mService.disconnect(device);
}
@Override @Override
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
@@ -124,21 +112,24 @@ public class HidDeviceProfile implements LocalBluetoothProfile {
} }
@Override @Override
public boolean isPreferred(BluetoothDevice device) { public boolean isEnabled(BluetoothDevice device) {
return getConnectionStatus(device) != BluetoothProfile.STATE_DISCONNECTED; return getConnectionStatus(device) != BluetoothProfile.STATE_DISCONNECTED;
} }
@Override @Override
public int getPreferred(BluetoothDevice device) { public int getConnectionPolicy(BluetoothDevice device) {
return PREFERRED_VALUE; return PREFERRED_VALUE;
} }
@Override @Override
public void setPreferred(BluetoothDevice device, boolean preferred) { public boolean setEnabled(BluetoothDevice device, boolean enabled) {
boolean isEnabled = false;
// if set preferred to false, then disconnect to the current device // if set preferred to false, then disconnect to the current device
if (!preferred) { if (!enabled) {
mService.disconnect(device); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
} }
return isEnabled;
} }
@Override @Override

View File

@@ -16,6 +16,9 @@
package com.android.settingslib.bluetooth; package com.android.settingslib.bluetooth;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
@@ -98,16 +101,6 @@ public class HidProfile implements LocalBluetoothProfile {
return true; return true;
} }
public boolean connect(BluetoothDevice device) {
if (mService == null) return false;
return mService.connect(device);
}
public boolean disconnect(BluetoothDevice device) {
if (mService == null) return false;
return mService.disconnect(device);
}
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
@@ -115,29 +108,37 @@ public class HidProfile implements LocalBluetoothProfile {
return mService.getConnectionState(device); return mService.getConnectionState(device);
} }
public boolean isPreferred(BluetoothDevice device) { @Override
public boolean isEnabled(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return false; return false;
} }
return mService.getConnectionPolicy(device) != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return mService.getConnectionPolicy(device) != CONNECTION_POLICY_FORBIDDEN;
} }
public int getPreferred(BluetoothDevice device) { @Override
public int getConnectionPolicy(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return CONNECTION_POLICY_FORBIDDEN;
} }
return mService.getConnectionPolicy(device); return mService.getConnectionPolicy(device);
} }
public void setPreferred(BluetoothDevice device, boolean preferred) { @Override
if (mService == null) return; public boolean setEnabled(BluetoothDevice device, boolean enabled) {
if (preferred) { boolean isEnabled = false;
if (mService.getConnectionPolicy(device) < BluetoothProfile.CONNECTION_POLICY_ALLOWED) { if (mService == null) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED); return false;
}
if (enabled) {
if (mService.getConnectionPolicy(device) < CONNECTION_POLICY_ALLOWED) {
isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED);
} }
} else { } else {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
} }
return isEnabled;
} }
public String toString() { public String toString() {

View File

@@ -35,17 +35,26 @@ public interface LocalBluetoothProfile {
*/ */
boolean isAutoConnectable(); boolean isAutoConnectable();
boolean connect(BluetoothDevice device);
boolean disconnect(BluetoothDevice device);
int getConnectionStatus(BluetoothDevice device); int getConnectionStatus(BluetoothDevice device);
boolean isPreferred(BluetoothDevice device); /**
* Return {@code true} if the profile is enabled, otherwise return {@code false}.
* @param device the device to query for enable status
*/
boolean isEnabled(BluetoothDevice device);
int getPreferred(BluetoothDevice device); /**
* Get the connection policy of the profile.
* @param device the device to query for enable status
*/
int getConnectionPolicy(BluetoothDevice device);
void setPreferred(BluetoothDevice device, boolean preferred); /**
* Enable the profile if {@code enabled} is {@code true}, otherwise disable profile.
* @param device the device to set profile status
* @param enabled {@code true} for enable profile, otherwise disable profile.
*/
boolean setEnabled(BluetoothDevice device, boolean enabled);
boolean isProfileReady(); boolean isProfileReady();

View File

@@ -528,14 +528,14 @@ public class LocalBluetoothProfileManager {
(mMapProfile.getConnectionStatus(device) == BluetoothProfile.STATE_CONNECTED)) { (mMapProfile.getConnectionStatus(device) == BluetoothProfile.STATE_CONNECTED)) {
profiles.add(mMapProfile); profiles.add(mMapProfile);
removedProfiles.remove(mMapProfile); removedProfiles.remove(mMapProfile);
mMapProfile.setPreferred(device, true); mMapProfile.setEnabled(device, true);
} }
if ((mPbapProfile != null) && if ((mPbapProfile != null) &&
(mPbapProfile.getConnectionStatus(device) == BluetoothProfile.STATE_CONNECTED)) { (mPbapProfile.getConnectionStatus(device) == BluetoothProfile.STATE_CONNECTED)) {
profiles.add(mPbapProfile); profiles.add(mPbapProfile);
removedProfiles.remove(mPbapProfile); removedProfiles.remove(mPbapProfile);
mPbapProfile.setPreferred(device, true); mPbapProfile.setEnabled(device, true);
} }
if (mMapClientProfile != null) { if (mMapClientProfile != null) {

View File

@@ -16,6 +16,9 @@
package com.android.settingslib.bluetooth; package com.android.settingslib.bluetooth;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
@@ -111,24 +114,6 @@ public final class MapClientProfile implements LocalBluetoothProfile {
return true; return true;
} }
public boolean connect(BluetoothDevice device) {
if (mService == null) {
return false;
}
return mService.connect(device);
}
public boolean disconnect(BluetoothDevice device) {
if (mService == null) {
return false;
}
// Downgrade priority as user is disconnecting.
if (mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
}
return mService.disconnect(device);
}
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
@@ -136,31 +121,37 @@ public final class MapClientProfile implements LocalBluetoothProfile {
return mService.getConnectionState(device); return mService.getConnectionState(device);
} }
public boolean isPreferred(BluetoothDevice device) { @Override
public boolean isEnabled(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return false; return false;
} }
return mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return mService.getConnectionPolicy(device) > CONNECTION_POLICY_FORBIDDEN;
} }
public int getPreferred(BluetoothDevice device) { @Override
public int getConnectionPolicy(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return CONNECTION_POLICY_FORBIDDEN;
} }
return mService.getConnectionPolicy(device); return mService.getConnectionPolicy(device);
} }
public void setPreferred(BluetoothDevice device, boolean preferred) { @Override
public boolean setEnabled(BluetoothDevice device, boolean enabled) {
boolean isEnabled = false;
if (mService == null) { if (mService == null) {
return; return false;
} }
if (preferred) { if (enabled) {
if (mService.getConnectionPolicy(device) < BluetoothProfile.CONNECTION_POLICY_ALLOWED) { if (mService.getConnectionPolicy(device) < CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED);
} }
} else { } else {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
} }
return isEnabled;
} }
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {

View File

@@ -16,6 +16,9 @@
package com.android.settingslib.bluetooth; package com.android.settingslib.bluetooth;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
@@ -110,21 +113,6 @@ public class MapProfile implements LocalBluetoothProfile {
return true; return true;
} }
public boolean connect(BluetoothDevice device) {
Log.d(TAG, "connect() - should not get called");
return false; // MAP never connects out
}
public boolean disconnect(BluetoothDevice device) {
if (mService == null) {
return false;
}
if (mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
}
return mService.disconnect(device);
}
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
@@ -132,31 +120,37 @@ public class MapProfile implements LocalBluetoothProfile {
return mService.getConnectionState(device); return mService.getConnectionState(device);
} }
public boolean isPreferred(BluetoothDevice device) { @Override
public boolean isEnabled(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return false; return false;
} }
return mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return mService.getConnectionPolicy(device) > CONNECTION_POLICY_FORBIDDEN;
} }
public int getPreferred(BluetoothDevice device) { @Override
public int getConnectionPolicy(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return CONNECTION_POLICY_FORBIDDEN;
} }
return mService.getConnectionPolicy(device); return mService.getConnectionPolicy(device);
} }
public void setPreferred(BluetoothDevice device, boolean preferred) { @Override
public boolean setEnabled(BluetoothDevice device, boolean enabled) {
boolean isEnabled = false;
if (mService == null) { if (mService == null) {
return; return false;
} }
if (preferred) { if (enabled) {
if (mService.getConnectionPolicy(device) < BluetoothProfile.CONNECTION_POLICY_ALLOWED) { if (mService.getConnectionPolicy(device) < CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED);
} }
} else { } else {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
} }
return isEnabled;
} }
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {

View File

@@ -40,27 +40,23 @@ final class OppProfile implements LocalBluetoothProfile {
return false; return false;
} }
public boolean connect(BluetoothDevice device) {
return false;
}
public boolean disconnect(BluetoothDevice device) {
return false;
}
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
return BluetoothProfile.STATE_DISCONNECTED; // Settings app doesn't handle OPP return BluetoothProfile.STATE_DISCONNECTED; // Settings app doesn't handle OPP
} }
public boolean isPreferred(BluetoothDevice device) { @Override
public boolean isEnabled(BluetoothDevice device) {
return false; return false;
} }
public int getPreferred(BluetoothDevice device) { @Override
public int getConnectionPolicy(BluetoothDevice device) {
return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; // Settings app doesn't handle OPP return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; // Settings app doesn't handle OPP
} }
public void setPreferred(BluetoothDevice device, boolean preferred) { @Override
public boolean setEnabled(BluetoothDevice device, boolean enabled) {
return false;
} }
public boolean isProfileReady() { public boolean isProfileReady() {

View File

@@ -16,6 +16,9 @@
package com.android.settingslib.bluetooth; package com.android.settingslib.bluetooth;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
@@ -83,22 +86,6 @@ public class PanProfile implements LocalBluetoothProfile {
return false; return false;
} }
public boolean connect(BluetoothDevice device) {
if (mService == null) return false;
List<BluetoothDevice> sinks = mService.getConnectedDevices();
if (sinks != null) {
for (BluetoothDevice sink : sinks) {
mService.disconnect(sink);
}
}
return mService.connect(device);
}
public boolean disconnect(BluetoothDevice device) {
if (mService == null) return false;
return mService.disconnect(device);
}
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
@@ -106,16 +93,36 @@ public class PanProfile implements LocalBluetoothProfile {
return mService.getConnectionState(device); return mService.getConnectionState(device);
} }
public boolean isPreferred(BluetoothDevice device) { @Override
public boolean isEnabled(BluetoothDevice device) {
return true; return true;
} }
public int getPreferred(BluetoothDevice device) { @Override
public int getConnectionPolicy(BluetoothDevice device) {
return -1; return -1;
} }
public void setPreferred(BluetoothDevice device, boolean preferred) { @Override
// ignore: isPreferred is always true for PAN public boolean setEnabled(BluetoothDevice device, boolean enabled) {
boolean isEnabled = false;
if (mService == null) {
return false;
}
if (enabled) {
final List<BluetoothDevice> sinks = mService.getConnectedDevices();
if (sinks != null) {
for (BluetoothDevice sink : sinks) {
mService.setConnectionPolicy(sink, CONNECTION_POLICY_FORBIDDEN);
}
}
isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED);
} else {
isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
}
return isEnabled;
} }
public String toString() { public String toString() {

View File

@@ -16,6 +16,9 @@
package com.android.settingslib.bluetooth; package com.android.settingslib.bluetooth;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
@@ -123,23 +126,6 @@ public final class PbapClientProfile implements LocalBluetoothProfile {
BluetoothProfile.STATE_DISCONNECTING}); BluetoothProfile.STATE_DISCONNECTING});
} }
public boolean connect(BluetoothDevice device) {
Log.d(TAG,"PBAPClientProfile got connect request");
if (mService == null) {
return false;
}
Log.d(TAG,"PBAPClientProfile attempting to connect to " + device.getAddress());
return mService.connect(device);
}
public boolean disconnect(BluetoothDevice device) {
Log.d(TAG,"PBAPClientProfile got disconnect request");
if (mService == null) {
return false;
}
return mService.disconnect(device);
}
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
@@ -147,31 +133,37 @@ public final class PbapClientProfile implements LocalBluetoothProfile {
return mService.getConnectionState(device); return mService.getConnectionState(device);
} }
public boolean isPreferred(BluetoothDevice device) { @Override
public boolean isEnabled(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return false; return false;
} }
return mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return mService.getConnectionPolicy(device) > CONNECTION_POLICY_FORBIDDEN;
} }
public int getPreferred(BluetoothDevice device) { @Override
public int getConnectionPolicy(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return CONNECTION_POLICY_FORBIDDEN;
} }
return mService.getConnectionPolicy(device); return mService.getConnectionPolicy(device);
} }
public void setPreferred(BluetoothDevice device, boolean preferred) { @Override
public boolean setEnabled(BluetoothDevice device, boolean enabled) {
boolean isEnabled = false;
if (mService == null) { if (mService == null) {
return; return false;
} }
if (preferred) { if (enabled) {
if (mService.getConnectionPolicy(device) < BluetoothProfile.CONNECTION_POLICY_ALLOWED) { if (mService.getConnectionPolicy(device) < CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED);
} }
} else { } else {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
} }
return isEnabled;
} }
public String toString() { public String toString() {

View File

@@ -16,6 +16,8 @@
package com.android.settingslib.bluetooth; package com.android.settingslib.bluetooth;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
@@ -89,32 +91,33 @@ public class PbapServerProfile implements LocalBluetoothProfile {
return false; return false;
} }
public boolean connect(BluetoothDevice device) {
/*Can't connect from server */
return false;
}
public boolean disconnect(BluetoothDevice device) {
if (mService == null) return false;
return mService.disconnect(device);
}
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) return BluetoothProfile.STATE_DISCONNECTED; if (mService == null) return BluetoothProfile.STATE_DISCONNECTED;
return mService.getConnectionState(device); return mService.getConnectionState(device);
} }
public boolean isPreferred(BluetoothDevice device) { @Override
public boolean isEnabled(BluetoothDevice device) {
return false; return false;
} }
public int getPreferred(BluetoothDevice device) { @Override
public int getConnectionPolicy(BluetoothDevice device) {
return -1; return -1;
} }
public void setPreferred(BluetoothDevice device, boolean preferred) { @Override
// ignore: isPreferred is always true for PBAP public boolean setEnabled(BluetoothDevice device, boolean enabled) {
boolean isEnabled = false;
if (mService == null) {
return false;
}
if (!enabled) {
isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
}
return isEnabled;
} }
public String toString() { public String toString() {

View File

@@ -16,6 +16,9 @@
package com.android.settingslib.bluetooth; package com.android.settingslib.bluetooth;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_ALLOWED;
import static android.bluetooth.BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
@@ -108,23 +111,6 @@ final class SapProfile implements LocalBluetoothProfile {
return true; return true;
} }
public boolean connect(BluetoothDevice device) {
if (mService == null) {
return false;
}
return mService.connect(device);
}
public boolean disconnect(BluetoothDevice device) {
if (mService == null) {
return false;
}
if (mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
}
return mService.disconnect(device);
}
public int getConnectionStatus(BluetoothDevice device) { public int getConnectionStatus(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
@@ -132,31 +118,37 @@ final class SapProfile implements LocalBluetoothProfile {
return mService.getConnectionState(device); return mService.getConnectionState(device);
} }
public boolean isPreferred(BluetoothDevice device) { @Override
public boolean isEnabled(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return false; return false;
} }
return mService.getConnectionPolicy(device) > BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return mService.getConnectionPolicy(device) > CONNECTION_POLICY_FORBIDDEN;
} }
public int getPreferred(BluetoothDevice device) { @Override
public int getConnectionPolicy(BluetoothDevice device) {
if (mService == null) { if (mService == null) {
return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; return CONNECTION_POLICY_FORBIDDEN;
} }
return mService.getConnectionPolicy(device); return mService.getConnectionPolicy(device);
} }
public void setPreferred(BluetoothDevice device, boolean preferred) { @Override
public boolean setEnabled(BluetoothDevice device, boolean enabled) {
boolean isEnabled = false;
if (mService == null) { if (mService == null) {
return; return false;
} }
if (preferred) { if (enabled) {
if (mService.getConnectionPolicy(device) < BluetoothProfile.CONNECTION_POLICY_ALLOWED) { if (mService.getConnectionPolicy(device) < CONNECTION_POLICY_ALLOWED) {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_ALLOWED);
} }
} else { } else {
mService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN); isEnabled = mService.setConnectionPolicy(device, CONNECTION_POLICY_FORBIDDEN);
} }
return isEnabled;
} }
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {

View File

@@ -108,9 +108,9 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
Log.d(TAG, "addConnectableA2dpDevices() device : " + cachedDevice.getName() Log.d(TAG, "addConnectableA2dpDevices() device : " + cachedDevice.getName()
+ ", is connected : " + cachedDevice.isConnected() + ", is connected : " + cachedDevice.isConnected()
+ ", is preferred : " + a2dpProfile.isPreferred(device)); + ", is enabled : " + a2dpProfile.isEnabled(device));
if (a2dpProfile.isPreferred(device) if (a2dpProfile.isEnabled(device)
&& BluetoothDevice.BOND_BONDED == cachedDevice.getBondState()) { && BluetoothDevice.BOND_BONDED == cachedDevice.getBondState()) {
addMediaDevice(cachedDevice); addMediaDevice(cachedDevice);
} }
@@ -140,13 +140,13 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
Log.d(TAG, "addConnectableHearingAidDevices() device : " + cachedDevice.getName() Log.d(TAG, "addConnectableHearingAidDevices() device : " + cachedDevice.getName()
+ ", is connected : " + cachedDevice.isConnected() + ", is connected : " + cachedDevice.isConnected()
+ ", is preferred : " + hapProfile.isPreferred(device)); + ", is enabled : " + hapProfile.isEnabled(device));
final long hiSyncId = hapProfile.getHiSyncId(device); final long hiSyncId = hapProfile.getHiSyncId(device);
// device with same hiSyncId should not be shown in the UI. // device with same hiSyncId should not be shown in the UI.
// So do not add it into connectedDevices. // So do not add it into connectedDevices.
if (!devicesHiSyncIds.contains(hiSyncId) && hapProfile.isPreferred(device) if (!devicesHiSyncIds.contains(hiSyncId) && hapProfile.isEnabled(device)
&& BluetoothDevice.BOND_BONDED == cachedDevice.getBondState()) { && BluetoothDevice.BOND_BONDED == cachedDevice.getBondState()) {
devicesHiSyncIds.add(hiSyncId); devicesHiSyncIds.add(hiSyncId);
addMediaDevice(cachedDevice); addMediaDevice(cachedDevice);

View File

@@ -18,7 +18,6 @@ package com.android.settingslib.bluetooth;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothA2dpSink; import android.bluetooth.BluetoothA2dpSink;
@@ -64,18 +63,6 @@ public class A2dpSinkProfileTest {
mServiceListener.onServiceConnected(BluetoothProfile.A2DP_SINK, mService); mServiceListener.onServiceConnected(BluetoothProfile.A2DP_SINK, mService);
} }
@Test
public void connect_shouldConnectBluetoothA2dpSink() {
mProfile.connect(mBluetoothDevice);
verify(mService).connect(mBluetoothDevice);
}
@Test
public void disconnect_shouldDisconnectBluetoothA2dpSink() {
mProfile.disconnect(mBluetoothDevice);
verify(mService).disconnect(mBluetoothDevice);
}
@Test @Test
public void getConnectionStatus_shouldReturnConnectionState() { public void getConnectionStatus_shouldReturnConnectionState() {
when(mService.getConnectionState(mBluetoothDevice)). when(mService.getConnectionState(mBluetoothDevice)).

View File

@@ -18,7 +18,6 @@ package com.android.settingslib.bluetooth;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
@@ -64,18 +63,6 @@ public class HfpClientProfileTest {
mServiceListener.onServiceConnected(BluetoothProfile.HEADSET_CLIENT, mService); mServiceListener.onServiceConnected(BluetoothProfile.HEADSET_CLIENT, mService);
} }
@Test
public void connect_shouldConnectBluetoothHeadsetClient() {
mProfile.connect(mBluetoothDevice);
verify(mService).connect(mBluetoothDevice);
}
@Test
public void disconnect_shouldDisconnectBluetoothHeadsetClient() {
mProfile.disconnect(mBluetoothDevice);
verify(mService).disconnect(mBluetoothDevice);
}
@Test @Test
public void getConnectionStatus_shouldReturnConnectionState() { public void getConnectionStatus_shouldReturnConnectionState() {
when(mService.getConnectionState(mBluetoothDevice)). when(mService.getConnectionState(mBluetoothDevice)).

View File

@@ -18,7 +18,6 @@ package com.android.settingslib.bluetooth;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
@@ -64,17 +63,6 @@ public class HidDeviceProfileTest {
mServiceListener.onServiceConnected(BluetoothProfile.HID_DEVICE, mService); mServiceListener.onServiceConnected(BluetoothProfile.HID_DEVICE, mService);
} }
@Test
public void connect_shouldReturnFalse() {
assertThat(mProfile.connect(mBluetoothDevice)).isFalse();
}
@Test
public void disconnect_shouldDisconnectBluetoothHidDevice() {
mProfile.disconnect(mBluetoothDevice);
verify(mService).disconnect(mBluetoothDevice);
}
@Test @Test
public void getConnectionStatus_shouldReturnConnectionState() { public void getConnectionStatus_shouldReturnConnectionState() {
when(mService.getConnectionState(mBluetoothDevice)). when(mService.getConnectionState(mBluetoothDevice)).

View File

@@ -18,7 +18,6 @@ package com.android.settingslib.bluetooth;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
@@ -64,18 +63,6 @@ public class MapClientProfileTest {
mServiceListener.onServiceConnected(BluetoothProfile.MAP_CLIENT, mService); mServiceListener.onServiceConnected(BluetoothProfile.MAP_CLIENT, mService);
} }
@Test
public void connect_shouldConnectBluetoothMapClient() {
mProfile.connect(mBluetoothDevice);
verify(mService).connect(mBluetoothDevice);
}
@Test
public void disconnect_shouldDisconnectBluetoothMapClient() {
mProfile.disconnect(mBluetoothDevice);
verify(mService).disconnect(mBluetoothDevice);
}
@Test @Test
public void getConnectionStatus_shouldReturnConnectionState() { public void getConnectionStatus_shouldReturnConnectionState() {
when(mService.getConnectionState(mBluetoothDevice)). when(mService.getConnectionState(mBluetoothDevice)).

View File

@@ -18,7 +18,6 @@ package com.android.settingslib.bluetooth;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
@@ -64,18 +63,6 @@ public class PbapClientProfileTest {
mServiceListener.onServiceConnected(BluetoothProfile.PBAP_CLIENT, mService); mServiceListener.onServiceConnected(BluetoothProfile.PBAP_CLIENT, mService);
} }
@Test
public void connect_shouldConnectBluetoothPbapClient() {
mProfile.connect(mBluetoothDevice);
verify(mService).connect(mBluetoothDevice);
}
@Test
public void disconnect_shouldDisconnectBluetoothPbapClient() {
mProfile.disconnect(mBluetoothDevice);
verify(mService).disconnect(mBluetoothDevice);
}
@Test @Test
public void getConnectionStatus_shouldReturnConnectionState() { public void getConnectionStatus_shouldReturnConnectionState() {
when(mService.getConnectionState(mBluetoothDevice)). when(mService.getConnectionState(mBluetoothDevice)).

View File

@@ -18,7 +18,6 @@ package com.android.settingslib.bluetooth;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
@@ -63,18 +62,6 @@ public class SapProfileTest {
mServiceListener.onServiceConnected(BluetoothProfile.SAP, mService); mServiceListener.onServiceConnected(BluetoothProfile.SAP, mService);
} }
@Test
public void connect_shouldConnectBluetoothSap() {
mProfile.connect(mBluetoothDevice);
verify(mService).connect(mBluetoothDevice);
}
@Test
public void disconnect_shouldDisconnectBluetoothSap() {
mProfile.disconnect(mBluetoothDevice);
verify(mService).disconnect(mBluetoothDevice);
}
@Test @Test
public void getConnectionStatus_shouldReturnConnectionState() { public void getConnectionStatus_shouldReturnConnectionState() {
when(mService.getConnectionState(mBluetoothDevice)). when(mService.getConnectionState(mBluetoothDevice)).

View File

@@ -96,7 +96,7 @@ public class BluetoothMediaManagerTest {
when(mA2dpProfile.getConnectableDevices()).thenReturn(devices); when(mA2dpProfile.getConnectableDevices()).thenReturn(devices);
when(mCachedDeviceManager.findDevice(bluetoothDevice)).thenReturn(cachedDevice); when(mCachedDeviceManager.findDevice(bluetoothDevice)).thenReturn(cachedDevice);
when(cachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); when(cachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mA2dpProfile.isPreferred(bluetoothDevice)).thenReturn(true); when(mA2dpProfile.isEnabled(bluetoothDevice)).thenReturn(true);
assertThat(mMediaManager.mMediaDevices).isEmpty(); assertThat(mMediaManager.mMediaDevices).isEmpty();
mMediaManager.startScan(); mMediaManager.startScan();
@@ -113,7 +113,7 @@ public class BluetoothMediaManagerTest {
when(mA2dpProfile.getConnectableDevices()).thenReturn(devices); when(mA2dpProfile.getConnectableDevices()).thenReturn(devices);
when(mCachedDeviceManager.findDevice(bluetoothDevice)).thenReturn(cachedDevice); when(mCachedDeviceManager.findDevice(bluetoothDevice)).thenReturn(cachedDevice);
when(cachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE); when(cachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE);
when(mA2dpProfile.isPreferred(bluetoothDevice)).thenReturn(true); when(mA2dpProfile.isEnabled(bluetoothDevice)).thenReturn(true);
assertThat(mMediaManager.mMediaDevices).isEmpty(); assertThat(mMediaManager.mMediaDevices).isEmpty();
mMediaManager.startScan(); mMediaManager.startScan();
@@ -141,7 +141,7 @@ public class BluetoothMediaManagerTest {
when(mHapProfile.getConnectableDevices()).thenReturn(devices); when(mHapProfile.getConnectableDevices()).thenReturn(devices);
when(mCachedDeviceManager.findDevice(bluetoothDevice)).thenReturn(cachedDevice); when(mCachedDeviceManager.findDevice(bluetoothDevice)).thenReturn(cachedDevice);
when(cachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); when(cachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mHapProfile.isPreferred(bluetoothDevice)).thenReturn(true); when(mHapProfile.isEnabled(bluetoothDevice)).thenReturn(true);
assertThat(mMediaManager.mMediaDevices).isEmpty(); assertThat(mMediaManager.mMediaDevices).isEmpty();
mMediaManager.startScan(); mMediaManager.startScan();