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

This commit is contained in:
The Android Open Source Project
2009-02-10 15:44:05 -08:00
parent 590c0a97ff
commit 1feaa85791
122 changed files with 5734 additions and 4925 deletions

View File

@@ -19,6 +19,7 @@ package com.android.settings.bluetooth;
import com.android.settings.R;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothError;
import android.bluetooth.BluetoothIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -56,14 +57,12 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
/*
* TODO: remove this once the BT framework broadcasts the
* MODE_CHANGED action when going into MODE_OFF.
*/
int mode = BluetoothIntent.DISABLED_ACTION.equals(intent.getAction())
? BluetoothDevice.MODE_OFF
: intent.getIntExtra(BluetoothIntent.MODE, BluetoothDevice.MODE_UNKNOWN);
handleModeChanged(mode);
if (BluetoothIntent.SCAN_MODE_CHANGED_ACTION.equals(intent.getAction())) {
int mode = intent.getIntExtra(BluetoothIntent.SCAN_MODE, BluetoothError.ERROR);
if (mode != BluetoothError.ERROR) {
handleModeChanged(mode);
}
}
}
};
@@ -92,12 +91,12 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
return;
}
IntentFilter filter = new IntentFilter(BluetoothIntent.MODE_CHANGED_ACTION);
IntentFilter filter = new IntentFilter(BluetoothIntent.SCAN_MODE_CHANGED_ACTION);
filter.addAction(BluetoothIntent.DISABLED_ACTION);
mContext.registerReceiver(mReceiver, filter);
mCheckBoxPreference.setOnPreferenceChangeListener(this);
handleModeChanged(mLocalManager.getBluetoothManager().getMode());
handleModeChanged(mLocalManager.getBluetoothManager().getScanMode());
}
public void pause() {
@@ -132,11 +131,9 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
long endTimestamp = System.currentTimeMillis() + timeout * 1000;
persistDiscoverableEndTimestamp(endTimestamp);
manager.setMode(BluetoothDevice.MODE_DISCOVERABLE);
handleModeChanged(BluetoothDevice.MODE_DISCOVERABLE);
manager.setScanMode(BluetoothDevice.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
} else {
manager.setMode(BluetoothDevice.MODE_CONNECTABLE);
manager.setScanMode(BluetoothDevice.SCAN_MODE_CONNECTABLE);
}
}
@@ -160,7 +157,7 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
Log.v(TAG, "Got mode changed: " + mode);
}
if (mode == BluetoothDevice.MODE_DISCOVERABLE) {
if (mode == BluetoothDevice.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
mCheckBoxPreference.setChecked(true);
updateCountdownSummary();
@@ -170,8 +167,8 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
}
private void updateCountdownSummary() {
int mode = mLocalManager.getBluetoothManager().getMode();
if (mode != BluetoothDevice.MODE_DISCOVERABLE) {
int mode = mLocalManager.getBluetoothManager().getScanMode();
if (mode != BluetoothDevice.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
return;
}

View File

@@ -52,31 +52,39 @@ public class BluetoothEventRedirector {
if (action.equals(BluetoothIntent.ENABLED_ACTION)) {
mManager.setBluetoothStateInt(ExtendedBluetoothState.ENABLED);
} else if (action.equals(BluetoothIntent.DISABLED_ACTION)) {
mManager.setBluetoothStateInt(ExtendedBluetoothState.DISABLED);
} else if (action.equals(BluetoothIntent.DISCOVERY_STARTED_ACTION)) {
mManager.onScanningStateChanged(true);
} else if (action.equals(BluetoothIntent.DISCOVERY_COMPLETED_ACTION)) {
mManager.onScanningStateChanged(false);
} else if (action.equals(BluetoothIntent.REMOTE_DEVICE_FOUND_ACTION)) {
short rssi = intent.getShortExtra(BluetoothIntent.RSSI, Short.MIN_VALUE);
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.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);
if (reason == BluetoothDevice.UNBOND_REASON_AUTH_FAILED ||
reason == BluetoothDevice.UNBOND_REASON_AUTH_REJECTED ||
reason == BluetoothDevice.UNBOND_REASON_REMOTE_DEVICE_DOWN) {
mManager.getLocalDeviceManager().onBondingError(address, reason);
}
}
} else if (action.equals(BluetoothIntent.HEADSET_STATE_CHANGED_ACTION)) {
mManager.getLocalDeviceManager().onProfileStateChanged(address);
@@ -96,6 +104,10 @@ public class BluetoothEventRedirector {
oldState == BluetoothA2dp.STATE_CONNECTING) {
mManager.getLocalDeviceManager().onConnectingError(address);
}
} else if (action.equals(BluetoothIntent.REMOTE_DEVICE_CLASS_UPDATED_ACTION)) {
mManager.getLocalDeviceManager().onBtClassChanged(address);
}
}
};
@@ -124,6 +136,7 @@ public class BluetoothEventRedirector {
// Fine-grained state broadcasts
filter.addAction(BluetoothA2dp.SINK_STATE_CHANGED_ACTION);
filter.addAction(BluetoothIntent.HEADSET_STATE_CHANGED_ACTION);
filter.addAction(BluetoothIntent.REMOTE_DEVICE_CLASS_UPDATED_ACTION);
mManager.getContext().registerReceiver(mBroadcastReceiver, filter);
}

