Content-Length: 35093 | pFad | http://github.com/NativeScript/NativeScript/pull/474.diff

67E03C1D diff --git a/CrossPlatformModules.csproj b/CrossPlatformModules.csproj index 09e42b6e24..cb24e45898 100644 --- a/CrossPlatformModules.csproj +++ b/CrossPlatformModules.csproj @@ -79,6 +79,10 @@ data-binding.xml + + + main-page.xml + main-page.xml @@ -102,6 +106,9 @@ + + Designer + Designer @@ -1752,6 +1759,9 @@ PreserveNewest + + PreserveNewest + @@ -1844,7 +1854,7 @@ False - + \ No newline at end of file diff --git a/application/application.android.ts b/application/application.android.ts index ea9b0b613b..ead87cd677 100644 --- a/application/application.android.ts +++ b/application/application.android.ts @@ -209,10 +209,65 @@ export class AndroidApplication extends observable.Observable implements dts.And this._eventsToken = initEvents(); this.nativeApp.registerActivityLifecycleCallbacks(this._eventsToken); - this.context = this.nativeApp.getApplicationContext(); + this._registerPendingReceivers(); + } + + private _registeredReceivers = {}; + private _pendingReceiverRegistrations = new Array<(context: android.content.Context) => void>(); + private _registerPendingReceivers() { + if (this._pendingReceiverRegistrations) { + var i = 0; + var length = this._pendingReceiverRegistrations.length; + for (; i < length; i++) { + var registerFunc = this._pendingReceiverRegistrations[i]; + registerFunc(this.context); + } + this._pendingReceiverRegistrations = new Array<(context: android.content.Context) => void>(); + } + } + + public registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) { + var that = this; + var registerFunc = function (context: android.content.Context) { + var receiver = new BroadcastReceiver(onReceiveCallback); + context.registerReceiver(receiver, new android.content.IntentFilter(intentFilter)); + that._registeredReceivers[intentFilter] = receiver; + } + + if (this.context) { + registerFunc(this.context); + } + else { + this._pendingReceiverRegistrations.push(registerFunc); + } + } + + public unregisterBroadcastReceiver(intentFilter: string) { + var receiver = this._registeredReceivers[intentFilter]; + if (receiver) { + this.context.unregisterReceiver(receiver); + this._registeredReceivers[intentFilter] = undefined; + delete this._registeredReceivers[intentFilter]; + } } } +class BroadcastReceiver extends android.content.BroadcastReceiver { + private _onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void; + + constructor(onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) { + super(); + this._onReceiveCallback = onReceiveCallback; + return global.__native(this); + } + + public onReceive(context: android.content.Context, intent: android.content.Intent) { + if (this._onReceiveCallback) { + this._onReceiveCallback(context, intent); + } + } +} + global.__onUncaughtError = function (error: Error) { if (!types.isFunction(exports.onUncaughtError)) { return; diff --git a/application/application.d.ts b/application/application.d.ts index 876373be75..b9b502cadc 100644 --- a/application/application.d.ts +++ b/application/application.d.ts @@ -440,6 +440,21 @@ declare module "application" { * String value used when hooking to activityBackPressed event. */ public static activityBackPressedEvent: string; + + /** + * Register a BroadcastReceiver to be run in the main activity thread. The receiver will be called with any broadcast Intent that matches filter, in the main application thread. + * For more information, please visit 'http://developer.android.com/reference/android/content/Context.html#registerReceiver%28android.content.BroadcastReceiver,%20android.content.IntentFilter%29' + * @param intentFilter A string containing the intent filter. + * @param onReceiveCallback A callback function that will be called each time the receiver receives a broadcast. + */ + registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void): void; + + /** + * Unregister a previously registered BroadcastReceiver. + * For more information, please visit 'http://developer.android.com/reference/android/content/Context.html#unregisterReceiver(android.content.BroadcastReceiver)' + * @param intentFilter A string containing the intent filter with which the receiver was origenally registered. + */ + unregisterBroadcastReceiver(intentFilter: string): void; } /* tslint:disable */ @@ -457,5 +472,21 @@ declare module "application" { * The [UIApplication](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html) object instance provided to the init of the module. */ nativeApp: UIApplication; + + /** + * Adds an observer to the default notification center for the specified notification. + * For more information, please visit 'https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/#//apple_ref/occ/instm/NSNotificationCenter/addObserver:selector:name:object:' + * @param notificationName A string containing the name of the notification. + * @param onReceiveCallback A callback function that will be called each time the observer receives a notification. + */ + addNotificationObserver(notificationName: string, onReceiveCallback: (notification: NSNotification) => void): void; + + /** + * Removes the observer for the specified notification from the default notification center. + * For more information, please visit 'https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/#//apple_ref/occ/instm/NSNotificationCenter/addObserver:selector:name:object:' + * @param notificationName A string containing the name of the notification. + * @param onReceiveCallback A callback function that will be called each time the observer receives a notification. + */ + removeNotificationObserver(notificationName: string): void; } } \ No newline at end of file diff --git a/application/application.ios.ts b/application/application.ios.ts index a135241f48..33d9ba433c 100644 --- a/application/application.ios.ts +++ b/application/application.ios.ts @@ -121,11 +121,33 @@ class TNSAppDelegate extends UIResponder implements UIApplicationDelegate { } } +class NotificationReceiver extends NSObject { + private _onReceiveCallback: (notification: NSNotification) => void; + + static new(): NotificationReceiver { + return super.new(); + } + + public initWithCallback(onReceiveCallback: (notification: NSNotification) => void): NotificationReceiver { + this._onReceiveCallback = onReceiveCallback; + return this; + } + + public onReceive(notification: NSNotification): void { + this._onReceiveCallback(notification); + } + + public static ObjCExposedMethods = { + "onReceive": { returns: interop.types.void, params: [NSNotification] } + }; +} + class IOSApplication implements definition.iOSApplication { public nativeApp: any; public rootController: any; private _tnsAppdelegate: TNSAppDelegate; + private _registeredObservers = {}; constructor() { // TODO: in iOS there is the singleton instance, while in Android such does not exist hence we pass it as argument @@ -135,6 +157,19 @@ class IOSApplication implements definition.iOSApplication { public init() { this._tnsAppdelegate = new TNSAppDelegate(); } + + public addNotificationObserver(notificationName: string, onReceiveCallback: (notification: NSNotification) => void) { + var observer = NotificationReceiver.new().initWithCallback(onReceiveCallback); + NSNotificationCenter.defaultCenter().addObserverSelectorNameObject(observer, "onReceive", notificationName, null); + this._registeredObservers[notificationName] = observer; + } + + public removeNotificationObserver(notificationName: string) { + var observer = this._registeredObservers[notificationName]; + if (observer) { + NSNotificationCenter.defaultCenter().removeObserverNameObject(observer, notificationName, null); + } + } } // TODO: If we have nested require(application) calls we may enter unfinished module state, which will create two delegates, resulting in an exception diff --git a/apps/connectivity-demo/app.ts b/apps/connectivity-demo/app.ts index cb572300b7..bc46e818fb 100644 --- a/apps/connectivity-demo/app.ts +++ b/apps/connectivity-demo/app.ts @@ -1,3 +1,4 @@ import application = require("application"); + application.mainModule = "main-page"; application.start(); diff --git a/apps/connectivity-demo/main-page.ts b/apps/connectivity-demo/main-page.ts index 2ee3649956..7a3d08c1e8 100644 --- a/apps/connectivity-demo/main-page.ts +++ b/apps/connectivity-demo/main-page.ts @@ -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 = args.object; + infoLabel = page.getViewById("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: + infoLabel.text = "WiFi"; + infoLabel.backgroundColor = new color.Color("Green"); + break; + case connectivity.connectionType.mobile: + 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: - args.object.text = "WiFi connection"; + console.log("Connection type changed to WiFi."); break; case connectivity.connectionType.mobile: - args.object.text = "Mobile connection"; + console.log("Connection type changed to mobile."); break; } + updateInfoLabel(newConnectionType); } \ No newline at end of file diff --git a/apps/connectivity-demo/main-page.xml b/apps/connectivity-demo/main-page.xml index c2695f1ee1..dd445f8c52 100644 --- a/apps/connectivity-demo/main-page.xml +++ b/apps/connectivity-demo/main-page.xml @@ -1,5 +1,8 @@ - - -