Prevent extra reloads in mobile networking page/controllers

On the mobile network details page, as well as in several preference
controllers used in various page, we had listeners for the
ACTION_RADIO_TECHNOLOGY_CHANGED broadcast that when fired would cause a
reload. It turns out that this gets broadcast as a sticky intent, so our
callbaks would fire just after registering to listen, resulting in lots
of unnecessary extra reloading. This was particularly noticable on the
mobile network details page because the entire page gets reloaded.

The fix is to make our listeners ignore the broadcast if it's the
initial sticky one.

Bug: 126419558
Test: make RunSettingsRoboTests
Change-Id: I6ab7b43d74b07a839e45ce5368e45809be658b9d
This commit is contained in:
Antony Sargent
2019-03-04 13:21:20 -08:00
parent ca299cf281
commit ce9a126add
4 changed files with 81 additions and 16 deletions

View File

@@ -23,17 +23,20 @@ import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.net.Uri;
import android.provider.Settings;
import android.telephony.SubscriptionManager;
import com.android.internal.telephony.TelephonyIntents;
import com.android.settings.network.SubscriptionsChangeListener.SubscriptionsChangeListenerClient;
import org.junit.Before;
@@ -94,6 +97,19 @@ public class SubscriptionsChangeListenerTest {
verify(mClient).onSubscriptionsChanged();
}
@Test
public void
onSubscriptionsChangedEvent_ignoresStickyBroadcastFromBeforeRegistering() {
final Intent intent = new Intent(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
mContext.sendStickyBroadcast(intent);
initListener(true);
verify(mClient, never()).onSubscriptionsChanged();
mContext.sendStickyBroadcast(intent);
verify(mClient, times(1)).onSubscriptionsChanged();
}
@Test
public void onSubscriptionsChangedEvent_radioTechnologyChangedBroadcast_eventDeliveredToUs() {
initListener(true);

View File

@@ -26,6 +26,7 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
@@ -35,10 +36,7 @@ import android.telephony.SubscriptionManager;
import android.view.Menu;
import android.view.View;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.android.internal.telephony.TelephonyIntents;
import com.android.internal.view.menu.ContextMenuBuilder;
import com.android.settings.R;
@@ -56,6 +54,10 @@ import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.List;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
@RunWith(RobolectricTestRunner.class)
public class MobileNetworkActivityTest {
@@ -141,6 +143,25 @@ public class MobileNetworkActivityTest {
MOBILE_SETTINGS_TAG + CURRENT_SUB_ID);
}
@Test
public void phoneChangeReceiver_ignoresStickyBroadcastFromBeforeRegistering() {
Activity activity = Robolectric.setupActivity(Activity.class);
final int[] onChangeCallbackCount = {0};
MobileNetworkActivity.PhoneChangeReceiver receiver =
new MobileNetworkActivity.PhoneChangeReceiver(activity, () -> {
onChangeCallbackCount[0]++;
});
Intent intent = new Intent(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
activity.sendStickyBroadcast(intent);
receiver.register();
assertThat(onChangeCallbackCount[0]).isEqualTo(0);
activity.sendStickyBroadcast(intent);
assertThat(onChangeCallbackCount[0]).isEqualTo(1);
}
@Test
public void getSubscriptionId_hasIntent_getIdFromIntent() {
final Intent intent = new Intent();