mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
getApplication() and getApplicationContext() added/used to/from Android utils.
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import appModule = require("application");
|
||||
import Common = require("application-settings/application-settings-common");
|
||||
import Common = require("application-settings/application-settings-common");
|
||||
import utils = require("utils/utils");
|
||||
|
||||
var sharedPreferences = appModule.android.context.getSharedPreferences("prefs.db", 0);
|
||||
var sharedPreferences = utils.ad.getApplicationContext().getSharedPreferences("prefs.db", 0);
|
||||
|
||||
export var hasKey = function (key: string): boolean {
|
||||
Common.checkKey(key);
|
||||
@ -17,7 +17,7 @@ export var getBoolean = function (key: string, defaultValue?: boolean): boolean
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
export var getString = function(key: string, defaultValue?: string): string {
|
||||
export var getString = function (key: string, defaultValue?: string): string {
|
||||
Common.checkKey(key);
|
||||
if (hasKey(key)) {
|
||||
return sharedPreferences.getString(key, "");
|
||||
@ -25,7 +25,7 @@ export var getString = function(key: string, defaultValue?: string): string {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
export var getNumber = function(key: string, defaultValue?: number): number {
|
||||
export var getNumber = function (key: string, defaultValue?: number): number {
|
||||
Common.checkKey(key);
|
||||
if (hasKey(key)) {
|
||||
return sharedPreferences.getFloat(key, float(0.0));
|
||||
@ -34,7 +34,7 @@ export var getNumber = function(key: string, defaultValue?: number): number {
|
||||
}
|
||||
|
||||
// setters
|
||||
export var setBoolean = function(key: string, value: boolean): void {
|
||||
export var setBoolean = function (key: string, value: boolean): void {
|
||||
Common.checkKey(key);
|
||||
Common.ensureValidValue(value, "boolean");
|
||||
var editor = sharedPreferences.edit();
|
||||
@ -42,7 +42,7 @@ export var setBoolean = function(key: string, value: boolean): void {
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
export var setString = function(key: string, value: string): void {
|
||||
export var setString = function (key: string, value: string): void {
|
||||
Common.checkKey(key);
|
||||
Common.ensureValidValue(value, "string");
|
||||
var editor = sharedPreferences.edit();
|
||||
@ -50,7 +50,7 @@ export var setString = function(key: string, value: string): void {
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
export var setNumber = function(key: string, value: number): void {
|
||||
export var setNumber = function (key: string, value: number): void {
|
||||
Common.checkKey(key);
|
||||
Common.ensureValidValue(value, "number");
|
||||
var editor = sharedPreferences.edit();
|
||||
|
@ -19,11 +19,11 @@ export var takePicture = function (options?: definition.CameraOptions): Promise<
|
||||
}
|
||||
var takePictureIntent = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
|
||||
var dateStamp = createDateTimeStamp();
|
||||
var tempPicturePath = fileSystem.path.join(appModule.android.currentContext.getExternalFilesDir(null).getAbsolutePath(), "cameraPicture_" + dateStamp + ".jpg");
|
||||
var tempPicturePath = fileSystem.path.join(utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath(), "cameraPicture_" + dateStamp + ".jpg");
|
||||
var nativeFile = new java.io.File(tempPicturePath);
|
||||
var tempPictureUri = android.net.Uri.fromFile(nativeFile);
|
||||
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri);
|
||||
if (takePictureIntent.resolveActivity(appModule.android.context.getPackageManager()) != null) {
|
||||
if (takePictureIntent.resolveActivity(utils.ad.getApplicationContext().getPackageManager()) != null) {
|
||||
|
||||
var previousResult = appModule.android.onActivityResult;
|
||||
appModule.android.onActivityResult = (requestCode: number, resultCode: number, data: android.content.Intent) => {
|
||||
|
@ -1,5 +1,6 @@
|
||||
import appModule = require("application");
|
||||
import common = require("connectivity/connectivity-common");
|
||||
import utils = require("utils/utils");
|
||||
|
||||
declare var exports;
|
||||
require("utils/module-merge").merge(common, exports);
|
||||
@ -9,11 +10,7 @@ var MOBILE = "MOBILE";
|
||||
|
||||
// Get Connection Type
|
||||
function getConnectivityManager(): android.net.ConnectivityManager {
|
||||
if (!appModule.android || !appModule.android.context) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return appModule.android.context.getSystemService(android.content.Context.CONNECTIVITY_SERVICE);
|
||||
return utils.ad.getApplicationContext().getSystemService(android.content.Context.CONNECTIVITY_SERVICE);
|
||||
}
|
||||
|
||||
function getActiveNetworkInfo(): android.net.NetworkInfo {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import appModule = require("application");
|
||||
import textModule = require("text");
|
||||
import textModule = require("text");
|
||||
import types = require("utils/types");
|
||||
import utils = require("utils/utils");
|
||||
|
||||
export class FileSystemAccess {
|
||||
private _pathSeparator = java.io.File.separator.toString();
|
||||
@ -211,14 +211,12 @@ export class FileSystemAccess {
|
||||
}
|
||||
|
||||
public getDocumentsFolderPath(): string {
|
||||
var context = appModule.android.context;
|
||||
var dir = context.getFilesDir();
|
||||
var dir = utils.ad.getApplicationContext().getFilesDir();
|
||||
return dir.getAbsolutePath();
|
||||
}
|
||||
|
||||
public getTempFolderPath(): string {
|
||||
var context = appModule.android.context;
|
||||
var dir = context.getCacheDir();
|
||||
var dir = utils.ad.getApplicationContext().getCacheDir();
|
||||
return dir.getAbsolutePath();
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
import types = require("utils/types");
|
||||
import fs = require("file-system");
|
||||
import appModule = require("application");
|
||||
import definition = require("image-source");
|
||||
import common = require("image-source/image-source-common");
|
||||
import enums = require("ui/enums");
|
||||
import utils = require("utils/utils");
|
||||
|
||||
// merge the exports of the common file with the exports of this file
|
||||
declare var exports;
|
||||
@ -16,10 +16,9 @@ export class ImageSource implements definition.ImageSource {
|
||||
public loadFromResource(name: string): boolean {
|
||||
this.android = null;
|
||||
|
||||
var androidApp = appModule.android;
|
||||
var res = androidApp.context.getResources();
|
||||
var res = utils.ad.getApplicationContext().getResources();
|
||||
if (res) {
|
||||
var identifier: number = res.getIdentifier(name, 'drawable', androidApp.packageName);
|
||||
var identifier: number = res.getIdentifier(name, 'drawable', utils.ad.getApplication().getPackageName());
|
||||
if (0 < identifier) {
|
||||
// Load BitmapDrawable with getDrawable to make use of Android internal caching
|
||||
var bitmapDrawable = <android.graphics.drawable.BitmapDrawable>res.getDrawable(identifier);
|
||||
|
@ -1,7 +1,7 @@
|
||||
import enums = require("ui/enums");
|
||||
import appModule = require("application");
|
||||
import locationModule = require("location");
|
||||
import common = require("location/location-common");
|
||||
import utils = require("utils/utils");
|
||||
|
||||
import merger = require("utils/module-merge");
|
||||
declare var exports;
|
||||
@ -101,7 +101,7 @@ export class LocationManager implements locationModule.LocationManager {
|
||||
public static isEnabled(): boolean {
|
||||
var criteria = new android.location.Criteria();
|
||||
criteria.setAccuracy(android.location.Criteria.ACCURACY_COARSE);
|
||||
var lm = appModule.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
|
||||
var lm = utils.ad.getApplicationContext().getSystemService(android.content.Context.LOCATION_SERVICE);
|
||||
// due to bug in android API getProviders() with criteria parameter overload should be called (so most loose acuracy is used).
|
||||
var enabledProviders = lm.getProviders(criteria, true);
|
||||
return (enabledProviders.size() > 0) ? true : false;
|
||||
@ -122,8 +122,8 @@ export class LocationManager implements locationModule.LocationManager {
|
||||
this.desiredAccuracy = enums.Accuracy.any;
|
||||
this.updateDistance = 0;
|
||||
|
||||
//this.androidLocationManager = appModule.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
|
||||
var alm = appModule.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
|
||||
//this.androidLocationManager = utils.ad.getApplicationContext().getSystemService(android.content.Context.LOCATION_SERVICE);
|
||||
var alm = utils.ad.getApplicationContext().getSystemService(android.content.Context.LOCATION_SERVICE);
|
||||
this.androidLocationManager = new AndroidLocationManager(alm);
|
||||
this.androidLocationManager.minimumUpdateTime = 200;
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
/* tslint:disable:class-name */
|
||||
import definition = require("platform");
|
||||
import enums = require("ui/enums");
|
||||
import application = require("application");
|
||||
import utils = require("utils/utils");
|
||||
|
||||
export module platformNames {
|
||||
export var android = "Android";
|
||||
@ -75,11 +75,11 @@ export class device implements definition.device {
|
||||
static get uuid(): string {
|
||||
if (!device._uuid) {
|
||||
device._uuid = android.provider.Settings.Secure.getString(
|
||||
application.android.context.getContentResolver(),
|
||||
android.provider.Settings.Secure.ANDROID_ID
|
||||
);
|
||||
utils.ad.getApplicationContext().getContentResolver(),
|
||||
android.provider.Settings.Secure.ANDROID_ID
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return device._uuid;
|
||||
}
|
||||
|
||||
@ -87,7 +87,7 @@ export class device implements definition.device {
|
||||
if (!device._language) {
|
||||
device._language = java.util.Locale.getDefault().toString();
|
||||
}
|
||||
|
||||
|
||||
return device._language;
|
||||
}
|
||||
}
|
||||
@ -99,7 +99,7 @@ var mainScreenInfo: definition.ScreenMetrics;
|
||||
export class screen implements definition.screen {
|
||||
static get mainScreen(): definition.ScreenMetrics {
|
||||
if (!mainScreenInfo) {
|
||||
var metrics = application.android.context.getResources().getDisplayMetrics();
|
||||
var metrics = utils.ad.getApplicationContext().getResources().getDisplayMetrics();
|
||||
mainScreenInfo = {
|
||||
widthPixels: metrics.widthPixels,
|
||||
heightPixels: metrics.heightPixels,
|
||||
|
@ -1,5 +1,4 @@
|
||||
import application = require("application");
|
||||
import common = require("utils/utils-common");
|
||||
import common = require("utils/utils-common");
|
||||
|
||||
// merge the exports of the common file with the exports of this file
|
||||
declare var exports;
|
||||
@ -17,9 +16,9 @@ export module layout {
|
||||
var useOldMeasureSpec = false;
|
||||
|
||||
export function makeMeasureSpec(size: number, mode: number): number {
|
||||
if (sdkVersion === -1 && application.android && application.android.context) {
|
||||
if (sdkVersion === -1) {
|
||||
// check whether the old layout is needed
|
||||
sdkVersion = application.android.context.getApplicationInfo().targetSdkVersion;
|
||||
sdkVersion = ad.getApplicationContext().getApplicationInfo().targetSdkVersion;
|
||||
useOldMeasureSpec = sdkVersion <= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
|
||||
}
|
||||
|
||||
@ -40,7 +39,7 @@ export module layout {
|
||||
|
||||
function getDisplayMetrics(): android.util.DisplayMetrics {
|
||||
if (!metrics) {
|
||||
metrics = application.android.context.getResources().getDisplayMetrics();
|
||||
metrics = ad.getApplicationContext().getResources().getDisplayMetrics();
|
||||
}
|
||||
|
||||
return metrics;
|
||||
@ -49,6 +48,10 @@ export module layout {
|
||||
|
||||
// We are using "ad" here to avoid namespace collision with the global android object
|
||||
export module ad {
|
||||
|
||||
export function getApplication() { return <android.app.Application>(<any>com.tns).NativeScriptApplication.getInstance(); }
|
||||
export function getApplicationContext() { return <android.content.Context>getApplication().getApplicationContext(); }
|
||||
|
||||
export module collections {
|
||||
export function stringArrayToStringSet(str: string[]): any {
|
||||
var hashSet = new java.util.HashSet();
|
||||
@ -84,9 +87,8 @@ export module ad {
|
||||
}
|
||||
|
||||
export function getId(name: string): number {
|
||||
var context = application.android.context;
|
||||
var resources = context.getResources();
|
||||
var packageName = context.getPackageName();
|
||||
var resources = getApplicationContext().getResources();
|
||||
var packageName = getApplicationContext().getPackageName();
|
||||
var uri = packageName + name;
|
||||
return resources.getIdentifier(uri, null, null);
|
||||
}
|
||||
|
10
utils/utils.d.ts
vendored
10
utils/utils.d.ts
vendored
@ -44,6 +44,16 @@
|
||||
* Module with android specific utilities.
|
||||
*/
|
||||
module ad {
|
||||
/**
|
||||
* Gets the native Android application instance.
|
||||
*/
|
||||
export function getApplication(): android.app.Application;
|
||||
|
||||
/**
|
||||
* Gets the Android application context.
|
||||
*/
|
||||
export function getApplicationContext(): android.content.Context;
|
||||
|
||||
/**
|
||||
* Utility module dealing with some android collections.
|
||||
*/
|
||||
|
Reference in New Issue
Block a user