mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
New BCL approach & BuildTasks
This commit is contained in:
131
Application/application.android.ts
Normal file
131
Application/application.android.ts
Normal file
@@ -0,0 +1,131 @@
|
||||
import app_common_module = require("Application/application_common");
|
||||
var currentApp = app_common_module.tk.ui.Application.current;
|
||||
|
||||
export module tk {
|
||||
// TODO: This is tricky, we have a module named android down in the hierarchy and we need to declare it here
|
||||
var callbacks = android.app.Application.ActivityLifecycleCallbacks;
|
||||
|
||||
export module ui {
|
||||
export module android {
|
||||
var initEvents = function () {
|
||||
var androidApp = <Application>app_common_module.tk.ui.Application.current.android;
|
||||
|
||||
var lifecycleCallbacks = new callbacks({
|
||||
onActivityCreated: function (activity: any, bundle: any) {
|
||||
if (!androidApp.startActivity) {
|
||||
androidApp.startActivity = activity;
|
||||
|
||||
//if (UI.Application.current.onLaunch) {
|
||||
// UI.Application.current.onLaunch();
|
||||
//}
|
||||
|
||||
if (androidApp.onActivityCreated) {
|
||||
androidApp.onActivityCreated(activity, bundle);
|
||||
}
|
||||
}
|
||||
},
|
||||
onActivityDestroyed: function (activity: any) {
|
||||
// Clear the current activity reference to prevent leak
|
||||
if (activity === androidApp.currentActivity) {
|
||||
androidApp.currentActivity = undefined;
|
||||
}
|
||||
|
||||
//if (activity === UI.Application.android.startActivity) {
|
||||
// UI.Application.android.currentActivity = undefined;
|
||||
// if (UI.Application.current.onExit) {
|
||||
// UI.Application.current.onExit();
|
||||
// }
|
||||
//}
|
||||
|
||||
if (androidApp.onActivityDestroyed) {
|
||||
androidApp.onActivityDestroyed(activity);
|
||||
}
|
||||
},
|
||||
onActivityPaused: function (activity: any) {
|
||||
//if (UI.Application.android.currentActivity === activity) {
|
||||
// if (UI.Application.current.onSuspend) {
|
||||
// UI.Application.current.onSuspend();
|
||||
// }
|
||||
//}
|
||||
|
||||
if (androidApp.onActivityPaused) {
|
||||
androidApp.onActivityPaused(activity);
|
||||
}
|
||||
},
|
||||
onActivityResumed: function (activity: any) {
|
||||
//if (UI.Application.android.currentActivity === activity) {
|
||||
// if (UI.Application.current.onResume) {
|
||||
// UI.Application.current.onResume();
|
||||
// }
|
||||
//}
|
||||
|
||||
if (androidApp.onActivityResumed) {
|
||||
androidApp.onActivityResumed(activity);
|
||||
}
|
||||
},
|
||||
onActivitySaveInstanceState: function (activity: any, bundle: any) {
|
||||
if (androidApp.onSaveActivityState) {
|
||||
androidApp.onSaveActivityState(activity, bundle);
|
||||
}
|
||||
},
|
||||
onActivityStarted: function (activity: any) {
|
||||
androidApp.currentActivity = activity;
|
||||
|
||||
//if (activity === UI.Application.android.startActivity) {
|
||||
// if (UI.Application.current.onStart) {
|
||||
// UI.Application.current.onStart();
|
||||
// }
|
||||
//}
|
||||
|
||||
if (androidApp.onActivityStarted) {
|
||||
androidApp.onActivityStarted(activity);
|
||||
}
|
||||
},
|
||||
onActivityStopped: function (activity: any) {
|
||||
if (androidApp.onActivityStopped) {
|
||||
androidApp.onActivityStopped(activity);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return lifecycleCallbacks;
|
||||
}
|
||||
|
||||
export var initApp = function (nativeApp: any) {
|
||||
var app = new Application(nativeApp);
|
||||
currentApp.os = app_common_module.tk.TargetOS.Android;
|
||||
currentApp.android = app;
|
||||
app.init();
|
||||
}
|
||||
|
||||
class Application {
|
||||
public nativeApp: any;
|
||||
public context: any;
|
||||
public currentActivity: any;
|
||||
public startActivity: any;
|
||||
public packageName: string;
|
||||
|
||||
// TODO: Provide type information once definitions are done - e.g. activity: android.widget.activity
|
||||
public onActivityCreated: (activity: any, bundle: any) => any;
|
||||
public onActivityDestroyed: (activity: any) => any;
|
||||
public onActivityStarted: (activity: any) => any;
|
||||
public onActivityPaused: (activity: any) => any;
|
||||
public onActivityResumed: (activity: any) => any;
|
||||
public onActivityStopped: (activity: any) => any;
|
||||
public onSaveActivityState: (activity: any, bundle: any) => any;
|
||||
|
||||
private _eventsToken: any;
|
||||
|
||||
constructor(nativeApp: any) {
|
||||
this.nativeApp = nativeApp;
|
||||
this.packageName = nativeApp.getPackageName();
|
||||
}
|
||||
|
||||
public init() {
|
||||
this._eventsToken = initEvents();
|
||||
this.nativeApp.registerActivityLifecycleCallbacks(this._eventsToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
50
Application/application.d.ts
vendored
Normal file
50
Application/application.d.ts
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
export declare module tk {
|
||||
export enum TargetOS {
|
||||
iOS,
|
||||
Android,
|
||||
//WP
|
||||
}
|
||||
|
||||
export module ui {
|
||||
export class Application {
|
||||
static current: Application;
|
||||
public os: TargetOS;
|
||||
|
||||
public onLaunch: () => any;
|
||||
public onSuspend: () => any;
|
||||
public onResume: () => any;
|
||||
public onExit: () => any;
|
||||
public onLowMemory: () => any;
|
||||
|
||||
public android: android.Application;
|
||||
public ios: ios.Application;
|
||||
}
|
||||
|
||||
export module android {
|
||||
export function initApp(nativeApp: any);
|
||||
export class Application {
|
||||
public nativeApp: any; // TODO: android.app
|
||||
public context: any; // TODO: android.context
|
||||
public currentActivity: any; // TODO: android.activity
|
||||
public startActivity: any; // TODO: android.activity
|
||||
public packageName: string;
|
||||
|
||||
// TODO: Provide type information once definitions are done - e.g. activity: android.widget.activity
|
||||
public onActivityCreated: (activity: any, bundle: any) => any;
|
||||
public onActivityDestroyed: (activity: any) => any;
|
||||
public onActivityStarted: (activity: any) => any;
|
||||
public onActivityPaused: (activity: any) => any;
|
||||
public onActivityResumed: (activity: any) => any;
|
||||
public onActivityStopped: (activity: any) => any;
|
||||
public onSaveActivityState: (activity: any, bundle: any) => any;
|
||||
}
|
||||
}
|
||||
|
||||
export module ios {
|
||||
export function initApp(nativeApp: any);
|
||||
export class Application {
|
||||
public rootController: any;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
91
Application/application.ios.ts
Normal file
91
Application/application.ios.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import app_common_module = require("Application/application_common");
|
||||
var currentApp = app_common_module.tk.ui.Application.current;
|
||||
|
||||
export module tk {
|
||||
export module ui {
|
||||
export module ios {
|
||||
// TODO: Declarations
|
||||
export var initApp = function (nativeApp: any) {
|
||||
var app = new Application(nativeApp);
|
||||
currentApp.os = app_common_module.tk.TargetOS.iOS;
|
||||
currentApp.ios = app;
|
||||
app.init();
|
||||
}
|
||||
|
||||
class Application {
|
||||
public nativeApp: any;
|
||||
public rootController: any;
|
||||
|
||||
constructor(nativeApp: any) {
|
||||
this.nativeApp = nativeApp;
|
||||
}
|
||||
|
||||
public init() {
|
||||
UIKit.UIResponder.extends({/*TODO: Empty parameter here, needs API improvement*/}, {
|
||||
name: "KimeraAppDelegate",
|
||||
}).extends({
|
||||
protocol: "UIApplicationDelegate",
|
||||
implementation: {
|
||||
applicationDidFinishLaunchingWithOptions: function () {
|
||||
log("Application launched: applicationDidFinishLaunchingWithOptions.");
|
||||
|
||||
this.window = new UIKit.UIWindow(UIKit.UIScreen.mainScreen().bounds());
|
||||
this.window.setBackgroundColor(UIKit.UIColor.whiteColor());
|
||||
|
||||
var iosApp = <Application>currentApp.ios;
|
||||
this.window.setRootViewController(iosApp.rootController);
|
||||
|
||||
if (currentApp.onLaunch) {
|
||||
currentApp.onLaunch();
|
||||
} else {
|
||||
log("Missing TK.UI.Application.current.onLaunch");
|
||||
}
|
||||
|
||||
this.window.makeKeyAndVisible();
|
||||
|
||||
log("applicationDidFinishLaunchingWithOptions finished.");
|
||||
return true;
|
||||
},
|
||||
|
||||
applicationDidBecomeActive: function (application) {
|
||||
log("applicationDidBecomeActive: " + application);
|
||||
if (currentApp.onResume) {
|
||||
currentApp.onResume();
|
||||
}
|
||||
},
|
||||
|
||||
applicationWillResignActive: function (application) {
|
||||
log("applicationWillResignActive: " + application);
|
||||
},
|
||||
|
||||
applicationDidEnterBackground: function (application) {
|
||||
log("applicationDidEnterBackground: " + application);
|
||||
if (currentApp.onSuspend) {
|
||||
currentApp.onSuspend();
|
||||
}
|
||||
},
|
||||
|
||||
applicationWillEnterForeground: function (application) {
|
||||
log("applicationWillEnterForeground: " + application);
|
||||
},
|
||||
|
||||
applicationWillTerminate: function (application) {
|
||||
log("applicationWillTerminate: " + application);
|
||||
if (currentApp.onExit) {
|
||||
currentApp.onExit();
|
||||
}
|
||||
},
|
||||
|
||||
applicationDidReceiveMemoryWarning: function (application) {
|
||||
log("applicationDidReceiveMemoryWarning: " + application);
|
||||
if (currentApp.onLowMemory) {
|
||||
currentApp.onLowMemory();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
26
Application/application_common.ts
Normal file
26
Application/application_common.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
export module tk {
|
||||
export enum TargetOS {
|
||||
iOS,
|
||||
Android,
|
||||
//WP
|
||||
}
|
||||
|
||||
export module ui {
|
||||
export class Application {
|
||||
|
||||
public os: TargetOS;
|
||||
|
||||
public onLaunch: () => any;
|
||||
public onSuspend: () => any;
|
||||
public onResume: () => any;
|
||||
public onExit: () => any;
|
||||
public onLowMemory: () => any;
|
||||
|
||||
public static current: Application = new Application();
|
||||
|
||||
// TODO: These fields are declared by the application.d.ts file and intellisense will come from there
|
||||
public android: any;
|
||||
public ios: any;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user