Skip to content

Commit

Permalink
use wifi manager for API version < 31
Browse files Browse the repository at this point in the history
looks like the NetworkCapabilities object given in the
callback before SDK 31 does not return its TransportInfo
  • Loading branch information
ostrya committed Sep 4, 2024
1 parent ecda061 commit 16e389d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public static ConnectivityManager.NetworkCallback getWifiCallback(Context contex
WifiEventConsumer consumer = new WifiEventConsumer(applicationContext);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
return new WifiCallback(consumer);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
return new WifiCallbackApi29(consumer);
} else {
WifiManager wifiManager =
(WifiManager) applicationContext.getSystemService(WIFI_SERVICE);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
import androidx.annotation.RequiresApi;
import androidx.core.util.Function;

import org.ostrya.presencepublisher.log.DatabaseLogger;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

@RequiresApi(api = Build.VERSION_CODES.M)
public class WifiNetworkHelper {
private final String TAG = "WifiNetworkHelper";
private final Map<Long, Optional<String>> ssidByNetworkHandle = new ConcurrentHashMap<>();
private final WifiEventConsumer consumer;
private final Function<NetworkCapabilities, Optional<WifiInfo>> wifiResolver;
Expand All @@ -28,20 +31,31 @@ public WifiNetworkHelper(

public void handleAvailableNetwork(
@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
long networkHandle = network.getNetworkHandle();
Optional<String> current = NetworkService.getSsid(wifiResolver.apply(networkCapabilities));
Optional<String> previous =
ssidByNetworkHandle.putIfAbsent(network.getNetworkHandle(), current);
Optional<String> previous = ssidByNetworkHandle.putIfAbsent(networkHandle, current);
// absent means not a Wi-Fi network, null means (previously) unknown
//noinspection OptionalAssignedToNull
if (previous == null && current.isPresent()) {
consumer.wifiConnected(current.get());
if (previous == null) {
if (current.isPresent()) {
DatabaseLogger.d(TAG, "Connected to Wifi network " + networkHandle);
consumer.wifiConnected(current.get());
} else {
DatabaseLogger.d(TAG, "Connected to non-Wifi network " + networkHandle);
}
}
}

public void handleLostNetwork(@NonNull Network network) {
Optional<String> removed = ssidByNetworkHandle.remove(network.getNetworkHandle());
if (removed != null && removed.isPresent()) {
consumer.wifiDisconnected(removed.get());
long networkHandle = network.getNetworkHandle();
Optional<String> removed = ssidByNetworkHandle.remove(networkHandle);
if (removed != null) {
if (removed.isPresent()) {
DatabaseLogger.d(TAG, "Lost Wifi network " + networkHandle);
consumer.wifiDisconnected(removed.get());
} else {
DatabaseLogger.d(TAG, "Lost non-Wifi network " + networkHandle);
}
}
}
}

0 comments on commit 16e389d

Please sign in to comment.