Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e96f04c1 authored by jhtop.kim's avatar jhtop.kim Committed by Jake Hamby
Browse files

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: default avatarjhtop.kim <jhtop.kim@samsung.com>
parent 2f6ea06a
Loading
Loading
Loading
Loading
+31 −8
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ import com.android.settings.R;
/**
 * 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 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
    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() {
        @Override
        public void onReceive(Context context, Intent intent) {
@@ -68,16 +75,22 @@ final class BluetoothNameDialogFragment extends DialogFragment implements TextWa
        }
    };

    public BluetoothNameDialogFragment(LocalBluetoothAdapter adapter) {
        mLocalAdapter = adapter;
    public BluetoothNameDialogFragment() {
        LocalBluetoothManager localManager = LocalBluetoothManager.getInstance(getActivity());
        mLocalAdapter = localManager.getBluetoothAdapter();
    }

    @Override
    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())
                .setIcon(android.R.drawable.ic_dialog_info)
                .setTitle(R.string.bluetooth_rename_device)
                .setView(createDialogView())
                .setView(createDialogView(deviceName))
                .setPositiveButton(R.string.bluetooth_rename_button,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
@@ -94,7 +107,13 @@ final class BluetoothNameDialogFragment extends DialogFragment implements TextWa
        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()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.dialog_edittext, null);
@@ -102,6 +121,7 @@ final class BluetoothNameDialogFragment extends DialogFragment implements TextWa
        mDeviceNameView.setFilters(new InputFilter[] {
                new Utf8ByteLengthFilter(BLUETOOTH_NAME_MAX_LENGTH_BYTES)
        });
        mDeviceNameView.setText(deviceName);    // set initial value before adding listener
        mDeviceNameView.addTextChangedListener(this);
        return view;
    }
@@ -119,13 +139,12 @@ final class BluetoothNameDialogFragment extends DialogFragment implements TextWa
        super.onResume();
        if (mOkButton == null) {
            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();
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
        getActivity().registerReceiver(mReceiver, filter);
        updateDeviceName();
    }

    @Override
@@ -137,6 +156,7 @@ final class BluetoothNameDialogFragment extends DialogFragment implements TextWa
    void updateDeviceName() {
        if (mLocalAdapter != null && mLocalAdapter.isEnabled()) {
            mDeviceNameUpdated = true;
            mDeviceNameEdited = false;
            mDeviceNameView.setText(mLocalAdapter.getName());
        }
    }
@@ -147,9 +167,12 @@ final class BluetoothNameDialogFragment extends DialogFragment implements TextWa
            mDeviceNameUpdated = false;
            mOkButton.setEnabled(false);
        } else {
            mDeviceNameEdited = true;
            if (mOkButton != null) {
                mOkButton.setEnabled(s.length() != 0);
            }
        }
    }

    /* Not used */
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
+4 −2
Original line number Diff line number Diff line
@@ -194,12 +194,12 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
                return true;

            case MENU_ID_RENAME_DEVICE:
                new BluetoothNameDialogFragment(mLocalAdapter).show(
                new BluetoothNameDialogFragment().show(
                        getFragmentManager(), "rename device");
                return true;

            case MENU_ID_VISIBILITY_TIMEOUT:
                new BluetoothVisibilityTimeoutFragment(mDiscoverableEnabler).show(
                new BluetoothVisibilityTimeoutFragment().show(
                        getFragmentManager(), "visibility timeout");
                return true;

@@ -261,6 +261,8 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
                    mDiscoverableEnabler = new BluetoothDiscoverableEnabler(getActivity(),
                            mLocalAdapter, mMyDevicePreference);
                    mDiscoverableEnabler.resume();
                    LocalBluetoothManager.getInstance(getActivity()).setDiscoverableEnabler(
                            mDiscoverableEnabler);
                }

                // Paired devices category
+4 −3
Original line number Diff line number Diff line
@@ -41,13 +41,14 @@ import com.android.settings.R;
/**
 * Dialog fragment for setting the discoverability timeout.
 */
final class BluetoothVisibilityTimeoutFragment extends DialogFragment
public final class BluetoothVisibilityTimeoutFragment extends DialogFragment
        implements DialogInterface.OnClickListener {

    private final BluetoothDiscoverableEnabler mDiscoverableEnabler;

    public BluetoothVisibilityTimeoutFragment(BluetoothDiscoverableEnabler enabler) {
        mDiscoverableEnabler = enabler;
    public BluetoothVisibilityTimeoutFragment() {
        mDiscoverableEnabler = LocalBluetoothManager.getInstance(getActivity())
                .getDiscoverableEnabler();
    }

    @Override
+10 −0
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ public final class LocalBluetoothManager {
    /** If a BT-related activity is in the foreground, this will be it. */
    private Context mForegroundActivity;

    private BluetoothDiscoverableEnabler mDiscoverableEnabler;

    private final LocalBluetoothAdapter mLocalAdapter;

    private final CachedBluetoothDeviceManager mCachedDeviceManager;
@@ -60,6 +62,14 @@ public final class LocalBluetoothManager {
        return sInstance;
    }

    public void setDiscoverableEnabler(BluetoothDiscoverableEnabler discoverableEnabler) {
        mDiscoverableEnabler = discoverableEnabler;
    }

    public BluetoothDiscoverableEnabler getDiscoverableEnabler() {
        return mDiscoverableEnabler;
    }

    private LocalBluetoothManager(LocalBluetoothAdapter adapter, Context context) {
        mContext = context;
        mLocalAdapter = adapter;