From 849d6bee267f4ccdf697441878c09663f981210d Mon Sep 17 00:00:00 2001 From: Rossen Hristov Date: Mon, 6 Jul 2015 16:27:02 +0300 Subject: [PATCH] Moved Activity related Android-specific events to application.android instance. Renamed cross-platform events by deleting their "event" suffix. --- application/application-common.ts | 12 +-- application/application.android.ts | 84 ++++------------ application/application.d.ts | 149 +++++++++++------------------ apps/tests/app/app.ts | 18 ++-- ui/frame/frame.android.ts | 8 +- 5 files changed, 93 insertions(+), 178 deletions(-) diff --git a/application/application-common.ts b/application/application-common.ts index a305cff7b..030d02d76 100644 --- a/application/application-common.ts +++ b/application/application-common.ts @@ -8,17 +8,17 @@ import observable = require("data/observable"); var events = new observable.Observable(); require("utils/module-merge").merge(events, exports); -export var launchEvent = "launchEvent"; +export var launchEvent = "launch"; -export var uncaughtErrorEvent = "uncaughtErrorEvent"; +export var uncaughtErrorEvent = "uncaughtError"; -export var suspendEvent = "suspendEvent"; +export var suspendEvent = "suspend"; -export var resumeEvent = "resumeEvent"; +export var resumeEvent = "resume"; -export var exitEvent = "exitEvent"; +export var exitEvent = "exit"; -export var lowMemoryEvent = "lowMemoryEvent"; +export var lowMemoryEvent = "lowMemory"; export var cssFile: string = "app.css" diff --git a/application/application.android.ts b/application/application.android.ts index d766d0bbc..cc836f5d2 100644 --- a/application/application.android.ts +++ b/application/application.android.ts @@ -2,6 +2,7 @@ import dts = require("application"); import frame = require("ui/frame"); import types = require("utils/types"); +import observable = require("data/observable"); // merge the exports of the application_common file with the exports of this file declare var exports; @@ -11,16 +12,6 @@ var callbacks = android.app.Application.ActivityLifecycleCallbacks; export var mainModule: string; -export var androidActivityCreatedEvent = "activityCreated"; -export var androidActivityDestroyedEvent = "activityDestroyed"; -export var androidActivityStartedEvent = "activityStarted"; -export var androidActivityPausedEvent = "activityPaused"; -export var androidActivityResumedEvent = "activityResumed"; -export var androidActivityStoppedEvent = "activityStopped"; -export var androidSaveActivityStateEvent = "saveActivityState"; -export var androidActivityResultEvent = "activityResult"; -export var androidActivityBackPressedEvent = "activityBackPressed"; - // We are using the exports object for the common events since we merge the appModule with this module's exports, which is what users will receive when require("application") is called; // TODO: This is kind of hacky and is "pure JS in TypeScript" @@ -32,7 +23,7 @@ var initEvents = function () { if (!androidApp.startActivity) { androidApp.startActivity = activity; - exports.notify({ eventName: androidActivityCreatedEvent, object: androidApp, activity: activity, bundle: bundle }); + androidApp.notify({ eventName: "activityCreated", object: androidApp, activity: activity, bundle: bundle }); if (androidApp.onActivityCreated) { androidApp.onActivityCreated(activity, bundle); @@ -62,7 +53,7 @@ var initEvents = function () { androidApp.startActivity = undefined; } - exports.notify({ eventName: androidActivityDestroyedEvent, object: androidApp, activity: activity }); + androidApp.notify({ eventName: "activityDestroyed", object: androidApp, activity: activity }); if (androidApp.onActivityDestroyed) { androidApp.onActivityDestroyed(activity); @@ -81,7 +72,7 @@ var initEvents = function () { exports.notify({ eventName: dts.suspendEvent, object: androidApp, android: activity }); } - exports.notify({ eventName: androidActivityPausedEvent, object: androidApp, activity: activity }); + androidApp.notify({ eventName: "activityPaused", object: androidApp, activity: activity }); if (androidApp.onActivityPaused) { androidApp.onActivityPaused(activity); @@ -97,7 +88,7 @@ var initEvents = function () { exports.notify({ eventName: dts.resumeEvent, object: androidApp, android: activity }); } - exports.notify({ eventName: androidActivityResumedEvent, object: androidApp, activity: activity }); + androidApp.notify({ eventName: "activityResumed", object: androidApp, activity: activity }); if (androidApp.onActivityResumed) { androidApp.onActivityResumed(activity); @@ -105,7 +96,7 @@ var initEvents = function () { }, onActivitySaveInstanceState: function (activity: any, bundle: any) { - exports.notify({ eventName: androidSaveActivityStateEvent, object: androidApp, activity: activity, bundle: bundle }); + androidApp.notify({ eventName: "saveActivityState", object: androidApp, activity: activity, bundle: bundle }); if (androidApp.onSaveActivityState) { androidApp.onSaveActivityState(activity, bundle); @@ -115,7 +106,7 @@ var initEvents = function () { onActivityStarted: function (activity: any) { androidApp.foregroundActivity = activity; - exports.notify({ eventName: androidActivityStartedEvent, object: androidApp, activity: activity }); + androidApp.notify({ eventName: "activityStarted", object: androidApp, activity: activity }); if (androidApp.onActivityStarted) { androidApp.onActivityStarted(activity); @@ -123,7 +114,7 @@ var initEvents = function () { }, onActivityStopped: function (activity: any) { - exports.notify({ eventName: androidActivityStoppedEvent, object: androidApp, activity: activity }); + androidApp.notify({ eventName: "activityStopped", object: androidApp, activity: activity }); if (androidApp.onActivityStopped) { androidApp.onActivityStopped(activity); @@ -141,13 +132,11 @@ app.init({ }, onCreate: function () { - var androidApp = new AndroidApplication(this); - exports.android = androidApp; - androidApp.init(); + exports.android.init(this); } }); -class AndroidApplication implements dts.AndroidApplication { +class AndroidApplication extends observable.Observable implements dts.AndroidApplication { public nativeApp: android.app.Application; public context: android.content.Context; public currentContext: android.content.Context; @@ -155,7 +144,6 @@ class AndroidApplication implements dts.AndroidApplication { public startActivity: android.app.Activity; public packageName: string; public hasActionBar: boolean; - // public getActivity: (intent: android.content.Intent) => any; public onActivityCreated: (activity: android.app.Activity, bundle: android.os.Bundle) => void; public onActivityDestroyed: (activity: android.app.Activity) => void; @@ -167,51 +155,7 @@ class AndroidApplication implements dts.AndroidApplication { public onActivityResult: (requestCode: number, resultCode: number, data: android.content.Intent) => void; private _eventsToken: any; - - //private _initialized: boolean; - constructor(nativeApp: any) { - this.nativeApp = nativeApp; - this.packageName = nativeApp.getPackageName(); - this.context = nativeApp.getApplicationContext(); - } - - //private setupUI() { - // // TODO: We probably don't need this flag if onCreate is going to be called once. - // if (!this._initialized) { - // this._initialized = true; - // if (mainModule && mainModule !== "") { - // var mainPage = require(mainModule).Page; - - // if (mainPage instanceof page.Page) { - // this._rootView.addView(mainPage.android); - // // TODO: We need to show ActionBar if there are any toolBar items - // // or if navigation page - showNavigationBar is true - - // var showActionBar = false; - // if (mainPage instanceof page.TabbedPage) { - // showActionBar = true; - // this.startActivity.getActionBar().NavigationMode = android.app.ActionBar.NAVIGATION_MODE_TABS; - // } - // else if (mainPage instanceof page.NavigationPage) { - // showActionBar = (mainPage).showActionBar; - // this.startActivity.getActionBar().NavigationMode = android.app.ActionBar.NAVIGATION_MODE_STANDARD; - // } - - // if (showActionBar) { - // this.startActivity.getActionBar().show(); - // } - // else { - // this.startActivity.getActionBar().hide(); - // } - // } - // else { - // // TODO: Throw exception when/if we remove Page/Frame support. - // } - // } - // } - //} - public getActivity(intent: android.content.Intent): Object { if (intent && intent.getAction() === android.content.Intent.ACTION_MAIN) { // application's main activity @@ -243,7 +187,11 @@ class AndroidApplication implements dts.AndroidApplication { return topFrame.android.onActivityRequested(intent); } - public init() { + public init(nativeApp: any) { + this.nativeApp = nativeApp; + this.packageName = nativeApp.getPackageName(); + this.context = nativeApp.getApplicationContext(); + this._eventsToken = initEvents(); this.nativeApp.registerActivityLifecycleCallbacks(this._eventsToken); this.context = this.nativeApp.getApplicationContext(); @@ -269,3 +217,5 @@ global.__onUncaughtError = function (error: Error) { exports.start = function () { dts.loadCss(); } + +exports.android = new AndroidApplication(); \ No newline at end of file diff --git a/application/application.d.ts b/application/application.d.ts index 09d57ccd5..8de6c5110 100644 --- a/application/application.d.ts +++ b/application/application.d.ts @@ -3,6 +3,7 @@ */ declare module "application" { import cssSelector = require("ui/styling/css-selector"); + import observable = require("data/observable"); /** * An extended JavaScript Error which will have the nativeError property initialized in case the error is caused by executing platform-specific code. @@ -62,6 +63,7 @@ declare module "application" { * The name of the event. */ eventName: string; + /** * The instance that has raised the event. */ @@ -260,100 +262,10 @@ declare module "application" { cancel: boolean; } - /** - * This event is raised on android application ActivityCreated. - */ - export function on(event: "activityCreated", callback: (args: AndroidActivityBundleEventData) => void, thisArg?: any); - - /** - * This event is raised on android application ActivityDestroyed. - */ - export function on(event: "activityDestroyed", callback: (args: AndroidActivityEventData) => void, thisArg?: any); - - /** - * This event is raised on android application ActivityStarted. - */ - export function on(event: "activityStarted", callback: (args: AndroidActivityEventData) => void, thisArg?: any); - - /** - * This event is raised on android application ActivityPaused. - */ - export function on(event: "activityPaused", callback: (args: AndroidActivityEventData) => void, thisArg?: any); - - /** - * This event is raised on android application ActivityResumed. - */ - export function on(event: "activityResumed", callback: (args: AndroidActivityEventData) => void, thisArg?: any); - - /** - * This event is raised on android application ActivityStopped. - */ - export function on(event: "activityStopped", callback: (args: AndroidActivityEventData) => void, thisArg?: any); - - /** - * This event is raised on android application SaveActivityState. - */ - export function on(event: "saveActivityState", callback: (args: AndroidActivityBundleEventData) => void, thisArg?: any); - - /** - * This event is raised on android application ActivityResult. - */ - export function on(event: "activityResult", callback: (args: AndroidActivityResultEventData) => void, thisArg?: any); - - /** - * This event is raised on the back button is pressed in an android application. - */ - export function on(event: "activityBackPressed", callback: (args: AndroidActivityBackPressedEventData) => void, thisArg?: any); - - /** - * String value used when hooking to ActivityCreated event. - */ - export var androidActivityCreatedEvent: string; - - /** - * String value used when hooking to ActivityDestroyed event. - */ - export var androidActivityDestroyedEvent: string; - - /** - * String value used when hooking to ActivityStarted event. - */ - export var androidActivityStartedEvent: string; - - /** - * String value used when hooking to ActivityPaused event. - */ - export var androidActivityPausedEvent: string; - - /** - * String value used when hooking to ActivityResumed event. - */ - export var androidActivityResumedEvent: string; - - /** - * String value used when hooking to ActivityStopped event. - */ - export var androidActivityStoppedEvent: string; - - /** - * String value used when hooking to SaveActivityState event. - */ - export var androidSaveActivityStateEvent: string; - - /** - * String value used when hooking to ActivityResult event. - */ - export var androidActivityResultEvent: string; - - /** - * String value used when hooking to ActivityBackPressed event. - */ - export var androidActivityBackPressedEvent: string; - /** * The abstraction of an Android-specific application object. */ - export class AndroidApplication { + export class AndroidApplication extends observable.Observable { /** * The [android Application](http://developer.android.com/reference/android/app/Application.html) object instance provided to the init of the module. */ @@ -429,7 +341,60 @@ declare module "application" { /** * Direct handler of the onActivityResult method. */ - onActivityResult: (requestCode: number, resultCode: number, data: android.content.Intent) => void + onActivityResult: (requestCode: number, resultCode: number, data: android.content.Intent) => void; + + /** + * A basic method signature to hook an event listener (shortcut alias to the addEventListener method). + * @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change"). + * @param callback - Callback function which will be executed when event is raised. + * @param thisArg - An optional parameter which will be used as `this` context for callback execution. + */ + on(eventNames: string, callback: (data: AndroidActivityEventData) => void, thisArg?: any); + + /** + * This event is raised on android application ActivityCreated. + */ + on(event: "activityCreated", callback: (args: AndroidActivityBundleEventData) => void, thisArg?: any); + + /** + * This event is raised on android application ActivityDestroyed. + */ + on(event: "activityDestroyed", callback: (args: AndroidActivityEventData) => void, thisArg?: any); + + /** + * This event is raised on android application ActivityStarted. + */ + on(event: "activityStarted", callback: (args: AndroidActivityEventData) => void, thisArg?: any); + + /** + * This event is raised on android application ActivityPaused. + */ + on(event: "activityPaused", callback: (args: AndroidActivityEventData) => void, thisArg?: any); + + /** + * This event is raised on android application ActivityResumed. + */ + on(event: "activityResumed", callback: (args: AndroidActivityEventData) => void, thisArg?: any); + + /** + * This event is raised on android application ActivityStopped. + */ + on(event: "activityStopped", callback: (args: AndroidActivityEventData) => void, thisArg?: any); + + /** + * This event is raised on android application SaveActivityState. + */ + on(event: "saveActivityState", callback: (args: AndroidActivityBundleEventData) => void, thisArg?: any); + + /** + * This event is raised on android application ActivityResult. + */ + on(event: "activityResult", callback: (args: AndroidActivityResultEventData) => void, thisArg?: any); + + /** + * This event is raised on the back button is pressed in an android application. + */ + on(event: "activityBackPressed", callback: (args: AndroidActivityBackPressedEventData) => void, thisArg?: any); } /* tslint:disable */ diff --git a/apps/tests/app/app.ts b/apps/tests/app/app.ts index 2357b3271..ec1ccdf87 100644 --- a/apps/tests/app/app.ts +++ b/apps/tests/app/app.ts @@ -66,40 +66,40 @@ application.on(application.uncaughtErrorEvent, function (args: application.Appli if (platform.device.os === platform.platformNames.android) { // Android activity events - application.on(application.androidActivityCreatedEvent, function (args: application.AndroidActivityBundleEventData) { + application.android.on("activityCreated", function (args: application.AndroidActivityBundleEventData) { console.log("Event: " + args.eventName + ", Activity: " + args.activity + ", Bundle: " + args.bundle); }); - application.on(application.androidActivityDestroyedEvent, function (args: application.AndroidActivityEventData) { + application.android.on("activityDestroyed", function (args: application.AndroidActivityEventData) { console.log("Event: " + args.eventName + ", Activity: " + args.activity); }); - application.on(application.androidActivityPausedEvent, function (args: application.AndroidActivityEventData) { + application.android.on("activityPaused", function (args: application.AndroidActivityEventData) { console.log("Event: " + args.eventName + ", Activity: " + args.activity); }); - application.on(application.androidActivityResultEvent, function (args: application.AndroidActivityResultEventData) { + application.android.on("activityResult", function (args: application.AndroidActivityResultEventData) { console.log("Event: " + args.eventName + ", Activity: " + args.activity + ", requestCode: " + args.requestCode + ", resultCode: " + args.resultCode + ", Intent: " + args.intent); }); - application.on(application.androidActivityResumedEvent, function (args: application.AndroidActivityEventData) { + application.android.on("activityResumed", function (args: application.AndroidActivityEventData) { console.log("Event: " + args.eventName + ", Activity: " + args.activity); }); - application.on(application.androidActivityStartedEvent, function (args: application.AndroidActivityEventData) { + application.android.on("activityStarted", function (args: application.AndroidActivityEventData) { console.log("Event: " + args.eventName + ", Activity: " + args.activity); }); - application.on(application.androidActivityStoppedEvent, function (args: application.AndroidActivityEventData) { + application.android.on("activityStopped", function (args: application.AndroidActivityEventData) { console.log("Event: " + args.eventName + ", Activity: " + args.activity); }); - application.on(application.androidSaveActivityStateEvent, function (args: application.AndroidActivityBundleEventData) { + application.android.on("saveActivityState", function (args: application.AndroidActivityBundleEventData) { console.log("Event: " + args.eventName + ", Activity: " + args.activity + ", Bundle: " + args.bundle); }); - application.on(application.androidActivityBackPressedEvent, function (args: application.AndroidActivityBackPressedEventData) { + application.android.on("activityBackPressed", function (args: application.AndroidActivityBackPressedEventData) { console.log("Event: " + args.eventName + ", Activity: " + args.activity); }); } diff --git a/ui/frame/frame.android.ts b/ui/frame/frame.android.ts index 2abaed24e..45dc84146 100644 --- a/ui/frame/frame.android.ts +++ b/ui/frame/frame.android.ts @@ -427,8 +427,8 @@ var NativeActivity = { result(requestCode, resultCode, data); } - application.notify({ - eventName: application.androidActivityResultEvent, + application.android.notify({ + eventName: "activityResult", object: application.android, activity: this, requestCode: requestCode, @@ -496,12 +496,12 @@ var NativeActivity = { trace.write("NativeScriptActivity.onBackPressed;", trace.categories.NativeLifecycle); var args = { - eventName: application.androidActivityBackPressedEvent, + eventName: "activityBackPressed", object: application.android, activity: this, cancel: false, }; - application.notify(args); + application.android.notify(args); if (args.cancel) { return;