FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NativeScript/utils/utils.android.ts at patch-1 · rclai/NativeScript · GitHub
rclai
/
NativeScript
Public
forked from
NativeScript/NativeScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
NativeScript
/
utils
/
utils.android.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
178 lines (146 loc) · 5.85 KB
Breadcrumbs
NativeScript
/
utils
/
utils.android.ts
Copy path
File metadata and controls
178 lines (146 loc) · 5.85 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
common
=
require
(
"./utils-common"
)
;
import
trace
=
require
(
"trace"
)
;
global
.
moduleMerge
(
common
,
exports
)
;
export
module
layout
{
var
density
=
-
1
;
var
metrics
:
android
.
util
.
DisplayMetrics
;
// cache the MeasureSpec constants here, to prevent extensive marshaling calls to and from Java
// TODO: While this boosts the performance it is error-prone in case Google changes these constants
var
MODE_SHIFT
=
30
;
var
MODE_MASK
=
0x3
<<
MODE_SHIFT
;
var
sdkVersion
=
-
1
;
var
useOldMeasureSpec
=
false
;
export
function
makeMeasureSpec
(
size
:
number
,
mode
:
number
)
:
number
{
if
(
sdkVersion
===
-
1
)
{
// check whether the old layout is needed
sdkVersion
=
ad
.
getApplicationContext
(
)
.
getApplicationInfo
(
)
.
targetSdkVersion
;
useOldMeasureSpec
=
sdkVersion
<=
android
.
os
.
Build
.
VERSION_CODES
.
JELLY_BEAN_MR1
;
}
if
(
useOldMeasureSpec
)
{
return
size
+
mode
;
}
return
(
size
&
~
MODE_MASK
)
|
(
mode
&
MODE_MASK
)
;
}
export
function
getDisplayMetrics
(
)
:
android
.
util
.
DisplayMetrics
{
if
(
!
metrics
)
{
metrics
=
ad
.
getApplicationContext
(
)
.
getResources
(
)
.
getDisplayMetrics
(
)
;
}
return
metrics
;
}
export
function
getDisplayDensity
(
)
:
number
{
if
(
density
===
-
1
)
{
density
=
getDisplayMetrics
(
)
.
density
;
}
return
density
;
}
export
function
toDevicePixels
(
value
:
number
)
:
number
{
return
value
*
getDisplayDensity
(
)
;
}
export
function
toDeviceIndependentPixels
(
value
:
number
)
:
number
{
return
value
/
getDisplayDensity
(
)
;
}
}
// 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
(
)
;
}
var
inputMethodManager
:
android
.
view
.
inputmethod
.
InputMethodManager
;
export
function
getInputMethodManager
(
)
{
if
(
!
inputMethodManager
)
{
inputMethodManager
=
<
android
.
view
.
inputmethod
.
InputMethodManager
>
getApplicationContext
(
)
.
getSystemService
(
android
.
content
.
Context
.
INPUT_METHOD_SERVICE
)
;
}
return
inputMethodManager
;
}
export
function
showSoftInput
(
nativeView
:
android
.
view
.
View
)
:
void
{
var
imm
=
getInputMethodManager
(
)
;
if
(
imm
&&
nativeView
instanceof
android
.
view
.
View
)
{
imm
.
showSoftInput
(
nativeView
,
android
.
view
.
inputmethod
.
InputMethodManager
.
SHOW_IMPLICIT
)
;
}
}
export
function
dismissSoftInput
(
nativeView
:
android
.
view
.
View
)
:
void
{
var
imm
=
getInputMethodManager
(
)
;
if
(
imm
&&
nativeView
instanceof
android
.
view
.
View
)
{
imm
.
hideSoftInputFromWindow
(
nativeView
.
getWindowToken
(
)
,
0
)
;
}
}
export
module
collections
{
export
function
stringArrayToStringSet
(
str
:
string
[
]
)
:
java
.
util
.
HashSet
<
string
>
{
var
hashSet
=
new
java
.
util
.
HashSet
<
string
>
(
)
;
if
(
"undefined"
!==
typeof
str
)
{
for
(
var
element
in
str
)
{
hashSet
.
add
(
''
+
str
[
element
]
)
;
}
}
return
hashSet
;
}
export
function
stringSetToStringArray
(
stringSet
:
any
)
:
string
[
]
{
var
arr
=
[
]
;
if
(
"undefined"
!==
typeof
stringSet
)
{
var
it
=
stringSet
.
iterator
(
)
;
while
(
it
.
hasNext
(
)
)
{
var
element
=
''
+
it
.
next
(
)
;
arr
.
push
(
element
)
;
}
}
return
arr
;
}
}
export
module
resources
{
var
attr
;
var
attrCache
=
new
Map
<
string
,
number
>
(
)
;
export
function
getDrawableId
(
name
)
{
return
getId
(
":drawable/"
+
name
)
;
}
export
function
getStringId
(
name
)
{
return
getId
(
":string/"
+
name
)
;
}
export
function
getId
(
name
:
string
)
:
number
{
var
resources
=
getApplicationContext
(
)
.
getResources
(
)
;
var
packageName
=
getApplicationContext
(
)
.
getPackageName
(
)
;
var
uri
=
packageName
+
name
;
return
resources
.
getIdentifier
(
uri
,
null
,
null
)
;
}
export
function
getPalleteColor
(
name
:
string
,
context
:
android
.
content
.
Context
)
:
number
{
if
(
attrCache
.
has
(
name
)
)
{
return
attrCache
.
get
(
name
)
;
}
var
result
=
0
;
try
{
if
(
!
attr
)
{
attr
=
java
.
lang
.
Class
.
forName
(
"android.support.v7.appcompat.R$attr"
)
}
let
colorID
=
0
;
let
field
=
attr
.
getField
(
name
)
;
if
(
field
)
{
colorID
=
field
.
getInt
(
null
)
;
}
if
(
colorID
)
{
let
typedValue
=
new
android
.
util
.
TypedValue
(
)
;
context
.
getTheme
(
)
.
resolveAttribute
(
colorID
,
typedValue
,
true
)
;
result
=
typedValue
.
data
;
}
}
catch
(
ex
)
{
trace
.
write
(
"Cannot get pallete color: "
+
name
,
trace
.
categories
.
Error
,
trace
.
messageType
.
error
)
;
}
attrCache
.
set
(
name
,
result
)
;
return
result
;
}
}
}
export
function
GC
(
)
{
gc
(
)
;
}
export
function
openUrl
(
location
:
string
)
:
boolean
{
var
context
=
ad
.
getApplicationContext
(
)
;
try
{
var
intent
=
new
android
.
content
.
Intent
(
android
.
content
.
Intent
.
ACTION_VIEW
,
android
.
net
.
Uri
.
parse
(
location
.
trim
(
)
)
)
;
intent
.
addFlags
(
android
.
content
.
Intent
.
FLAG_ACTIVITY_NEW_TASK
)
;
context
.
startActivity
(
intent
)
;
}
catch
(
e
)
{
// We Don't do anything with an error. We just output it
console
.
error
(
"Error in OpenURL"
,
e
)
;
return
false
;
}
return
true
;
}
Back
|
FazBrowse Home
|
New Git URL