Bind service in onCreate
This cl puts the binding to onCreate instead of constructor, so preference controller won't bind/unbind service without registering Lifecycle. Bug: 62105045 Test: RunSettingsRoboTests Change-Id: I1e60d4b6ad7270aa5d04b7ec9fae1d3200fccc5f
This commit is contained in:
@@ -25,6 +25,7 @@ import android.content.IntentFilter;
|
|||||||
import android.database.ContentObserver;
|
import android.database.ContentObserver;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
@@ -38,6 +39,7 @@ import com.android.settings.core.PreferenceControllerMixin;
|
|||||||
import com.android.settingslib.core.AbstractPreferenceController;
|
import com.android.settingslib.core.AbstractPreferenceController;
|
||||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||||
|
import com.android.settingslib.core.lifecycle.events.OnCreate;
|
||||||
import com.android.settingslib.core.lifecycle.events.OnDestroy;
|
import com.android.settingslib.core.lifecycle.events.OnDestroy;
|
||||||
import com.android.settingslib.core.lifecycle.events.OnPause;
|
import com.android.settingslib.core.lifecycle.events.OnPause;
|
||||||
import com.android.settingslib.core.lifecycle.events.OnResume;
|
import com.android.settingslib.core.lifecycle.events.OnResume;
|
||||||
@@ -45,11 +47,12 @@ import com.android.settingslib.core.lifecycle.events.OnResume;
|
|||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
import static android.os.UserManager.DISALLOW_CONFIG_TETHERING;
|
import static android.os.UserManager.DISALLOW_CONFIG_TETHERING;
|
||||||
|
|
||||||
import static com.android.settingslib.RestrictedLockUtils.checkIfRestrictionEnforced;
|
import static com.android.settingslib.RestrictedLockUtils.checkIfRestrictionEnforced;
|
||||||
import static com.android.settingslib.RestrictedLockUtils.hasBaseUserRestriction;
|
import static com.android.settingslib.RestrictedLockUtils.hasBaseUserRestriction;
|
||||||
|
|
||||||
public class TetherPreferenceController extends AbstractPreferenceController
|
public class TetherPreferenceController extends AbstractPreferenceController implements
|
||||||
implements PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause, OnDestroy {
|
PreferenceControllerMixin, LifecycleObserver, OnCreate, OnResume, OnPause, OnDestroy {
|
||||||
|
|
||||||
private static final String KEY_TETHER_SETTINGS = "tether_settings";
|
private static final String KEY_TETHER_SETTINGS = "tether_settings";
|
||||||
|
|
||||||
@@ -57,7 +60,8 @@ public class TetherPreferenceController extends AbstractPreferenceController
|
|||||||
private final AtomicReference<BluetoothPan> mBluetoothPan;
|
private final AtomicReference<BluetoothPan> mBluetoothPan;
|
||||||
private final ConnectivityManager mConnectivityManager;
|
private final ConnectivityManager mConnectivityManager;
|
||||||
private final BluetoothAdapter mBluetoothAdapter;
|
private final BluetoothAdapter mBluetoothAdapter;
|
||||||
private final BluetoothProfile.ServiceListener mBtProfileServiceListener =
|
@VisibleForTesting
|
||||||
|
final BluetoothProfile.ServiceListener mBtProfileServiceListener =
|
||||||
new android.bluetooth.BluetoothProfile.ServiceListener() {
|
new android.bluetooth.BluetoothProfile.ServiceListener() {
|
||||||
public void onServiceConnected(int profile, BluetoothProfile proxy) {
|
public void onServiceConnected(int profile, BluetoothProfile proxy) {
|
||||||
mBluetoothPan.set((BluetoothPan) proxy);
|
mBluetoothPan.set((BluetoothPan) proxy);
|
||||||
@@ -93,10 +97,6 @@ public class TetherPreferenceController extends AbstractPreferenceController
|
|||||||
if (lifecycle != null) {
|
if (lifecycle != null) {
|
||||||
lifecycle.addObserver(this);
|
lifecycle.addObserver(this);
|
||||||
}
|
}
|
||||||
if (mBluetoothAdapter != null) {
|
|
||||||
mBluetoothAdapter.getProfileProxy(context, mBtProfileServiceListener,
|
|
||||||
BluetoothProfile.PAN);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -131,6 +131,14 @@ public class TetherPreferenceController extends AbstractPreferenceController
|
|||||||
return KEY_TETHER_SETTINGS;
|
return KEY_TETHER_SETTINGS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
if (mBluetoothAdapter != null) {
|
||||||
|
mBluetoothAdapter.getProfileProxy(mContext, mBtProfileServiceListener,
|
||||||
|
BluetoothProfile.PAN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
if (mAirplaneModeObserver == null) {
|
if (mAirplaneModeObserver == null) {
|
||||||
@@ -140,7 +148,7 @@ public class TetherPreferenceController extends AbstractPreferenceController
|
|||||||
mTetherReceiver = new TetherBroadcastReceiver();
|
mTetherReceiver = new TetherBroadcastReceiver();
|
||||||
}
|
}
|
||||||
mContext.registerReceiver(
|
mContext.registerReceiver(
|
||||||
mTetherReceiver, new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED));
|
mTetherReceiver, new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED));
|
||||||
mContext.getContentResolver()
|
mContext.getContentResolver()
|
||||||
.registerContentObserver(mAirplaneModeObserver.uri, false, mAirplaneModeObserver);
|
.registerContentObserver(mAirplaneModeObserver.uri, false, mAirplaneModeObserver);
|
||||||
}
|
}
|
||||||
|
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package com.android.settings.network;
|
package com.android.settings.network;
|
||||||
|
|
||||||
|
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothPan;
|
import android.bluetooth.BluetoothPan;
|
||||||
import android.bluetooth.BluetoothProfile;
|
import android.bluetooth.BluetoothProfile;
|
||||||
@@ -77,6 +76,14 @@ public class TetherPreferenceControllerTest {
|
|||||||
ReflectionHelpers.setField(mController, "mPreference", mPreference);
|
ReflectionHelpers.setField(mController, "mPreference", mPreference);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void lifeCycle_onCreate_shouldInitBluetoothPan() {
|
||||||
|
mController.onCreate(null);
|
||||||
|
|
||||||
|
verify(mBluetoothAdapter).getProfileProxy(mContext, mController.mBtProfileServiceListener,
|
||||||
|
BluetoothProfile.PAN);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void goThroughLifecycle_shouldDestoryBluetoothProfile() {
|
public void goThroughLifecycle_shouldDestoryBluetoothProfile() {
|
||||||
final BluetoothPan pan = mock(BluetoothPan.class);
|
final BluetoothPan pan = mock(BluetoothPan.class);
|
||||||
@@ -161,7 +168,8 @@ public class TetherPreferenceControllerTest {
|
|||||||
mController.onResume();
|
mController.onResume();
|
||||||
|
|
||||||
verify(mContext).registerReceiver(
|
verify(mContext).registerReceiver(
|
||||||
any(TetherPreferenceController.TetherBroadcastReceiver.class), any(IntentFilter.class));
|
any(TetherPreferenceController.TetherBroadcastReceiver.class),
|
||||||
|
any(IntentFilter.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -172,7 +180,7 @@ public class TetherPreferenceControllerTest {
|
|||||||
mController.onPause();
|
mController.onPause();
|
||||||
|
|
||||||
verify(mContext).unregisterReceiver(
|
verify(mContext).unregisterReceiver(
|
||||||
any(TetherPreferenceController.TetherBroadcastReceiver.class));
|
any(TetherPreferenceController.TetherBroadcastReceiver.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Reference in New Issue
Block a user