auto import from //branches/cupcake/...@127436

This commit is contained in:
The Android Open Source Project
2009-01-22 00:13:44 -08:00
parent 01f172366c
commit 590c0a97ff
34 changed files with 894 additions and 101 deletions

View File

@@ -21,13 +21,12 @@ import com.android.settings.bluetooth.LocalBluetoothManager.ExtendedBluetoothSta
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothError;
import android.bluetooth.BluetoothIntent;
import android.bluetooth.IBluetoothDeviceCallback;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.util.Log;
/**
@@ -40,29 +39,6 @@ public class BluetoothEventRedirector {
private static final boolean V = LocalBluetoothManager.V;
private LocalBluetoothManager mManager;
private Handler mUiHandler = new Handler();
private IBluetoothDeviceCallback mBtDevCallback = new IBluetoothDeviceCallback.Stub() {
public void onCreateBondingResult(final String address, final int result) {
if (V) {
Log.v(TAG, "onCreateBondingResult(" + address + ", " + result + ")");
}
mUiHandler.post(new Runnable() {
public void run() {
boolean wasSuccess = result == BluetoothDevice.RESULT_SUCCESS;
LocalBluetoothDeviceManager deviceManager = mManager.getLocalDeviceManager();
deviceManager.onBondingStateChanged(address, wasSuccess);
if (!wasSuccess) {
deviceManager.onBondingError(address);
}
}
});
}
public void onEnableResult(int result) { }
public void onGetRemoteServiceChannelResult(String address, int channel) { }
};
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
@@ -86,17 +62,21 @@ public class BluetoothEventRedirector {
} else if (action.equals(BluetoothIntent.REMOTE_DEVICE_FOUND_ACTION)) {
short rssi = intent.getShortExtra(BluetoothIntent.RSSI, Short.MIN_VALUE);
mManager.getLocalDeviceManager().onDeviceAppeared(address, rssi);
mManager.getLocalDeviceManager().onDeviceAppeared(address, rssi);
} else if (action.equals(BluetoothIntent.REMOTE_DEVICE_DISAPPEARED_ACTION)) {
mManager.getLocalDeviceManager().onDeviceDisappeared(address);
} else if (action.equals(BluetoothIntent.REMOTE_NAME_UPDATED_ACTION)) {
mManager.getLocalDeviceManager().onDeviceNameUpdated(address);
} else if (action.equals(BluetoothIntent.BONDING_CREATED_ACTION)) {
mManager.getLocalDeviceManager().onBondingStateChanged(address, true);
} else if (action.equals(BluetoothIntent.BONDING_REMOVED_ACTION)) {
mManager.getLocalDeviceManager().onBondingStateChanged(address, false);
} else if (action.equals(BluetoothIntent.BOND_STATE_CHANGED_ACTION)) {
int bondState = intent.getIntExtra(BluetoothIntent.BOND_STATE,
BluetoothError.ERROR);
mManager.getLocalDeviceManager().onBondingStateChanged(address, bondState);
if (bondState == BluetoothDevice.BOND_NOT_BONDED) {
int reason = intent.getIntExtra(BluetoothIntent.REASON, BluetoothError.ERROR);
Log.w(TAG, address + " unbonded with reason " + reason +
", TODO: handle this nicely in the UI"); //TODO
mManager.getLocalDeviceManager().onBondingError(address);
}
} else if (action.equals(BluetoothIntent.HEADSET_STATE_CHANGED_ACTION)) {
mManager.getLocalDeviceManager().onProfileStateChanged(address);
@@ -139,8 +119,7 @@ public class BluetoothEventRedirector {
filter.addAction(BluetoothIntent.REMOTE_NAME_UPDATED_ACTION);
// Pairing broadcasts
filter.addAction(BluetoothIntent.BONDING_CREATED_ACTION);
filter.addAction(BluetoothIntent.BONDING_REMOVED_ACTION);
filter.addAction(BluetoothIntent.BOND_STATE_CHANGED_ACTION);
// Fine-grained state broadcasts
filter.addAction(BluetoothA2dp.SINK_STATE_CHANGED_ACTION);
@@ -152,8 +131,4 @@ public class BluetoothEventRedirector {
public void stop() {
mManager.getContext().unregisterReceiver(mBroadcastReceiver);
}
public IBluetoothDeviceCallback getBluetoothDeviceCallback() {
return mBtDevCallback;
}
}

View File

@@ -60,7 +60,7 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
private boolean mVisible;
private int mPairingStatus;
private int mBondState;
private final LocalBluetoothManager mLocalManager;
@@ -85,13 +85,13 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
}
public void onClicked() {
int pairingStatus = getPairingStatus();
int bondState = getBondState();
if (isConnected()) {
askDisconnect();
} else if (pairingStatus == SettingsBtStatus.PAIRING_STATUS_PAIRED) {
} else if (bondState == BluetoothDevice.BOND_BONDED) {
connect();
} else if (pairingStatus == SettingsBtStatus.PAIRING_STATUS_UNPAIRED) {
} else if (bondState == BluetoothDevice.BOND_NOT_BONDED) {
pair();
}
}
@@ -195,55 +195,54 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
}
private boolean ensurePaired() {
if (getPairingStatus() == SettingsBtStatus.PAIRING_STATUS_UNPAIRED) {
if (getBondState() == BluetoothDevice.BOND_NOT_BONDED) {
pair();
return false;
} else {
return true;
}
}
public void pair() {
BluetoothDevice manager = mLocalManager.getBluetoothManager();
// Pairing doesn't work if scanning, so cancel
// Pairing is unreliable while scanning, so cancel discovery
if (manager.isDiscovering()) {
manager.cancelDiscovery();
}
if (mLocalManager.createBonding(mAddress)) {
setPairingStatus(SettingsBtStatus.PAIRING_STATUS_PAIRING);
//TODO: consider removing this line - UI will update through Intent
setBondState(BluetoothDevice.BOND_BONDING);
}
}
public void unpair() {
BluetoothDevice manager = mLocalManager.getBluetoothManager();
switch (getPairingStatus()) {
case SettingsBtStatus.PAIRING_STATUS_PAIRED:
manager.removeBonding(mAddress);
break;
case SettingsBtStatus.PAIRING_STATUS_PAIRING:
manager.cancelBondingProcess(mAddress);
break;
switch (getBondState()) {
case BluetoothDevice.BOND_BONDED:
manager.removeBond(mAddress);
break;
case BluetoothDevice.BOND_BONDING:
manager.cancelBondProcess(mAddress);
break;
}
}
private void fillData() {
BluetoothDevice manager = mLocalManager.getBluetoothManager();
fetchName();
fetchName();
mBtClass = manager.getRemoteClass(mAddress);
LocalBluetoothProfileManager.fill(mBtClass, mProfiles);
mPairingStatus = manager.hasBonding(mAddress)
? SettingsBtStatus.PAIRING_STATUS_PAIRED
: SettingsBtStatus.PAIRING_STATUS_UNPAIRED;
mBondState = manager.getBondState(mAddress);
mVisible = false;
dispatchAttributesChanged();
}
@@ -283,17 +282,17 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
}
}
public int getPairingStatus() {
return mPairingStatus;
public int getBondState() {
return mBondState;
}
void setPairingStatus(int pairingStatus) {
if (mPairingStatus != pairingStatus) {
mPairingStatus = pairingStatus;
void setBondState(int bondState) {
if (mBondState != bondState) {
mBondState = bondState;
dispatchAttributesChanged();
}
}
void setRssi(short rssi) {
if (mRssi != rssi) {
mRssi = rssi;
@@ -327,7 +326,7 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
}
}
if (getPairingStatus() == SettingsBtStatus.PAIRING_STATUS_PAIRING) {
if (getBondState() == BluetoothDevice.BOND_BONDING) {
return true;
}
@@ -374,9 +373,8 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
return SettingsBtStatus.getConnectionStatusSummary(connectionStatus);
}
}
int pairingStatus = getPairingStatus();
return SettingsBtStatus.getPairingStatusSummary(pairingStatus);
return SettingsBtStatus.getPairingStatusSummary(getBondState());
}
/**
@@ -430,7 +428,7 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
// No context menu if there are no profiles
if (mProfiles.size() == 0) return;
int pairingStatus = getPairingStatus();
int bondState = getBondState();
boolean isConnected = isConnected();
menu.setHeaderTitle(getName());
@@ -439,13 +437,13 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
menu.add(0, CONTEXT_ITEM_DISCONNECT, 0, R.string.bluetooth_device_context_disconnect);
} else {
// For connection action, show either "Connect" or "Pair & connect"
int connectString = pairingStatus == SettingsBtStatus.PAIRING_STATUS_UNPAIRED
int connectString = (bondState == BluetoothDevice.BOND_NOT_BONDED)
? R.string.bluetooth_device_context_pair_connect
: R.string.bluetooth_device_context_connect;
menu.add(0, CONTEXT_ITEM_CONNECT, 0, connectString);
}
if (pairingStatus == SettingsBtStatus.PAIRING_STATUS_PAIRED) {
if (bondState == BluetoothDevice.BOND_BONDED) {
// For unpair action, show either "Unpair" or "Disconnect & unpair"
int unpairString = isConnected
? R.string.bluetooth_device_context_disconnect_unpair
@@ -540,8 +538,8 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
if (comparison != 0) return comparison;
// Paired above not paired
comparison = (another.mPairingStatus == SettingsBtStatus.PAIRING_STATUS_PAIRED ? 1 : 0) -
(mPairingStatus == SettingsBtStatus.PAIRING_STATUS_PAIRED ? 1 : 0);
comparison = (another.mBondState == BluetoothDevice.BOND_BONDED ? 1 : 0) -
(mBondState == BluetoothDevice.BOND_BONDED ? 1 : 0);
if (comparison != 0) return comparison;
// Visible above not visible

View File

@@ -47,7 +47,7 @@ public class LocalBluetoothDeviceManager {
private synchronized void readPairedDevices() {
BluetoothDevice manager = mLocalManager.getBluetoothManager();
String[] bondedAddresses = manager.listBondings();
String[] bondedAddresses = manager.listBonds();
if (bondedAddresses == null) return;
for (String address : bondedAddresses) {
@@ -97,7 +97,7 @@ public class LocalBluetoothDeviceManager {
}
private void checkForDeviceRemoval(LocalBluetoothDevice device) {
if (device.getPairingStatus() == SettingsBtStatus.PAIRING_STATUS_UNPAIRED &&
if (device.getBondState() == BluetoothDevice.BOND_NOT_BONDED &&
!device.isVisible()) {
// If device isn't paired, remove it altogether
mDevices.remove(device);
@@ -154,19 +154,18 @@ public class LocalBluetoothDeviceManager {
}
}
public synchronized void onBondingStateChanged(String address, boolean created) {
public synchronized void onBondingStateChanged(String address, int bondState) {
LocalBluetoothDevice device = findDevice(address);
if (device == null) {
Log.e(TAG, "Got bonding state changed for " + address +
", but we have no record of that device.");
return;
}
device.setPairingStatus(created ? SettingsBtStatus.PAIRING_STATUS_PAIRED
: SettingsBtStatus.PAIRING_STATUS_UNPAIRED);
device.setBondState(bondState); //TODO: might be unecessary
checkForDeviceRemoval(device);
if (created) {
if (bondState == BluetoothDevice.BOND_BONDED) {
// Auto-connect after pairing
device.connect();
}

View File

@@ -227,7 +227,7 @@ public class LocalBluetoothManager {
}
public boolean createBonding(String address) {
return mManager.createBonding(address, mEventRedirector.getBluetoothDeviceCallback());
return mManager.createBond(address);
}
public void showError(String address, int titleResId, int messageResId) {

View File

@@ -16,6 +16,8 @@
package com.android.settings.bluetooth;
import android.bluetooth.BluetoothDevice;
import com.android.settings.R;
/**
@@ -64,19 +66,13 @@ public class SettingsBtStatus {
|| connectionStatus == CONNECTION_STATUS_DISCONNECTING;
}
// Pairing status
public static final int PAIRING_STATUS_UNPAIRED = 0;
public static final int PAIRING_STATUS_PAIRED = 1;
public static final int PAIRING_STATUS_PAIRING = 2;
public static final int getPairingStatusSummary(int pairingStatus) {
switch (pairingStatus) {
case PAIRING_STATUS_PAIRED:
public static final int getPairingStatusSummary(int bondState) {
switch (bondState) {
case BluetoothDevice.BOND_BONDED:
return R.string.bluetooth_paired;
case PAIRING_STATUS_PAIRING:
case BluetoothDevice.BOND_BONDING:
return R.string.bluetooth_pairing;
case PAIRING_STATUS_UNPAIRED:
case BluetoothDevice.BOND_NOT_BONDED:
return R.string.bluetooth_not_connected;
default:
return 0;