Resolved Issue #451: Improve the Network Stack

Resolved Issue #473: Add support for Notification Observers (iOS) and Broadcast Receivers (Android)
This commit is contained in:
Rossen Hristov
2015-07-23 16:16:54 +03:00
parent d83e3a6ccd
commit 548ea66d37
18 changed files with 503 additions and 50 deletions

View File

@@ -1,3 +1,4 @@
import application = require("application");
application.mainModule = "main-page";
application.start();

View File

@@ -1,16 +1,57 @@
import connectivity = require("connectivity");
import observable = require("data/observable");
import pages = require("ui/page");
import connectivity = require("connectivity");
import labelModule = require("ui/label");
import color = require("color");
export function onGetConnectionType(args) {
var infoLabel: labelModule.Label;
export function onPageLoaded(args: observable.EventData) {
var page = <pages.Page>args.object;
infoLabel = page.getViewById<labelModule.Label>("infoLabel");
}
export function onGetConnectionType(args: observable.EventData) {
var connectionType = connectivity.getConnectionType();
updateInfoLabel(connectionType);
}
export function onStartMonitoring(args: observable.EventData) {
onGetConnectionType(null);
connectivity.starMonitoring(onConnectionTypeChanged);
}
export function onStopMonitoring(args: observable.EventData) {
connectivity.stopMonitoring();
}
function updateInfoLabel(connectionType: number) {
switch (connectionType) {
case connectivity.connectionType.none:
args.object.text = "No connection";
infoLabel.text = "None";
infoLabel.backgroundColor = new color.Color("Red");
break;
case connectivity.connectionType.wifi:
args.object.text = "WiFi connection";
infoLabel.text = "WiFi";
infoLabel.backgroundColor = new color.Color("Green");
break;
case connectivity.connectionType.mobile:
args.object.text = "Mobile connection";
infoLabel.text = "Mobile";
infoLabel.backgroundColor = new color.Color("Yellow");
break;
}
}
function onConnectionTypeChanged(newConnectionType: number) {
switch (newConnectionType) {
case connectivity.connectionType.none:
console.log("Connection type changed to none.");
break;
case connectivity.connectionType.wifi:
console.log("Connection type changed to WiFi.");
break;
case connectivity.connectionType.mobile:
console.log("Connection type changed to mobile.");
break;
}
updateInfoLabel(newConnectionType);
}

View File

@@ -1,5 +1,8 @@
<Page xmlns="http://www.nativescript.org/tns.xsd" navigatedTo="onNavigatedTo">
<StackLayout>
<Button text="Get Connection Type" tap="onGetConnectionType" style.fontSize="30"/>
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="onPageLoaded">
<StackLayout id="stackLayout">
<Button text="Get Connection Type" tap="onGetConnectionType" style.fontSize="30" horizontalAlignment="stretch" textAlignment="center"/>
<Button text="Start Monitoring" tap="onStartMonitoring" style.fontSize="30" horizontalAlignment="stretch" textAlignment="center"/>
<Button text="Stop Monitoring" tap="onStopMonitoring" style.fontSize="30" horizontalAlignment="stretch" textAlignment="center"/>
<Label id="infoLabel" style.fontSize="30" horizontalAlignment="stretch" textAlignment="center" width="200" height="200"/>
</StackLayout>
</Page>

View File

@@ -0,0 +1,14 @@
import application = require("application");
application.mainModule = "main-page";
application.on(application.exitEvent, () => {
if (application.android) {
application.android.unregisterBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED);
}
else {
application.ios.removeNotificationObserver(UIDeviceBatteryLevelDidChangeNotification);
}
});
application.start();

View File

