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

Commit f5f71a9b authored by Fan Zhang's avatar Fan Zhang
Browse files

Fix ServiceConnectionLeak in network fragment

Change-Id: I43efe9ae31fc2f58361abdb034b0205a3f3e2b71
Fix: 35957226
Test: make RunSettingsRoboTests
parent 50aa0008
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -88,4 +88,11 @@ public abstract class ObservablePreferenceFragment extends PreferenceFragment {
        super.onPause();
    }

    @CallSuper
    @Override
    public void onDestroy() {
        mLifecycle.onDestroy();
        super.onDestroy();
    }

}
+1 −1
Original line number Diff line number Diff line
@@ -95,7 +95,7 @@ public class NetworkDashboardFragment extends DashboardFragment implements
        final List<PreferenceController> controllers = new ArrayList<>();
        controllers.add(airplaneModePreferenceController);
        controllers.add(mobileNetworkPreferenceController);
        controllers.add(new TetherPreferenceController(context));
        controllers.add(new TetherPreferenceController(context, lifecycle));
        controllers.add(vpnPreferenceController);
        controllers.add(new ProxyPreferenceController(context));
        controllers.add(mobilePlanPreferenceController);
+19 −4
Original line number Diff line number Diff line
@@ -28,6 +28,9 @@ import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.TetherSettings;
import com.android.settings.core.PreferenceController;
import com.android.settings.core.lifecycle.Lifecycle;
import com.android.settings.core.lifecycle.LifecycleObserver;
import com.android.settings.core.lifecycle.events.OnDestroy;

import java.util.concurrent.atomic.AtomicReference;

@@ -35,7 +38,8 @@ import static android.os.UserManager.DISALLOW_CONFIG_TETHERING;
import static com.android.settingslib.RestrictedLockUtils.checkIfRestrictionEnforced;
import static com.android.settingslib.RestrictedLockUtils.hasBaseUserRestriction;

public class TetherPreferenceController extends PreferenceController {
public class TetherPreferenceController extends PreferenceController
        implements LifecycleObserver, OnDestroy {

    private static final String KEY_TETHER_SETTINGS = "tether_settings";

@@ -62,12 +66,12 @@ public class TetherPreferenceController extends PreferenceController {
    TetherPreferenceController() {
        super(null);
        mAdminDisallowedTetherConfig = false;
        mBluetoothPan = null;
        mBluetoothPan = new AtomicReference<>();
        mConnectivityManager = null;
        mBluetoothAdapter = null;
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    public TetherPreferenceController(Context context) {
    public TetherPreferenceController(Context context, Lifecycle lifecycle) {
        super(context);
        mBluetoothPan = new AtomicReference<>();
        mAdminDisallowedTetherConfig = checkIfRestrictionEnforced(
@@ -75,6 +79,9 @@ public class TetherPreferenceController extends PreferenceController {
        mConnectivityManager =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (lifecycle != null) {
            lifecycle.addObserver(this);
        }
        if (mBluetoothAdapter != null) {
            mBluetoothAdapter.getProfileProxy(context, mBtProfileServiceListener,
                    BluetoothProfile.PAN);
@@ -113,6 +120,14 @@ public class TetherPreferenceController extends PreferenceController {
        return KEY_TETHER_SETTINGS;
    }

    @Override
    public void onDestroy() {
        final BluetoothProfile profile = mBluetoothPan.getAndSet(null);
        if (profile != null && mBluetoothAdapter != null) {
            mBluetoothAdapter.closeProfileProxy(BluetoothProfile.PAN, profile);
        }
    }

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    void updateSummary() {
        if (mPreference == null) {
+17 −1
Original line number Diff line number Diff line
@@ -18,9 +18,10 @@ package com.android.settings.network;


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothPan;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.UserManager;
import android.support.v7.preference.Preference;

import com.android.settings.R;
@@ -35,6 +36,9 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;

import java.util.concurrent.atomic.AtomicReference;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
@@ -64,6 +68,18 @@ public class TetherPreferenceControllerTest {
        ReflectionHelpers.setField(mController, "mBluetoothAdapter", mBluetoothAdapter);
    }

    @Test
    public void goThroughLifecycle_shouldDestoryBluetoothProfile() {
        final BluetoothPan pan = mock(BluetoothPan.class);
        final AtomicReference<BluetoothPan> panRef =
                ReflectionHelpers.getField(mController, "mBluetoothPan");
        panRef.set(pan);

        mController.onDestroy();

        verify(mBluetoothAdapter).closeProfileProxy(BluetoothProfile.PAN, pan);
    }

    @Test
    public void updateSummary_noPreference_noInteractionWithConnectivityManager() {
        mController.updateSummary();