Bluetooth : fix for Settings crashes while rotating the screen.

- Add some codes preventing setting crash when rotating the screen
  when the Rename device or Visibility timeout dialogs are visible.

Change-Id: I5e142a5d869a0d5c91c6ace80093d09178d79558
Signed-off-by: jhtop.kim <jhtop.kim@samsung.com>
This commit is contained in:
jhtop.kim
2011-07-20 14:03:07 +09:00
committed by Jake Hamby
parent 2f6ea06a63
commit e96f04c1ff
4 changed files with 49 additions and 13 deletions

View File

@@ -40,7 +40,7 @@ import com.android.settings.R;
/** /**
* Dialog fragment for renaming the local Bluetooth device. * Dialog fragment for renaming the local Bluetooth device.
*/ */
final class BluetoothNameDialogFragment extends DialogFragment implements TextWatcher { public final class BluetoothNameDialogFragment extends DialogFragment implements TextWatcher {
private static final int BLUETOOTH_NAME_MAX_LENGTH_BYTES = 248; private static final int BLUETOOTH_NAME_MAX_LENGTH_BYTES = 248;
private AlertDialog mAlertDialog; private AlertDialog mAlertDialog;
@@ -54,6 +54,13 @@ final class BluetoothNameDialogFragment extends DialogFragment implements TextWa
// This flag is set when the name is updated by code, to distinguish from user changes // This flag is set when the name is updated by code, to distinguish from user changes
private boolean mDeviceNameUpdated; private boolean mDeviceNameUpdated;
// This flag is set when the user edits the name (preserved on rotation)
private boolean mDeviceNameEdited;
// Key to save the edited name and edit status for restoring after rotation
private static final String KEY_NAME = "device_name";
private static final String KEY_NAME_EDITED = "device_name_edited";
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
@@ -68,16 +75,22 @@ final class BluetoothNameDialogFragment extends DialogFragment implements TextWa
} }
}; };
public BluetoothNameDialogFragment(LocalBluetoothAdapter adapter) { public BluetoothNameDialogFragment() {
mLocalAdapter = adapter; LocalBluetoothManager localManager = LocalBluetoothManager.getInstance(getActivity());
mLocalAdapter = localManager.getBluetoothAdapter();
} }
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
String deviceName = mLocalAdapter.getName();
if (savedInstanceState != null) {
deviceName = savedInstanceState.getString(KEY_NAME, deviceName);
mDeviceNameEdited = savedInstanceState.getBoolean(KEY_NAME_EDITED, false);
}
mAlertDialog = new AlertDialog.Builder(getActivity()) mAlertDialog = new AlertDialog.Builder(getActivity())
.setIcon(android.R.drawable.ic_dialog_info) .setIcon(android.R.drawable.ic_dialog_info)
.setTitle(R.string.bluetooth_rename_device) .setTitle(R.string.bluetooth_rename_device)
.setView(createDialogView()) .setView(createDialogView(deviceName))
.setPositiveButton(R.string.bluetooth_rename_button, .setPositiveButton(R.string.bluetooth_rename_button,
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
@@ -94,7 +107,13 @@ final class BluetoothNameDialogFragment extends DialogFragment implements TextWa
return mAlertDialog; return mAlertDialog;
} }
private View createDialogView() { @Override
public void onSaveInstanceState(Bundle outState) {
outState.putString(KEY_NAME, mDeviceNameView.getText().toString());
outState.putBoolean(KEY_NAME_EDITED, mDeviceNameEdited);
}
private View createDialogView(String deviceName) {
final LayoutInflater layoutInflater = (LayoutInflater)getActivity() final LayoutInflater layoutInflater = (LayoutInflater)getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.dialog_edittext, null); View view = layoutInflater.inflate(R.layout.dialog_edittext, null);
@@ -102,6 +121,7 @@ final class BluetoothNameDialogFragment extends DialogFragment implements TextWa
mDeviceNameView.setFilters(new InputFilter[] { mDeviceNameView.setFilters(new InputFilter[] {
new Utf8ByteLengthFilter(BLUETOOTH_NAME_MAX_LENGTH_BYTES) new Utf8ByteLengthFilter(BLUETOOTH_NAME_MAX_LENGTH_BYTES)
}); });
mDeviceNameView.setText(deviceName); // set initial value before adding listener
mDeviceNameView.addTextChangedListener(this); mDeviceNameView.addTextChangedListener(this);
return view; return view;
} }
@@ -119,13 +139,12 @@ final class BluetoothNameDialogFragment extends DialogFragment implements TextWa
super.onResume(); super.onResume();
if (mOkButton == null) { if (mOkButton == null) {
mOkButton = mAlertDialog.getButton(DialogInterface.BUTTON_POSITIVE); mOkButton = mAlertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
mOkButton.setEnabled(false); // Ok button is enabled when the user edits the name mOkButton.setEnabled(mDeviceNameEdited); // Ok button enabled after user edits
} }
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED); filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
getActivity().registerReceiver(mReceiver, filter); getActivity().registerReceiver(mReceiver, filter);
updateDeviceName();
} }
@Override @Override
@@ -137,6 +156,7 @@ final class BluetoothNameDialogFragment extends DialogFragment implements TextWa
void updateDeviceName() { void updateDeviceName() {
if (mLocalAdapter != null && mLocalAdapter.isEnabled()) { if (mLocalAdapter != null && mLocalAdapter.isEnabled()) {
mDeviceNameUpdated = true; mDeviceNameUpdated = true;
mDeviceNameEdited = false;
mDeviceNameView.setText(mLocalAdapter.getName()); mDeviceNameView.setText(mLocalAdapter.getName());
} }
} }
@@ -147,7 +167,10 @@ final class BluetoothNameDialogFragment extends DialogFragment implements TextWa
mDeviceNameUpdated = false; mDeviceNameUpdated = false;
mOkButton.setEnabled(false); mOkButton.setEnabled(false);
} else { } else {
mOkButton.setEnabled(s.length() != 0); mDeviceNameEdited = true;
if (mOkButton != null) {
mOkButton.setEnabled(s.length() != 0);
}
} }
} }

