mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
Modified the Application module. Removed the Application class and added its methods to the exports directly.
This commit is contained in:
@ -7,7 +7,7 @@ require("Utils/module_merge").merge(appModule, exports);
|
|||||||
var callbacks = android.app.Application.ActivityLifecycleCallbacks;
|
var callbacks = android.app.Application.ActivityLifecycleCallbacks;
|
||||||
|
|
||||||
var initEvents = function () {
|
var initEvents = function () {
|
||||||
var androidApp = appModule.current.android;
|
var androidApp = exports.android;
|
||||||
var lifecycleCallbacks = new callbacks({
|
var lifecycleCallbacks = new callbacks({
|
||||||
onActivityCreated: function (activity: any, bundle: any) {
|
onActivityCreated: function (activity: any, bundle: any) {
|
||||||
if (!androidApp.startActivity) {
|
if (!androidApp.startActivity) {
|
||||||
@ -87,8 +87,7 @@ var initEvents = function () {
|
|||||||
|
|
||||||
export var init = function (nativeApp: android.app.Application) {
|
export var init = function (nativeApp: android.app.Application) {
|
||||||
var app = new AndroidApplication(nativeApp);
|
var app = new AndroidApplication(nativeApp);
|
||||||
appModule.current.os = appModule.TargetOS.Android;
|
exports.android = app;
|
||||||
appModule.current.android = app;
|
|
||||||
app.init();
|
app.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
88
Application/application.d.ts
vendored
88
Application/application.d.ts
vendored
@ -1,67 +1,42 @@
|
|||||||
/**
|
/**
|
||||||
* Defines the available target operating systems.
|
* The main entry point event. This method is expected to return an instance of the root UI for the application.
|
||||||
|
* This will be an Activity extends for Android and a RootViewController for iOS.
|
||||||
*/
|
*/
|
||||||
export declare enum TargetOS {
|
export declare function onLaunch(): any;
|
||||||
/**
|
|
||||||
* iOS operating system.
|
|
||||||
*/
|
|
||||||
iOS,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Android operating system.
|
|
||||||
*/
|
|
||||||
Android
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The abstraction of an Application object, common for each target OS.
|
* This method will be called when the Application is suspended.
|
||||||
*/
|
*/
|
||||||
export declare class Application {
|
export declare function onSuspend();
|
||||||
/**
|
|
||||||
* The target operating system of the application.
|
|
||||||
*/
|
|
||||||
public os: TargetOS;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main entry point event. This method is expected to return an instance of the root UI for the application.
|
* This method will be called when the Application is resumed after it has been suspended.
|
||||||
* This will be an Activity extends for Android and a RootViewController for iOS.
|
*/
|
||||||
*/
|
export declare function onResume();
|
||||||
public onLaunch: () => any;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method will be called when the Application is suspended.
|
* This method will be called when the Application is about to exit.
|
||||||
*/
|
*/
|
||||||
public onSuspend: () => any;
|
export declare function onExit();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method will be called when the Application is resumed after it has been suspended.
|
* This method will be called when there is low memory on the target device.
|
||||||
*/
|
*/
|
||||||
public onResume: () => any;
|
export declare function onLowMemory();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method will be called when the Application is about to exit.
|
* This is the Android-specific application object instance.
|
||||||
*/
|
* Encapsulates methods and properties specific to the Android platform.
|
||||||
public onExit: () => any;
|
* Will be undefined when TargetOS is iOS.
|
||||||
|
*/
|
||||||
|
export declare var android: AndroidApplication;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method will be called when there is low memory on the target device.
|
* This is the iOS-specific application object instance.
|
||||||
*/
|
* Encapsulates methods and properties specific to the iOS platform.
|
||||||
public onLowMemory: () => any;
|
* Will be undefined when TargetOS is Android.
|
||||||
|
*/
|
||||||
/**
|
export declare var ios: iOSApplication;
|
||||||
* This is the Android-specific application object instance.
|
|
||||||
* Encapsulates methods and properties specific to the Android platform.
|
|
||||||
* Will be undefined when TargetOS is iOS.
|
|
||||||
*/
|
|
||||||
public android: AndroidApplication;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is the iOS-specific application object instance.
|
|
||||||
* Encapsulates methods and properties specific to the iOS platform.
|
|
||||||
* Will be undefined when TargetOS is Android.
|
|
||||||
*/
|
|
||||||
public ios: iOSApplication;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The abstraction of an Android-specific application object.
|
* The abstraction of an Android-specific application object.
|
||||||
@ -148,8 +123,3 @@ export declare class iOSApplication {
|
|||||||
* Entry point for the module. Initializes the Application singleton and hooks application lifecycle events.
|
* Entry point for the module. Initializes the Application singleton and hooks application lifecycle events.
|
||||||
*/
|
*/
|
||||||
export declare function init(nativeApp: any);
|
export declare function init(nativeApp: any);
|
||||||
|
|
||||||
/**
|
|
||||||
* The current singleton instance of the application object.
|
|
||||||
*/
|
|
||||||
export declare var current: Application;
|
|
@ -29,8 +29,7 @@ require("Utils/module_merge").merge(appModule, exports);
|
|||||||
// TODO: Declarations
|
// TODO: Declarations
|
||||||
export var init = function (nativeApp: any) {
|
export var init = function (nativeApp: any) {
|
||||||
var app = new iOSApplication(nativeApp);
|
var app = new iOSApplication(nativeApp);
|
||||||
appModule.current.os = appModule.TargetOS.iOS;
|
exports.ios = app;
|
||||||
appModule.current.ios = app;
|
|
||||||
app.init();
|
app.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,10 +54,8 @@ class iOSApplication {
|
|||||||
this.window.backgroundColor = UIKit.UIColor.whiteColor();
|
this.window.backgroundColor = UIKit.UIColor.whiteColor();
|
||||||
this.window.makeKeyAndVisible();
|
this.window.makeKeyAndVisible();
|
||||||
|
|
||||||
var iosApp = appModule.current.ios;
|
if (appModule.onLaunch) {
|
||||||
|
this.window.rootViewController = appModule.onLaunch();
|
||||||
if (appModule.current.onLaunch) {
|
|
||||||
this.window.rootViewController = appModule.current.onLaunch();
|
|
||||||
} else {
|
} else {
|
||||||
log("Missing TK.UI.Application.current.onLaunch");
|
log("Missing TK.UI.Application.current.onLaunch");
|
||||||
}
|
}
|
||||||
@ -69,8 +66,8 @@ class iOSApplication {
|
|||||||
|
|
||||||
applicationDidBecomeActive: function (application) {
|
applicationDidBecomeActive: function (application) {
|
||||||
log("applicationDidBecomeActive: " + application);
|
log("applicationDidBecomeActive: " + application);
|
||||||
if (appModule.current.onResume) {
|
if (appModule.onResume) {
|
||||||
appModule.current.onResume();
|
appModule.onResume();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -80,8 +77,8 @@ class iOSApplication {
|
|||||||
|
|
||||||
applicationDidEnterBackground: function (application) {
|
applicationDidEnterBackground: function (application) {
|
||||||
log("applicationDidEnterBackground: " + application);
|
log("applicationDidEnterBackground: " + application);
|
||||||
if (appModule.current.onSuspend) {
|
if (appModule.onSuspend) {
|
||||||
appModule.current.onSuspend();
|
appModule.onSuspend();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -91,15 +88,15 @@ class iOSApplication {
|
|||||||
|
|
||||||
applicationWillTerminate: function (application) {
|
applicationWillTerminate: function (application) {
|
||||||
log("applicationWillTerminate: " + application);
|
log("applicationWillTerminate: " + application);
|
||||||
if (appModule.current.onExit) {
|
if (appModule.onExit) {
|
||||||
appModule.current.onExit();
|
appModule.onExit();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
applicationDidReceiveMemoryWarning: function (application) {
|
applicationDidReceiveMemoryWarning: function (application) {
|
||||||
log("applicationDidReceiveMemoryWarning: " + application);
|
log("applicationDidReceiveMemoryWarning: " + application);
|
||||||
if (appModule.current.onLowMemory) {
|
if (appModule.onLowMemory) {
|
||||||
appModule.current.onLowMemory();
|
appModule.onLowMemory();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,23 @@
|
|||||||
import consoleModule = require("Console/console");
|
import consoleModule = require("Console/console");
|
||||||
|
|
||||||
export enum TargetOS {
|
// TODO: This is put in the global context, is this the preferred approach
|
||||||
iOS,
|
console = new consoleModule.Console();
|
||||||
Android
|
|
||||||
|
export var onLaunch = function (): any {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Application {
|
export var onSuspend = function (): void {
|
||||||
public os: TargetOS;
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
// TODO: This is put in the global context, is this the preferred approach
|
|
||||||
console = new consoleModule.Console();
|
|
||||||
}
|
|
||||||
|
|
||||||
public onLaunch: () => any;
|
|
||||||
public onSuspend: () => any;
|
|
||||||
public onResume: () => any;
|
|
||||||
public onExit: () => any;
|
|
||||||
public onLowMemory: () => any;
|
|
||||||
|
|
||||||
// TODO: These fields are declared by the application.d.ts file and intellisense will come from there
|
|
||||||
public android: any;
|
|
||||||
public ios: any;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export var current = new Application();
|
export var onResume = function (): void {
|
||||||
|
}
|
||||||
|
|
||||||
|
export var onExit = function (): void {
|
||||||
|
}
|
||||||
|
|
||||||
|
export var onLowMemory = function (): void {
|
||||||
|
}
|
||||||
|
|
||||||
|
export var android = undefined;
|
||||||
|
|
||||||
|
export var ios = undefined;
|
@ -6,7 +6,7 @@ var REQUEST_SELECT_PICTURE: number = 2;
|
|||||||
export class CameraManager {
|
export class CameraManager {
|
||||||
public takePicture(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any) {
|
public takePicture(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any) {
|
||||||
var takePictureIntent = new android.content.Intent('android.media.action.IMAGE_CAPTURE');
|
var takePictureIntent = new android.content.Intent('android.media.action.IMAGE_CAPTURE');
|
||||||
var androidApp = appModule.current.android;
|
var androidApp = appModule.android;
|
||||||
|
|
||||||
if (takePictureIntent.resolveActivity(androidApp.context.getPackageManager()) !== null) {
|
if (takePictureIntent.resolveActivity(androidApp.context.getPackageManager()) !== null) {
|
||||||
androidApp.currentActivity.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
|
androidApp.currentActivity.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
|
||||||
@ -16,7 +16,7 @@ export class CameraManager {
|
|||||||
// options { useSavedPhotos: true }
|
// options { useSavedPhotos: true }
|
||||||
public pictureFromLibrary(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any) {
|
public pictureFromLibrary(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any) {
|
||||||
var readPictureIntent = new android.content.Intent();
|
var readPictureIntent = new android.content.Intent();
|
||||||
var androidApp = appModule.current.android;
|
var androidApp = appModule.android;
|
||||||
|
|
||||||
readPictureIntent.setType('image/*');
|
readPictureIntent.setType('image/*');
|
||||||
readPictureIntent.setAction('android.intent.action.GET_CONTENT');
|
readPictureIntent.setAction('android.intent.action.GET_CONTENT');
|
||||||
|
@ -207,13 +207,13 @@ export class FileSystemAccess {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public getDocumentsFolderPath(): string {
|
public getDocumentsFolderPath(): string {
|
||||||
var context = appModule.current.android.context;
|
var context = appModule.android.context;
|
||||||
var dir = context.getFilesDir();
|
var dir = context.getFilesDir();
|
||||||
return dir.getAbsolutePath();
|
return dir.getAbsolutePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
public getTempFolderPath(): string {
|
public getTempFolderPath(): string {
|
||||||
var context = appModule.current.android.context;
|
var context = appModule.android.context;
|
||||||
var dir = context.getCacheDir();
|
var dir = context.getCacheDir();
|
||||||
return dir.getAbsolutePath();
|
return dir.getAbsolutePath();
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ export class Image {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public loadFromResource(name: string): boolean {
|
public loadFromResource(name: string): boolean {
|
||||||
var androidApp = appModule.current.android;
|
var androidApp = appModule.android;
|
||||||
var res = androidApp.context.getResources();
|
var res = androidApp.context.getResources();
|
||||||
if (res) {
|
if (res) {
|
||||||
var identifier: number = res.getIdentifier(name, 'drawable', androidApp.packageName);
|
var identifier: number = res.getIdentifier(name, 'drawable', androidApp.packageName);
|
||||||
|
@ -59,7 +59,7 @@ export class LocationManager {
|
|||||||
public static isEnabled(): boolean {
|
public static isEnabled(): boolean {
|
||||||
var criteria = new android.location.Criteria();
|
var criteria = new android.location.Criteria();
|
||||||
criteria.setAccuracy(1); // low ? fine ? who knows what 1 means (bug in android docs?)
|
criteria.setAccuracy(1); // low ? fine ? who knows what 1 means (bug in android docs?)
|
||||||
var lm = appModule.current.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
|
var lm = appModule.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
|
||||||
return (lm.getBestProvider(criteria, true) != null) ? true : false;
|
return (lm.getBestProvider(criteria, true) != null) ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ export class LocationManager {
|
|||||||
this.minimumUpdateTime = 200;
|
this.minimumUpdateTime = 200;
|
||||||
this.isStarted = false;
|
this.isStarted = false;
|
||||||
|
|
||||||
this.androidLocationManager = appModule.current.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
|
this.androidLocationManager = appModule.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////
|
////////////////////////
|
||||||
|
@ -5,7 +5,7 @@ export class UserPreferences {
|
|||||||
private sharedPreferences: any;
|
private sharedPreferences: any;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.sharedPreferences = appModule.current.android.context.getSharedPreferences("prefs.db", 0);
|
this.sharedPreferences = appModule.android.context.getSharedPreferences("prefs.db", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public containsKey(key: string): boolean {
|
public containsKey(key: string): boolean {
|
||||||
|
@ -11,7 +11,7 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
|
|||||||
|
|
||||||
var isImage = options.url.match(/\.(jpeg|jpg|gif|png)$/i) != null;
|
var isImage = options.url.match(/\.(jpeg|jpg|gif|png)$/i) != null;
|
||||||
|
|
||||||
var context = require("Application/application").Application.current.android.context;
|
var context = require("Application/application").android.context;
|
||||||
|
|
||||||
if (isImage && options.method && options.method.toLowerCase() == "get") {
|
if (isImage && options.method && options.method.toLowerCase() == "get") {
|
||||||
var request = com.koushikdutta.ion.Ion.with(context, options.url);
|
var request = com.koushikdutta.ion.Ion.with(context, options.url);
|
||||||
|
Reference in New Issue
Block a user