Listener to tether state change on Network & internet page.
User can navigates to the page and turn hotspot on or off from the quick settings. Need to listen to the tether state updates to refresh the summary for the Hotspot & tethering preference accordingly. Change-Id: I7c6869b909306b09556e19cf7b7543ce9dcd890e Fix: 38452559 Test: make RunSettingsRoboTests
This commit is contained in:
@@ -18,7 +18,10 @@ package com.android.settings.network;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothPan;
|
||||
import android.bluetooth.BluetoothProfile;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.database.ContentObserver;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.Uri;
|
||||
@@ -67,6 +70,7 @@ public class TetherPreferenceController extends PreferenceController
|
||||
|
||||
private SettingObserver mAirplaneModeObserver;
|
||||
private Preference mPreference;
|
||||
private TetherBroadcastReceiver mTetherReceiver;
|
||||
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
|
||||
TetherPreferenceController() {
|
||||
@@ -131,6 +135,11 @@ public class TetherPreferenceController extends PreferenceController
|
||||
if (mAirplaneModeObserver == null) {
|
||||
mAirplaneModeObserver = new SettingObserver();
|
||||
}
|
||||
if (mTetherReceiver == null) {
|
||||
mTetherReceiver = new TetherBroadcastReceiver();
|
||||
}
|
||||
mContext.registerReceiver(
|
||||
mTetherReceiver, new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED));
|
||||
mContext.getContentResolver()
|
||||
.registerContentObserver(mAirplaneModeObserver.uri, false, mAirplaneModeObserver);
|
||||
}
|
||||
@@ -140,6 +149,9 @@ public class TetherPreferenceController extends PreferenceController
|
||||
if (mAirplaneModeObserver != null) {
|
||||
mContext.getContentResolver().unregisterContentObserver(mAirplaneModeObserver);
|
||||
}
|
||||
if (mTetherReceiver != null) {
|
||||
mContext.unregisterReceiver(mTetherReceiver);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -238,4 +250,14 @@ public class TetherPreferenceController extends PreferenceController
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
class TetherBroadcastReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
updateSummary();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -20,7 +20,10 @@ package com.android.settings.network;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothPan;
|
||||
import android.bluetooth.BluetoothProfile;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.database.ContentObserver;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.provider.Settings;
|
||||
@@ -41,6 +44,7 @@ import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -150,4 +154,36 @@ public class TetherPreferenceControllerTest {
|
||||
verify(mPreference).setSummary(R.string.switch_off_text);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onResume_shouldRegisterTetherReceiver() {
|
||||
when(mContext.getContentResolver()).thenReturn(mock(ContentResolver.class));
|
||||
|
||||
mController.onResume();
|
||||
|
||||
verify(mContext).registerReceiver(
|
||||
any(TetherPreferenceController.TetherBroadcastReceiver.class), any(IntentFilter.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPause_shouldUnregisterTetherReceiver() {
|
||||
when(mContext.getContentResolver()).thenReturn(mock(ContentResolver.class));
|
||||
mController.onResume();
|
||||
|
||||
mController.onPause();
|
||||
|
||||
verify(mContext).unregisterReceiver(
|
||||
any(TetherPreferenceController.TetherBroadcastReceiver.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tetherStatesChanged_shouldUpdateSummary() {
|
||||
final Context context = RuntimeEnvironment.application;
|
||||
ReflectionHelpers.setField(mController, "mContext", context);
|
||||
mController.onResume();
|
||||
|
||||
context.sendBroadcast(new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED));
|
||||
|
||||
verify(mController).updateSummary();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user