mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
Merge pull request #464 from NativeScript/connectivity
Added connectivity module.
This commit is contained in:
@ -79,6 +79,10 @@
|
||||
<TypeScriptCompile Include="apps\action-bar-demo\pages\data-binding.ts">
|
||||
<DependentUpon>data-binding.xml</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="apps\connectivity-demo\app.ts" />
|
||||
<TypeScriptCompile Include="apps\connectivity-demo\main-page.ts">
|
||||
<DependentUpon>main-page.xml</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="apps\list-view-demo\app.ts" />
|
||||
<TypeScriptCompile Include="apps\list-view-demo\main-page.ts">
|
||||
<DependentUpon>main-page.xml</DependentUpon>
|
||||
@ -98,6 +102,9 @@
|
||||
<Content Include="apps\action-bar-demo\pages\center-view-segmented.xml" />
|
||||
<Content Include="apps\action-bar-demo\pages\center-view.xml" />
|
||||
<Content Include="apps\action-bar-demo\pages\data-binding.xml" />
|
||||
<Content Include="apps\connectivity-demo\main-page.xml">
|
||||
<SubType>Designer</SubType>
|
||||
</Content>
|
||||
<Content Include="apps\list-view-demo\another-page.xml">
|
||||
<SubType>Designer</SubType>
|
||||
</Content>
|
||||
@ -177,6 +184,7 @@
|
||||
<DependentUpon>location-example.xml</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="apps\tests\app\style_props.ts" />
|
||||
<TypeScriptCompile Include="apps\tests\connectivity-tests.ts" />
|
||||
<TypeScriptCompile Include="apps\tests\console-tests.ts" />
|
||||
<TypeScriptCompile Include="apps\tests\file-name-resolver-tests\file-name-resolver-tests.ts" />
|
||||
<TypeScriptCompile Include="apps\tests\frame-tests.ts" />
|
||||
@ -298,8 +306,18 @@
|
||||
<TypeScriptCompile Include="apps\ui-tests-app\web-view\webview.ts">
|
||||
<DependentUpon>webview.xml</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="connectivity\connectivity-common.ts">
|
||||
<DependentUpon>connectivity.d.ts</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="es-collections.d.ts" />
|
||||
<TypeScriptCompile Include="es6-promise.d.ts" />
|
||||
<TypeScriptCompile Include="connectivity\connectivity.android.ts">
|
||||
<DependentUpon>connectivity.d.ts</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="connectivity\connectivity.d.ts" />
|
||||
<TypeScriptCompile Include="connectivity\connectivity.ios.ts">
|
||||
<DependentUpon>connectivity.d.ts</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="file-system\file-name-resolver.d.ts" />
|
||||
<TypeScriptCompile Include="file-system\file-name-resolver.ts">
|
||||
<DependentUpon>file-name-resolver.d.ts</DependentUpon>
|
||||
@ -1693,6 +1711,12 @@
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="xhr\Readme.md" />
|
||||
<Content Include="connectivity\package.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="apps\connectivity-demo\package.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<None Include="js-libs\esprima\LICENSE.BSD" />
|
||||
<Content Include="source-control.md" />
|
||||
<Content Include="ui\segmented-bar\package.json">
|
||||
@ -1784,7 +1808,7 @@
|
||||
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
|
||||
</WebProjectProperties>
|
||||
</FlavorProperties>
|
||||
<UserProperties ui_2layouts_2wrap-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2grid-layout_2package_1json__JSONSchema="" ui_2layouts_2dock-layout_2package_1json__JSONSchema="" ui_2layouts_2absolute-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2linear-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2web-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2content-view_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2gallery-app_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2absolute-layout-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2editable-text-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2scroll-view_2package_1json__JSONSchema="http://json.schemastore.org/package" />
|
||||
<UserProperties ui_2scroll-view_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2editable-text-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2absolute-layout-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2gallery-app_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2content-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2web-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2linear-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2absolute-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2dock-layout_2package_1json__JSONSchema="" ui_2layouts_2grid-layout_2package_1json__JSONSchema="" ui_2layouts_2wrap-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" />
|
||||
</VisualStudio>
|
||||
</ProjectExtensions>
|
||||
</Project>
|
3
apps/connectivity-demo/app.ts
Normal file
3
apps/connectivity-demo/app.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import application = require("application");
|
||||
application.mainModule = "main-page";
|
||||
application.start();
|
16
apps/connectivity-demo/main-page.ts
Normal file
16
apps/connectivity-demo/main-page.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import connectivity = require("connectivity");
|
||||
|
||||
export function onGetConnectionType(args) {
|
||||
var connectionType = connectivity.getConnectionType();
|
||||
switch (connectionType) {
|
||||
case connectivity.connectionType.none:
|
||||
args.object.text = "No connection";
|
||||
break;
|
||||
case connectivity.connectionType.wifi:
|
||||
args.object.text = "WiFi connection";
|
||||
break;
|
||||
case connectivity.connectionType.mobile:
|
||||
args.object.text = "Mobile connection";
|
||||
break;
|
||||
}
|
||||
}
|
5
apps/connectivity-demo/main-page.xml
Normal file
5
apps/connectivity-demo/main-page.xml
Normal file
@ -0,0 +1,5 @@
|
||||
<Page xmlns="http://www.nativescript.org/tns.xsd" navigatedTo="onNavigatedTo">
|
||||
<StackLayout>
|
||||
<Button text="Get Connection Type" tap="onGetConnectionType" style.fontSize="30"/>
|
||||
</StackLayout>
|
||||
</Page>
|
2
apps/connectivity-demo/package.json
Normal file
2
apps/connectivity-demo/package.json
Normal file
@ -0,0 +1,2 @@
|
||||
{ "name" : "connectivity-demo",
|
||||
"main" : "app.js" }
|
27
apps/tests/connectivity-tests.ts
Normal file
27
apps/tests/connectivity-tests.ts
Normal file
@ -0,0 +1,27 @@
|
||||
// <snippet module="connectivity" title="connectivity">
|
||||
// # Connectivity
|
||||
// Obtaining connectivity information requires the "connectivity" module.
|
||||
// ``` JavaScript
|
||||
import connectivity = require("connectivity");
|
||||
// ```
|
||||
// </snippet>
|
||||
|
||||
export var test_DummyTestForSnippetOnly0 = function () {
|
||||
// <snippet module="connectivity" title="connectivity">
|
||||
// ### Getting connection type
|
||||
// ``` JavaScript
|
||||
var connectionType = connectivity.getConnectionType();
|
||||
switch (connectionType) {
|
||||
case connectivity.connectionType.none:
|
||||
////console.log("No connection");
|
||||
break;
|
||||
case connectivity.connectionType.wifi:
|
||||
////console.log("WiFi connection");
|
||||
break;
|
||||
case connectivity.connectionType.mobile:
|
||||
////console.log("Mobile connection");
|
||||
break;
|
||||
}
|
||||
// ```
|
||||
// </snippet>
|
||||
}
|
@ -76,6 +76,7 @@ allTests["HTML-VIEW"] = require("./ui/html-view/html-view-tests");
|
||||
allTests["WEAK-EVENTS"] = require("./weak-event-listener-tests");
|
||||
allTests["REPEATER"] = require("./ui/repeater/repeater-tests");
|
||||
allTests["SEARCH-BAR"] = require('./ui/search-bar/search-bar-tests');
|
||||
allTests["CONNECTIVITY"] = require("./connectivity-tests");
|
||||
|
||||
if (!isRunningOnEmulator()) {
|
||||
allTests["LOCATION"] = require("./location-tests");
|
||||
|
5
connectivity/connectivity-common.ts
Normal file
5
connectivity/connectivity-common.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export module connectionType {
|
||||
export var none = 0;
|
||||
export var wifi = 1;
|
||||
export var mobile = 2;
|
||||
}
|
31
connectivity/connectivity.android.ts
Normal file
31
connectivity/connectivity.android.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import appModule = require("application");
|
||||
import common = require("connectivity/connectivity-common");
|
||||
|
||||
declare var exports;
|
||||
require("utils/module-merge").merge(common, exports);
|
||||
|
||||
var WIFI = "WIFI";
|
||||
var MOBILE = "MOBILE";
|
||||
|
||||
function getActiveNetworkInfo(): android.net.NetworkInfo {
|
||||
if (!appModule.android || !appModule.android.context) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return appModule.android.context.getSystemService(android.content.Context.CONNECTIVITY_SERVICE).getActiveNetworkInfo();
|
||||
}
|
||||
|
||||
export function getConnectionType(): number {
|
||||
var activeNetworkInfo = getActiveNetworkInfo();
|
||||
if (!activeNetworkInfo || !activeNetworkInfo.isConnected()) {
|
||||
return common.connectionType.none;
|
||||
}
|
||||
|
||||
var connectionType = activeNetworkInfo.getTypeName();
|
||||
switch (connectionType) {
|
||||
case WIFI:
|
||||
return common.connectionType.wifi;
|
||||
case MOBILE:
|
||||
return common.connectionType.mobile;
|
||||
}
|
||||
}
|
31
connectivity/connectivity.d.ts
vendored
Normal file
31
connectivity/connectivity.d.ts
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Contains connectivity utility methods.
|
||||
*/
|
||||
declare module "connectivity" {
|
||||
/**
|
||||
* Gets the type of connection.
|
||||
* Returns a value from the connectivity.connectionType enumeration.
|
||||
* To use this method on Android you need to have the android.permission.ACCESS_NETWORK_STATE permission added to the AndroidManifest.xml file.
|
||||
*/
|
||||
export function getConnectionType(): number;
|
||||
|
||||
/**
|
||||
* Defines the different connection types.
|
||||
*/
|
||||
export module connectionType {
|
||||
/**
|
||||
* Denotes no connection.
|
||||
*/
|
||||
export var none: number;
|
||||
|
||||
/**
|
||||
* Denotes a WiFi connection.
|
||||
*/
|
||||
export var wifi: number;
|
||||
|
||||
/**
|
||||
* Denotes a mobile connection, i.e. cellular network or WAN
|
||||
*/
|
||||
export var mobile: number;
|
||||
}
|
||||
}
|
54
connectivity/connectivity.ios.ts
Normal file
54
connectivity/connectivity.ios.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import common = require("connectivity/connectivity-common");
|
||||
|
||||
declare var exports;
|
||||
require("utils/module-merge").merge(common, exports);
|
||||
|
||||
declare var sockaddr;
|
||||
|
||||
function getNetworkReachability(host?: string): any {
|
||||
if (host) {
|
||||
return SCNetworkReachabilityCreateWithName(null, host);
|
||||
}
|
||||
else {
|
||||
var zeroAddress = new interop.Reference(sockaddr, {
|
||||
sa_len: 16,
|
||||
sa_family: 2
|
||||
});
|
||||
return SCNetworkReachabilityCreateWithAddress(null, zeroAddress);
|
||||
}
|
||||
}
|
||||
|
||||
function getReachabilityFlags(host?: string): number {
|
||||
var reachability = getNetworkReachability(host);
|
||||
var flagsRef = new interop.Reference<number>();
|
||||
var gotFlags = SCNetworkReachabilityGetFlags(reachability, flagsRef);
|
||||
CFRelease(reachability);
|
||||
if (!gotFlags) {
|
||||
return null;
|
||||
}
|
||||
return flagsRef.value;
|
||||
}
|
||||
|
||||
function getConnectionTypeToHost(host?: string): number {
|
||||
var flags = getReachabilityFlags(host);
|
||||
if (!flags) {
|
||||
return common.connectionType.none;
|
||||
}
|
||||
|
||||
var isReachable = flags & kSCNetworkReachabilityFlagsReachable;
|
||||
var connectionRequired = flags & kSCNetworkReachabilityFlagsConnectionRequired;
|
||||
if (!isReachable || connectionRequired) {
|
||||
return common.connectionType.none;
|
||||
}
|
||||
|
||||
var isWWAN = flags & kSCNetworkReachabilityFlagsIsWWAN;
|
||||
if (isWWAN) {
|
||||
return common.connectionType.mobile;
|
||||
}
|
||||
|
||||
return common.connectionType.wifi;
|
||||
}
|
||||
|
||||
export function getConnectionType(): number {
|
||||
return getConnectionTypeToHost();
|
||||
}
|
2
connectivity/package.json
Normal file
2
connectivity/package.json
Normal file
@ -0,0 +1,2 @@
|
||||
{ "name" : "connectivity",
|
||||
"main" : "connectivity.js" }
|
Reference in New Issue
Block a user