Loading src/com/android/settings/core/lifecycle/ObservablePreferenceFragment.java +7 −0 Original line number Diff line number Diff line Loading @@ -88,4 +88,11 @@ public abstract class ObservablePreferenceFragment extends PreferenceFragment { super.onPause(); } @CallSuper @Override public void onDestroy() { mLifecycle.onDestroy(); super.onDestroy(); } } src/com/android/settings/network/NetworkDashboardFragment.java +1 −1 Original line number Diff line number Diff line Loading @@ -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); Loading src/com/android/settings/network/TetherPreferenceController.java +19 −4 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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"; Loading @@ -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( Loading @@ -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); Loading Loading @@ -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) { Loading tests/robotests/src/com/android/settings/network/TetherPreferenceControllerTest.java +17 −1 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading Loading @@ -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(); Loading Loading
src/com/android/settings/core/lifecycle/ObservablePreferenceFragment.java +7 −0 Original line number Diff line number Diff line Loading @@ -88,4 +88,11 @@ public abstract class ObservablePreferenceFragment extends PreferenceFragment { super.onPause(); } @CallSuper @Override public void onDestroy() { mLifecycle.onDestroy(); super.onDestroy(); } }
src/com/android/settings/network/NetworkDashboardFragment.java +1 −1 Original line number Diff line number Diff line Loading @@ -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); Loading
src/com/android/settings/network/TetherPreferenceController.java +19 −4 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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"; Loading @@ -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( Loading @@ -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); Loading Loading @@ -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) { Loading
tests/robotests/src/com/android/settings/network/TetherPreferenceControllerTest.java +17 −1 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading Loading @@ -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(); Loading