Add ingress rate limit to developer settings

Test: make -j64 RunSettingsRoboTests
ROBOTEST_FILTER="com.android.settings.development.IngressRateLimitPreferenceControllerTest"
Bug: 157552970
Bug: 122993151

Change-Id: I0d0aa40610016c1f9e94596cfe3ed6c9a0614d89
Merged-In: I0d0aa40610016c1f9e94596cfe3ed6c9a0614d89
(cherry picked from commit b0e5e84d49)
This commit is contained in:
Patrick Rohr
2022-03-03 12:46:31 +01:00
parent 6840252c11
commit 40ebbb17cf
6 changed files with 227 additions and 0 deletions

View File

@@ -1626,4 +1626,24 @@
<item>300000</item>
</string-array>
<!-- Developer settings: ingress rate limit entries. [DO NOT TRANSLATE] -->
<string-array name="ingress_rate_limit_entries">
<item>@string/ingress_rate_limit_no_limit_entry</item>
<item>128kbps</item>
<item>256kbps</item>
<item>1Mbps</item>
<item>5Mbps</item>
<item>15Mbps</item>
</string-array>
<!-- Developer settings: ingress rate limit values. [DO NOT TRANSLATE] -->
<string-array name="ingress_rate_limit_values">
<item>-1</item> <!-- -1 codes for disabled -->
<item>16000</item> <!-- 128kbps == 16000B/s -->
<item>32000</item> <!-- 256kbps == 32000B/s -->
<item>125000</item> <!-- 1Mbps == 125000B/s -->
<item>625000</item> <!-- 5Mbps == 625000/s -->
<item>1875000</item> <!-- 15Mbps == 1875000/s -->
</string-array>
</resources>

View File

@@ -14035,4 +14035,13 @@
<string name="bluetooth_details_head_tracking_title">Make audio more realistic</string>
<!-- The summary of the head tracking [CHAR LIMIT=none] -->
<string name="bluetooth_details_head_tracking_summary">Shift positioning of audio so it sounds more natural.</string>
<!-- Developer Settings: Title for network bandwidth ingress rate limit [CHAR LIMIT=none] -->
<string name="ingress_rate_limit_title">Network download rate limit</string>
<!-- Developer Settings: Summary for network bandwidth ingress rate limit [CHAR LIMIT=none] -->
<string name="ingress_rate_limit_summary">Configure the network bandwidth ingress rate limit which is applied to all networks that provide internet connectivity.</string>
<!-- Developer Settings: Dialog for network bandwidth ingress rate limit [CHAR LIMIT=none] -->
<string name="ingress_rate_limit_dialog_title">Configure network download rate limit</string>
<!-- Developer Settings: Dialog ListPreference option to disable network bandwidth ingress rate limit [CHAR LIMIT=none] -->
<string name="ingress_rate_limit_no_limit_entry">No limit</string>
</resources>

View File

@@ -297,6 +297,14 @@
android:title="@string/tethering_hardware_offload"
android:summary="@string/tethering_hardware_offload_summary" />
<ListPreference
android:key="ingress_rate_limit"
android:title="@string/ingress_rate_limit_title"
android:summary="@string/ingress_rate_limit_summary"
android:dialogTitle="@string/ingress_rate_limit_dialog_title"
android:entries="@array/ingress_rate_limit_entries"
android:entryValues="@array/ingress_rate_limit_values" />
<com.android.settingslib.RestrictedPreference
android:key="default_usb_configuration"
android:fragment="com.android.settings.connecteddevice.usb.UsbDefaultFragment"

View File

