Skip to content

Commit 9d4fa4e

Browse files
committed
Added connectivity module.
1 parent d6cc796 commit 9d4fa4e

File tree

12 files changed

+201
-1
lines changed

12 files changed

+201
-1
lines changed

CrossPlatformModules.csproj

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@
7979
<TypeScriptCompile Include="apps\action-bar-demo\pages\data-binding.ts">
8080
<DependentUpon>data-binding.xml</DependentUpon>
8181
</TypeScriptCompile>
82+
<TypeScriptCompile Include="apps\connectivity-demo\app.ts" />
83+
<TypeScriptCompile Include="apps\connectivity-demo\main-page.ts">
84+
<DependentUpon>main-page.xml</DependentUpon>
85+
</TypeScriptCompile>
8286
<TypeScriptCompile Include="apps\list-view-demo\app.ts" />
8387
<TypeScriptCompile Include="apps\list-view-demo\main-page.ts">
8488
<DependentUpon>main-page.xml</DependentUpon>
@@ -98,6 +102,9 @@
98102
<Content Include="apps\action-bar-demo\pages\center-view-segmented.xml" />
99103
<Content Include="apps\action-bar-demo\pages\center-view.xml" />
100104
<Content Include="apps\action-bar-demo\pages\data-binding.xml" />
105+
<Content Include="apps\connectivity-demo\main-page.xml">
106+
<SubType>Designer</SubType>
107+
</Content>
101108
<Content Include="apps\list-view-demo\another-page.xml">
102109
<SubType>Designer</SubType>
103110
</Content>
@@ -177,6 +184,7 @@
177184
<DependentUpon>location-example.xml</DependentUpon>
178185
</TypeScriptCompile>
179186
<TypeScriptCompile Include="apps\tests\app\style_props.ts" />
187+
<TypeScriptCompile Include="apps\tests\connectivity-tests.ts" />
180188
<TypeScriptCompile Include="apps\tests\console-tests.ts" />
181189
<TypeScriptCompile Include="apps\tests\file-name-resolver-tests\file-name-resolver-tests.ts" />
182190
<TypeScriptCompile Include="apps\tests\frame-tests.ts" />
@@ -298,8 +306,18 @@
298306
<TypeScriptCompile Include="apps\ui-tests-app\web-view\webview.ts">
299307
<DependentUpon>webview.xml</DependentUpon>
300308
</TypeScriptCompile>
309+
<TypeScriptCompile Include="connectivity\connectivity-common.ts">
310+
<DependentUpon>connectivity.d.ts</DependentUpon>
311+
</TypeScriptCompile>
301312
<TypeScriptCompile Include="es-collections.d.ts" />
302313
<TypeScriptCompile Include="es6-promise.d.ts" />
314+
<TypeScriptCompile Include="connectivity\connectivity.android.ts">
315+
<DependentUpon>connectivity.d.ts</DependentUpon>
316+
</TypeScriptCompile>
317+
<TypeScriptCompile Include="connectivity\connectivity.d.ts" />
318+
<TypeScriptCompile Include="connectivity\connectivity.ios.ts">
319+
<DependentUpon>connectivity.d.ts</DependentUpon>
320+
</TypeScriptCompile>
303321
<TypeScriptCompile Include="file-system\file-name-resolver.d.ts" />
304322
<TypeScriptCompile Include="file-system\file-name-resolver.ts">
305323
<DependentUpon>file-name-resolver.d.ts</DependentUpon>
@@ -1693,6 +1711,12 @@
16931711
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
16941712
</Content>
16951713
<Content Include="xhr\Readme.md" />
1714+
<Content Include="connectivity\package.json">
1715+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1716+
</Content>
1717+
<Content Include="apps\connectivity-demo\package.json">
1718+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1719+
</Content>
16961720
<None Include="js-libs\esprima\LICENSE.BSD" />
16971721
<Content Include="source-control.md" />
16981722
<Content Include="ui\segmented-bar\package.json">
@@ -1784,7 +1808,7 @@
17841808
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
17851809
</WebProjectProperties>
17861810
</FlavorProperties>
1787-
<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" />
1811+
<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" />
17881812
</VisualStudio>
17891813
</ProjectExtensions>
17901814
</Project>

