Implement individual Bluetooth profile connect/disconnect.

Allow individual Bluetooth profiles to be connected/disconnected from
the device's settings pane.

Bug: 3137982
Change-Id: I21512c5bf965fc523f3dc1e83d029b16b5e22440
This commit is contained in:
Jake Hamby
2010-11-03 12:47:51 -07:00
parent b14e1e0493
commit e79f990490
9 changed files with 96 additions and 41 deletions

View File

@@ -837,6 +837,17 @@
<!-- Bluetooth settings. The summary string when a device is connected to the PAN profile. [CHAR LIMIT=35]-->
<string name="bluetooth_summary_connected_to_pan">Tethered</string>
<!-- Bluetooth settings. Message for disconnecting from the A2DP profile. -->
<string name="bluetooth_disconnect_a2dp_profile"><xliff:g id="device_name">%1$s</xliff:g> will be disconnected from media audio.</string>
<!-- Bluetooth settings. Message for disconnecting from the headset profile. -->
<string name="bluetooth_disconnect_headset_profile" product="tablet"><xliff:g id="device_name">%1$s</xliff:g> will be disconnected from tablet audio.</string>
<!-- Bluetooth settings. Message for disconnecting from the headset profile. -->
<string name="bluetooth_disconnect_headset_profile" product="default"><xliff:g id="device_name">%1$s</xliff:g> will be disconnected from phone audio.</string>
<!-- Bluetooth settings. Message for disconnecting from the HID profile. -->
<string name="bluetooth_disconnect_hid_profile"><xliff:g id="device_name">%1$s</xliff:g> will be disconnected from input device.</string>
<!-- Bluetooth settings. Message for disconnecting from the PAN profile. -->
<string name="bluetooth_disconnect_pan_profile"><xliff:g id="device_name">%1$s</xliff:g> will be disconnected from tethering.</string>
<!-- Bluetooth settings. Connection options screen. The title of the screen. -->
<string name="bluetooth_device_advanced_title"><xliff:g id="device_name">%1$s</xliff:g> options</string>
<!-- Bluetooth settings. Connection options screen. Title of device actions section. [CHAR LIMIT=30] -->

View File

@@ -155,7 +155,6 @@ public class SettingsPreferenceFragment extends PreferenceFragment
mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
}
@Override
public Dialog onCreateDialog(int dialogId) {
return null;
}

View File

@@ -46,8 +46,8 @@ public class BluetoothPairingDialog extends AlertActivity implements DialogInter
TextWatcher {
private static final String TAG = "BluetoothPairingDialog";
private final int BLUETOOTH_PIN_MAX_LENGTH = 16;
private final int BLUETOOTH_PASSKEY_MAX_LENGTH = 6;
private static final int BLUETOOTH_PIN_MAX_LENGTH = 16;
private static final int BLUETOOTH_PASSKEY_MAX_LENGTH = 6;
private LocalBluetoothManager mLocalManager;
private BluetoothDevice mDevice;
private int mType;

View File

@@ -251,7 +251,6 @@ public class BluetoothSettings extends SettingsPreferenceFragment
}
}
@Override
public void onUserLeaveHint() {
mLocalManager.stopScanning();
}

View File

