Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 77abe668 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Fix Wi-Fi level out of range crash" into tm-dev am: 8146cd00

parents 00cf40a4 8146cd00
Loading
Loading
Loading
Loading
+11 −4
Original line number Diff line number Diff line
@@ -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);
        }
        return noInternet ? NO_INTERNET_WIFI_PIE[level] : WIFI_PIE[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[wifiLevel] : WIFI_PIE[wifiLevel];
    }

    /**
+13 −0
Original line number Diff line number Diff line
@@ -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);