forked from NathanaelA/nativescript-master-technology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster-technology.js
286 lines (257 loc) · 8.98 KB
/
master-technology.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**********************************************************************************
* (c) 2015-2017, Master Technology
* Licensed under the MIT license or contact me for a Support or Commercial License
*
* I do contract work in most languages, so let me solve your problems!
*
* Any questions please feel free to email me or put a issue up on the github repo
* Version 0.0.9 Nathan@master-technology.com
*********************************************************************************/
"use strict";
/* jshint node: true, browser: true, unused: true, undef: true */
/* global NSObject, NSString, global, android, com, java, javax, exit, UIDevice, CACurrentMediaTime, NSRunLoop, NSDate, require */
// global.android is already defined on android devices
// We are defining global.ios on ios devices because the ios team refuses to do it
if (global.NSObject && global.NSString && typeof global.ios === "undefined") {
global.ios = true;
Object.freeze(global.ios);
}
if (!global.setImmediate) {
global.setImmediate = global.setTimeout;
global.clearImmediate = global.clearTimeout;
}
/***
* Creates a performance.now() function
*/
if (!global.performance) {
global.performance = {};
}
if (!global.performance.now) {
if (global.android) {
global.performance.now = function () {
return java.lang.System.nanoTime() / 1000000;
};
} else if (global.ios) {
global.performance.now = function() {
return CACurrentMediaTime();
};
}
}
/***
* Creates a process class
*/
if (!global.masterProcess) {
global.masterProcess = {};
}
if (!global.masterProcess.restart) {
global.masterProcess.restart = function (msg) {
const application = require('application');
const dialogs= require('ui/dialogs');
if (global.android) {
//noinspection JSUnresolvedFunction,JSUnresolvedVariable
const mStartActivity = new android.content.Intent(application.android.context, application.android.startActivity.getClass());
const mPendingIntentId = parseInt(Math.random() * 100000, 10);
//noinspection JSUnresolvedFunction,JSUnresolvedVariable
const mPendingIntent = android.app.PendingIntent.getActivity(application.android.context, mPendingIntentId, mStartActivity, android.app.PendingIntent.FLAG_CANCEL_CURRENT);
//noinspection JSUnresolvedFunction,JSUnresolvedVariable
const mgr = application.android.context.getSystemService(android.content.Context.ALARM_SERVICE);
//noinspection JSUnresolvedFunction,JSUnresolvedVariable
mgr.set(android.app.AlarmManager.RTC, java.lang.System.currentTimeMillis() + 100, mPendingIntent);
//noinspection JSUnresolvedFunction,JSUnresolvedVariable
android.os.Process.killProcess(android.os.Process.myPid());
} else if (global.ios) {
dialogs.alert({
title: "Please restart application",
message: msg || "The application needs to be restarted.",
okButtonText: "Quit!"
}).then(function () {
//noinspection JSUnresolvedFunction,JSHint
exit(0);
});
return false;
}
};
}
if (!global.masterProcess.exit) {
global.masterProcess.exit = function() {
if (global.android) {
const application = require('application');
application.android.foregroundActivity.finish();
} else if (global.ios) {
exit(0);
}
};
}
if (!global.masterProcess.isDebug) {
if (global.android) {
const getAppSignatures = function() {
const context = getContext();
try {
const packageManager = context.getPackageManager();
// GET_SIGNATURES = 64
return packageManager.getPackageInfo(context.getPackageName(), 64).signatures;
} catch (err) {
return [];
}
};
global.masterProcess.isDebug = function () {
const DEBUG_PRINCIPAL = new javax.security.auth.x500.X500Principal("CN=Android Debug,O=Android,C=US");
try {
const signatures = getAppSignatures();
const cf = java.security.cert.CertificateFactory.getInstance("X.509");
for (let i = 0; i < signatures.length; i++) {
// Convert back into a Certificate
const stream = new java.io.ByteArrayInputStream(signatures[i].toByteArray());
const cert = cf.generateCertificate(stream);
// Get the Principal for the signing Signature
const SigningPrincipal = cert.getSubjectX500Principal();
if (SigningPrincipal.equals(DEBUG_PRINCIPAL)) {
this._isDebugMode = true;
return true;
}
}
this._isDebugMode = false;
}
catch (err) {
}
return false;
};
} else if (global.ios) {
global.masterProcess.isDebug = function() {
// TODO: At this point their doesn't seem to be an easy way to determine if the app is debuggable on iOS from the environment
// So We will just check for if we are running on an emulator.
// TODO: We might be able to use the ASN.1 info, see https://github.com/blindsightcorp/BSMobileProvision
// This is not defined in Debug mode as of TNS 1.8, need to check --release (No console available)
// TODO: Check NSProcessInfo.processInfo().environment.objectForKey('BUILD_CONFIGURATION');
return global.masterProcess.isEmulator();
};
}
}
if (!global.masterProcess.isEmulator) {
if (global.android) {
global.masterProcess.isEmulator = function() {
var res = android.os.Build.FINGERPRINT;
if (res.indexOf("vbox86") >= 0 || res.indexOf("generic") >= 0) { return true; }
return false;
};
} else if (global.ios) {
global.masterProcess.isEmulator = function() {
return iosProperty(UIDevice, UIDevice.currentDevice).name.toLowerCase().indexOf("simulator") !== -1;
};
}
}
if (!global.masterProcess.version) {
if (global.android) {
global.masterProcess.version = function() {
const context = getContext();
try {
const packageManager = context.getPackageManager();
// GET_PACKAGE = 0
const pkg = packageManager.getPackageInfo(context.getPackageName(), 0);
return {version : pkg.versionName, build: pkg.versionCode};
}
catch (err) {
return {version: "", build: 0};
}
};
} else {
global.masterProcess.version = function () {
const mainBundle = iosProperty(NSBundle, NSBundle.mainBundle);
const appVersion = mainBundle.objectForInfoDictionaryKey("CFBundleShortVersionString");
const buildNumber = mainBundle.objectForInfoDictionaryKey("CFBundleVersion");
return {version: appVersion, build: buildNumber};
};
}
}
function iosProperty(theClass, theProperty) {
if (typeof theProperty === "function") {
// xCode 7 and below
return theProperty.call(theClass);
} else {
// xCode 8+
return theProperty;
}
}
// ---------------------------------------------------------------
// Getting android context
// ---------------------------------------------------------------
function getContext() {
const ctx = java.lang.Class.forName("android.app.AppGlobals").getMethod("getInitialApplication", null).invoke(null, null);
if (ctx) { return ctx; }
return java.lang.Class.forName("android.app.ActivityThread").getMethod("currentApplication", null).invoke(null, null);
}
// Thanks to the NativeScript guys (Yavor Georgiev & Georgi Atanasov) for the basis of the processMessages code
if (!global.masterProcess.processMessages) {
if (global.android) {
const platform = require('platform');
let nextMethod, targetField, prepared=false;
const sdkVersion = parseInt(platform.device.sdkVersion);
const prepareMethods = function() {
const clsMsgQueue = java.lang.Class.forName("android.os.MessageQueue");
const clsMsg = java.lang.Class.forName("android.os.Message");
const methods = clsMsgQueue.getDeclaredMethods();
let i;
for (i = 0; i < methods.length; i++) {
if (methods[i].getName() === "next") {
nextMethod = methods[i];
nextMethod.setAccessible(true);
break;
}
}
let fields = clsMsg.getDeclaredFields();
for (i = 0; i < fields.length; i++) {
if (fields[i].getName() === "target") {
targetField = fields[i];
targetField.setAccessible(true);
break;
}
}
prepared = true;
};
global.masterProcess.processMessages = function() {
let quit = false, counter = 0;
if (!prepared) { prepareMethods(); }
let queue = android.os.Looper.myQueue(), msg;
setTimeout(function() { quit = true;}, 250);
while (!quit ) {
counter++;
msg = nextMethod.invoke(queue, null);
if (msg) {
const target = targetField.get(msg);
if (!target) {
quit = true;
} else {
target.dispatchMessage(msg);
}
if (sdkVersion < 21) {//https://code.google.com/p/android-test-kit/issues/detail?id=84
msg.recycle();
}
} else {
quit = true;
}
}
};
} else if (global.ios) {
global.masterProcess.processMessages = function() {
NSRunLoop.currentRunLoop().runUntilDate(NSDate.dateWithTimeIntervalSinceNow(0.1));
};
}
}
if (typeof global.console.keys === 'undefined') {
console.keys = function(data, printValue) {
if (typeof data === "string") {
console.log(data); return;
}
console.log("=========[ Keys ]==========");
for(var key in data) {
if (data.hasOwnProperty(key)) {
if (printValue) {
console.log(key + ': ', data[key]);
} else {
console.log(key);
}
}
}
console.log("===========================");
};
}