Skip to content

Commit

Permalink
feat: Add Windows support (#103 by @sbeca)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbeca authored and matt-oakes committed May 26, 2019
1 parent 8350d2f commit cf0fb8f
Show file tree
Hide file tree
Showing 12 changed files with 911 additions and 13 deletions.
48 changes: 35 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

# `@react-native-community/netinfo`

[![CircleCI Status](https://img.shields.io/circleci/project/github/react-native-community/react-native-netinfo/master.svg)](https://circleci.com/gh/react-native-community/workflows/react-native-netinfo/tree/master) ![Supports Android and iOS](https://img.shields.io/badge/platforms-android%20|%20ios-lightgrey.svg) ![MIT License](https://img.shields.io/npm/l/@react-native-community/netinfo.svg)
[![CircleCI Status](https://img.shields.io/circleci/project/github/react-native-community/react-native-netinfo/master.svg)](https://circleci.com/gh/react-native-community/workflows/react-native-netinfo/tree/master) ![Supports Android, iOS, and Windows](https://img.shields.io/badge/platforms-android%20|%20ios%20|%20windows-lightgrey.svg) ![MIT License](https://img.shields.io/npm/l/@react-native-community/netinfo.svg)

React Native Network Info API for Android & iOS. It allows you to get information on:
React Native Network Info API for Android, iOS & Windows. It allows you to get information on:

* Connection type
* Connection quality
Expand Down Expand Up @@ -79,6 +79,28 @@ protected List<ReactPackage> getPackages() {
```
</details>

<details>
<summary>Manually link the library on Windows</summary>

* Open the solution in Visual Studio for your Windows apps
* Right click in the Explorer and click Add > Existing Project...
* Navigate to `./<app-name>/node_modules/@react-native-community/netinfo/windows/RNCNetInfo/` and add `RNCNetInfo.csproj`
* This time right click on your React Native Windows app under your solutions directory and click Add > Reference...
* Check the `RNCNetInfo` you just added and press ok
* Open up `MainReactNativeHost.cs` for your app and edit the file like so:

```diff
+ using ReactNativeCommunity.NetInfo;
......
protected override List<IReactPackage> Packages => new List<IReactPackage>
{
new MainReactPackage(),
+ new RNCNetInfoPackage(),
};
```

</details>

## Migrating from the core `react-native` module
This module was created when the NetInfo was split out from the core of React Native. To migrate to this module you need to follow the installation instructions above and then change you imports from:

Expand Down Expand Up @@ -170,17 +192,17 @@ The `details` value depends on the `type` value.
#### `NetInfoStateType`
Describes the current type of network connection. It is an enum with these possible values:

| Value | Platform | Description |
| ----------- | ------------ | ---------------------------------------------------------- |
| `none` | Android, iOS | No network connection is active |
| `unknown` | Android, iOS | The network state could not or has yet to be be determined |
| `cellular` | Android, iOS | Active network over cellular |
| `wifi` | Android, iOS | Active network over Wifi |
| `bluetooth` | Android | Active network over Bluetooth |
| `ethernet` | Android | Active network over wired ethernet |
| `wimax` | Android | Active network over WiMax |
| `vpn` | Android | Active network over VPN |
| `other` | Android, iOS | Active network over another type of network |
| Value | Platform | Description |
| ----------- | --------------------- | ---------------------------------------------------------- |
| `none` | Android, iOS, Windows | No network connection is active |
| `unknown` | Android, iOS, Windows | The network state could not or has yet to be be determined |
| `cellular` | Android, iOS, Windows | Active network over cellular |
| `wifi` | Android, iOS, Windows | Active network over Wifi |
| `bluetooth` | Android | Active network over Bluetooth |
| `ethernet` | Android, Windows | Active network over wired ethernet |
| `wimax` | Android | Active network over WiMax |
| `vpn` | Android | Active network over VPN |
| `other` | Android, iOS, Windows | Active network over another type of network |

#### `NetInfoCellularGeneration`
Describes the current generation of the `cellular` connection. It is an enum with these possible values:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"/android",
"!/android/build",
"/ios",
"/windows",
"/src",
"/lib",
"/*.podspec"
Expand Down
78 changes: 78 additions & 0 deletions windows/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
*AppPackages*
*BundleArtifacts*
*ReactAssets*

# OS junk files
[Tt]humbs.db
*.DS_Store

# Visual Studio files
*.[Oo]bj
*.user
*.aps
*.pch
*.vspscc
*.vssscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.[Cc]ache
*.ilk
*.log
*.lib
*.sbr
*.sdf
*.opensdf
*.opendb
*.unsuccessfulbuild
ipch/
[Oo]bj/
[Bb]in
[Dd]ebug*/
[Rr]elease*/
Ankh.NoLoad

# MonoDevelop
*.pidb
*.userprefs

# Tooling
_ReSharper*/
*.resharper
[Tt]est[Rr]esult*
*.sass-cache

# Project files
[Bb]uild/

# Subversion files
.svn

# Office Temp Files
~$*

# vim Temp Files
*~

# NuGet
packages/
*.nupkg

# ncrunch
*ncrunch*
*crunch*.local.xml

# Visual Studio database projects
*.dbmdl

# Test files
*.testsettings

#Other files
*.DotSettings
.vs/
*project.lock.json
86 changes: 86 additions & 0 deletions windows/RNCNetInfo.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RNCNetInfo", "RNCNetInfo\RNCNetInfo.csproj", "{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReactNative", "..\node_modules\react-native-windows\ReactWindows\ReactNative\ReactNative.csproj", "{C7673AD5-E3AA-468C-A5FD-FA38154E205C}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ChakraBridge", "..\node_modules\react-native-windows\ReactWindows\ChakraBridge\ChakraBridge.vcxproj", "{4B72C796-16D5-4E3A-81C0-3E36F531E578}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
..\node_modules\react-native-windows\ReactWindows\ReactNative.Shared\ReactNative.Shared.projitems*{c7673ad5-e3aa-468c-a5fd-fa38154e205c}*SharedItemsImports = 4
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM = Debug|ARM
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Development|ARM = Development|ARM
Development|x64 = Development|x64
Development|x86 = Development|x86
Release|ARM = Release|ARM
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Debug|ARM.ActiveCfg = Debug|ARM
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Debug|ARM.Build.0 = Debug|ARM
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Debug|x64.ActiveCfg = Debug|x64
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Debug|x64.Build.0 = Debug|x64
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Debug|x86.ActiveCfg = Debug|x86
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Debug|x86.Build.0 = Debug|x86
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Development|ARM.ActiveCfg = Development|ARM
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Development|ARM.Build.0 = Development|ARM
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Development|x64.ActiveCfg = Development|x64
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Development|x64.Build.0 = Development|x64
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Development|x86.ActiveCfg = Development|x86
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Development|x86.Build.0 = Development|x86
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Release|ARM.ActiveCfg = Release|ARM
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Release|ARM.Build.0 = Release|ARM
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Release|x64.ActiveCfg = Release|x64
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Release|x64.Build.0 = Release|x64
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Release|x86.ActiveCfg = Release|x86
{4ACDD44C-E556-423E-BD74-B4CEF3E67FF0}.Release|x86.Build.0 = Release|x86
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Debug|ARM.ActiveCfg = Debug|ARM
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Debug|ARM.Build.0 = Debug|ARM
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Debug|x64.ActiveCfg = Debug|x64
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Debug|x64.Build.0 = Debug|x64
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Debug|x86.ActiveCfg = Debug|x86
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Debug|x86.Build.0 = Debug|x86
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Development|ARM.ActiveCfg = Debug|ARM
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Development|ARM.Build.0 = Debug|ARM
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Development|x64.ActiveCfg = Debug|x64
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Development|x64.Build.0 = Debug|x64
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Development|x86.ActiveCfg = Debug|x86
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Development|x86.Build.0 = Debug|x86
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Release|ARM.ActiveCfg = Release|ARM
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Release|ARM.Build.0 = Release|ARM
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Release|x64.ActiveCfg = Release|x64
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Release|x64.Build.0 = Release|x64
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Release|x86.ActiveCfg = Release|x86
{C7673AD5-E3AA-468C-A5FD-FA38154E205C}.Release|x86.Build.0 = Release|x86
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Debug|ARM.ActiveCfg = Debug|ARM
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Debug|ARM.Build.0 = Debug|ARM
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Debug|x64.ActiveCfg = Debug|x64
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Debug|x64.Build.0 = Debug|x64
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Debug|x86.ActiveCfg = Debug|Win32
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Debug|x86.Build.0 = Debug|Win32
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Development|ARM.ActiveCfg = Debug|ARM
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Development|ARM.Build.0 = Debug|ARM
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Development|x64.ActiveCfg = Debug|x64
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Development|x64.Build.0 = Debug|x64
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Development|x86.ActiveCfg = Debug|Win32
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Development|x86.Build.0 = Debug|Win32
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Release|ARM.ActiveCfg = Release|ARM
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Release|ARM.Build.0 = Release|ARM
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Release|x64.ActiveCfg = Release|x64
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Release|x64.Build.0 = Release|x64
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Release|x86.ActiveCfg = Release|Win32
{4B72C796-16D5-4E3A-81C0-3E36F531E578}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
129 changes: 129 additions & 0 deletions windows/RNCNetInfo/DefaultNetworkInformation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Windows.Networking.Connectivity;

namespace ReactNativeCommunity.NetInfo
{
class DefaultNetworkInformation : INetworkInformation
{
public event NetworkStatusChangedEventHandler NetworkStatusChanged;

public void Start()
{
NetworkInformation.NetworkStatusChanged += OnNetworkStatusChanged;
}

public void Stop()
{
NetworkInformation.NetworkStatusChanged -= OnNetworkStatusChanged;
}

public IConnectionProfile GetInternetConnectionProfile()
{
var profile = NetworkInformation.GetInternetConnectionProfile();
return profile != null
? new ConnectionProfileImpl(profile)
: null;
}

private void OnNetworkStatusChanged(object sender)
{
NetworkStatusChanged?.Invoke(sender);
}

class ConnectionProfileImpl : IConnectionProfile
{
private readonly ConnectionProfile _profile;

public ConnectionProfileImpl(ConnectionProfile profile)
{
_profile = profile;
}

public NetworkConnectivityLevel ConnectivityLevel
{
get
{
return _profile.GetNetworkConnectivityLevel();
}
}

public NetworkConnectionType ConnectionType
{
get
{
if (_profile.IsWlanConnectionProfile)
{
return NetworkConnectionType.Wifi;
}
else if (_profile.IsWwanConnectionProfile)
{
return NetworkConnectionType.Cellular;
}

var networkAdapter = _profile.NetworkAdapter;
if (networkAdapter == null)
{
return NetworkConnectionType.Unknown;
}

// Possible values: https://www.iana.org/assignments/ianaiftype-mib/ianaiftype-mib
if (networkAdapter.IanaInterfaceType == 6u)
{
return NetworkConnectionType.Ethernet;
}
else
{
return NetworkConnectionType.Other;
}
}
}

public CellularGeneration CellularGeneration
{
get
{
if (!_profile.IsWwanConnectionProfile)
{
return CellularGeneration.None;
}

var dataClass = _profile.WwanConnectionProfileDetails.GetCurrentDataClass();

switch (dataClass) {
case WwanDataClass.None:
return CellularGeneration.None;
case WwanDataClass.Edge:
case WwanDataClass.Gprs:
return CellularGeneration.Generation2;
case WwanDataClass.Cdma1xEvdo:
case WwanDataClass.Cdma1xEvdoRevA:
case WwanDataClass.Cdma1xEvdoRevB:
case WwanDataClass.Cdma1xEvdv:
case WwanDataClass.Cdma1xRtt:
case WwanDataClass.Cdma3xRtt:
case WwanDataClass.Hsdpa:
case WwanDataClass.Hsupa:
case WwanDataClass.Umts:
return CellularGeneration.Generation3;
case WwanDataClass.CdmaUmb:
case WwanDataClass.LteAdvanced:
return CellularGeneration.Generation4;
case WwanDataClass.Custom:
default:
return CellularGeneration.Unknown;
}
}
}

public NetworkCostType ConnectionCost
{
get
{
return _profile.GetConnectionCost().NetworkCostType;
}
}
}
}
}
Loading

0 comments on commit cf0fb8f

Please sign in to comment.