View File

@@ -18,13 +18,19 @@ package com.android.settings.bluetooth;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.method.DigitsKeyListener;
import android.text.TextWatcher;
import android.text.InputFilter.LengthFilter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
@@ -36,12 +42,31 @@ import com.android.settings.R;
* BluetoothPinDialog asks the user to enter a PIN for pairing with a remote
* Bluetooth device. It is an activity that appears as a dialog.
*/
public class BluetoothPinDialog extends AlertActivity implements DialogInterface.OnClickListener {
public class BluetoothPinDialog extends AlertActivity implements DialogInterface.OnClickListener,
TextWatcher {
private static final String TAG = "BluetoothPinDialog";
private LocalBluetoothManager mLocalManager;
private String mAddress;
private EditText mPinView;
private Button mOkButton;
private static final String INSTANCE_KEY_PAIRING_CANCELED = "received_pairing_canceled";
private boolean mReceivedPairingCanceled;
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!BluetoothIntent.PAIRING_CANCEL_ACTION.equals(intent.getAction())) {
return;
}
String address = intent.getStringExtra(BluetoothIntent.ADDRESS);
if (address == null || address.equals(mAddress)) {
onReceivedPairingCanceled();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -69,6 +94,39 @@ public class BluetoothPinDialog extends AlertActivity implements DialogInterface
p.mNegativeButtonText = getString(android.R.string.cancel);
p.mNegativeButtonListener = this;
setupAlert();
mOkButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE);
mOkButton.setEnabled(false);
/*
* Leave this registered through pause/resume since we still want to
* finish the activity in the background if pairing is canceled.
*/
registerReceiver(mReceiver, new IntentFilter(BluetoothIntent.PAIRING_CANCEL_ACTION));
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mReceivedPairingCanceled = savedInstanceState.getBoolean(INSTANCE_KEY_PAIRING_CANCELED);
if (mReceivedPairingCanceled) {
onReceivedPairingCanceled();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(INSTANCE_KEY_PAIRING_CANCELED, mReceivedPairingCanceled);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
private View createView() {
@@ -79,9 +137,32 @@ public class BluetoothPinDialog extends AlertActivity implements DialogInterface
messageView.setText(getString(R.string.bluetooth_enter_pin_msg, name));
mPinView = (EditText) view.findViewById(R.id.text);
mPinView.addTextChangedListener(this);
// Maximum of 10 characters in a PIN
mPinView.setFilters(new InputFilter[] { new LengthFilter(10) });
return view;
}
public void afterTextChanged(Editable s) {
if (s.length() > 0) {
mOkButton.setEnabled(true);
}
}
private void onReceivedPairingCanceled() {
mReceivedPairingCanceled = true;
TextView messageView = (TextView) findViewById(R.id.message);
messageView.setText(getString(R.string.bluetooth_pairing_error_message,
mLocalManager.getLocalDeviceManager().getName(mAddress)));
mPinView.setEnabled(false);
mPinView.clearFocus();
mPinView.removeTextChangedListener(this);
mAlert.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
}
private void onPair(String pin) {
byte[] pinBytes = BluetoothDevice.convertPinToBytes(pin);
@@ -94,7 +175,7 @@ public class BluetoothPinDialog extends AlertActivity implements DialogInterface
}
private void onCancel() {
mLocalManager.getBluetoothManager().cancelPin(mAddress);
mLocalManager.getBluetoothManager().cancelBondProcess(mAddress);
}
public void onClick(DialogInterface dialog, int which) {
@@ -109,4 +190,12 @@ public class BluetoothPinDialog extends AlertActivity implements DialogInterface
}
}
/* Not used */
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
/* Not used */
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}

View File

