This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
/
connectivity_linux.dart
214 lines (181 loc) · 7.85 KB
/
connectivity_linux.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import 'dart:async';
import 'package:connectivity_platform_interface/connectivity_platform_interface.dart';
import 'package:dbus/dbus.dart';
import 'package:flutter/services.dart';
/// The Linux implementation of the ConnectivityPlatform of the Connectivity plugin.
class ConnectivityLinux extends ConnectivityPlatform {
ConnectivityLinux() : _networkManager = NetworkManager();
final NetworkManager _networkManager;
/// Factory method that initializes the connectivity plugin platform with an instance
/// of the plugin for Linux.
static void register() {
ConnectivityPlatform.instance = ConnectivityLinux();
}
@override
Future<ConnectivityResult> checkConnectivity() async {
final NetworkManagerState state = await _networkManager.state();
return _stateToResult(state);
}
@override
Stream<ConnectivityResult> get onConnectivityChanged {
return _networkManager.stateChanged.map(_stateToResult);
}
// Creates an "unsupported_operation" PlatformException for a given `method` name.
Exception _unsupported(String method) {
return PlatformException(
code: 'UNSUPPORTED_OPERATION',
message: '$method() is not supported on Linux.',
);
}
/// Obtains the wifi name (SSID) of the connected network
@override
Future<String> getWifiName() {
throw _unsupported('getWifiName');
}
/// Obtains the wifi BSSID of the connected network.
@override
Future<String> getWifiBSSID() {
throw _unsupported('getWifiBSSID');
}
/// Obtains the IP address of the connected wifi network
@override
Future<String> getWifiIP() {
throw _unsupported('getWifiIP');
}
/// Request to authorize the location service (Only on iOS).
@override
Future<LocationAuthorizationStatus> requestLocationServiceAuthorization({
bool requestAlwaysLocationUsage = false,
}) {
throw _unsupported('requestLocationServiceAuthorization');
}
/// Get the current location service authorization (Only on iOS).
@override
Future<LocationAuthorizationStatus> getLocationServiceAuthorization() {
throw _unsupported('getLocationServiceAuthorization');
}
ConnectivityResult _stateToResult(NetworkManagerState state) {
return state == NetworkManagerState.connectedGlobal || state == NetworkManagerState.unknown
? ConnectivityResult.wifi
: ConnectivityResult.none;
}
}
/// Interface over DBus for NetworkManager.
///
/// This exposes the state() method and the `StateChanged` signal.
/// see [NetworkManager](https://developer.gnome.org/NetworkManager/stable/gdbus-org.freedesktop.NetworkManager.html)
class NetworkManager {
NetworkManager() : _client = DBusClient.system() {
_stateController = StreamController<NetworkManagerState>.broadcast(
onListen: () async {
await _stateController.addStream(state().asStream());
_stateSubscription = await _client.subscribeSignals(
_onData,
path: DBusObjectPath('/org/freedesktop/NetworkManager'),
sender: 'org.freedesktop.NetworkManager',
interface: 'org.freedesktop.NetworkManager',
member: 'StateChanged',
);
},
onCancel: () {
_client.unsubscribeSignals(_stateSubscription);
_stateSubscription = null;
},
);
}
final DBusClient _client;
StreamController<NetworkManagerState> _stateController;
DBusSignalSubscription _stateSubscription;
/// The overall networking state as determined by the NetworkManager daemon,
/// based on the state of network devices under its management.
///
/// [state()](https://developer.gnome.org/NetworkManager/stable/gdbus-org.freedesktop.NetworkManager.html#gdbus-method-org-freedesktop-NetworkManager.state)
Future<NetworkManagerState> state() async {
final DBusMethodResponse result = await _client.callMethod(
path: DBusObjectPath('/org/freedesktop/NetworkManager'),
destination: 'org.freedesktop.NetworkManager',
interface: 'org.freedesktop.NetworkManager',
member: 'state',
);
if (result is DBusMethodErrorResponse) {
return Future<NetworkManagerState>.error(
StateError('${result.errorName}, ${result.values.join(',')}'), StackTrace.current);
}
final DBusUint32 rawValue = result.returnValues.first;
return NetworkManagerState.valueOf(rawValue.value);
}
/// Emits the the NetworkManage's state when ever it changes.
///
/// [StateChanged](https://developer.gnome.org/NetworkManager/stable/gdbus-org.freedesktop.NetworkManager.html#gdbus-signal-org-freedesktop-NetworkManager.StateChanged)
Stream<NetworkManagerState> get stateChanged => _stateController.stream;
void _onData(DBusObjectPath path, String interface, String member, List<DBusValue> values) {
if (values.length == 1 && values[0].signature == const DBusSignature('u')) {
final DBusUint32 rawValue = values.first;
_stateController.add(NetworkManagerState.valueOf(rawValue.value));
}
}
}
/// Values that indicate the current overall network state.
class NetworkManagerState {
const NetworkManagerState._(this._i, this._value);
final int _i;
final int _value;
/// Networking state is unknown.
///
/// This indicates a daemon error that makes it unable to reasonably assess the state.
/// In such event the applications are expected to assume Internet connectivity might
/// be present and not disable controls that require network access. The graphical shells
/// may hide the network accessibility indicator altogether since no meaningful status
/// indication can be provided.
static const NetworkManagerState unknown = NetworkManagerState._(0, 0);
/// Networking is not enabled, the system is being suspended or resumed from suspend.
static const NetworkManagerState asleep = NetworkManagerState._(1, 10);
/// There is no active network connection. The graphical shell should indicate no
/// network connectivity and the applications should not attempt to access the network.
static const NetworkManagerState disconnected = NetworkManagerState._(2, 20);
/// Network connections are being cleaned up. The applications should tear down their
/// network sessions.
static const NetworkManagerState disconnecting = NetworkManagerState._(3, 30);
/// A network connection is being started The graphical shell should indicate the
/// network is being connected while the applications should still make no attempts
/// to connect the network.
static const NetworkManagerState connecting = NetworkManagerState._(4, 40);
/// There is only local IPv4 and/or IPv6 connectivity, but no default route to access
/// the Internet. The graphical shell should indicate no network connectivity.
static const NetworkManagerState connectedLocal = NetworkManagerState._(5, 50);
/// There is only site-wide IPv4 and/or IPv6 connectivity. This means a default route
/// is available, but the Internet connectivity check did not succeed. The graphical
/// shell should indicate limited network connectivity.
static const NetworkManagerState connectedSite = NetworkManagerState._(6, 60);
/// There is global IPv4 and/or IPv6 Internet connectivity This means the Internet
/// connectivity check succeeded, the graphical shell should indicate full network
/// connectivity.
static const NetworkManagerState connectedGlobal = NetworkManagerState._(7, 70);
static const List<NetworkManagerState> values = <NetworkManagerState>[
unknown,
asleep,
disconnected,
disconnecting,
connecting,
connectedLocal,
connectedSite,
connectedGlobal,
];
static const List<String> _names = <String>[
'unknown',
'asleep',
'disconnected',
'disconnecting',
'connecting',
'connectedLocal',
'connectedSite',
'connectedGlobal',
];
static NetworkManagerState valueOf(int value) {
return values.firstWhere((NetworkManagerState state) => state._value == value);
}
@override
String toString() {
return 'NetworkManagerState.${_names[_i]}';
}
}