Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d871521
flutter create -t plugin connectivity_web
ditman Feb 20, 2020
f1c9cec
Remove default example
ditman Feb 20, 2020
025ffc4
Docs
ditman Feb 20, 2020
be7cbbd
Script that auto-generates the Dart facade from TS.
ditman Feb 21, 2020
f2c80ce
Facade as it comes from the generator script (has bugs)
ditman Feb 21, 2020
6b20c50
Tweaks to the generated API
ditman Feb 21, 2020
3e20200
Initial web version of the plugin.
ditman Feb 21, 2020
14e5f9c
Modify core example to run on web.
ditman Feb 21, 2020
a4a2f62
Update facade with output from latest version of the script.
ditman Feb 21, 2020
3776379
Overridable script.
ditman Feb 21, 2020
35706db
Tests WIP
ditman Feb 24, 2020
25bdff9
Update facade generation script.
ditman Feb 24, 2020
1e05c00
Remove autogenerated file, and add gitignore.
ditman Feb 25, 2020
61edefc
Make the NetworkInformation object used by the plugin overridable (fo…
ditman Feb 25, 2020
6720cc9
Another go at e2e tests.
ditman Feb 25, 2020
c050e7c
Update instructions to run tests.
ditman Feb 25, 2020
a64d700
Fix analyzer
ditman Feb 25, 2020
44b64d8
Revert "Modify core example to run on web."
ditman Feb 25, 2020
5e94fcd
Fix publishable errors:
ditman Feb 25, 2020
2900d38
Fix dart:js -> package:js import.
ditman Feb 25, 2020
af042d0
Fix publishability of the package.
ditman Feb 25, 2020
a80c2a0
Call the named constructor from the default one properly.
ditman Feb 25, 2020
de4e9dc
Address PR feedback.
ditman Feb 26, 2020
442852f
Return `null` in unsupported browsers.
ditman Mar 4, 2020
bd56ea1
Preparing to publish from my flutter fork.
ditman Apr 9, 2020
fe645cd
Some more renaming
ditman Apr 9, 2020
4cc6238
Final renaming?
ditman Apr 9, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
.dart_tool/

.packages
.pub/

build/
lib/generated_plugin_registrant.dart
10 changes: 10 additions & 0 deletions packages/connectivity/experimental_connectivity_web/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: 52ee8a6c6565cd421dfa32042941eb40691f4746
channel: master

project_type: plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.1.0

* Initial release.
26 changes: 26 additions & 0 deletions packages/connectivity/experimental_connectivity_web/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright 2016, the Flutter project authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 changes: 57 additions & 0 deletions packages/connectivity/experimental_connectivity_web/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# experimental_connectivity_web

A web implementation of [connectivity](https://pub.dev/connectivity/connectivity). Currently this package uses an experimental API, so not all browsers that Flutter web supports are supported.

## Usage

### Import the package

This package is a non-endorsed implementation of `connectivity` for the web platform, so you need to modify your `pubspec.yaml` to use it:

```yaml
...
dependencies:
...
connectivity: ^0.4.9
experimental_connectivity_web: ^0.1.0
...
...
```

## Example

Find the example wiring in the [Google sign-in example application](https://github.com/flutter/plugins/blob/master/packages/connectivity/connectivity/example/lib/main.dart).

## Limitations on the web platform

The web implementation of the `connectivity` plugin uses the Browser's [**NetworkInformation** Web API](https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation), which as of this writing (February 2020) is still "experimental".

![Data on support for the netinfo feature across the major browsers from caniuse.com](https://caniuse.bitsofco.de/image/netinfo.png)

On desktop browsers, the API only returns a very broad set of connectivity statuses (One of `'slow-2g', '2g', '3g', or '4g'`), and may *not* provide an Stream of changes. Firefox still hasn't enabled this feature by default.

Other than the approximate "downlink" speed, and due to security and privacy concerns, this Web API will not provide any specific information about the actual network your users' device is connected to, like the SSID on a Wi-Fi, or the MAC address of their device, in any web platform (mobile or desktop).

### `null` connectivity results

Because of the limitations above, unsupported browsers will return `null` connectivity results, both on the `checkConnectivity` call, and the `onConnectivityChanged` stream. You should adapt your app code to check for nulls being returned from calls to the plugin.

## Contributions and Testing

Tests are a crucial to contributions to this package. All new contributions should be reasonably tested.

In order to run tests in this package, do:

```
cd test
flutter run -d chrome
```

Contributions to this package are welcome. Read the [Contributing to Flutter Plugins](https://github.com/flutter/plugins/blob/master/CONTRIBUTING.md) guide to get started.

## Issues and feedback

Please file an [issue](https://github.com/ditman/plugins/issues/new)
to send feedback or report a bug.

**Thank you!**
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
group 'com.example.connectivity_web'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

rootProject.allprojects {
repositories {
google()
jcenter()
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 28

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
minSdkVersion 16
}
lintOptions {
disable 'InvalidPackage'
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'connectivity_web'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.connectivity_web">
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.connectivity_web

import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar

/** ConnectivityWebPlugin */
public class ConnectivityWebPlugin: FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private lateinit var channel : MethodChannel

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "connectivity_web")
channel.setMethodCallHandler(this);
}

// This static function is optional and equivalent to onAttachedToEngine. It supports the old
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
// plugin registration via this function while apps migrate to use the new Android APIs
// post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
//
// It is encouraged to share logic between onAttachedToEngine and registerWith to keep
// them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
// depending on the user's project. onAttachedToEngine or registerWith must both be defined
// in the same class.
companion object {
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "connectivity_web")
channel.setMethodCallHandler(ConnectivityWebPlugin())
}
}

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else {
result.notImplemented()
}
}

override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint connectivity_web.podspec' to validate before publishing.
#
Pod::Spec.new do |s|
s.name = 'connectivity_web'
s.version = '0.1.0'
s.summary = 'No-op implementation of connectivity_web web plugin to avoid build issues on iOS'
s.description = <<-DESC
temp fake connectivity_web plugin
DESC
s.homepage = 'https://github.com/flutter/plugins/tree/master/packages/connectivity/connectivity_web'
s.license = { :file => '../LICENSE' }
s.author = { 'Flutter Team' => '[email protected]' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'Flutter'
s.platform = :ios, '8.0'

# Flutter.framework does not contain a i386 slice. Only x86_64 simulators are supported.
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'VALID_ARCHS[sdk=iphonesimulator*]' => 'x86_64' }
s.swift_version = '5.0'
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import 'dart:async';

import 'package:connectivity_platform_interface/connectivity_platform_interface.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:js/js.dart';

import 'src/generated/network_information_types.dart' as dom;
import 'src/utils/connectivity_result.dart';

/// The web implementation of the ConnectivityPlatform of the Connectivity plugin.
class ConnectivityPlugin extends ConnectivityPlatform {
/// Factory method that initializes the connectivity plugin platform with an instance
/// of the plugin for the web.
static void registerWith(Registrar registrar) {
ConnectivityPlatform.instance = ConnectivityPlugin();
}

final dom.NetworkInformation _networkInformation;
final bool _networkInformationApiSupported;

/// The constructor of the plugin.
ConnectivityPlugin() : this.withConnection(dom.navigator?.connection);

/// Creates the plugin, with an override of the NetworkInformation object.
@visibleForTesting
ConnectivityPlugin.withConnection(dom.NetworkInformation connection)
: _networkInformationApiSupported = connection != null,
_networkInformation = connection;

/// Checks the connection status of the device.
@override
Future<ConnectivityResult> checkConnectivity() async {
if (!_networkInformationApiSupported) {
return null;
}
return networkInformationToConnectivityResult(_networkInformation);
}

Stream<ConnectivityResult> get _noopStream async* {
yield null;
}

StreamController<ConnectivityResult> _connectivityResult;

/// Returns a Stream of ConnectivityResults changes.
@override
Stream<ConnectivityResult> get onConnectivityChanged {
if (!_networkInformationApiSupported) {
return _noopStream;
}
if (_connectivityResult == null) {
_connectivityResult = StreamController<ConnectivityResult>();
_networkInformation.onchange = allowInterop((_) {
_connectivityResult
.add(networkInformationToConnectivityResult(_networkInformation));
});
}
return _connectivityResult.stream;
}

/// Obtains the wifi name (SSID) of the connected network
@override
Future<String> getWifiName() {
throw PlatformException(
code: 'UNSUPPORTED_OPERATION',
message: 'getWifiName() is not supported on the web platform.',
);
}

/// Obtains the wifi BSSID of the connected network.
@override
Future<String> getWifiBSSID() {
throw PlatformException(
code: 'UNSUPPORTED_OPERATION',
message: 'getWifiBSSID() is not supported on the web platform.',
);
}

/// Obtains the IP address of the connected wifi network
@override
Future<String> getWifiIP() {
throw PlatformException(
code: 'UNSUPPORTED_OPERATION',
message: 'getWifiIP() is not supported on the web platform.',
);
}

/// Request to authorize the location service (Only on iOS).
@override
Future<LocationAuthorizationStatus> requestLocationServiceAuthorization(
{bool requestAlwaysLocationUsage = false}) {
throw PlatformException(
code: 'UNSUPPORTED_OPERATION',
message:
'requestLocationServiceAuthorization() is not supported on the web platform.',
);
}

/// Get the current location service authorization (Only on iOS).
@override
Future<LocationAuthorizationStatus> getLocationServiceAuthorization() {
throw PlatformException(
code: 'UNSUPPORTED_OPERATION',
message:
'getLocationServiceAuthorization() is not supported on the web platform.',
);
}
}
Loading