@@ -25,10 +25,7 @@ import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.util.Log;
/**
@@ -114,7 +111,7 @@ public class ConnectSpecificProfilesActivity extends PreferenceActivity
mManager.setForegroundActivity(this);
mDevice.registerCallback(this);
refresh(true);
refresh();
}
@Override
@@ -169,7 +166,7 @@ public class ConnectSpecificProfilesActivity extends PreferenceActivity
}
private void onOnlineModeCheckedStateChanged(boolean checked) {
switchModes(checked, false);
setOnlineMode(checked, true);
}
private void onProfileCheckedStateChanged(Profile profile, boolean checked) {
@@ -187,39 +184,34 @@ public class ConnectSpecificProfilesActivity extends PreferenceActivity
}
public void onDeviceAttributesChanged(LocalBluetoothDevice device) {
refresh(false);
refresh();
}
private void refresh(boolean forceRefresh) {
// The online mode could have changed
updateOnlineMode(forceRefresh);
private void refresh() {
// We are in 'online mode' if we are connected, connecting, or disconnecting
setOnlineMode(mDevice.isConnected() || mDevice.isBusy(), false);
refreshProfiles();
refreshOnlineModePreference();
}
private void updateOnlineMode(boolean force) {
// Connected or Connecting (and Disconnecting, which is fine)
boolean onlineMode = mDevice.isConnected() || mDevice.isBusy();
switchModes(onlineMode, force);
}
/**
* Switches between online/offline mode.
*
* @param onlineMode Whether to be in online mode, or offline mode.
* @param takeAction Whether to take action (i.e., connect or disconnect)
* based on the new online mode.
*/
private void switchModes(boolean onlineMode, boolean force) {
if (mOnlineMode != onlineMode || force) {
mOnlineMode = onlineMode;
private void setOnlineMode(boolean onlineMode, boolean takeAction) {
mOnlineMode = onlineMode;
if (takeAction) {
if (onlineMode) {
mDevice.connect();
} else {
mDevice.disconnect();
}
refreshOnlineModePreference();
}
refreshOnlineModePreference();
}
private void refreshOnlineModePreference() {

View File

@@ -60,8 +60,6 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
private boolean mVisible;
private int mBondState;
private final LocalBluetoothManager mLocalManager;
private List<Callback> mCallbacks = new ArrayList<Callback>();
@@ -144,6 +142,9 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
public void connect() {
if (!ensurePaired()) return;
// Reset the only-show-one-error-dialog tracking variable
mIsConnectingErrorPossible = true;
Context context = mLocalManager.getContext();
boolean hasAtLeastOnePreferredProfile = false;
for (Profile profile : mProfiles) {
@@ -151,7 +152,7 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
LocalBluetoothProfileManager.getProfileManager(mLocalManager, profile);
if (profileManager.isPreferred(mAddress)) {
hasAtLeastOnePreferredProfile = true;
connect(profile);
connectInt(profile);
}
}
@@ -163,23 +164,31 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
private void connectAndPreferAllProfiles() {
if (!ensurePaired()) return;
// Reset the only-show-one-error-dialog tracking variable
mIsConnectingErrorPossible = true;
Context context = mLocalManager.getContext();
for (Profile profile : mProfiles) {
LocalBluetoothProfileManager profileManager =
LocalBluetoothProfileManager.getProfileManager(mLocalManager, profile);
profileManager.setPreferred(mAddress, true);
connect(profile);
connectInt(profile);
}
}
public void connect(Profile profile) {
// Reset the only-show-one-error-dialog tracking variable
mIsConnectingErrorPossible = true;
connectInt(profile);
}
public void connectInt(Profile profile) {
if (!ensurePaired()) return;
LocalBluetoothProfileManager profileManager =
LocalBluetoothProfileManager.getProfileManager(mLocalManager, profile);
int status = profileManager.getConnectionStatus(mAddress);
if (!SettingsBtStatus.isConnectionStatusConnected(status)) {
mIsConnectingErrorPossible = true;
if (profileManager.connect(mAddress) != BluetoothDevice.RESULT_SUCCESS) {
showConnectingError();
}
@@ -211,9 +220,9 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
manager.cancelDiscovery();
}
if (mLocalManager.createBonding(mAddress)) {
//TODO: consider removing this line - UI will update through Intent
setBondState(BluetoothDevice.BOND_BONDING);
if (!mLocalManager.getBluetoothManager().createBond(mAddress)) {
mLocalManager.showError(mAddress, R.string.bluetooth_error_title,
R.string.bluetooth_pairing_error_message);
}
}
@@ -235,11 +244,7 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
BluetoothDevice manager = mLocalManager.getBluetoothManager();
fetchName();
mBtClass = manager.getRemoteClass(mAddress);
LocalBluetoothProfileManager.fill(mBtClass, mProfiles);
mBondState = manager.getBondState(mAddress);
fetchBtClass();
mVisible = false;
@@ -283,14 +288,7 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
}
public int getBondState() {
return mBondState;
}
void setBondState(int bondState) {
if (mBondState != bondState) {
mBondState = bondState;
dispatchAttributesChanged();
}
return mLocalManager.getBluetoothManager().getBondState(mAddress);
}
void setRssi(short rssi) {
@@ -355,6 +353,24 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
}
}
/**
* Fetches a new value for the cached BT class.
*/
private void fetchBtClass() {
mBtClass = mLocalManager.getBluetoothManager().getRemoteClass(mAddress);
mProfiles.clear();
LocalBluetoothProfileManager.fill(mBtClass, mProfiles);
}
/**
* Refreshes the UI for the BT class, including fetching the latest value
* for the class.
*/
public void refreshBtClass() {
fetchBtClass();
dispatchAttributesChanged();
}
public int getSummary() {
// TODO: clean up
int oneOffSummary = getOneOffSummary();
@@ -425,17 +441,15 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
// No context menu if it is busy (none of these items are applicable if busy)
if (isBusy()) return;
// No context menu if there are no profiles
if (mProfiles.size() == 0) return;
int bondState = getBondState();
boolean isConnected = isConnected();
boolean hasProfiles = mProfiles.size() > 0;
menu.setHeaderTitle(getName());
if (isConnected) {
menu.add(0, CONTEXT_ITEM_DISCONNECT, 0, R.string.bluetooth_device_context_disconnect);
} else {
} else if (hasProfiles) {
// For connection action, show either "Connect" or "Pair & connect"
int connectString = (bondState == BluetoothDevice.BOND_NOT_BONDED)
? R.string.bluetooth_device_context_pair_connect
@@ -538,8 +552,8 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
if (comparison != 0) return comparison;
// Paired above not paired
comparison = (another.mBondState == BluetoothDevice.BOND_BONDED ? 1 : 0) -
(mBondState == BluetoothDevice.BOND_BONDED ? 1 : 0);
comparison = (another.getBondState() == BluetoothDevice.BOND_BONDED ? 1 : 0) -
(getBondState() == BluetoothDevice.BOND_BONDED ? 1 : 0);
if (comparison != 0) return comparison;
// Visible above not visible

View File

@@ -45,19 +45,23 @@ public class LocalBluetoothDeviceManager {
readPairedDevices();
}
private synchronized void readPairedDevices() {
private synchronized boolean readPairedDevices() {
BluetoothDevice manager = mLocalManager.getBluetoothManager();
String[] bondedAddresses = manager.listBonds();
if (bondedAddresses == null) return;
if (bondedAddresses == null) return false;
boolean deviceAdded = false;
for (String address : bondedAddresses) {
LocalBluetoothDevice device = findDevice(address);
if (device == null) {
device = new LocalBluetoothDevice(mLocalManager.getContext(), address);
mDevices.add(device);
dispatchDeviceAdded(device);
dispatchDeviceAdded(device);
deviceAdded = true;
}
}
return deviceAdded;
}
public synchronized List<LocalBluetoothDevice> getDevicesCopy() {
@@ -157,23 +161,33 @@ public class LocalBluetoothDeviceManager {
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.");
if (!readPairedDevices()) {
Log.e(TAG, "Got bonding state changed for " + address +
", but we have no record of that device.");
}
return;
}
device.setBondState(bondState); //TODO: might be unecessary
checkForDeviceRemoval(device);
device.refresh();
if (bondState == BluetoothDevice.BOND_BONDED) {
// Auto-connect after pairing
device.connect();
}
}
public synchronized void onBondingError(String address) {
/**
* Called when there is a bonding error.
*
* @param address The address of the remote device.
* @param reason The reason, one of the error reasons from
* BluetoothDevice.UNBOND_REASON_*
*/
public synchronized void onBondingError(String address, int reason) {
mLocalManager.showError(address, R.string.bluetooth_error_title,
R.string.bluetooth_pairing_error_message);
(reason == BluetoothDevice.UNBOND_REASON_AUTH_FAILED) ?
R.string.bluetooth_pairing_pin_error_message :
R.string.bluetooth_pairing_error_message);
}
public synchronized void onProfileStateChanged(String address) {
@@ -205,4 +219,11 @@ public class LocalBluetoothDeviceManager {
checkForDeviceRemoval(device);
}
}
public synchronized void onBtClassChanged(String address) {
LocalBluetoothDevice device = findDevice(address);
if (device != null) {
device.refreshBtClass();
}
}
}

View File

@@ -226,10 +226,6 @@ public class LocalBluetoothManager {
}
}
public boolean createBonding(String address) {
return mManager.createBond(address);
}
public void showError(String address, int titleResId, int messageResId) {
LocalBluetoothDevice device = mLocalDeviceManager.findDevice(address);
if (device == null) return;