@@ -0,0 +1,39 @@
import application = require("application");
import observable = require("data/observable");
import pages = require("ui/page");
import labelModule = require("ui/label");
var batteryLabel: labelModule.Label;
var registered = false;
export function onPageLoaded(args: observable.EventData) {
var page = <pages.Page>args.object;
batteryLabel = page.getViewById<labelModule.Label>("batteryLabel");
if (registered) {
return;
}
if (application.android) {
application.android.registerBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED,
function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
var level = intent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, -1);
var scale = intent.getIntExtra(android.os.BatteryManager.EXTRA_SCALE, -1);
var percent = (level / scale) * 100.0;
var message = "Battery: " + percent + "%";
console.log(message);
batteryLabel.text = message;
});
}
else {
var onReceiveCallback = function onReceiveCallback(notification: NSNotification) {
var percent = UIDevice.currentDevice().batteryLevel * 100;
var message = "Battery: " + percent + "%";
console.log(message);
batteryLabel.text = message;
}
UIDevice.currentDevice().batteryMonitoringEnabled = true;
onReceiveCallback(null);
application.ios.addNotificationObserver(UIDeviceBatteryLevelDidChangeNotification, onReceiveCallback);
}
registered = true;
}

View File

@@ -0,0 +1,5 @@
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="onPageLoaded">
<StackLayout>
<Label id="batteryLabel" text="Battery" style.fontSize="30" horizontalAlignment="stretch" textAlignment="center"/>
</StackLayout>
</Page>

View File

@@ -0,0 +1,2 @@
{ "name" : "notifications-demo",
"main" : "app.js" }

View File

@@ -26,7 +26,26 @@ var dir = context.getFilesDir();
// ### Tracking the current Activity
// ``` JavaScript
if (androidApp.foregroundActivity === androidApp.startActivity) {
console.log("We are currently in the main (start) activity of the application");
////console.log("We are currently in the main (start) activity of the application");
}
// ```
// </snippet>
// <snippet module="application" title="application">
// ### Registering a Broadcast Receiver (Android)
// ``` JavaScript
//// Register the broadcast receiver
if (app.android) {
app.android.registerBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED,
function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
var level = intent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, -1);
var scale = intent.getIntExtra(android.os.BatteryManager.EXTRA_SCALE, -1);
var percent = (level / scale) * 100.0;
////console.log("Battery: " + percent + "%");
});
}
//// When no longer needed, unregister the broadcast receiver
if (app.android) {
app.android.unregisterBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED);
}
// ```
// </snippet>

View File

@@ -1 +1,27 @@

/* tslint:disable:no-unused-variable */
import app = require("application");
import TKUnit = require("./TKUnit");
import commonTests = require("./application-tests-common");
// merge the exports of the application_common file with the exports of this file
declare var exports;
require("utils/module-merge").merge(commonTests, exports);
// <snippet module="application" title="application">
// ### Adding a Notification Observer (iOS)
// ``` JavaScript
//// Add the notification observer
if (app.ios) {
app.ios.addNotificationObserver(UIDeviceBatteryLevelDidChangeNotification,
function onReceiveCallback(notification: NSNotification) {
var percent = UIDevice.currentDevice().batteryLevel * 100;
var message = "Battery: " + percent + "%";
////console.log(message);
});
}
//// When no longer needed, remove the notification observer
if (app.ios) {
app.ios.removeNotificationObserver(UIDeviceBatteryLevelDidChangeNotification);
}
// ```
// </snippet>

View File

@@ -24,4 +24,27 @@ export var test_DummyTestForSnippetOnly0 = function () {
}
// ```
// </snippet>
}
export var test_DummyTestForSnippetOnly1 = function () {
// <snippet module="connectivity" title="connectivity">
// ### Monitoring connection type.
// ``` JavaScript
connectivity.starMonitoring(function onConnectionTypeChanged(newConnectionType: number) {
switch (newConnectionType) {
case connectivity.connectionType.none:
////console.log("Connection type changed to none.");
break;
case connectivity.connectionType.wifi:
////console.log("Connection type changed to WiFi.");
break;
case connectivity.connectionType.mobile:
////console.log("Connection type changed to mobile.");
break;
}
});
////...
connectivity.stopMonitoring();
// ```
// </snippet>
}