Refactor Bluetooth settings for readability and performance.
Major refactoring of Bluetooth settings classes. - Moved all functionality from LocalBluetoothManager into new LocalBluetoothAdapter and LocalBluetoothPreferences, and into existing classes. - Refactored functionality from BluetoothEventRedirector into new BluetoothEventManager class, deleting the original version. New version uses a HashMap from action Strings to implementers of the BluetoothEventManager.Handler interface. - Created new BluetoothDiscoveryReceiver to update shared preferences timestamp for Bluetooth discovery start/finish. This is the only event handling we need to do when the settings app is not visible, so it has its own receiver entry in AndroidManifest.xml. Edits are written using QueuedWork.singleThreadExecutor(), which BroadcastReceiver knows about and will wait for completion, eliminating the need for PendingResult. - Miscellaneous cleanups to code style and logic for readability. - Pulled some large switch statement code blocks into new methods. - Changed all Bluetooth state references to the new BluetoothProfile constants. - Changed use of deprecated Notification constructor in BluetoothPairingRequest to use Notification.Builder. - Moved Utf8ByteLengthFilter helper function from BluetoothNamePreference into its own class, and moved test cases into the same package. - Moved all LocalBluetoothProfileManager functionality related to specific profiles into new top-level classes (A2dpProfile, etc.), all implementing the LocalBluetoothProfile interface. - Moved all UI-related methods from CachedBluetoothDevice into the class that uses the method, or into the static Utils class for shared methods. Change-Id: I6d49b7f4ae0c7d7dcf62551ee40b51ecb5fe4f47
This commit is contained in:
@@ -22,12 +22,12 @@ import android.app.Dialog;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.preference.EditTextPreference;
|
||||
import android.text.Editable;
|
||||
import android.text.InputFilter;
|
||||
import android.text.Spanned;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.Button;
|
||||
@@ -38,13 +38,13 @@ import android.widget.EditText;
|
||||
* Bluetooth name. It asks the user for a name, and persists it via the
|
||||
* Bluetooth API.
|
||||
*/
|
||||
public class BluetoothNamePreference extends EditTextPreference implements TextWatcher {
|
||||
private static final String TAG = "BluetoothNamePreference";
|
||||
public final class BluetoothNamePreference extends EditTextPreference implements TextWatcher {
|
||||
// private static final String TAG = "BluetoothNamePreference";
|
||||
private static final int BLUETOOTH_NAME_MAX_LENGTH_BYTES = 248;
|
||||
|
||||
private LocalBluetoothManager mLocalManager;
|
||||
private final LocalBluetoothAdapter mLocalAdapter;
|
||||
|
||||
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
@@ -61,7 +61,7 @@ public class BluetoothNamePreference extends EditTextPreference implements TextW
|
||||
public BluetoothNamePreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
|
||||
mLocalManager = LocalBluetoothManager.getInstance(context);
|
||||
mLocalAdapter = LocalBluetoothManager.getInstance(context).getBluetoothAdapter();
|
||||
|
||||
setSummaryToName();
|
||||
}
|
||||
@@ -97,16 +97,17 @@ public class BluetoothNamePreference extends EditTextPreference implements TextW
|
||||
}
|
||||
|
||||
private void setSummaryToName() {
|
||||
BluetoothAdapter adapter = mLocalManager.getBluetoothAdapter();
|
||||
if (adapter.isEnabled()) {
|
||||
setSummary(adapter.getName());
|
||||
if (mLocalAdapter != null && mLocalAdapter.isEnabled()) {
|
||||
setSummary(mLocalAdapter.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean persistString(String value) {
|
||||
BluetoothAdapter adapter = mLocalManager.getBluetoothAdapter();
|
||||
adapter.setName(value);
|
||||
// Persist with Bluez instead of shared preferences
|
||||
if (mLocalAdapter != null) {
|
||||
mLocalAdapter.setName(value);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -116,8 +117,8 @@ public class BluetoothNamePreference extends EditTextPreference implements TextW
|
||||
|
||||
// The dialog should be created by now
|
||||
EditText et = getEditText();
|
||||
if (et != null) {
|
||||
et.setText(mLocalManager.getBluetoothAdapter().getName());
|
||||
if (et != null && mLocalAdapter != null) {
|
||||
et.setText(mLocalAdapter.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,7 +126,7 @@ public class BluetoothNamePreference extends EditTextPreference implements TextW
|
||||
public void afterTextChanged(Editable s) {
|
||||
Dialog d = getDialog();
|
||||
if (d instanceof AlertDialog) {
|
||||
((AlertDialog) d).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(s.length() > 0);
|
||||
((AlertDialog) d).getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(s.length() > 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,67 +140,4 @@ public class BluetoothNamePreference extends EditTextPreference implements TextW
|
||||
// not used
|
||||
}
|
||||
|
||||
/**
|
||||
* This filter will constrain edits so that the text length is not
|
||||
* greater than the specified number of bytes using UTF-8 encoding.
|
||||
* <p>The JNI method used by {@link android.server.BluetoothService}
|
||||
* to convert UTF-16 to UTF-8 doesn't support surrogate pairs,
|
||||
* therefore code points outside of the basic multilingual plane
|
||||
* (0000-FFFF) will be encoded as a pair of 3-byte UTF-8 characters,
|
||||
* rather than a single 4-byte UTF-8 encoding. Dalvik implements this
|
||||
* conversion in {@code convertUtf16ToUtf8()} in
|
||||
* {@code dalvik/vm/UtfString.c}.
|
||||
* <p>This JNI method is unlikely to change in the future due to
|
||||
* backwards compatibility requirements. It's also unclear whether
|
||||
* the installed base of Bluetooth devices would correctly handle the
|
||||
* encoding of surrogate pairs in UTF-8 as 4 bytes rather than 6.
|
||||
* However, this filter will still work in scenarios where surrogate
|
||||
* pairs are encoded as 4 bytes, with the caveat that the maximum
|
||||
* length will be constrained more conservatively than necessary.
|
||||
*/
|
||||
public static class Utf8ByteLengthFilter implements InputFilter {
|
||||
private int mMaxBytes;
|
||||
|
||||
public Utf8ByteLengthFilter(int maxBytes) {
|
||||
mMaxBytes = maxBytes;
|
||||
}
|
||||
|
||||
public CharSequence filter(CharSequence source, int start, int end,
|
||||
Spanned dest, int dstart, int dend) {
|
||||
int srcByteCount = 0;
|
||||
// count UTF-8 bytes in source substring
|
||||
for (int i = start; i < end; i++) {
|
||||
char c = source.charAt(i);
|
||||
srcByteCount += (c < 0x0080) ? 1 : (c < 0x0800 ? 2 : 3);
|
||||
}
|
||||
int destLen = dest.length();
|
||||
int destByteCount = 0;
|
||||
// count UTF-8 bytes in destination excluding replaced section
|
||||
for (int i = 0; i < destLen; i++) {
|
||||
if (i < dstart || i >= dend) {
|
||||
char c = dest.charAt(i);
|
||||
destByteCount += (c < 0x0080) ? 1 : (c < 0x0800 ? 2 : 3);
|
||||
}
|
||||
}
|
||||
int keepBytes = mMaxBytes - destByteCount;
|
||||
if (keepBytes <= 0) {
|
||||
return "";
|
||||
} else if (keepBytes >= srcByteCount) {
|
||||
return null; // use original dest string
|
||||
} else {
|
||||
// find end position of largest sequence that fits in keepBytes
|
||||
for (int i = start; i < end; i++) {
|
||||
char c = source.charAt(i);
|
||||
keepBytes -= (c < 0x0080) ? 1 : (c < 0x0800 ? 2 : 3);
|
||||
if (keepBytes < 0) {
|
||||
return source.subSequence(start, i);
|
||||
}
|
||||
}
|
||||
// If the entire substring fits, we should have returned null
|
||||
// above, so this line should not be reached. If for some
|
||||
// reason it is, return null to use the original dest string.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user