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

Commit cf37cb6b authored by Fabrice Di Meglio's avatar Fabrice Di Meglio
Browse files

Use SwitchBar for Bluetooth Settings

- follow up CL to 41937766

Related to bug #14898161 On/Off switches must move down from Action Bar

Change-Id: Ic04de39599c91388cba8510bfd46d96e7bc30260
parent 41937766
Loading
Loading
Loading
Loading
+28 −36
Original line number Diff line number Diff line
@@ -31,15 +31,17 @@ import android.widget.Toast;
import com.android.settings.R;
import com.android.settings.WirelessSettings;
import com.android.settings.search.Index;
import com.android.settings.widget.SwitchBar;

/**
 * BluetoothEnabler is a helper to manage the Bluetooth on/off checkbox
 * preference. It turns on/off Bluetooth and ensures the summary of the
 * preference reflects the current state.
 */
public final class BluetoothEnabler implements CompoundButton.OnCheckedChangeListener {
public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener {
    private Context mContext;
    private Switch mSwitch;
    private SwitchBar mSwitchBar;
    private boolean mValidListener;
    private final LocalBluetoothAdapter mLocalAdapter;
    private final IntentFilter mIntentFilter;
@@ -70,9 +72,10 @@ public final class BluetoothEnabler implements CompoundButton.OnCheckedChangeLis
        }
    };

    public BluetoothEnabler(Context context, Switch switch_) {
    public BluetoothEnabler(Context context, SwitchBar switchBar) {
        mContext = context;
        mSwitch = switch_;
        mSwitchBar = switchBar;
        mSwitch = switchBar.getSwitch();
        mValidListener = false;

        LocalBluetoothManager manager = LocalBluetoothManager.getInstance(context);
@@ -100,7 +103,8 @@ public final class BluetoothEnabler implements CompoundButton.OnCheckedChangeLis
        handleStateChanged(mLocalAdapter.getBluetoothState());

        mContext.registerReceiver(mReceiver, mIntentFilter);
        mSwitch.setOnCheckedChangeListener(this);
        mSwitchBar.addOnSwitchChangeListener(this);
        mSwitchBar.show();
        mValidListener = true;
    }

@@ -110,39 +114,11 @@ public final class BluetoothEnabler implements CompoundButton.OnCheckedChangeLis
        }

        mContext.unregisterReceiver(mReceiver);
        mSwitch.setOnCheckedChangeListener(null);
        mSwitchBar.removeOnSwitchChangeListener(this);
        mSwitchBar.hide();
        mValidListener = false;
    }

    public void setSwitch(Switch switch_) {
        if (mSwitch == switch_) return;
        mSwitch.setOnCheckedChangeListener(null);
        mSwitch = switch_;
        mSwitch.setOnCheckedChangeListener(mValidListener ? this : null);

        int bluetoothState = BluetoothAdapter.STATE_OFF;
        if (mLocalAdapter != null) bluetoothState = mLocalAdapter.getBluetoothState();
        boolean isOn = bluetoothState == BluetoothAdapter.STATE_ON;
        boolean isOff = bluetoothState == BluetoothAdapter.STATE_OFF;
        setChecked(isOn);
        mSwitch.setEnabled(isOn || isOff);
    }

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // Show toast message if Bluetooth is not allowed in airplane mode
        if (isChecked &&
                !WirelessSettings.isRadioAllowed(mContext, Settings.Global.RADIO_BLUETOOTH)) {
            Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
            // Reset switch to off
            buttonView.setChecked(false);
        }

        if (mLocalAdapter != null) {
            mLocalAdapter.setBluetoothEnabled(isChecked);
        }
        mSwitch.setEnabled(false);
    }

    void handleStateChanged(int state) {
        switch (state) {
            case BluetoothAdapter.STATE_TURNING_ON:
@@ -173,11 +149,11 @@ public final class BluetoothEnabler implements CompoundButton.OnCheckedChangeLis
            // set listener to null, so onCheckedChanged won't be called
            // if the checked status on Switch isn't changed by user click
            if (mValidListener) {
                mSwitch.setOnCheckedChangeListener(null);
                mSwitchBar.removeOnSwitchChangeListener(this);
            }
            mSwitch.setChecked(isChecked);
            if (mValidListener) {
                mSwitch.setOnCheckedChangeListener(this);
                mSwitchBar.addOnSwitchChangeListener(this);
            }
        }
    }
@@ -190,4 +166,20 @@ public final class BluetoothEnabler implements CompoundButton.OnCheckedChangeLis
        msg.getData().putBoolean(EVENT_DATA_IS_BT_ON, isBluetoothOn);
        mHandler.sendMessage(msg);
    }

    @Override
    public void onSwitchChanged(Switch switchView, boolean isChecked) {
        // Show toast message if Bluetooth is not allowed in airplane mode
        if (isChecked &&
                !WirelessSettings.isRadioAllowed(mContext, Settings.Global.RADIO_BLUETOOTH)) {
            Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
            // Reset switch to off
            switchView.setChecked(false);
        }

        if (mLocalAdapter != null) {
            mLocalAdapter.setBluetoothEnabled(isChecked);
        }
        mSwitch.setEnabled(false);
    }
}
+4 −28
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ import com.android.settings.SettingsActivity;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settings.search.SearchIndexableRaw;
import com.android.settings.widget.SwitchBar;

import java.util.ArrayList;
import java.util.List;
@@ -77,7 +78,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
    private boolean mActivityStarted;

    private TextView mEmptyView;
    private Switch mSwitch;
    private SwitchBar mSwitchBar;

    private final IntentFilter mIntentFilter;

@@ -114,35 +115,10 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
        mEmptyView = (TextView) getView().findViewById(android.R.id.empty);
        getListView().setEmptyView(mEmptyView);

        final Activity activity = getActivity();
        final int padding = activity.getResources().getDimensionPixelSize(
                R.dimen.action_bar_switch_padding);
        mSwitch = new Switch(activity.getActionBar().getThemedContext());
        mSwitch.setPaddingRelative(0, 0, padding, 0);

        mBluetoothEnabler = new BluetoothEnabler(activity, mSwitch);
    }

    @Override
    public void onStart() {
        super.onStart();

        final SettingsActivity activity = (SettingsActivity) getActivity();
        mSwitchBar = activity.getSwitchBar();

        activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
                ActionBar.DISPLAY_SHOW_CUSTOM);
        activity.getActionBar().setCustomView(mSwitch, new ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.WRAP_CONTENT,
                Gravity.CENTER_VERTICAL | Gravity.END));
    }

    @Override
    public void onStop() {
        super.onStop();
        final SettingsActivity activity = (SettingsActivity) getActivity();
        activity.getActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_CUSTOM);
        activity.getActionBar().setCustomView(null);
        mBluetoothEnabler = new BluetoothEnabler(activity, mSwitchBar);
    }

    @Override