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
|
// merge the exports of the application_common file with the exports of this file
|
||||||
declare var exports;
|
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 callbacks = android.app.Application.ActivityLifecycleCallbacks;
|
||||||
|
|
||||||
var initEvents = function () {
|
var initEvents = function () {
|
||||||
var androidApp = app_common_module.Application.current.android;
|
var androidApp = appModule.current.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) {
|
||||||
@ -92,8 +91,8 @@ 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);
|
||||||
currentApp.os = app_common_module.TargetOS.Android;
|
appModule.current.os = appModule.TargetOS.Android;
|
||||||
currentApp.android = app;
|
appModule.current.android = app;
|
||||||
app.init();
|
app.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
3
Application/application.d.ts
vendored
3
Application/application.d.ts
vendored
@ -3,8 +3,9 @@
|
|||||||
Android
|
Android
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export declare var current: Application;
|
||||||
|
|
||||||
export declare class Application {
|
export declare class Application {
|
||||||
static current: Application;
|
|
||||||
public os: TargetOS;
|
public os: TargetOS;
|
||||||
|
|
||||||
public onLaunch: () => any;
|
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
|
// merge the exports of the application_common file with the exports of this file
|
||||||
declare var exports;
|
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;
|
|
||||||
|
|
||||||
// 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);
|
||||||
currentApp.os = app_common_module.TargetOS.iOS;
|
appModule.current.os = appModule.TargetOS.iOS;
|
||||||
currentApp.ios = app;
|
appModule.current.ios = app;
|
||||||
app.init();
|
app.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,10 +55,10 @@ class iOSApplication {
|
|||||||
this.window.backgroundColor = UIKit.UIColor.whiteColor();
|
this.window.backgroundColor = UIKit.UIColor.whiteColor();
|
||||||
this.window.makeKeyAndVisible();
|
this.window.makeKeyAndVisible();
|
||||||
|
|
||||||
var iosApp = currentApp.ios;
|
var iosApp = appModule.current.ios;
|
||||||
|
|
||||||
if (currentApp.onLaunch) {
|
if (appModule.current.onLaunch) {
|
||||||
this.window.rootViewController = currentApp.onLaunch();
|
this.window.rootViewController = appModule.current.onLaunch();
|
||||||
} else {
|
} else {
|
||||||
log("Missing TK.UI.Application.current.onLaunch");
|
log("Missing TK.UI.Application.current.onLaunch");
|
||||||
}
|
}
|
||||||
@ -71,8 +69,8 @@ class iOSApplication {
|
|||||||
|
|
||||||
applicationDidBecomeActive: function (application) {
|
applicationDidBecomeActive: function (application) {
|
||||||
log("applicationDidBecomeActive: " + application);
|
log("applicationDidBecomeActive: " + application);
|
||||||
if (currentApp.onResume) {
|
if (appModule.current.onResume) {
|
||||||
currentApp.onResume();
|
appModule.current.onResume();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -82,8 +80,8 @@ class iOSApplication {
|
|||||||
|
|
||||||
applicationDidEnterBackground: function (application) {
|
applicationDidEnterBackground: function (application) {
|
||||||
log("applicationDidEnterBackground: " + application);
|
log("applicationDidEnterBackground: " + application);
|
||||||
if (currentApp.onSuspend) {
|
if (appModule.current.onSuspend) {
|
||||||
currentApp.onSuspend();
|
appModule.current.onSuspend();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -93,15 +91,15 @@ class iOSApplication {
|
|||||||
|
|
||||||
applicationWillTerminate: function (application) {
|
applicationWillTerminate: function (application) {
|
||||||
log("applicationWillTerminate: " + application);
|
log("applicationWillTerminate: " + application);
|
||||||
if (currentApp.onExit) {
|
if (appModule.current.onExit) {
|
||||||
currentApp.onExit();
|
appModule.current.onExit();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
applicationDidReceiveMemoryWarning: function (application) {
|
applicationDidReceiveMemoryWarning: function (application) {
|
||||||
log("applicationDidReceiveMemoryWarning: " + application);
|
log("applicationDidReceiveMemoryWarning: " + application);
|
||||||
if (currentApp.onLowMemory) {
|
if (appModule.current.onLowMemory) {
|
||||||
currentApp.onLowMemory();
|
appModule.current.onLowMemory();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,9 +18,9 @@ export class Application {
|
|||||||
public onExit: () => any;
|
public onExit: () => any;
|
||||||
public onLowMemory: () => 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
|
// TODO: These fields are declared by the application.d.ts file and intellisense will come from there
|
||||||
public android: any;
|
public android: any;
|
||||||
public ios: 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_IMAGE_CAPTURE: number = 1;
|
||||||
var REQUEST_SELECT_PICTURE: number = 2;
|
var REQUEST_SELECT_PICTURE: number = 2;
|
||||||
@ -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 = app_module.Application.current.android;
|
var androidApp = appModule.current.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 = app_module.Application.current.android;
|
var androidApp = appModule.current.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.Application.current.android.context;
|
var context = appModule.current.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.Application.current.android.context;
|
var context = appModule.current.android.context;
|
||||||
var dir = context.getCacheDir();
|
var dir = context.getCacheDir();
|
||||||
return dir.getAbsolutePath();
|
return dir.getAbsolutePath();
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import app_module = require("Application/application");
|
import appModule = require("Application/application");
|
||||||
|
|
||||||
export enum ImageType {
|
export enum ImageType {
|
||||||
PNG = 0,
|
PNG = 0,
|
||||||
@ -33,7 +33,7 @@ export class Image {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public loadFromResource(name: string): boolean {
|
public loadFromResource(name: string): boolean {
|
||||||
var androidApp = app_module.Application.current.android;
|
var androidApp = appModule.current.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);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import types = require("Location/location_types");
|
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
|
// merge the exports of the types module with the exports of this file
|
||||||
declare var exports;
|
declare var exports;
|
||||||
@ -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 = 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;
|
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 = 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 utils_module = require("Utils/utils_android");
|
||||||
import app_module = require("Application/application");
|
import appModule = require("Application/application");
|
||||||
|
|
||||||
export class UserPreferences {
|
export class UserPreferences {
|
||||||
private sharedPreferences: any;
|
private sharedPreferences: any;
|
||||||
|
|
||||||
constructor() {
|
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 {
|
public containsKey(key: string): boolean {
|
||||||
|
Reference in New Issue
Block a user