@@ -104,9 +104,9 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
* @param profile Profile to describe
* @return Description of the device and profile
*/
private String describe(CachedBluetoothDevice cachedDevice, Profile profile) {
private String describe(Profile profile) {
StringBuilder sb = new StringBuilder();
sb.append("Address:").append(cachedDevice.mDevice);
sb.append("Address:").append(mDevice);
if (profile != null) {
sb.append(" Profile:").append(profile.name());
}
@@ -114,10 +114,6 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
return sb.toString();
}
private String describe(Profile profile) {
return describe(this, profile);
}
public void onProfileStateChanged(Profile profile, int newProfileState) {
if (D) {
Log.d(TAG, "onProfileStateChanged: profile " + profile.toString() +
@@ -166,20 +162,13 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
}
public void disconnect(Profile profile) {
disconnectInt(this, profile);
}
private boolean disconnectInt(CachedBluetoothDevice cachedDevice, Profile profile) {
LocalBluetoothProfileManager profileManager =
LocalBluetoothProfileManager.getProfileManager(mLocalManager, profile);
int status = profileManager.getConnectionStatus(cachedDevice.mDevice);
if (profileManager.disconnect(cachedDevice.mDevice)) {
if (profileManager.disconnect(mDevice)) {
if (D) {
Log.d(TAG, "Command sent successfully:DISCONNECT " + describe(profile));
}
return true;
}
return false;
}
public void askDisconnect() {
@@ -204,6 +193,57 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
}
};
showDisconnectDialog(context, disconnectListener, message);
}
public void askDisconnect(final Profile profile) {
Context context = mLocalManager.getForegroundActivity();
if (context == null) {
// Cannot ask, since we need an activity context
disconnect(profile);
return;
}
Resources res = context.getResources();
String name = getName();
if (TextUtils.isEmpty(name)) {
name = res.getString(R.string.bluetooth_device);
}
int disconnectMessage;
switch (profile) {
case A2DP:
disconnectMessage = R.string.bluetooth_disconnect_a2dp_profile;
break;
case HEADSET:
disconnectMessage = R.string.bluetooth_disconnect_headset_profile;
break;
case HID:
disconnectMessage = R.string.bluetooth_disconnect_hid_profile;
break;
case PAN:
disconnectMessage = R.string.bluetooth_disconnect_pan_profile;
break;
default:
Log.w(TAG, "askDisconnect: unexpected profile " + profile);
disconnectMessage = R.string.bluetooth_disconnect_blank;
break;
}
String message = res.getString(disconnectMessage, name);
DialogInterface.OnClickListener disconnectListener =
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
disconnect(profile);
}
};
showDisconnectDialog(context, disconnectListener, message);
}
private void showDisconnectDialog(Context context,
DialogInterface.OnClickListener disconnectListener,
String message) {
if (mDialog == null) {
mDialog = new AlertDialog.Builder(context)
.setPositiveButton(android.R.string.ok, disconnectListener)
@@ -213,6 +253,10 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
if (mDialog.isShowing()) {
mDialog.dismiss();
}
// use disconnectListener for the correct profile(s)
CharSequence okText = context.getText(android.R.string.ok);
mDialog.setButton(DialogInterface.BUTTON_POSITIVE,
okText, disconnectListener);
}
mDialog.setTitle(getName());
mDialog.setMessage(message);
@@ -311,7 +355,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
CachedBluetoothDevice cachedDevice = cachedDeviceManager.findDevice(btDevice);
if (cachedDevice != null && !cachedDevice.equals(device)) {
disconnectInt(cachedDevice, profile);
cachedDevice.disconnect(profile);
}
}
}
@@ -321,7 +365,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
LocalBluetoothProfileManager profileManager =
LocalBluetoothProfileManager.getProfileManager(mLocalManager, profile);
int status = profileManager.getConnectionStatus(cachedDevice.mDevice);
if (profileManager.connect(cachedDevice.mDevice)) {
if (D) {
Log.d(TAG, "Command sent successfully:CONNECT " + describe(profile));

View File

@@ -21,7 +21,6 @@ import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
@@ -35,8 +34,8 @@ import android.view.View;
import java.util.HashMap;
/**
* ConnectSpecificProfilesActivity presents the user with all of the profiles
* for a particular device, and allows him to choose which should be connected
* This preference fragment presents the user with all of the profiles
* for a particular device, and allows them to be individually connected
* (or disconnected).
*/
public class DeviceProfilesSettings extends SettingsPreferenceFragment
@@ -155,9 +154,6 @@ public class DeviceProfilesSettings extends SettingsPreferenceFragment
pref.setOrder(getProfilePreferenceIndex(profile));
pref.setOnExpandClickListener(this);
LocalBluetoothProfileManager profileManager = LocalBluetoothProfileManager
.getProfileManager(mManager, profile);
/**
* Gray out profile while connecting and disconnecting
*/
@@ -172,7 +168,7 @@ public class DeviceProfilesSettings extends SettingsPreferenceFragment
public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
String key = preference.getKey();
if (preference instanceof BluetoothProfilePreference) {
onProfileClicked(preference, Profile.valueOf(key));
onProfileClicked(Profile.valueOf(key));
return true;
} else if (key.equals(KEY_UNPAIR)) {
unpairDevice();
@@ -196,18 +192,24 @@ public class DeviceProfilesSettings extends SettingsPreferenceFragment
return true;
}
private void onProfileClicked(Preference preference, Profile profile) {
private void onProfileClicked(Profile profile) {
BluetoothDevice device = mCachedDevice.getDevice();
LocalBluetoothProfileManager profileManager = LocalBluetoothProfileManager
.getProfileManager(mManager, profile);
// TODO: Get the current state and flip it, updating the summary for the preference
// profileManager.setPreferred(mCachedDevice.getDevice(), checked);
//
// if (checked) {
// mCachedDevice.connect(profile);
// } else {
// mCachedDevice.disconnect(profile);
// }
int status = profileManager.getConnectionStatus(device);
boolean isConnected =
SettingsBtStatus.isConnectionStatusConnected(status);
// TODO: only change the preference on disconnect if user confirms
// TODO: add method to change priority of individual profiles
// profileManager.setPreferred(device, !isConnected);
if (isConnected) {
mCachedDevice.askDisconnect(profile);
} else {
mCachedDevice.connect(profile);
}
}
public void onDeviceAttributesChanged(CachedBluetoothDevice cachedDevice) {

View File

@@ -496,7 +496,7 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
private CharSequence[] initBtSettings(DockService service, BluetoothDevice device, int state,
boolean firstTime) {
// TODO Avoid hardcoding dock and profiles. Read from system properties
int numOfProfiles = 0;
int numOfProfiles;
switch (state) {
case Intent.EXTRA_DOCK_STATE_DESK:
numOfProfiles = 1;
@@ -729,7 +729,7 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
profileManager.setPreferred(device, mCheckedItems[i]);
if (DEBUG) {
if (mCheckedItems[i] != profileManager.isPreferred(device)) {
Log.e(TAG, "Can't save prefered value");
Log.e(TAG, "Can't save preferred value");
}
}
}

View File

@@ -70,7 +70,7 @@ public class LocalBluetoothManager {
// If a device was picked from the device picker or was in discoverable mode
// in the last 60 seconds, show the pairing dialogs in foreground instead
// of raising notifications
private static long GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND = 60 * 1000;
private static final int GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND = 60 * 1000;
public static final String SHARED_PREFERENCES_KEY_DISCOVERING_TIMESTAMP =
"last_discovering_time";

View File

@@ -225,7 +225,7 @@ public abstract class LocalBluetoothProfileManager {
}
// TODO: int instead of enum
public enum Profile {
public static enum Profile {
HEADSET(R.string.bluetooth_profile_headset),
A2DP(R.string.bluetooth_profile_a2dp),
OPP(R.string.bluetooth_profile_opp),
@@ -433,7 +433,7 @@ public abstract class LocalBluetoothProfileManager {
public boolean disconnect(BluetoothDevice device) {
List<BluetoothDevice> deviceList = getConnectedDevices();
if (deviceList.size() != 0 && deviceList.get(0).equals(device)) {
// Downgrade prority as user is disconnecting the headset.
// Downgrade priority as user is disconnecting the headset.
if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) {
mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
}