-
Notifications
You must be signed in to change notification settings - Fork 23
/
util.ts
159 lines (133 loc) · 5.05 KB
/
util.ts
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
import {View} from "ui/core/view";
import * as platform from "platform";
import {Color} from "color";
import * as application from "application";
import * as utils from 'utils/utils';
import config from '../../config'
try {
var ArgonPrivate = require('argon-private');
} catch (e) {}
application.on(application.orientationChangedEvent, ()=>{
updateScreenOrientation();
setTimeout(updateScreenOrientation, 100);
});
let iosSharedApplication:UIApplication;
function getNativeScreenOrientation() {
if (application.ios) {
iosSharedApplication = iosSharedApplication || utils.ios.getter(UIApplication, UIApplication.sharedApplication);
const orientation = iosSharedApplication.statusBarOrientation;
switch (orientation) {
case UIInterfaceOrientation.Unknown:
case UIInterfaceOrientation.Portrait: return 0;
case UIInterfaceOrientation.PortraitUpsideDown: return 180;
case UIInterfaceOrientation.LandscapeLeft: return 90;
case UIInterfaceOrientation.LandscapeRight: return -90;
}
}
if (application.android) {
const context:android.content.Context = utils.ad.getApplicationContext();
const display:android.view.Display = context.getSystemService(android.content.Context.WINDOW_SERVICE).getDefaultDisplay();
const rotation = display.getRotation();
switch (rotation) {
case android.view.Surface.ROTATION_0: return 0;
case android.view.Surface.ROTATION_180: return 180;
case android.view.Surface.ROTATION_90: return -90;
case android.view.Surface.ROTATION_270: return 90;
}
}
return 0;
}
export let screenOrientation:number = 0;
function updateScreenOrientation() {
screenOrientation = getNativeScreenOrientation();
}
export const canDecrypt = !!ArgonPrivate;
export function decrypt(encryptedData:string) : Promise<string> {
if (!ArgonPrivate) return Promise.reject(new Error("This build of Argon is incapable of decrypting messages."))
return Promise.resolve().then(()=>{
return ArgonPrivate.decrypt(encryptedData)
});
}
export function getInternalVuforiaKey() : string|undefined {
return ArgonPrivate && ArgonPrivate.getVuforiaLicenseKey() || config.VUFORIA_LICENSE_KEY;
}
export function bringToFront(view: View) {
if (view.android) {
view.android.bringToFront();
} else if (view.ios) {
view.ios.superview.bringSubviewToFront(view.ios);
}
}
export function linearGradient(view:View, colors:(Color|string)[]) {
var _colors:any[] = [];
var nativeView = view['_nativeView'];
if (!nativeView) {
return;
}
colors.forEach(function (c, idx) {
if (!(c instanceof Color)) {
colors[idx] = new Color(c);
}
});
if (platform.device.os === platform.platformNames.android) {
var backgroundDrawable = nativeView.getBackground(),
LINEAR_GRADIENT = 0;
colors.forEach(function (c:Color) {
_colors.push(c.android);
});
if (!(backgroundDrawable instanceof android.graphics.drawable.GradientDrawable)) {
backgroundDrawable = new android.graphics.drawable.GradientDrawable();
backgroundDrawable.setColors(_colors);
backgroundDrawable.setGradientType(LINEAR_GRADIENT);
nativeView.setBackgroundDrawable(backgroundDrawable);
}
} else if (platform.device.os === platform.platformNames.ios) {
var iosView:UIView = view.ios;
var colorsArray = NSMutableArray.alloc().initWithCapacity(2);
colors.forEach(function (c:Color) {
colorsArray.addObject(interop.types.id(c.ios.CGColor));
});
var gradientLayer = CAGradientLayer.layer();
gradientLayer.colors = colorsArray;
gradientLayer.frame = iosView.bounds;
iosView.layer.insertSublayerAtIndex(gradientLayer, 0);
}
}
declare const inet_ntoa:any;
declare const getifaddrs:any;
declare const sockaddr_in:any;
declare const freeifaddrs:any;
function ipToString(inAddr) {
if (!inAddr) {
throw new Error('in == NULL');
}
if (inAddr.s_addr === 0x00000000) {
return '*';
} else {
return NSString.stringWithCStringEncoding(inet_ntoa(inAddr), 1).toString();
}
}
export function getIPAddressOfInterface($interface) {
var address = '-';
if (!$interface) {
return address;
}
var interfacesPtrPtr = new interop.Reference();
if (getifaddrs(interfacesPtrPtr) === 0) {
var interfacesPtr = interfacesPtrPtr[0];
var temp_addrPtr = interfacesPtr;
while (temp_addrPtr != null) {
if (temp_addrPtr[0].ifa_addr[0].sa_family === 2) {
var name = NSString.stringWithUTF8String(temp_addrPtr[0].ifa_name).toString().trim();
if (name == $interface) {
var ifa_addrPtr = temp_addrPtr[0].ifa_addr;
var ifa_addrPtrAsSockAddtr_in = new interop.Reference(sockaddr_in, ifa_addrPtr);
address = ipToString(ifa_addrPtrAsSockAddtr_in[0].sin_addr);
}
}
temp_addrPtr = temp_addrPtr[0].ifa_next;
}
freeifaddrs(interfacesPtr);
}
return address;
}