1 |
|
2 | import { Application } from '../application';
|
3 | import { SDK_VERSION } from '../utils/constants';
|
4 | import { platformNames } from './common';
|
5 | const MIN_TABLET_PIXELS = 600;
|
6 | export * from './common';
|
7 | class MainScreen {
|
8 | reinitMetrics() {
|
9 | if (!this._metrics) {
|
10 | this._metrics = new android.util.DisplayMetrics();
|
11 | }
|
12 | this.initMetrics();
|
13 | }
|
14 | initMetrics() {
|
15 | const nativeApp = Application.android.getNativeApplication();
|
16 | nativeApp.getSystemService(android.content.Context.WINDOW_SERVICE).getDefaultDisplay().getRealMetrics(this._metrics);
|
17 | }
|
18 | get metrics() {
|
19 | if (!this._metrics) {
|
20 |
|
21 | Application.on('cssChanged', this.reinitMetrics, this);
|
22 | Application.on(Application.orientationChangedEvent, this.reinitMetrics, this);
|
23 | this._metrics = new android.util.DisplayMetrics();
|
24 | this.initMetrics();
|
25 | }
|
26 | return this._metrics;
|
27 | }
|
28 | get widthPixels() {
|
29 | return this.metrics.widthPixels;
|
30 | }
|
31 | get heightPixels() {
|
32 | return this.metrics.heightPixels;
|
33 | }
|
34 | get scale() {
|
35 | return this.metrics.density;
|
36 | }
|
37 | get widthDIPs() {
|
38 | return this.metrics.widthPixels / this.metrics.density;
|
39 | }
|
40 | get heightDIPs() {
|
41 | return this.metrics.heightPixels / this.metrics.density;
|
42 | }
|
43 | }
|
44 | export class Screen {
|
45 | }
|
46 | Screen.mainScreen = new MainScreen();
|
47 |
|
48 | export const screen = Screen;
|
49 | class DeviceRef {
|
50 | get manufacturer() {
|
51 | if (!this._manufacturer) {
|
52 | this._manufacturer = android.os.Build.MANUFACTURER;
|
53 | }
|
54 | return this._manufacturer;
|
55 | }
|
56 | get os() {
|
57 | return platformNames.android;
|
58 | }
|
59 | get osVersion() {
|
60 | if (!this._osVersion) {
|
61 | this._osVersion = android.os.Build.VERSION.RELEASE;
|
62 | }
|
63 | return this._osVersion;
|
64 | }
|
65 | get model() {
|
66 | if (!this._model) {
|
67 | this._model = android.os.Build.MODEL;
|
68 | }
|
69 | return this._model;
|
70 | }
|
71 | get sdkVersion() {
|
72 | if (!this._sdkVersion) {
|
73 | this._sdkVersion = android.os.Build.VERSION.SDK;
|
74 | }
|
75 | return this._sdkVersion;
|
76 | }
|
77 | get deviceType() {
|
78 | if (!this._deviceType) {
|
79 | const dips = Math.min(Screen.mainScreen.widthPixels, Screen.mainScreen.heightPixels) / Screen.mainScreen.scale;
|
80 |
|
81 | if (dips >= MIN_TABLET_PIXELS) {
|
82 | this._deviceType = 'Tablet';
|
83 | }
|
84 | else {
|
85 | this._deviceType = 'Phone';
|
86 | }
|
87 | }
|
88 | return this._deviceType;
|
89 | }
|
90 | get uuid() {
|
91 | if (!this._uuid) {
|
92 | const nativeApp = Application.android.getNativeApplication();
|
93 | this._uuid = android.provider.Settings.Secure.getString(nativeApp.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
|
94 | }
|
95 | return this._uuid;
|
96 | }
|
97 | get language() {
|
98 | let defaultNativeLocale;
|
99 | if (SDK_VERSION >= 24) {
|
100 | defaultNativeLocale = android.content.res.Resources.getSystem().getConfiguration().getLocales().get(0);
|
101 | }
|
102 | else {
|
103 | defaultNativeLocale = android.content.res.Resources.getSystem().getConfiguration().locale;
|
104 | }
|
105 | return defaultNativeLocale.getLanguage().replace('_', '-');
|
106 | }
|
107 | get region() {
|
108 | let defaultNativeLocale;
|
109 | if (SDK_VERSION >= 24) {
|
110 | defaultNativeLocale = android.content.res.Resources.getSystem().getConfiguration().getLocales().get(0);
|
111 | }
|
112 | else {
|
113 | defaultNativeLocale = android.content.res.Resources.getSystem().getConfiguration().locale;
|
114 | }
|
115 | return defaultNativeLocale.getCountry();
|
116 | }
|
117 | }
|
118 | export const Device = new DeviceRef();
|
119 |
|
120 | export const device = Device;
|
121 |
|
\ | No newline at end of file |