mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +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:
@@ -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;
|
||||
@@ -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(<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();
|
||||
Reference in New Issue
Block a user