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

@@ -7,12 +7,22 @@ require("utils/module-merge").merge(common, exports);
var WIFI = "WIFI";
var MOBILE = "MOBILE";
function getActiveNetworkInfo(): android.net.NetworkInfo {
// Get Connection Type
function getConnectivityManager(): android.net.ConnectivityManager {
if (!appModule.android || !appModule.android.context) {
return null;
}
return appModule.android.context.getSystemService(android.content.Context.CONNECTIVITY_SERVICE).getActiveNetworkInfo();
return appModule.android.context.getSystemService(android.content.Context.CONNECTIVITY_SERVICE);
}
function getActiveNetworkInfo(): android.net.NetworkInfo {
var connectivityManager = getConnectivityManager();
if (!connectivityManager) {
return null;
}
return connectivityManager.getActiveNetworkInfo();
}
export function getConnectionType(): number {
@@ -28,4 +38,16 @@ export function getConnectionType(): number {
case MOBILE:
return common.connectionType.mobile;
}
}
export function starMonitoring(connectionTypeChangedCallback: (newConnectionType: number) => void): void {
var onReceiveCallback = function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
var newConnectionType = getConnectionType();
connectionTypeChangedCallback(newConnectionType);
}
appModule.android.registerBroadcastReceiver(android.net.ConnectivityManager.CONNECTIVITY_ACTION, onReceiveCallback);
}
export function stopMonitoring(): void {
appModule.android.unregisterBroadcastReceiver(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
}

View File

@@ -28,4 +28,15 @@ declare module "connectivity" {
*/
export var mobile: number;
}
/**
* Starts monitoring the connection type.
* @param connectionChangedCallback A function that will be called when the connection type changes.
*/
export function starMonitoring(connectionTypeChangedCallback: (newConnectionType: number) => void): void;
/**
* Stops monitoring the connection type.
*/
export function stopMonitoring(): void;
}

View File

@@ -3,9 +3,9 @@
declare var exports;
require("utils/module-merge").merge(common, exports);
// Get Connection Type
declare var sockaddr;
function getNetworkReachability(host?: string): any {
function _createReachability(host?: string): any {
if (host) {
return SCNetworkReachabilityCreateWithName(null, host);
}
@@ -18,8 +18,8 @@ function getNetworkReachability(host?: string): any {
}
}
function getReachabilityFlags(host?: string): number {
var reachability = getNetworkReachability(host);
function _getReachabilityFlags(host?: string): number {
var reachability = _createReachability(host);
var flagsRef = new interop.Reference<number>();
var gotFlags = SCNetworkReachabilityGetFlags(reachability, flagsRef);
CFRelease(reachability);
@@ -29,8 +29,12 @@ function getReachabilityFlags(host?: string): number {
return flagsRef.value;
}
function getConnectionTypeToHost(host?: string): number {
var flags = getReachabilityFlags(host);
function _getConnectionType(host?: string): number {
var flags = _getReachabilityFlags(host);
return _getConnectionTypeFromFlags(flags);
}
function _getConnectionTypeFromFlags(flags: number): number {
if (!flags) {
return common.connectionType.none;
}
@@ -50,5 +54,35 @@ function getConnectionTypeToHost(host?: string): number {
}
export function getConnectionType(): number {
return getConnectionTypeToHost();
return _getConnectionType();
}
// Start/Stop Monitoring
function _reachabilityCallback(target: any, flags: number, info: any) {
if (_connectionTypeChangedCallback) {
var newConnectionType = _getConnectionTypeFromFlags(flags);
_connectionTypeChangedCallback(newConnectionType);
}
}
var _reachabilityCallbackFunctionRef = new interop.FunctionReference(_reachabilityCallback)
var _monitorReachabilityRef: any;
var _connectionTypeChangedCallback: (newConnectionType: number) => void;
export function starMonitoring(connectionTypeChangedCallback: (newConnectionType: number) => void): void {
if (!_monitorReachabilityRef) {
_monitorReachabilityRef = _createReachability();
_connectionTypeChangedCallback = connectionTypeChangedCallback;
SCNetworkReachabilitySetCallback(_monitorReachabilityRef, _reachabilityCallbackFunctionRef, null);
SCNetworkReachabilityScheduleWithRunLoop(_monitorReachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
}
}
export function stopMonitoring(): void {
if (_monitorReachabilityRef) {
SCNetworkReachabilityUnscheduleFromRunLoop(_monitorReachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFRelease(_monitorReachabilityRef);
_monitorReachabilityRef = undefined;
_connectionTypeChangedCallback = undefined;;
}
}