@@ -585,6 +585,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
controllers.add(new SharedDataPreferenceController(context));
controllers.add(new OverlaySettingsPreferenceController(context));
controllers.add(new StylusHandwritingPreferenceController(context));
controllers.add(new IngressRateLimitPreferenceController((context)));
return controllers;
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.development;
import android.content.Context;
import android.net.ConnectivitySettingsManager;
import android.util.Log;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
/**
* Controller for ingress rate limit developer setting.
*/
public class IngressRateLimitPreferenceController extends DeveloperOptionsPreferenceController
implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
private static final String TAG = "IngressRateLimitPreferenceController";
private static final String INGRESS_RATE_LIMIT_KEY = "ingress_rate_limit";
private static final int RATE_LIMIT_DISABLED = -1;
public IngressRateLimitPreferenceController(Context context) {
super(context);
}
@Override
public String getPreferenceKey() {
return INGRESS_RATE_LIMIT_KEY;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final long value = Long.parseLong(newValue.toString());
try {
ConnectivitySettingsManager.setIngressRateLimitInBytesPerSecond(mContext, value);
return true;
} catch (IllegalArgumentException e) {
Log.e(TAG, "invalid rate limit", e);
return false;
}
}
@Override
public void updateState(Preference preference) {
final String ingressRateLimit = String.valueOf(
ConnectivitySettingsManager.getIngressRateLimitInBytesPerSecond(mContext));
// verify ingressRateLimit is valid / present in ListPreference; else do nothing.
final CharSequence[] entryValues = ((ListPreference) preference).getEntryValues();
for (int i = 0; i < entryValues.length; i++) {
if (ingressRateLimit.contentEquals(entryValues[i])) {
((ListPreference) preference).setValue(ingressRateLimit);
return;
}
}
}
@Override
protected void onDeveloperOptionsSwitchDisabled() {
super.onDeveloperOptionsSwitchDisabled();
// disable rate limiting when developer options are disabled
ConnectivitySettingsManager.setIngressRateLimitInBytesPerSecond(mContext,
RATE_LIMIT_DISABLED);
((ListPreference) mPreference).setValue(String.valueOf(RATE_LIMIT_DISABLED));
}
}

View File

@@ -0,0 +1,107 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.development;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.ConnectivitySettingsManager;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class IngressRateLimitPreferenceControllerTest {
private Context mContext = RuntimeEnvironment.application;
private ListPreference mPreference;
private IngressRateLimitPreferenceController mController;
@Mock
private PreferenceScreen mPreferenceScreen;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mPreference = new ListPreference(mContext);
mPreference.setEntries(R.array.ingress_rate_limit_entries);
mPreference.setEntryValues(R.array.ingress_rate_limit_values);
mController = new IngressRateLimitPreferenceController(mContext);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
mPreference);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void onPreferenceChanged_select5Mbits_shouldEnableIngressRateLimit() {
final long newRateLimit = 625000; // 5mbit == 625000 B/s
assertThat(mController.onPreferenceChange(mPreference, newRateLimit)).isTrue();
final long configuredRateLimit =
ConnectivitySettingsManager.getIngressRateLimitInBytesPerSecond(mContext);
assertThat(configuredRateLimit).isEqualTo(newRateLimit);
}
@Test
public void onPreferenceChanged_selectDisabled_shouldDisableIngressRateLimit() {
final long disabledRateLimit = -1; // -1 == disabled
assertThat(mController.onPreferenceChange(mPreference, disabledRateLimit)).isTrue();
final long configuredRateLimit =
ConnectivitySettingsManager.getIngressRateLimitInBytesPerSecond(mContext);
assertThat(configuredRateLimit).isEqualTo(disabledRateLimit);
}
@Test
public void onPreferenceChanged_invalidValue_returnsFalse() {
final long invalidRateLimit = -123;
assertThat(mController.onPreferenceChange(mPreference, invalidRateLimit)).isFalse();
}
@Test
public void updateState_preferenceShouldBeSelected() {
final long newRateLimit = 625000; // 5mbit == 625000 B/s
ConnectivitySettingsManager.setIngressRateLimitInBytesPerSecond(mContext, newRateLimit);
mController.updateState(mPreference);
assertThat(Long.parseLong(mPreference.getValue())).isEqualTo(newRateLimit);
}
@Test
public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() {
final long newRateLimit = 625000; // 5mbit == 625000 B/s
ConnectivitySettingsManager.setIngressRateLimitInBytesPerSecond(mContext, newRateLimit);
mController.updateState(mPreference);
mController.onDeveloperOptionsSwitchDisabled();
assertThat(Long.parseLong(mPreference.getValue())).isEqualTo(-1);
assertThat(ConnectivitySettingsManager.getIngressRateLimitInBytesPerSecond(
mContext)).isEqualTo(-1);
}
}