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

Commit 91db20a3 authored by Jorge Ruesga's avatar Jorge Ruesga
Browse files

settings: check bt pan service before use it



If pan service is not allowed, the service listener is notified, but with an invalid reference.
This patch checks that the pan service has a valid reference before use it.

Change-Id: I3fed496d4a6b3c8d2620bc5913943e58fecddf6d
Signed-off-by: default avatarJorge Ruesga <jorge@ruesga.com>
parent 4720d1b1
Loading
Loading
Loading
Loading
+33 −11
Original line number Diff line number Diff line
@@ -127,6 +127,12 @@ public class TetherSettings extends SettingsPreferenceFragment
            return;
        }

        mEnableWifiAp =
                (SwitchPreference) findPreference(ENABLE_WIFI_AP);
        Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
        mUsbTether = (SwitchPreference) findPreference(USB_TETHER_SETTINGS);
        mBluetoothTether = (SwitchPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);

        final Activity activity = getActivity();
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter != null) {
@@ -134,12 +140,6 @@ public class TetherSettings extends SettingsPreferenceFragment
                    BluetoothProfile.PAN);
        }

        mEnableWifiAp =
                (SwitchPreference) findPreference(ENABLE_WIFI_AP);
        Preference wifiApSettings = findPreference(WIFI_AP_SSID_AND_SECURITY);
        mUsbTether = (SwitchPreference) findPreference(USB_TETHER_SETTINGS);
        mBluetoothTether = (SwitchPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);

        mStorageManager = (StorageManager)getSystemService(Context.STORAGE_SERVICE);
        ConnectivityManager cm =
                (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
@@ -168,7 +168,7 @@ public class TetherSettings extends SettingsPreferenceFragment
            getPreferenceScreen().removePreference(mBluetoothTether);
        } else {
            BluetoothPan pan = mBluetoothPan.get();
            if (pan != null && pan.isTetheringOn()) {
            if (isBtPanAllowed() && pan.isTetheringOn()) {
                mBluetoothTether.setChecked(true);
            } else {
                mBluetoothTether.setChecked(false);
@@ -210,9 +210,17 @@ public class TetherSettings extends SettingsPreferenceFragment
        new BluetoothProfile.ServiceListener() {
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            mBluetoothPan.set((BluetoothPan) proxy);
            if (mBluetoothTether != null) {
                boolean enabled = isBtPanAllowed();
                mBluetoothTether.setEnabled(enabled);
                mBluetoothTether.setChecked(enabled && ((BluetoothPan)proxy).isTetheringOn());
            }
        }
        public void onServiceDisconnected(int profile) {
            mBluetoothPan.set(null);
            if (mBluetoothTether != null) {
                mBluetoothTether.setEnabled(false);
            }
        }
    };

@@ -257,7 +265,7 @@ public class TetherSettings extends SettingsPreferenceFragment
                            .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
                        case BluetoothAdapter.STATE_ON:
                            BluetoothPan bluetoothPan = mBluetoothPan.get();
                            if (bluetoothPan != null) {
                            if (isBtPanAllowed()) {
                                bluetoothPan.setBluetoothTethering(true);
                                mBluetoothEnableForTether = false;
                            }
@@ -444,7 +452,7 @@ public class TetherSettings extends SettingsPreferenceFragment
            mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
        } else {
            BluetoothPan bluetoothPan = mBluetoothPan.get();
            if (btState == BluetoothAdapter.STATE_ON && bluetoothPan != null &&
            if (btState == BluetoothAdapter.STATE_ON && isBtPanAllowed() &&
                    bluetoothPan.isTetheringOn()) {
                mBluetoothTether.setChecked(true);
                mBluetoothTether.setEnabled(true);
@@ -561,7 +569,7 @@ public class TetherSettings extends SettingsPreferenceFragment
                    mBluetoothTether.setEnabled(false);
                } else {
                    BluetoothPan bluetoothPan = mBluetoothPan.get();
                    if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(true);
                    if (isBtPanAllowed()) bluetoothPan.setBluetoothTethering(true);
                    mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
                }
                break;
@@ -585,6 +593,20 @@ public class TetherSettings extends SettingsPreferenceFragment
        mUsbTether.setSummary("");
    }

    private boolean isBtPanAllowed() {
        try {
            BluetoothPan btPan = mBluetoothPan.get();
            if (btPan == null) {
                return false;
            }
            btPan.isTetheringOn();
        } catch (NullPointerException ex) {
            // Ignore
            return false;
        }
        return true;
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
        ConnectivityManager cm =
@@ -620,7 +642,7 @@ public class TetherSettings extends SettingsPreferenceFragment
                }

                BluetoothPan bluetoothPan = mBluetoothPan.get();
                if (bluetoothPan != null) bluetoothPan.setBluetoothTethering(false);
                if (isBtPanAllowed()) bluetoothPan.setBluetoothTethering(false);
                if (errored) {
                    mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
                } else {
+24 −5
Original line number Diff line number Diff line
@@ -83,6 +83,8 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
    private static final String BTOPP_ACTION_OPEN_RECEIVED_FILES =
            "android.btopp.intent.action.OPEN_RECEIVED_FILES";

    private SettingsActivity mActivity;

    private static View mSettingsDialogView = null;

    private BluetoothEnabler mBluetoothEnabler;
@@ -104,6 +106,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
            new BluetoothProfile.ServiceListener() {
                public void onServiceConnected(int profile, BluetoothProfile proxy) {
                    mBluetoothPan.set((BluetoothPan) proxy);
                    mActivity.invalidateOptionsMenu();
                }
                public void onServiceDisconnected(int profile) {
                    mBluetoothPan.set(null);
@@ -152,15 +155,15 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
        mEmptyView = (TextView) getView().findViewById(android.R.id.empty);
        getListView().setEmptyView(mEmptyView);

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

        mBluetoothEnabler = new BluetoothEnabler(activity, mSwitchBar);
        mBluetoothEnabler = new BluetoothEnabler(mActivity, mSwitchBar);
        mBluetoothEnabler.setupSwitchBar();

        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter != null) {
            adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
            adapter.getProfileProxy(mActivity.getApplicationContext(), mProfileServiceListener,
                    BluetoothProfile.PAN);
        }

@@ -242,7 +245,8 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
                Settings.System.BLUETOOTH_ACCEPT_ALL_FILES, 0) == 1;

        final BluetoothPan bluetoothPan = mBluetoothPan.get();
        boolean isBluetoothTetheringEnabled = bluetoothPan != null && bluetoothPan.isTetheringOn();
        boolean isBluetoothPanAllowed = isBtPanAllowed();
        boolean isBluetoothTetheringEnabled = isBluetoothPanAllowed && bluetoothPan.isTetheringOn();

        menu.add(Menu.NONE, MENU_ID_SCAN, 0, textId)
                .setEnabled(bluetoothIsEnabled && !isDiscovering)
@@ -260,6 +264,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
                R.string.bluetooth_tether_checkbox_text)
                .setCheckable(true)
                .setChecked(isBluetoothTetheringEnabled)
                .setEnabled(isBluetoothPanAllowed)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
        super.onCreateOptionsMenu(menu, inflater);
    }
@@ -458,6 +463,20 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
        updateContent(mLocalAdapter.getBluetoothState());
    }

    private boolean isBtPanAllowed() {
        try {
            BluetoothPan btPan = mBluetoothPan.get();
            if (btPan == null) {
                return false;
            }
            btPan.isTetheringOn();
        } catch (NullPointerException ex) {
            // Ignore
            return false;
        }
        return true;
    }

    private final View.OnClickListener mDeviceProfilesListener = new View.OnClickListener() {
        public void onClick(View v) {
            // User clicked on advanced options icon for a device in the list