Fix 'Advanced' collapse point on Network & internet page

On the Network & internet page, we want to collapse all the prefs into
'Advanced' that appear after 'Data Saver'. The mechanism for specifying
where the collapse point starts is just a static count, and it doesn't
really understand the concept of dynamically added preferences like the
ones we add at the top when the device is in DSDS mode. To fix this in
the short term, this CL makes the header that manages these prefs
manually adjust the count as needed. In the future we'd like to have a
better mechanism for this added in the support library.

Fixes: 128855968
Test: make RunSettingsRoboTests
Change-Id: I4509726ff29bc71e1f0b3d4a2f60dffe4b1dd7ac
This commit is contained in:
Antony Sargent
2019-04-04 10:10:31 -07:00
parent 44427259e8
commit 24aa3b3504
5 changed files with 55 additions and 4 deletions

View File

@@ -18,7 +18,8 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:key="network_and_internet_screen"
android:title="@string/network_dashboard_title">
android:title="@string/network_dashboard_title"
settings:initialExpandedChildrenCount="6">
<PreferenceCategory
android:key="multi_network_header"

View File

@@ -38,6 +38,8 @@ public class MultiNetworkHeaderController extends BasePreferenceController imple
private WifiConnectionPreferenceController mWifiController;
private SubscriptionsPreferenceController mSubscriptionsController;
private PreferenceCategory mPreferenceCategory;
private PreferenceScreen mPreferenceScreen;
private int mOriginalExpandedChildrenCount;
public MultiNetworkHeaderController(Context context, String key) {
super(context, key);
@@ -65,6 +67,8 @@ public class MultiNetworkHeaderController extends BasePreferenceController imple
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreferenceScreen = screen;
mOriginalExpandedChildrenCount = mPreferenceScreen.getInitialExpandedChildrenCount();
mPreferenceCategory = screen.findPreference(mPreferenceKey);
mPreferenceCategory.setVisible(isAvailable());
mWifiController.displayPreference(screen);
@@ -82,6 +86,18 @@ public class MultiNetworkHeaderController extends BasePreferenceController imple
@Override
public void onChildrenUpdated() {
mPreferenceCategory.setVisible(isAvailable());
final boolean available = isAvailable();
// TODO(b/129893781) we need a better way to express where the advanced collapsing starts
// for preference groups that have items dynamically added/removed in the top expanded
// section.
if (mOriginalExpandedChildrenCount != Integer.MAX_VALUE) {
if (available) {
mPreferenceScreen.setInitialExpandedChildrenCount(
mOriginalExpandedChildrenCount + mPreferenceCategory.getPreferenceCount());
} else {
mPreferenceScreen.setInitialExpandedChildrenCount(mOriginalExpandedChildrenCount);
}
}
mPreferenceCategory.setVisible(available);
}
}

View File

@@ -116,7 +116,7 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
return;
}
if (mSubscriptionsListener.isAirplaneModeOn()) {
if (!isAvailable()) {
for (Preference pref : mSubscriptionPreferences.values()) {
mPreferenceGroup.removePreference(pref);
}

View File

@@ -18,6 +18,7 @@ package com.android.settings.network;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doReturn;
@@ -50,6 +51,7 @@ import androidx.preference.PreferenceScreen;
@RunWith(RobolectricTestRunner.class)
public class MultiNetworkHeaderControllerTest {
private static final String KEY_HEADER = "multi_network_header";
private static final int EXPANDED_CHILDREN_COUNT = 5;
@Mock
private PreferenceScreen mPreferenceScreen;
@@ -75,6 +77,9 @@ public class MultiNetworkHeaderControllerTest {
mLifecycle = new Lifecycle(mLifecycleOwner);
when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
when(mPreferenceScreen.findPreference(eq(KEY_HEADER))).thenReturn(mPreferenceCategory);
when(mPreferenceCategory.getPreferenceCount()).thenReturn(3);
when(mPreferenceScreen.getInitialExpandedChildrenCount()).thenReturn(
EXPANDED_CHILDREN_COUNT);
mHeaderController = spy(new MultiNetworkHeaderController(mContext, KEY_HEADER));
doReturn(mWifiController).when(mHeaderController).createWifiController(mLifecycle);
@@ -133,6 +138,11 @@ public class MultiNetworkHeaderControllerTest {
verify(mPreferenceCategory, atLeastOnce()).setVisible(captor.capture());
List<Boolean> values = captor.getAllValues();
assertThat(values.get(values.size()-1)).isEqualTo(Boolean.TRUE);
ArgumentCaptor<Integer> expandedCountCaptor = ArgumentCaptor.forClass(Integer.class);
verify(mPreferenceScreen).setInitialExpandedChildrenCount(expandedCountCaptor.capture());
assertThat(expandedCountCaptor.getValue()).isEqualTo(
EXPANDED_CHILDREN_COUNT + mPreferenceCategory.getPreferenceCount());
}
@Test
@@ -148,5 +158,23 @@ public class MultiNetworkHeaderControllerTest {
verify(mPreferenceCategory, atLeastOnce()).setVisible(captor.capture());
List<Boolean> values = captor.getAllValues();
assertThat(values.get(values.size()-1)).isEqualTo(Boolean.FALSE);
ArgumentCaptor<Integer> expandedCountCaptor = ArgumentCaptor.forClass(Integer.class);
verify(mPreferenceScreen).setInitialExpandedChildrenCount(expandedCountCaptor.capture());
assertThat(expandedCountCaptor.getValue()).isEqualTo(EXPANDED_CHILDREN_COUNT);
}
@Test
public void onChildUpdated_noExpandedChildCountAndAvailable_doesNotSetExpandedCount() {
when(mPreferenceScreen.getInitialExpandedChildrenCount()).thenReturn(Integer.MAX_VALUE);
when(mSubscriptionsController.isAvailable()).thenReturn(false);
mHeaderController.init(mLifecycle);
mHeaderController.displayPreference(mPreferenceScreen);
when(mSubscriptionsController.isAvailable()).thenReturn(true);
mHeaderController.onChildrenUpdated();
verify(mPreferenceScreen, never()).setInitialExpandedChildrenCount(anyInt());
}
}

View File

@@ -20,6 +20,7 @@ import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
@@ -186,19 +187,24 @@ public class SubscriptionsPreferenceControllerTest {
}
@Test
public void onSubscriptionsChanged_countBecameOne_eventFired() {
public void onSubscriptionsChanged_countBecameOne_eventFiredAndPrefsRemoved() {
final SubscriptionInfo sub1 = mock(SubscriptionInfo.class);
final SubscriptionInfo sub2 = mock(SubscriptionInfo.class);
when(sub1.getSubscriptionId()).thenReturn(1);
when(sub2.getSubscriptionId()).thenReturn(2);
SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(sub1, sub2));
mController.onResume();
mController.displayPreference(mScreen);
assertThat(mController.isAvailable()).isTrue();
verify(mPreferenceCategory, times(2)).addPreference(any(Preference.class));
final int updateCountBeforeSubscriptionChange = mOnChildUpdatedCount;
SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(sub1));
mController.onSubscriptionsChanged();
assertThat(mController.isAvailable()).isFalse();
assertThat(mOnChildUpdatedCount).isEqualTo(updateCountBeforeSubscriptionChange + 1);
verify(mPreferenceCategory, times(2)).removePreference(any(Preference.class));
}