Skip to content

Commit ff7838d

Browse files
[connectivity_platform_interface] Move here as much code as possible from the core package (flutter#2526)
* Bring ConnectivityResult and LocationAuthorizationStatus enums from core package. * Use the above Enums as return values for ConnectivityPlatformInterface methods. * Modify the MethodChannel implementation so it returns the right types. * Bring utility methods, asserts and other logic that is only needed on the MethodChannel implementation from the core package, so it's simpler. * Bring MethodChannel unit tests from core package. Co-authored-by: David Iglesias <[email protected]>
1 parent 1edaccb commit ff7838d

File tree

8 files changed

+270
-134
lines changed

8 files changed

+270
-134
lines changed

packages/connectivity/connectivity_platform_interface/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 1.0.2
2+
3+
* Bring ConnectivityResult and LocationAuthorizationStatus enums from the core package.
4+
* Use the above Enums as return values for ConnectivityPlatformInterface methods.
5+
* Modify the MethodChannel implementation so it returns the right types.
6+
* Bring all utility methods, asserts and other logic that is only needed on the MethodChannel implementation from the core package.
7+
* Bring MethodChannel unit tests from core package.
8+
19
## 1.0.1
210

311
* Fix README.md link.

packages/connectivity/connectivity_platform_interface/lib/connectivity_platform_interface.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import 'dart:async';
66

77
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
88

9-
import 'method_channel_connectivity.dart';
9+
import 'src/enums.dart';
10+
import 'src/method_channel_connectivity.dart';
11+
12+
export 'src/enums.dart';
1013

1114
/// The interface that implementations of connectivity must implement.
1215
///
@@ -36,10 +39,16 @@ abstract class ConnectivityPlatform extends PlatformInterface {
3639
}
3740

3841
/// Checks the connection status of the device.
39-
Future<String> checkConnectivity() {
42+
Future<ConnectivityResult> checkConnectivity() {
4043
throw UnimplementedError('checkConnectivity() has not been implemented.');
4144
}
4245

46+
/// Returns a Stream of ConnectivityResults changes.
47+
Stream<ConnectivityResult> get onConnectivityChanged {
48+
throw UnimplementedError(
49+
'get onConnectivityChanged has not been implemented.');
50+
}
51+
4352
/// Obtains the wifi name (SSID) of the connected network
4453
Future<String> getWifiName() {
4554
throw UnimplementedError('getWifiName() has not been implemented.');
@@ -56,14 +65,14 @@ abstract class ConnectivityPlatform extends PlatformInterface {
5665
}
5766

5867
/// Request to authorize the location service (Only on iOS).
59-
Future<String> requestLocationServiceAuthorization(
68+
Future<LocationAuthorizationStatus> requestLocationServiceAuthorization(
6069
{bool requestAlwaysLocationUsage = false}) {
6170
throw UnimplementedError(
6271
'requestLocationServiceAuthorization() has not been implemented.');
6372
}
6473

6574
/// Get the current location service authorization (Only on iOS).
66-
Future<String> getLocationServiceAuthorization() {
75+
Future<LocationAuthorizationStatus> getLocationServiceAuthorization() {
6776
throw UnimplementedError(
6877
'getLocationServiceAuthorization() has not been implemented.');
6978
}

packages/connectivity/connectivity_platform_interface/lib/method_channel_connectivity.dart

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/// Connection status check result.
2+
enum ConnectivityResult {
3+
/// WiFi: Device connected via Wi-Fi
4+
wifi,
5+
6+
/// Mobile: Device connected to cellular network
7+
mobile,
8+
9+
/// None: Device not connected to any network
10+
none
11+
}
12+
13+
/// The status of the location service authorization.
14+
enum LocationAuthorizationStatus {
15+
/// The authorization of the location service is not determined.
16+
notDetermined,
17+
18+
/// This app is not authorized to use location.
19+
restricted,
20+
21+
/// User explicitly denied the location service.
22+
denied,
23+
24+
/// User authorized the app to access the location at any time.
25+
authorizedAlways,
26+
27+
/// User authorized the app to access the location when the app is visible to them.
28+
authorizedWhenInUse,
29+
30+
/// Status unknown.
31+
unknown
32+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2020 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
import 'dart:io' show Platform;
7+
8+
import 'package:connectivity_platform_interface/connectivity_platform_interface.dart';
9+
import 'package:flutter/services.dart';
10+
import 'package:meta/meta.dart';
11+
12+
import 'utils.dart';
13+
14+
/// An implementation of [ConnectivityPlatform] that uses method channels.
15+
class MethodChannelConnectivity extends ConnectivityPlatform {
16+
/// The method channel used to interact with the native platform.
17+
@visibleForTesting
18+
MethodChannel methodChannel =
19+
MethodChannel('plugins.flutter.io/connectivity');
20+
21+
/// The event channel used to receive ConnectivityResult changes from the native platform.
22+
@visibleForTesting
23+
EventChannel eventChannel =
24+
EventChannel('plugins.flutter.io/connectivity_status');
25+
26+
Stream<ConnectivityResult> _onConnectivityChanged;
27+
28+
/// Fires whenever the connectivity state changes.
29+
Stream<ConnectivityResult> get onConnectivityChanged {
30+
if (_onConnectivityChanged == null) {
31+
_onConnectivityChanged = eventChannel
32+
.receiveBroadcastStream()
33+
.map((dynamic result) => result.toString())
34+
.map(parseConnectivityResult);
35+
}
36+
return _onConnectivityChanged;
37+
}
38+
39+
@override
40+
Future<ConnectivityResult> checkConnectivity() {
41+
return methodChannel
42+
.invokeMethod<String>('check')
43+
.then(parseConnectivityResult);
44+
}
45+
46+
@override
47+
Future<String> getWifiName() async {
48+
String wifiName = await methodChannel.invokeMethod<String>('wifiName');
49+
// as Android might return <unknown ssid>, uniforming result
50+
// our iOS implementation will return null
51+
if (wifiName == '<unknown ssid>') {
52+
wifiName = null;
53+
}
54+
return wifiName;
55+
}
56+
57+
@override
58+
Future<String> getWifiBSSID() {
59+
return methodChannel.invokeMethod<String>('wifiBSSID');
60+
}
61+
62+
@override
63+
Future<String> getWifiIP() {
64+
return methodChannel.invokeMethod<String>('wifiIPAddress');
65+
}
66+
67+
@override
68+
Future<LocationAuthorizationStatus> requestLocationServiceAuthorization({
69+
bool requestAlwaysLocationUsage = false,
70+
}) {
71+
// `assert(Platform.isIOS)` will prevent us from doing dart side unit testing.
72+
// TODO: These should noop for non-Android, instead of throwing, so people don't need to rely on dart:io for this.
73+
assert(!Platform.isAndroid);
74+
return methodChannel.invokeMethod<String>(
75+
'requestLocationServiceAuthorization', <bool>[
76+
requestAlwaysLocationUsage
77+
]).then(parseLocationAuthorizationStatus);
78+
}
79+
80+
@override
81+
Future<LocationAuthorizationStatus> getLocationServiceAuthorization() {
82+
// `assert(Platform.isIOS)` will prevent us from doing dart side unit testing.
83+
assert(!Platform.isAndroid);
84+
return methodChannel
85+
.invokeMethod<String>('getLocationServiceAuthorization')
86+
.then(parseLocationAuthorizationStatus);
87+
}
88+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'package:connectivity_platform_interface/connectivity_platform_interface.dart';
2+
3+
/// Convert a String to a ConnectivityResult value.
4+
ConnectivityResult parseConnectivityResult(String state) {
5+
switch (state) {
6+
case 'wifi':
7+
return ConnectivityResult.wifi;
8+
case 'mobile':
9+
return ConnectivityResult.mobile;
10+
case 'none':
11+
default:
12+
return ConnectivityResult.none;
13+
}
14+
}
15+
16+
/// Convert a String to a LocationAuthorizationStatus value.
17+
LocationAuthorizationStatus parseLocationAuthorizationStatus(String result) {
18+
switch (result) {
19+
case 'notDetermined':
20+
return LocationAuthorizationStatus.notDetermined;
21+
case 'restricted':
22+
return LocationAuthorizationStatus.restricted;
23+
case 'denied':
24+
return LocationAuthorizationStatus.denied;
25+
case 'authorizedAlways':
26+
return LocationAuthorizationStatus.authorizedAlways;
27+
case 'authorizedWhenInUse':
28+
return LocationAuthorizationStatus.authorizedWhenInUse;
29+
default:
30+
return LocationAuthorizationStatus.unknown;
31+
}
32+
}

packages/connectivity/connectivity_platform_interface/pubspec.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: A common platform interface for the connectivity plugin.
33
homepage: https://github.com/flutter/plugins/tree/master/packages/connectivity/connectivity_platform_interface
44
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
55
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
6-
version: 1.0.1
6+
version: 1.0.2
77

88
dependencies:
99
flutter:
@@ -14,7 +14,6 @@ dependencies:
1414
dev_dependencies:
1515
flutter_test:
1616
sdk: flutter
17-
mockito: ^4.1.1
1817

1918
environment:
2019
sdk: ">=2.0.0-dev.28.0 <3.0.0"

0 commit comments

Comments
 (0)