mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-18 22:01:42 +08:00
Added current variable in the Application module (to provide a shortcut to the Application singleton).
This commit is contained in:
@ -1,14 +1,13 @@
|
||||
import app_common_module = require("Application/application_common");
|
||||
import appModule = require("Application/application_common");
|
||||
|
||||
// merge the exports of the application_common file with the exports of this file
|
||||
declare var exports;
|
||||
require("Utils/module_merge").merge(app_common_module, exports);
|
||||
require("Utils/module_merge").merge(appModule, exports);
|
||||
|
||||
var currentApp = app_common_module.Application.current;
|
||||
var callbacks = android.app.Application.ActivityLifecycleCallbacks;
|
||||
|
||||
var initEvents = function () {
|
||||
var androidApp = app_common_module.Application.current.android;
|
||||
var androidApp = appModule.current.android;
|
||||
var lifecycleCallbacks = new callbacks({
|
||||
onActivityCreated: function (activity: any, bundle: any) {
|
||||
if (!androidApp.startActivity) {
|
||||
@ -92,8 +91,8 @@ var initEvents = function () {
|
||||
|
||||
export var init = function (nativeApp: android.app.Application) {
|
||||
var app = new AndroidApplication(nativeApp);
|
||||
currentApp.os = app_common_module.TargetOS.Android;
|
||||
currentApp.android = app;
|
||||
appModule.current.os = appModule.TargetOS.Android;
|
||||
appModule.current.android = app;
|
||||
app.init();
|
||||
}
|
||||
|
||||
|
3
Application/application.d.ts
vendored
3
Application/application.d.ts
vendored
@ -3,8 +3,9 @@
|
||||
Android
|
||||
}
|
||||
|
||||
export declare var current: Application;
|
||||
|
||||
export declare class Application {
|
||||
static current: Application;
|
||||
public os: TargetOS;
|
||||
|
||||
public onLaunch: () => any;
|
||||
|
@ -20,19 +20,17 @@ log("JavaScript loading ended.");
|
||||
|
||||
*/
|
||||
|
||||
import app_common_module = require("Application/application_common");
|
||||
import appModule = require("Application/application_common");
|
||||
|
||||
// merge the exports of the application_common file with the exports of this file
|
||||
declare var exports;
|
||||
require("Utils/module_merge").merge(app_common_module, exports);
|
||||
|
||||
var currentApp = app_common_module.Application.current;
|
||||
require("Utils/module_merge").merge(appModule, exports);
|
||||
|
||||
// TODO: Declarations
|
||||
export var init = function (nativeApp: any) {
|
||||
var app = new iOSApplication(nativeApp);
|
||||
currentApp.os = app_common_module.TargetOS.iOS;
|
||||
currentApp.ios = app;
|
||||
appModule.current.os = appModule.TargetOS.iOS;
|
||||
appModule.current.ios = app;
|
||||
app.init();
|
||||
}
|
||||
|
||||
@ -57,10 +55,10 @@ class iOSApplication {
|
||||
this.window.backgroundColor = UIKit.UIColor.whiteColor();
|
||||
this.window.makeKeyAndVisible();
|
||||
|
||||
var iosApp = currentApp.ios;
|
||||
var iosApp = appModule.current.ios;
|
||||
|
||||
if (currentApp.onLaunch) {
|
||||
this.window.rootViewController = currentApp.onLaunch();
|
||||
if (appModule.current.onLaunch) {
|
||||
this.window.rootViewController = appModule.current.onLaunch();
|
||||
} else {
|
||||
log("Missing TK.UI.Application.current.onLaunch");
|
||||
}
|
||||
@ -71,8 +69,8 @@ class iOSApplication {
|
||||
|
||||
applicationDidBecomeActive: function (application) {
|
||||
log("applicationDidBecomeActive: " + application);
|
||||
if (currentApp.onResume) {
|
||||
currentApp.onResume();
|
||||
if (appModule.current.onResume) {
|
||||
appModule.current.onResume();
|
||||
}
|
||||
},
|
||||
|
||||
@ -82,8 +80,8 @@ class iOSApplication {
|
||||
|
||||
applicationDidEnterBackground: function (application) {
|
||||
log("applicationDidEnterBackground: " + application);
|
||||
if (currentApp.onSuspend) {
|
||||
currentApp.onSuspend();
|
||||
if (appModule.current.onSuspend) {
|
||||
appModule.current.onSuspend();
|
||||
}
|
||||
},
|
||||
|
||||
@ -93,15 +91,15 @@ class iOSApplication {
|
||||
|
||||
applicationWillTerminate: function (application) {
|
||||
log("applicationWillTerminate: " + application);
|
||||
if (currentApp.onExit) {
|
||||
currentApp.onExit();
|
||||
if (appModule.current.onExit) {
|
||||
appModule.current.onExit();
|
||||
}
|
||||
},
|
||||
|
||||
applicationDidReceiveMemoryWarning: function (application) {
|
||||
log("applicationDidReceiveMemoryWarning: " + application);
|
||||
if (currentApp.onLowMemory) {
|
||||
currentApp.onLowMemory();
|
||||
if (appModule.current.onLowMemory) {
|
||||
appModule.current.onLowMemory();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,9 +18,9 @@ export class Application {
|
||||
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;
|
||||
}
|
||||
|
||||
export var current = new Application();
|
@ -1,4 +1,4 @@
|
||||
import app_module = require("Application/application");
|
||||
import appModule = require("Application/application");
|
||||
|
||||
var REQUEST_IMAGE_CAPTURE: number = 1;
|
||||
var REQUEST_SELECT_PICTURE: number = 2;
|
||||
@ -6,7 +6,7 @@ var REQUEST_SELECT_PICTURE: number = 2;
|
||||
export class CameraManager {
|
||||
public takePicture(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any) {
|
||||
var takePictureIntent = new android.content.Intent('android.media.action.IMAGE_CAPTURE');
|
||||
var androidApp = app_module.Application.current.android;
|
||||
var androidApp = appModule.current.android;
|
||||
|
||||
if (takePictureIntent.resolveActivity(androidApp.context.getPackageManager()) !== null) {
|
||||
androidApp.currentActivity.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
|
||||
@ -16,7 +16,7 @@ export class CameraManager {
|
||||
// options { useSavedPhotos: true }
|
||||
public pictureFromLibrary(params: any, onSuccess: (imageData: any) => any, onError?: (error: any) => any) {
|
||||
var readPictureIntent = new android.content.Intent();
|
||||
var androidApp = app_module.Application.current.android;
|
||||
var androidApp = appModule.current.android;
|
||||
|
||||
readPictureIntent.setType('image/*');
|
||||
readPictureIntent.setAction('android.intent.action.GET_CONTENT');
|
||||
|
@ -207,13 +207,13 @@ export class FileSystemAccess {
|
||||
}
|
||||
|
||||
public getDocumentsFolderPath(): string {
|
||||
var context = appModule.Application.current.android.context;
|
||||
var context = appModule.current.android.context;
|
||||
var dir = context.getFilesDir();
|
||||
return dir.getAbsolutePath();
|
||||
}
|
||||
|
||||
public getTempFolderPath(): string {
|
||||
var context = appModule.Application.current.android.context;
|
||||
var context = appModule.current.android.context;
|
||||
var dir = context.getCacheDir();
|
||||
return dir.getAbsolutePath();
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import app_module = require("Application/application");
|
||||
import appModule = require("Application/application");
|
||||
|
||||
export enum ImageType {
|
||||
PNG = 0,
|
||||
@ -33,7 +33,7 @@ export class Image {
|
||||
}
|
||||
|
||||
public loadFromResource(name: string): boolean {
|
||||
var androidApp = app_module.Application.current.android;
|
||||
var androidApp = appModule.current.android;
|
||||
var res = androidApp.context.getResources();
|
||||
if (res) {
|
||||
var identifier: number = res.getIdentifier(name, 'drawable', androidApp.packageName);
|
||||
|
@ -1,5 +1,5 @@
|
||||
import types = require("Location/location_types");
|
||||
import app_module = require("Application/application");
|
||||
import appModule = require("Application/application");
|
||||
|
||||
// merge the exports of the types module with the exports of this file
|
||||
declare var exports;
|
||||
@ -59,7 +59,7 @@ export class LocationManager {
|
||||
public static isEnabled(): boolean {
|
||||
var criteria = new android.location.Criteria();
|
||||
criteria.setAccuracy(1); // low ? fine ? who knows what 1 means (bug in android docs?)
|
||||
var lm = app_module.Application.current.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
|
||||
var lm = appModule.current.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
|
||||
return (lm.getBestProvider(criteria, true) != null) ? true : false;
|
||||
}
|
||||
|
||||
@ -80,7 +80,7 @@ export class LocationManager {
|
||||
this.minimumUpdateTime = 200;
|
||||
this.isStarted = false;
|
||||
|
||||
this.androidLocationManager = app_module.Application.current.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
|
||||
this.androidLocationManager = appModule.current.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
|
||||
}
|
||||
|
||||
////////////////////////
|
||||
|
@ -1,11 +1,11 @@
|
||||
import utils_module = require("Utils/utils_android");
|
||||
import app_module = require("Application/application");
|
||||
import appModule = require("Application/application");
|
||||
|
||||
export class UserPreferences {
|
||||
private sharedPreferences: any;
|
||||
|
||||
constructor() {
|
||||
this.sharedPreferences = app_module.Application.current.android.context.getSharedPreferences("prefs.db", 0);
|
||||
this.sharedPreferences = appModule.current.android.context.getSharedPreferences("prefs.db", 0);
|
||||
}
|
||||
|
||||
public containsKey(key: string): boolean {
|
||||
|
Reference in New Issue
Block a user