FrameClass implemented as TypeScript class

NativeActivity is now NativeScriptActivity and is real class
Added NativeScriptApplication class
Added JavaProxy attribute to FrameClass, NativeScriptActivity & NativeScriptApplication
This commit is contained in:
Hristo Hristov
2016-02-29 09:57:45 +02:00
parent 22631ce102
commit 6273bb8d2f
7 changed files with 316 additions and 358 deletions

View File

@@ -8,6 +8,34 @@ import * as fileResolverModule from "file-system/file-name-resolver";
global.moduleMerge(appModule, exports);
var typedExports: typeof definition = exports;
@JavaProxy("com.tns.NativeScriptApplication")
class NativeScriptApplication extends android.app.Application {
constructor() {
super();
return global.__native(this);
}
public onCreate(): void {
androidApp.init(this);
setupOrientationListener(androidApp);
}
public onLowMemory(): void {
gc();
java.lang.System.gc();
super.onLowMemory();
typedExports.notify(<definition.ApplicationEventData>{ eventName: typedExports.lowMemoryEvent, object: this, android: this });
}
public onTrimMemory(level: number): void {
gc();
java.lang.System.gc();
super.onTrimMemory(level);
}
}
// 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"
@@ -15,13 +43,8 @@ function initEvents() {
// TODO: Verify whether the logic for triggerring application-wide events based on Activity callbacks is working properly
var lifecycleCallbacks = new android.app.Application.ActivityLifecycleCallbacks({
onActivityCreated: function (activity: any, bundle: any) {
if (!(activity instanceof (<any>com).tns.NativeScriptActivity)) {
return;
}
if (!androidApp.startActivity) {
androidApp.startActivity = activity;
androidApp.notify(<definition.AndroidActivityBundleEventData>{ eventName: "activityCreated", object: androidApp, activity: activity, bundle: bundle });
if (androidApp.onActivityCreated) {
@@ -33,9 +56,6 @@ function initEvents() {
},
onActivityDestroyed: function (activity: any) {
if (!(activity instanceof (<any>com).tns.NativeScriptActivity)) {
return;
}
// Clear the current activity reference to prevent leak
if (activity === androidApp.foregroundActivity) {
@@ -67,10 +87,6 @@ function initEvents() {
},
onActivityPaused: function (activity: any) {
if (!(activity instanceof (<any>com).tns.NativeScriptActivity)) {
return;
}
androidApp.paused = true;
if (activity === androidApp.foregroundActivity) {
@@ -89,10 +105,6 @@ function initEvents() {
},
onActivityResumed: function (activity: any) {
if (!(activity instanceof (<any>com).tns.NativeScriptActivity)) {
return;
}
androidApp.paused = false;
if (activity === androidApp.foregroundActivity) {
@@ -111,10 +123,6 @@ function initEvents() {
},
onActivitySaveInstanceState: function (activity: any, bundle: any) {
if (!(activity instanceof (<any>com).tns.NativeScriptActivity)) {
return;
}
androidApp.notify(<definition.AndroidActivityBundleEventData>{ eventName: "saveActivityState", object: androidApp, activity: activity, bundle: bundle });
if (androidApp.onSaveActivityState) {
@@ -123,10 +131,6 @@ function initEvents() {
},
onActivityStarted: function (activity: any) {
if (!(activity instanceof (<any>com).tns.NativeScriptActivity)) {
return;
}
androidApp.foregroundActivity = activity;
androidApp.notify(<definition.AndroidActivityEventData>{ eventName: "activityStarted", object: androidApp, activity: activity });
@@ -137,10 +141,6 @@ function initEvents() {
},
onActivityStopped: function (activity: any) {
if (!(activity instanceof (<any>com).tns.NativeScriptActivity)) {
return;
}
androidApp.notify(<definition.AndroidActivityEventData>{ eventName: "activityStopped", object: androidApp, activity: activity });
if (androidApp.onActivityStopped) {
@@ -152,7 +152,7 @@ function initEvents() {
return lifecycleCallbacks;
}
export class AndroidApplication extends observable.Observable implements definition.AndroidApplication {
class AndroidApplication extends observable.Observable implements definition.AndroidApplication {
public static activityCreatedEvent = "activityCreated";
public static activityDestroyedEvent = "activityDestroyed";
public static activityStartedEvent = "activityStarted";
@@ -190,10 +190,6 @@ export class AndroidApplication extends observable.Observable implements definit
private _eventsToken: any;
public getActivity(intent: android.content.Intent): Object {
return frame.getActivity();
}
public init(nativeApp: any) {
this.nativeApp = nativeApp;
this.packageName = nativeApp.getPackageName();
@@ -245,6 +241,10 @@ export class AndroidApplication extends observable.Observable implements definit
}
}
var androidApp = new AndroidApplication();
// use the exports object instead of 'export var' due to global namespace collision
typedExports.android = androidApp;
var BroadcastReceiverClass;
function ensureBroadCastReceiverClass() {
if (BroadcastReceiverClass) {
@@ -270,21 +270,6 @@ function ensureBroadCastReceiverClass() {
BroadcastReceiverClass = BroadcastReceiver;
}
global.__onUncaughtError = function (error: definition.NativeScriptError) {
var types: typeof typesModule = require("utils/types");
// TODO: Obsolete this
if (types.isFunction(typedExports.onUncaughtError)) {
typedExports.onUncaughtError(error);
}
typedExports.notify({ eventName: typedExports.uncaughtErrorEvent, object: appModule.android, android: error });
}
function loadCss() {
typedExports.cssSelectorsCache = typedExports.loadCss(typedExports.cssFile);
}
var started = false;
export function start(entry?: frame.NavigationEntry) {
if (started) {
@@ -292,31 +277,13 @@ export function start(entry?: frame.NavigationEntry) {
}
started = true;
if (entry) {
typedExports.mainEntry = entry;
}
// this should be the first call, to avoid issues when someone accesses the Application singleton prior to extending its onCreate method
app.init({
getActivity: function (activity: android.app.Activity) {
var intent = activity.getIntent()
return androidApp.getActivity(intent);
},
onCreate: function () {
androidApp.init(this);
setupOrientationListener(androidApp);
}
});
loadCss();
}
var androidApp = new AndroidApplication();
// use the exports object instead of 'export var' due to global namespace collision
typedExports.android = androidApp;
var currentOrientation: number;
function setupOrientationListener(androidApp: AndroidApplication) {
androidApp.registerBroadcastReceiver(android.content.Intent.ACTION_CONFIGURATION_CHANGED, onConfigurationChanged);
@@ -353,6 +320,10 @@ function onConfigurationChanged(context: android.content.Context, intent: androi
}
}
function loadCss() {
typedExports.cssSelectorsCache = typedExports.loadCss(typedExports.cssFile);
}
global.__onLiveSync = function () {
if (typedExports.android && typedExports.android.paused) {
return;
@@ -368,4 +339,15 @@ global.__onLiveSync = function () {
// Reload current page.
frame.reloadPage();
}
global.__onUncaughtError = function (error: definition.NativeScriptError) {
var types: typeof typesModule = require("utils/types");
// TODO: Obsolete this
if (types.isFunction(typedExports.onUncaughtError)) {
typedExports.onUncaughtError(error);
}
typedExports.notify({ eventName: typedExports.uncaughtErrorEvent, object: appModule.android, android: error });
}

View File

@@ -347,13 +347,6 @@ declare module "application" {
*/
paused: boolean;
/**
* This method is called by the JavaScript Bridge when navigation to a new activity is triggered.
* @param intent - Native (android) intent used to create the activity.
* Returns com.tns.NativeScriptActivity.extend implementation.
*/
getActivity(intent: any /* android.content.Intent */): any;
/**
* [Deprecated. Please use the respective event instead.] Direct handler of the [onActivityCreated method](http://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks.html).
*/