apps/connectivity-demo/app.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import application = require("application");
2+
application.mainModule = "main-page";
3+
application.start();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import connectivity = require("connectivity");
2+
3+
export function onGetConnectionType(args) {
4+
var connectionType = connectivity.getConnectionType();
5+
switch (connectionType) {
6+
case connectivity.connectionType.none:
7+
args.object.text = "No connection";
8+
break;
9+
case connectivity.connectionType.wifi:
10+
args.object.text = "WiFi connection";
11+
break;
12+
case connectivity.connectionType.mobile:
13+
args.object.text = "Mobile connection";
14+
break;
15+
}
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Page xmlns="http://www.nativescript.org/tns.xsd" navigatedTo="onNavigatedTo">
2+
<StackLayout>
3+
<Button text="Get Connection Type" tap="onGetConnectionType" style.fontSize="30"/>
4+
</StackLayout>
5+
</Page>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{ "name" : "connectivity-demo",
2+
"main" : "app.js" }

apps/tests/connectivity-tests.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// <snippet module="connectivity" title="connectivity">
2+
// # Connectivity
3+
// Obtaining connectivity information requires the "connectivity" module.
4+
// ``` JavaScript
5+
import connectivity = require("connectivity");
6+
// ```
7+
// </snippet>
8+
9+
export var test_DummyTestForSnippetOnly0 = function () {
10+
// <snippet module="connectivity" title="connectivity">
11+
// ### Getting connection type
12+
// ``` JavaScript
13+
var connectionType = connectivity.getConnectionType();
14+
switch (connectionType) {
15+
case connectivity.connectionType.none:
16+
////console.log("No connection");
17+
break;
18+
case connectivity.connectionType.wifi:
19+
////console.log("WiFi connection");
20+
break;
21+
case connectivity.connectionType.mobile:
22+
////console.log("Mobile connection");
23+
break;
24+
}
25+
// ```
26+
// </snippet>
27+
}

apps/tests/testRunner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ allTests["HTML-VIEW"] = require("./ui/html-view/html-view-tests");
7676
allTests["WEAK-EVENTS"] = require("./weak-event-listener-tests");
7777
allTests["REPEATER"] = require("./ui/repeater/repeater-tests");
7878
allTests["SEARCH-BAR"] = require('./ui/search-bar/search-bar-tests');
79+
allTests["CONNECTIVITY"] = require("./connectivity-tests");
7980

8081
if (!isRunningOnEmulator()) {
8182
allTests["LOCATION"] = require("./location-tests");
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export module connectionType {
2+
export var none = 0;
3+
export var wifi = 1;
4+
export var mobile = 2;
5+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import appModule = require("application");
2+
import common = require("connectivity/connectivity-common");
3+
4+
declare var exports;
5+
require("utils/module-merge").merge(common, exports);
6+
7+
var WIFI = "WIFI";
8+
var MOBILE = "MOBILE";
9+
10+
function getActiveNetworkInfo(): android.net.NetworkInfo {
11+
if (!appModule.android || !appModule.android.context) {
12+
return null;
13+
}
14+
15+
return appModule.android.context.getSystemService(android.content.Context.CONNECTIVITY_SERVICE).getActiveNetworkInfo();
16+
}
17+
18+
export function getConnectionType(): number {
19+
var activeNetworkInfo = getActiveNetworkInfo();
20+
if (!activeNetworkInfo || !activeNetworkInfo.isConnected()) {
21+
return common.connectionType.none;
22+
}
23+
24+
var connectionType = activeNetworkInfo.getTypeName();
25+
switch (connectionType) {
26+
case WIFI:
27+
return common.connectionType.wifi;
28+
case MOBILE:
29+
return common.connectionType.mobile;
30+
}
31+
}

connectivity/connectivity.d.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Contains connectivity utility methods.
3+
*/
4+
declare module "connectivity" {
5+
/**
6+
* Gets the type of connection.
7+
* Returns a value from the connectivity.connectionType enumeration.
8+
*/
9+
export function getConnectionType(): number;
10+
11+
/**
12+
* Defines the different connection types.
13+
*/
14+
export module connectionType {
15+
/**
16+
* Denotes no connection.
17+
*/
18+
export var none: number;
19+
20+
/**
21+
* Denotes a WiFi connection.
22+
*/
23+
export var wifi: number;
24+
25+
/**
26+
* Denotes a mobile connection, i.e. cellular network or WAN
27+
*/
28+
export var mobile: number;
29+
}
30+
}

0 commit comments

Comments
 (0)