Fix Wi-Fi level out of range crash
- If the Wi-Fi level is less than the minimum level, use the minimum level instead. - If the Wi-Fi level is greater than the maximum level, use the maximum level instead. Bug: 230457323 Test: manual test make RunSettingsLibRoboTests ROBOTEST_FILTER=WifiUtilsTest Change-Id: I7ec748ac200a7930ea08604ecf9cece6fcbfbb86
This commit is contained in:
@@ -29,6 +29,7 @@ import android.net.wifi.WifiConfiguration.NetworkSelectionStatus;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.os.Bundle;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
@@ -40,6 +41,8 @@ import java.util.Map;
|
||||
|
||||
public class WifiUtils {
|
||||
|
||||
private static final String TAG = "WifiUtils";
|
||||
|
||||
private static final int INVALID_RSSI = -127;
|
||||
|
||||
/**
|
||||
@@ -314,13 +317,17 @@ public class WifiUtils {
|
||||
*
|
||||
* @param level The number of bars to show (0-4)
|
||||
* @param noInternet True if a connected Wi-Fi network cannot access the Internet
|
||||
* @throws IllegalArgumentException if an invalid RSSI level is given.
|
||||
*/
|
||||
public static int getInternetIconResource(int level, boolean noInternet) {
|
||||
if (level < 0 || level >= WIFI_PIE.length) {
|
||||
throw new IllegalArgumentException("No Wifi icon found for level: " + level);
|
||||
int wifiLevel = level;
|
||||
if (wifiLevel < 0) {
|
||||
Log.e(TAG, "Wi-Fi level is out of range! level:" + level);
|
||||
wifiLevel = 0;
|
||||
} else if (level >= WIFI_PIE.length) {
|
||||
Log.e(TAG, "Wi-Fi level is out of range! level:" + level);
|
||||
wifiLevel = WIFI_PIE.length - 1;
|
||||
}
|
||||
return noInternet ? NO_INTERNET_WIFI_PIE[level] : WIFI_PIE[level];
|
||||
return noInternet ? NO_INTERNET_WIFI_PIE[wifiLevel] : WIFI_PIE[wifiLevel];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -187,6 +187,19 @@ public class WifiUtilsTest {
|
||||
assertThat(bundle.getString(WifiUtils.KEY_CHOSEN_WIFIENTRY_KEY)).isEqualTo(key);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getInternetIconResource_levelOutOfRange_shouldNotCrash() {
|
||||
// Verify that Wi-Fi level is less than the minimum level (0)
|
||||
int level = -1;
|
||||
WifiUtils.getInternetIconResource(level, false /* noInternet*/);
|
||||
WifiUtils.getInternetIconResource(level, true /* noInternet*/);
|
||||
|
||||
// Verify that Wi-Fi level is greater than the maximum level (4)
|
||||
level = WifiUtils.WIFI_PIE.length;
|
||||
WifiUtils.getInternetIconResource(level, false /* noInternet*/);
|
||||
WifiUtils.getInternetIconResource(level, true /* noInternet*/);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInternetIconInjector_getIcon_returnsCorrectValues() {
|
||||
WifiUtils.InternetIconInjector iconInjector = new WifiUtils.InternetIconInjector(mContext);
|
||||
|
||||
Reference in New Issue
Block a user