View File

@@ -194,12 +194,12 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
return true; return true;
case MENU_ID_RENAME_DEVICE: case MENU_ID_RENAME_DEVICE:
new BluetoothNameDialogFragment(mLocalAdapter).show( new BluetoothNameDialogFragment().show(
getFragmentManager(), "rename device"); getFragmentManager(), "rename device");
return true; return true;
case MENU_ID_VISIBILITY_TIMEOUT: case MENU_ID_VISIBILITY_TIMEOUT:
new BluetoothVisibilityTimeoutFragment(mDiscoverableEnabler).show( new BluetoothVisibilityTimeoutFragment().show(
getFragmentManager(), "visibility timeout"); getFragmentManager(), "visibility timeout");
return true; return true;
@@ -261,6 +261,8 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
mDiscoverableEnabler = new BluetoothDiscoverableEnabler(getActivity(), mDiscoverableEnabler = new BluetoothDiscoverableEnabler(getActivity(),
mLocalAdapter, mMyDevicePreference); mLocalAdapter, mMyDevicePreference);
mDiscoverableEnabler.resume(); mDiscoverableEnabler.resume();
LocalBluetoothManager.getInstance(getActivity()).setDiscoverableEnabler(
mDiscoverableEnabler);
} }
// Paired devices category // Paired devices category

View File

@@ -41,13 +41,14 @@ import com.android.settings.R;
/** /**
* Dialog fragment for setting the discoverability timeout. * Dialog fragment for setting the discoverability timeout.
*/ */
final class BluetoothVisibilityTimeoutFragment extends DialogFragment public final class BluetoothVisibilityTimeoutFragment extends DialogFragment
implements DialogInterface.OnClickListener { implements DialogInterface.OnClickListener {
private final BluetoothDiscoverableEnabler mDiscoverableEnabler; private final BluetoothDiscoverableEnabler mDiscoverableEnabler;
public BluetoothVisibilityTimeoutFragment(BluetoothDiscoverableEnabler enabler) { public BluetoothVisibilityTimeoutFragment() {
mDiscoverableEnabler = enabler; mDiscoverableEnabler = LocalBluetoothManager.getInstance(getActivity())
.getDiscoverableEnabler();
} }
@Override @Override

View File

@@ -36,6 +36,8 @@ public final class LocalBluetoothManager {
/** If a BT-related activity is in the foreground, this will be it. */ /** If a BT-related activity is in the foreground, this will be it. */
private Context mForegroundActivity; private Context mForegroundActivity;
private BluetoothDiscoverableEnabler mDiscoverableEnabler;
private final LocalBluetoothAdapter mLocalAdapter; private final LocalBluetoothAdapter mLocalAdapter;
private final CachedBluetoothDeviceManager mCachedDeviceManager; private final CachedBluetoothDeviceManager mCachedDeviceManager;
@@ -60,6 +62,14 @@ public final class LocalBluetoothManager {
return sInstance; return sInstance;
} }
public void setDiscoverableEnabler(BluetoothDiscoverableEnabler discoverableEnabler) {
mDiscoverableEnabler = discoverableEnabler;
}
public BluetoothDiscoverableEnabler getDiscoverableEnabler() {
return mDiscoverableEnabler;
}
private LocalBluetoothManager(LocalBluetoothAdapter adapter, Context context) { private LocalBluetoothManager(LocalBluetoothAdapter adapter, Context context) {
mContext = context; mContext = context;
mLocalAdapter = adapter; mLocalAdapter = adapter;