Merge "Fix Wi-Fi level out of range crash" into tm-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
8146cd009a
@@ -29,6 +29,7 @@ import android.net.wifi.WifiConfiguration.NetworkSelectionStatus;
|
|||||||
import android.net.wifi.WifiInfo;
|
import android.net.wifi.WifiInfo;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
|
||||||
@@ -40,6 +41,8 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class WifiUtils {
|
public class WifiUtils {
|
||||||
|
|
||||||
|
private static final String TAG = "WifiUtils";
|
||||||
|
|
||||||
private static final int INVALID_RSSI = -127;
|
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 level The number of bars to show (0-4)
|
||||||
* @param noInternet True if a connected Wi-Fi network cannot access the Internet
|
* @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) {
|
public static int getInternetIconResource(int level, boolean noInternet) {
|
||||||
if (level < 0 || level >= WIFI_PIE.length) {
|
int wifiLevel = level;
|
||||||
throw new IllegalArgumentException("No Wifi icon found for level: " + 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);
|
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
|
@Test
|
||||||
public void testInternetIconInjector_getIcon_returnsCorrectValues() {
|
public void testInternetIconInjector_getIcon_returnsCorrectValues() {
|
||||||
WifiUtils.InternetIconInjector iconInjector = new WifiUtils.InternetIconInjector(mContext);
|
WifiUtils.InternetIconInjector iconInjector = new WifiUtils.InternetIconInjector(mContext);
|
||||||
|
|||||||
Reference in New Issue
Block a user