mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
Moved Activity related Android-specific events to application.android instance. Renamed cross-platform events by deleting their "event" suffix.
This commit is contained in:
@ -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"
|
||||
|
||||
|
@ -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(<dts.AndroidActivityBundleEventData>{ eventName: androidActivityCreatedEvent, object: androidApp, activity: activity, bundle: bundle });
|
||||
androidApp.notify(<dts.AndroidActivityBundleEventData>{ 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(<dts.AndroidActivityEventData>{ eventName: androidActivityDestroyedEvent, object: androidApp, activity: activity });
|
||||
androidApp.notify(<dts.AndroidActivityEventData>{ eventName: "activityDestroyed", object: androidApp, activity: activity });
|
||||
|
||||
if (androidApp.onActivityDestroyed) {
|
||||
androidApp.onActivityDestroyed(activity);
|
||||
@ -81,7 +72,7 @@ var initEvents = function () {
|
||||
exports.notify(<dts.ApplicationEventData>{ eventName: dts.suspendEvent, object: androidApp, android: activity });
|
||||
}
|
||||
|
||||
exports.notify(<dts.AndroidActivityEventData>{ eventName: androidActivityPausedEvent, object: androidApp, activity: activity });
|
||||
androidApp.notify(<dts.AndroidActivityEventData>{ eventName: "activityPaused", object: androidApp, activity: activity });
|
||||
|
||||
if (androidApp.onActivityPaused) {
|
||||
androidApp.onActivityPaused(activity);
|
||||
@ -97,7 +88,7 @@ var initEvents = function () {
|
||||
exports.notify(<dts.ApplicationEventData>{ eventName: dts.resumeEvent, object: androidApp, android: activity });
|
||||
}
|
||||
|
||||
exports.notify(<dts.AndroidActivityEventData>{ eventName: androidActivityResumedEvent, object: androidApp, activity: activity });
|
||||
androidApp.notify(<dts.AndroidActivityEventData>{ 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(<dts.AndroidActivityBundleEventData>{ eventName: androidSaveActivityStateEvent, object: androidApp, activity: activity, bundle: bundle });
|
||||
androidApp.notify(<dts.AndroidActivityBundleEventData>{ 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(<dts.AndroidActivityEventData>{ eventName: androidActivityStartedEvent, object: androidApp, activity: activity });
|
||||
androidApp.notify(<dts.AndroidActivityEventData>{ 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(<dts.AndroidActivityEventData>{ eventName: androidActivityStoppedEvent, object: androidApp, activity: activity });
|
||||
androidApp.notify(<dts.AndroidActivityEventData>{ 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;
|
||||
@ -168,50 +156,6 @@ class AndroidApplication implements dts.AndroidApplication {
|
||||
|
||||
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(<android.view.View>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 = (<page.NavigationPage>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();
|
149
application/application.d.ts
vendored
149
application/application.d.ts
vendored
@ -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 */
|
||||
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
@ -427,8 +427,8 @@ var NativeActivity = {
|
||||
result(requestCode, resultCode, data);
|
||||
}
|
||||
|
||||
application.notify(<application.AndroidActivityResultEventData>{
|
||||
eventName: application.androidActivityResultEvent,
|
||||
application.android.notify(<application.AndroidActivityResultEventData>{
|
||||
eventName: "activityResult",
|
||||
object: application.android,
|
||||
activity: this,
|
||||
requestCode: requestCode,
|
||||
@ -496,12 +496,12 @@ var NativeActivity = {
|
||||
trace.write("NativeScriptActivity.onBackPressed;", trace.categories.NativeLifecycle);
|
||||
|
||||
var args = <application.AndroidActivityBackPressedEventData>{
|
||||
eventName: application.androidActivityBackPressedEvent,
|
||||
eventName: "activityBackPressed",
|
||||
object: application.android,
|
||||
activity: this,
|
||||
cancel: false,
|
||||
};
|
||||
application.notify(args);
|
||||
application.android.notify(args);
|
||||
|
||||
if (args.cancel) {
|
||||
return;
|
||||
|
Reference in New Issue
Block a user