mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 04:41:36 +08:00
Platform module refactoring
This commit is contained in:
@ -3,138 +3,133 @@ import definition = require("platform");
|
|||||||
import utils = require("utils/utils");
|
import utils = require("utils/utils");
|
||||||
import * as enumsModule from "ui/enums";
|
import * as enumsModule from "ui/enums";
|
||||||
|
|
||||||
|
const MIN_TABLET_PIXELS = 600;
|
||||||
|
|
||||||
export module platformNames {
|
export module platformNames {
|
||||||
export var android = "Android";
|
export var android = "Android";
|
||||||
export var ios = "iOS";
|
export var ios = "iOS";
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is a "static" class and it is used like a name-space.
|
class Device implements definition.Device {
|
||||||
// It is not meant to be initialized - thus it is not capitalized
|
private _manufacturer: string;
|
||||||
export class device implements definition.device {
|
private _model: string;
|
||||||
private static MIN_TABLET_PIXELS = 600;
|
private _osVersion: string;
|
||||||
private static _manufacturer: string;
|
private _sdkVersion: string;
|
||||||
private static _model: string;
|
private _deviceType: string;
|
||||||
private static _osVersion: string;
|
private _uuid: string;
|
||||||
private static _sdkVersion: string;
|
private _language: string;
|
||||||
private static _deviceType: string;
|
private _region: string;
|
||||||
private static _uuid: string;
|
|
||||||
private static _language: string;
|
|
||||||
private static _region: string;
|
|
||||||
|
|
||||||
static get os(): string {
|
get os(): string {
|
||||||
return platformNames.android;
|
return platformNames.android;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get manufacturer(): string {
|
get manufacturer(): string {
|
||||||
if (!device._manufacturer) {
|
if (!this._manufacturer) {
|
||||||
device._manufacturer = android.os.Build.MANUFACTURER;
|
this._manufacturer = android.os.Build.MANUFACTURER;
|
||||||
}
|
}
|
||||||
|
|
||||||
return device._manufacturer;
|
return this._manufacturer;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get osVersion(): string {
|
get osVersion(): string {
|
||||||
if (!device._osVersion) {
|
if (!this._osVersion) {
|
||||||
device._osVersion = android.os.Build.VERSION.RELEASE;
|
this._osVersion = android.os.Build.VERSION.RELEASE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return device._osVersion;
|
return this._osVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get model(): string {
|
get model(): string {
|
||||||
if (!device._model) {
|
if (!this._model) {
|
||||||
device._model = android.os.Build.MODEL;
|
this._model = android.os.Build.MODEL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return device._model;
|
return this._model;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get sdkVersion(): string {
|
get sdkVersion(): string {
|
||||||
if (!device._sdkVersion) {
|
if (!this._sdkVersion) {
|
||||||
device._sdkVersion = android.os.Build.VERSION.SDK;
|
this._sdkVersion = android.os.Build.VERSION.SDK;
|
||||||
}
|
}
|
||||||
|
|
||||||
return device._sdkVersion;
|
return this._sdkVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get deviceType(): string {
|
get deviceType(): string {
|
||||||
if (!device._deviceType) {
|
if (!this._deviceType) {
|
||||||
var dips = Math.min(screen.mainScreen.widthPixels, screen.mainScreen.heightPixels) / screen.mainScreen.scale;
|
var dips = Math.min(screen.mainScreen.widthPixels, screen.mainScreen.heightPixels) / screen.mainScreen.scale;
|
||||||
var enums: typeof enumsModule = require("ui/enums");
|
var enums: typeof enumsModule = require("ui/enums");
|
||||||
|
|
||||||
// If the device has more than 600 dips it is considered to be a tablet.
|
// If the device has more than 600 dips it is considered to be a tablet.
|
||||||
if (dips >= device.MIN_TABLET_PIXELS) {
|
if (dips >= MIN_TABLET_PIXELS) {
|
||||||
device._deviceType = enums.DeviceType.Tablet;
|
this._deviceType = enums.DeviceType.Tablet;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
device._deviceType = enums.DeviceType.Phone;
|
this._deviceType = enums.DeviceType.Phone;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return device._deviceType;
|
return this._deviceType;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get uuid(): string {
|
get uuid(): string {
|
||||||
if (!device._uuid) {
|
if (!this._uuid) {
|
||||||
device._uuid = android.provider.Settings.Secure.getString(
|
this._uuid = android.provider.Settings.Secure.getString(
|
||||||
utils.ad.getApplicationContext().getContentResolver(),
|
utils.ad.getApplicationContext().getContentResolver(),
|
||||||
android.provider.Settings.Secure.ANDROID_ID
|
android.provider.Settings.Secure.ANDROID_ID
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return device._uuid;
|
return this._uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get language(): string {
|
get language(): string {
|
||||||
if (!device._language) {
|
if (!this._language) {
|
||||||
device._language = java.util.Locale.getDefault().getLanguage().replace("_", "-");
|
this._language = java.util.Locale.getDefault().getLanguage().replace("_", "-");
|
||||||
}
|
}
|
||||||
|
|
||||||
return device._language;
|
return this._language;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get region(): string {
|
get region(): string {
|
||||||
if(!device._region) {
|
if(!this._region) {
|
||||||
device._region = java.util.Locale.getDefault().getCountry();
|
this._region = java.util.Locale.getDefault().getCountry();
|
||||||
}
|
}
|
||||||
|
|
||||||
return device._region;
|
return this._region;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var mainScreen: MainScreen;
|
|
||||||
|
|
||||||
// This is a "static" class and it is used like a namespace.
|
|
||||||
// It is not meant to be initialized - thus it is not capitalized
|
|
||||||
export class screen implements definition.screen {
|
|
||||||
static get mainScreen(): definition.ScreenMetrics {
|
|
||||||
if (!mainScreen) {
|
|
||||||
var metrics = utils.ad.getApplicationContext().getResources().getDisplayMetrics();
|
|
||||||
mainScreen = new MainScreen(metrics);
|
|
||||||
}
|
|
||||||
return mainScreen;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MainScreen implements definition.ScreenMetrics {
|
class MainScreen implements definition.ScreenMetrics {
|
||||||
private _metrics: android.util.DisplayMetrics;
|
private _metrics: android.util.DisplayMetrics;
|
||||||
constructor(metrics: android.util.DisplayMetrics) {
|
private get metrics(): android.util.DisplayMetrics {
|
||||||
this._metrics = metrics;
|
if (!this._metrics) {
|
||||||
|
this._metrics = utils.ad.getApplicationContext().getResources().getDisplayMetrics();
|
||||||
|
}
|
||||||
|
return this._metrics;
|
||||||
}
|
}
|
||||||
|
|
||||||
get widthPixels(): number {
|
get widthPixels(): number {
|
||||||
return this._metrics.widthPixels;
|
return this.metrics.widthPixels;
|
||||||
}
|
}
|
||||||
get heightPixels(): number {
|
get heightPixels(): number {
|
||||||
return this._metrics.heightPixels;
|
return this.metrics.heightPixels;
|
||||||
}
|
}
|
||||||
get scale(): number {
|
get scale(): number {
|
||||||
return this._metrics.density;
|
return this.metrics.density;
|
||||||
}
|
}
|
||||||
get widthDIPs(): number {
|
get widthDIPs(): number {
|
||||||
return this._metrics.widthPixels / this._metrics.density;
|
return this.metrics.widthPixels / this.metrics.density;
|
||||||
}
|
}
|
||||||
get heightDIPs(): number {
|
get heightDIPs(): number {
|
||||||
return this._metrics.heightPixels / this._metrics.density;
|
return this.metrics.heightPixels / this.metrics.density;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export var device: definition.Device = new Device();
|
||||||
|
|
||||||
|
export module screen {
|
||||||
|
export var mainScreen = new MainScreen();
|
||||||
|
}
|
||||||
|
29
platform/platform.d.ts
vendored
29
platform/platform.d.ts
vendored
@ -15,42 +15,42 @@ declare module "platform" {
|
|||||||
/*
|
/*
|
||||||
* An object containing device specific information.
|
* An object containing device specific information.
|
||||||
*/
|
*/
|
||||||
export class device {
|
export interface Device {
|
||||||
/**
|
/**
|
||||||
* Gets the manufacturer of the device.
|
* Gets the manufacturer of the device.
|
||||||
* For example: "Apple" or "HTC" or "Samsung".
|
* For example: "Apple" or "HTC" or "Samsung".
|
||||||
*/
|
*/
|
||||||
static manufacturer: string;
|
manufacturer: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the model of the device.
|
* Gets the model of the device.
|
||||||
* For example: "Nexus 5" or "iPhone".
|
* For example: "Nexus 5" or "iPhone".
|
||||||
*/
|
*/
|
||||||
static model: string;
|
model: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the model of the device.
|
* Gets the model of the device.
|
||||||
* For example: "Android" or "iOS".
|
* For example: "Android" or "iOS".
|
||||||
*/
|
*/
|
||||||
static os: string;
|
os: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the OS version.
|
* Gets the OS version.
|
||||||
* For example: 4.4.4(android), 8.1(ios)
|
* For example: 4.4.4(android), 8.1(ios)
|
||||||
*/
|
*/
|
||||||
static osVersion: string;
|
osVersion: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the OS version.
|
* Gets the OS version.
|
||||||
* For example: 19(android), 8.1(ios).
|
* For example: 19(android), 8.1(ios).
|
||||||
*/
|
*/
|
||||||
static sdkVersion: string;
|
sdkVersion: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the type current device.
|
* Gets the type current device.
|
||||||
* Available values: "phone", "tablet".
|
* Available values: "phone", "tablet".
|
||||||
*/
|
*/
|
||||||
static deviceType: string;
|
deviceType: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the uuid.
|
* Gets the uuid.
|
||||||
@ -58,17 +58,17 @@ declare module "platform" {
|
|||||||
* If you need to receive the same uuid even after the application has been re-installed on the device,
|
* If you need to receive the same uuid even after the application has been re-installed on the device,
|
||||||
* use this plugin: https://www.npmjs.com/package/nativescript-ios-uuid
|
* use this plugin: https://www.npmjs.com/package/nativescript-ios-uuid
|
||||||
*/
|
*/
|
||||||
static uuid: string;
|
uuid: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the preferred language. For example "en"
|
* Gets the preferred language. For example "en"
|
||||||
*/
|
*/
|
||||||
static language: string;
|
language: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the preferred region. For example "US"
|
* Gets the preferred region. For example "US"
|
||||||
*/
|
*/
|
||||||
static region: string;
|
region: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -104,10 +104,15 @@ declare module "platform" {
|
|||||||
/**
|
/**
|
||||||
* An object describing general information about a display.
|
* An object describing general information about a display.
|
||||||
*/
|
*/
|
||||||
export class screen {
|
export module screen {
|
||||||
/**
|
/**
|
||||||
* Gets information about the main screen of the current device.
|
* Gets information about the main screen of the current device.
|
||||||
*/
|
*/
|
||||||
static mainScreen: ScreenMetrics;
|
export var mainScreen: ScreenMetrics;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the current device information
|
||||||
|
*/
|
||||||
|
export var device: Device;
|
||||||
}
|
}
|
||||||
|
@ -5,65 +5,63 @@ export module platformNames {
|
|||||||
export var android = "Android";
|
export var android = "Android";
|
||||||
export var ios = "iOS";
|
export var ios = "iOS";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Device implements definition.Device {
|
||||||
|
private _model: string;
|
||||||
|
private _osVersion: string;
|
||||||
|
private _sdkVersion: string;
|
||||||
|
private _deviceType: string;
|
||||||
|
private _language: string;
|
||||||
|
private _region: string;
|
||||||
|
|
||||||
// This is a "static" class and it is used like a name-space.
|
get manufacturer(): string {
|
||||||
// It is not meant to be initialized - thus it is not capitalized
|
|
||||||
export class device implements definition.device {
|
|
||||||
private static _model: string;
|
|
||||||
private static _osVersion: string;
|
|
||||||
private static _sdkVersion: string;
|
|
||||||
private static _deviceType: string;
|
|
||||||
private static _language: string;
|
|
||||||
private static _region: string;
|
|
||||||
|
|
||||||
static get manufacturer(): string {
|
|
||||||
return "Apple";
|
return "Apple";
|
||||||
}
|
}
|
||||||
|
|
||||||
static get os(): string {
|
get os(): string {
|
||||||
return platformNames.ios;
|
return platformNames.ios;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get osVersion(): string {
|
get osVersion(): string {
|
||||||
if (!device._osVersion) {
|
if (!this._osVersion) {
|
||||||
device._osVersion = UIDevice.currentDevice().systemVersion;
|
this._osVersion = UIDevice.currentDevice().systemVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
return device._osVersion;
|
return this._osVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get model(): string {
|
get model(): string {
|
||||||
if (!device._model) {
|
if (!this._model) {
|
||||||
device._model = UIDevice.currentDevice().model;
|
this._model = UIDevice.currentDevice().model;
|
||||||
}
|
}
|
||||||
|
|
||||||
return device._model;
|
return this._model;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get sdkVersion(): string {
|
get sdkVersion(): string {
|
||||||
if (!device._sdkVersion) {
|
if (!this._sdkVersion) {
|
||||||
device._sdkVersion = UIDevice.currentDevice().systemVersion;
|
this._sdkVersion = UIDevice.currentDevice().systemVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
return device._sdkVersion;
|
return this._sdkVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get deviceType(): string {
|
get deviceType(): string {
|
||||||
if (!device._deviceType) {
|
if (!this._deviceType) {
|
||||||
var enums = require("ui/enums");
|
var enums = require("ui/enums");
|
||||||
|
|
||||||
if (UIDevice.currentDevice().userInterfaceIdiom === UIUserInterfaceIdiom.UIUserInterfaceIdiomPhone) {
|
if (UIDevice.currentDevice().userInterfaceIdiom === UIUserInterfaceIdiom.UIUserInterfaceIdiomPhone) {
|
||||||
device._deviceType = enums.DeviceType.Phone;
|
this._deviceType = enums.DeviceType.Phone;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
device._deviceType = enums.DeviceType.Tablet;
|
this._deviceType = enums.DeviceType.Tablet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return device._deviceType;
|
return this._deviceType;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get uuid(): string {
|
get uuid(): string {
|
||||||
var userDefaults = NSUserDefaults.standardUserDefaults();
|
var userDefaults = NSUserDefaults.standardUserDefaults();
|
||||||
var uuid_key = "TNSUUID";
|
var uuid_key = "TNSUUID";
|
||||||
var app_uuid = userDefaults.stringForKey(uuid_key);
|
var app_uuid = userDefaults.stringForKey(uuid_key);
|
||||||
@ -78,41 +76,32 @@ export class device implements definition.device {
|
|||||||
return app_uuid;
|
return app_uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get language(): string {
|
get language(): string {
|
||||||
if (!device._language) {
|
if (!this._language) {
|
||||||
var languages = NSLocale.preferredLanguages();
|
var languages = NSLocale.preferredLanguages();
|
||||||
device._language = languages[0];
|
this._language = languages[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
return device._language;
|
return this._language;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get region(): string {
|
get region(): string {
|
||||||
if(!device._region) {
|
if(!this._region) {
|
||||||
device._region = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode);
|
this._region = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
return device._region;
|
return this._region;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var mainScreen: MainScreen;
|
|
||||||
|
|
||||||
// This is a "static" class and it is used like a name-space.
|
|
||||||
// It is not meant to be initialized - thus it is not capitalized
|
|
||||||
export class screen implements definition.screen {
|
|
||||||
static get mainScreen(): definition.ScreenMetrics {
|
|
||||||
if (!mainScreen) {
|
|
||||||
mainScreen = new MainScreen(UIScreen.mainScreen());
|
|
||||||
}
|
|
||||||
return mainScreen;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MainScreen implements definition.ScreenMetrics {
|
class MainScreen implements definition.ScreenMetrics {
|
||||||
private _screen: UIScreen;
|
private _screen: UIScreen;
|
||||||
constructor(metrics: UIScreen) {
|
private get screen(): UIScreen {
|
||||||
this._screen = metrics;
|
if (!this._screen) {
|
||||||
|
this._screen = UIScreen.mainScreen();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this._screen;
|
||||||
}
|
}
|
||||||
|
|
||||||
get widthPixels(): number {
|
get widthPixels(): number {
|
||||||
@ -122,12 +111,18 @@ class MainScreen implements definition.ScreenMetrics {
|
|||||||
return this.heightDIPs * this.scale;
|
return this.heightDIPs * this.scale;
|
||||||
}
|
}
|
||||||
get scale(): number {
|
get scale(): number {
|
||||||
return this._screen.scale;
|
return this.screen.scale;
|
||||||
}
|
}
|
||||||
get widthDIPs(): number {
|
get widthDIPs(): number {
|
||||||
return this._screen.bounds.size.width;
|
return this.screen.bounds.size.width;
|
||||||
}
|
}
|
||||||
get heightDIPs(): number {
|
get heightDIPs(): number {
|
||||||
return this._screen.bounds.size.height;
|
return this.screen.bounds.size.height;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export var device: definition.Device = new Device();
|
||||||
|
|
||||||
|
export module screen {
|
||||||
|
export var mainScreen = new MainScreen();
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user