Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
defensive null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanosFisherman committed Oct 28, 2019
1 parent ea3aa5b commit 6a82f8f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ ext {
buildToolsVersion = '28.0.3'
minSdkVersion = 15
targetSdkVersion = 28
publishVersionName = '1.4.1'
publishVersionCode = 13
publishVersionName = '1.4.2'
publishVersionCode = 14

currentVersionDesc = 'WifiUtils current version'
bintrayRepo = 'maven'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ private static boolean checkForExcessOpenNetworkAndSave(@NonNull final ContentRe

}

private static int getMaxPriority(@NonNull final WifiManager wifiManager) {
private static int getMaxPriority(@Nullable final WifiManager wifiManager) {
if (wifiManager == null)
return 0;
final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks();
int pri = 0;
for (final WifiConfiguration config : configurations) {
Expand All @@ -76,7 +78,9 @@ private static int getMaxPriority(@NonNull final WifiManager wifiManager) {
return pri;
}

private static int shiftPriorityAndSave(@NonNull final WifiManager wifiMgr) {
private static int shiftPriorityAndSave(@Nullable final WifiManager wifiMgr) {
if (wifiMgr == null)
return 0;
final List<WifiConfiguration> configurations = wifiMgr.getConfiguredNetworks();
sortByPriority(configurations);
final int size = configurations.size();
Expand Down Expand Up @@ -159,7 +163,9 @@ static void unregisterReceiver(@NonNull Context context, @Nullable BroadcastRece
}
}

static boolean connectToWifi(@NonNull Context context, @NonNull WifiManager wifiManager, @NonNull ScanResult scanResult, @NonNull String password) {
static boolean connectToWifi(@NonNull Context context, @Nullable WifiManager wifiManager, @NonNull ScanResult scanResult, @NonNull String password) {
if (wifiManager == null)
return false;
WifiConfiguration config = ConfigSecurities.getWifiConfiguration(wifiManager, scanResult);
if (config != null && password.isEmpty()) {
wifiLog("PASSWORD WAS EMPTY. TRYING TO CONNECT TO EXISTING NETWORK CONFIGURATION");
Expand Down Expand Up @@ -199,8 +205,8 @@ static boolean connectToWifi(@NonNull Context context, @NonNull WifiManager wifi
return connectToConfiguredNetwork(wifiManager, config, true);
}

private static boolean connectToConfiguredNetwork(@NonNull WifiManager wifiManager, @Nullable WifiConfiguration config, boolean reassociate) {
if (config == null)
private static boolean connectToConfiguredNetwork(@Nullable WifiManager wifiManager, @Nullable WifiConfiguration config, boolean reassociate) {
if (config == null || wifiManager == null)
return false;

if (Build.VERSION.SDK_INT >= 23)
Expand Down Expand Up @@ -236,37 +242,49 @@ private static boolean connectToConfiguredNetwork(@NonNull WifiManager wifiManag

}

private static boolean disableAllButOne(@NonNull final WifiManager wifiManager, @Nullable final WifiConfiguration config) {
private static boolean disableAllButOne(@Nullable final WifiManager wifiManager, @Nullable final WifiConfiguration config) {
if (wifiManager == null)
return false;
@Nullable final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks();
if (configurations == null || config == null || configurations.isEmpty())
return false;
boolean result = false;

for (WifiConfiguration wifiConfig : configurations)
for (WifiConfiguration wifiConfig : configurations) {
if (wifiConfig == null)
continue;
if (wifiConfig.networkId == config.networkId)
result = wifiManager.enableNetwork(wifiConfig.networkId, true);
else
wifiManager.disableNetwork(wifiConfig.networkId);
}
wifiLog("disableAllButOne " + result);
return result;
}


@SuppressWarnings("UnusedReturnValue")
private static boolean disableAllButOne(@NonNull final WifiManager wifiManager, @Nullable final ScanResult scanResult) {
private static boolean disableAllButOne(@Nullable final WifiManager wifiManager, @Nullable final ScanResult scanResult) {
if (wifiManager == null)
return false;
@Nullable final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks();
if (configurations == null || scanResult == null || configurations.isEmpty())
return false;
boolean result = false;
for (WifiConfiguration wifiConfig : configurations)
for (WifiConfiguration wifiConfig : configurations) {
if (wifiConfig == null)
continue;
if (Objects.equals(scanResult.BSSID, wifiConfig.BSSID) && Objects.equals(scanResult.SSID, trimQuotes(wifiConfig.SSID)))
result = wifiManager.enableNetwork(wifiConfig.networkId, true);
else
wifiManager.disableNetwork(wifiConfig.networkId);
}
return result;
}

public static boolean reEnableNetworkIfPossible(@NonNull final WifiManager wifiManager, @Nullable final ScanResult scanResult) {
public static boolean reEnableNetworkIfPossible(@Nullable final WifiManager wifiManager, @Nullable final ScanResult scanResult) {
if (wifiManager == null)
return false;
@Nullable final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks();
if (configurations == null || scanResult == null || configurations.isEmpty())
return false;
Expand All @@ -281,8 +299,12 @@ public static boolean reEnableNetworkIfPossible(@NonNull final WifiManager wifiM
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
static void connectWps(@NonNull final WifiManager wifiManager, @NonNull final ScanResult scanResult, @NonNull String pin, long timeOutMillis,
static void connectWps(@Nullable final WifiManager wifiManager, @NonNull final ScanResult scanResult, @NonNull String pin, long timeOutMillis,
@NonNull final ConnectionWpsListener connectionWpsListener) {
if (wifiManager == null) {
connectionWpsListener.isSuccessful(false);
return;
}
final WeakHandler handler = new WeakHandler();
final WpsInfo wpsInfo = new WpsInfo();
final Runnable handlerTimeoutRunnable = new Runnable() {
Expand Down Expand Up @@ -351,7 +373,9 @@ public void onFailed(int reason) {
wifiManager.startWps(wpsInfo, wpsCallback);
}

static boolean cleanPreviousConfiguration(@NonNull final WifiManager wifiManager, @NonNull final ScanResult scanResult) {
static boolean cleanPreviousConfiguration(@Nullable final WifiManager wifiManager, @NonNull final ScanResult scanResult) {
if (wifiManager == null)
return false;
//On Android 6.0 (API level 23) and above if my app did not create the configuration in the first place, it can not remove it either.
final WifiConfiguration config = ConfigSecurities.getWifiConfiguration(wifiManager, scanResult);
wifiLog("Attempting to remove previous network config...");
Expand All @@ -365,8 +389,10 @@ static boolean cleanPreviousConfiguration(@NonNull final WifiManager wifiManager
return false;
}

static boolean cleanPreviousConfiguration(@NonNull final WifiManager wifiManager, @Nullable final WifiConfiguration config) {
static boolean cleanPreviousConfiguration(@Nullable final WifiManager wifiManager, @Nullable final WifiConfiguration config) {
//On Android 6.0 (API level 23) and above if my app did not create the configuration in the first place, it can not remove it either.
if (wifiManager == null)
return false;

wifiLog("Attempting to remove previous network config...");
if (config == null)
Expand All @@ -379,7 +405,9 @@ static boolean cleanPreviousConfiguration(@NonNull final WifiManager wifiManager
return false;
}

static void reenableAllHotspots(@NonNull WifiManager wifi) {
static void reenableAllHotspots(@Nullable WifiManager wifi) {
if (wifi == null)
return;
final List<WifiConfiguration> configurations = wifi.getConfiguredNetworks();
if (configurations != null && !configurations.isEmpty())
for (final WifiConfiguration config : configurations)
Expand Down

0 comments on commit 6a82f8f

Please sign in to comment.