Split Lan and Internet connected callbacks.

PiperOrigin-RevId: 816845853
This commit is contained in:
Francis Tsui
2025-10-08 13:51:51 -07:00
committed by Copybara-Service
parent 2143967802
commit 382f607d3d
20 changed files with 287 additions and 470 deletions
+12 -27
View File
@@ -15,36 +15,23 @@
#ifndef THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_NETWORK_MONITOR_H_
#define THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_NETWORK_MONITOR_H_
#include <functional>
#include <string>
#include <utility>
#include "absl/functional/any_invocable.h"
namespace nearby {
namespace api {
class NetworkMonitor {
public:
enum class ConnectionType : int {
kUnknown = 0, // A connection exists, but its type is unknown.
// Also used as a default value.
kEthernet = 1,
kWifi = 2,
k2G = 3,
k3G = 4,
k4G = 5,
kNone = 6, // No connection.
kBluetooth = 7,
k5G = 8,
kLast = k5G
};
// Registers a callback for connection changes. The callback will be called
// with the current connection type, whether the device is connected to a
// LAN network and whether the device is connected to internet.
explicit NetworkMonitor(
std::function<void(ConnectionType, bool, bool)> callback) {
callback_ = std::move(callback);
}
// Registers callbacks for connection changes. The callbacks will be called
// when device is connected to a LAN network or when the device is connected
// to internet.
NetworkMonitor(
absl::AnyInvocable<void(bool)> lan_connected_callback,
absl::AnyInvocable<void(bool)> internet_connected_callback)
: lan_connected_callback_(std::move(lan_connected_callback)),
internet_connected_callback_(std::move(internet_connected_callback)){}
virtual ~NetworkMonitor() = default;
@@ -55,11 +42,9 @@ class NetworkMonitor {
// Returns true if connected to internet.
virtual bool IsInternetConnected() = 0;
// Returns the type of connection used currently to access the internet
virtual ConnectionType GetCurrentConnection() = 0;
protected:
std::function<void(ConnectionType, bool, bool)> callback_;
absl::AnyInvocable<void(bool)> lan_connected_callback_;
absl::AnyInvocable<void(bool)> internet_connected_callback_;
};
} // namespace api