1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.development; 18 19 import android.content.Context; 20 import android.net.ConnectivitySettingsManager; 21 import android.util.Log; 22 23 import androidx.preference.ListPreference; 24 import androidx.preference.Preference; 25 26 import com.android.settings.core.PreferenceControllerMixin; 27 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 28 29 /** 30 * Controller for ingress rate limit developer setting. 31 */ 32 public class IngressRateLimitPreferenceController extends DeveloperOptionsPreferenceController 33 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { 34 private static final String TAG = "IngressRateLimitPreferenceController"; 35 private static final String INGRESS_RATE_LIMIT_KEY = "ingress_rate_limit"; 36 private static final int RATE_LIMIT_DISABLED = -1; 37 IngressRateLimitPreferenceController(Context context)38 public IngressRateLimitPreferenceController(Context context) { 39 super(context); 40 } 41 42 @Override getPreferenceKey()43 public String getPreferenceKey() { 44 return INGRESS_RATE_LIMIT_KEY; 45 } 46 47 @Override onPreferenceChange(Preference preference, Object newValue)48 public boolean onPreferenceChange(Preference preference, Object newValue) { 49 final long value = Long.parseLong(newValue.toString()); 50 try { 51 ConnectivitySettingsManager.setIngressRateLimitInBytesPerSecond(mContext, value); 52 return true; 53 } catch (IllegalArgumentException e) { 54 Log.e(TAG, "invalid rate limit", e); 55 return false; 56 } 57 } 58 59 @Override updateState(Preference preference)60 public void updateState(Preference preference) { 61 final String ingressRateLimit = String.valueOf( 62 ConnectivitySettingsManager.getIngressRateLimitInBytesPerSecond(mContext)); 63 64 // verify ingressRateLimit is valid / present in ListPreference; else do nothing. 65 final CharSequence[] entryValues = ((ListPreference) preference).getEntryValues(); 66 for (int i = 0; i < entryValues.length; i++) { 67 if (ingressRateLimit.contentEquals(entryValues[i])) { 68 ((ListPreference) preference).setValue(ingressRateLimit); 69 return; 70 } 71 } 72 } 73 74 @Override onDeveloperOptionsSwitchDisabled()75 protected void onDeveloperOptionsSwitchDisabled() { 76 super.onDeveloperOptionsSwitchDisabled(); 77 // disable rate limiting when developer options are disabled 78 ConnectivitySettingsManager.setIngressRateLimitInBytesPerSecond(mContext, 79 RATE_LIMIT_DISABLED); 80 ((ListPreference) mPreference).setValue(String.valueOf(RATE_LIMIT_DISABLED)); 81 } 82 } 83