FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NativeScript/application/application.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
/
application
/
application.android.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
369 lines (289 loc) · 13.4 KB
Breadcrumbs
NativeScript
/
application
/
application.android.ts
Copy path
File metadata and controls
369 lines (289 loc) · 13.4 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import
appModule
=
require
(
"./application-common"
)
;
import
dts
=
require
(
"application"
)
;
import
frame
=
require
(
"ui/frame"
)
;
import
types
=
require
(
"utils/types"
)
;
import
observable
=
require
(
"data/observable"
)
;
import
enums
=
require
(
"ui/enums"
)
;
import
fileResolverModule
=
require
(
"file-system/file-name-resolver"
)
;
global
.
moduleMerge
(
appModule
,
exports
)
;
// We are using the exports object for the common events since we merge the appModule with this module's exports, which is what users will receive when require("application") is called;
// TODO: This is kind of hacky and is "pure JS in TypeScript"
var
initEvents
=
function
(
)
{
var
androidApp
:
dts
.
AndroidApplication
=
exports
.
android
;
// TODO: Verify whether the logic for triggerring application-wide events based on Activity callbacks is working properly
var
lifecycleCallbacks
=
new
android
.
app
.
Application
.
ActivityLifecycleCallbacks
(
{
onActivityCreated
:
function
(
activity
:
any
,
bundle
:
any
)
{
if
(
!
(
activity
instanceof
(
<
any
>
com
)
.
tns
.
NativeScriptActivity
)
)
{
return
;
}
if
(
!
androidApp
.
startActivity
)
{
androidApp
.
startActivity
=
activity
;
androidApp
.
notify
(
<
dts
.
AndroidActivityBundleEventData
>
{
eventName
:
"activityCreated"
,
object
:
androidApp
,
activity
:
activity
,
bundle
:
bundle
}
)
;
if
(
androidApp
.
onActivityCreated
)
{
androidApp
.
onActivityCreated
(
activity
,
bundle
)
;
}
}
androidApp
.
currentContext
=
activity
;
}
,
onActivityDestroyed
:
function
(
activity
:
any
)
{
if
(
!
(
activity
instanceof
(
<
any
>
com
)
.
tns
.
NativeScriptActivity
)
)
{
return
;
}
// Clear the current activity reference to prevent leak
if
(
activity
===
androidApp
.
foregroundActivity
)
{
androidApp
.
foregroundActivity
=
undefined
;
}
if
(
activity
===
androidApp
.
currentContext
)
{
androidApp
.
currentContext
=
undefined
;
}
if
(
activity
===
androidApp
.
startActivity
)
{
if
(
exports
.
onExit
)
{
exports
.
onExit
(
)
;
}
exports
.
notify
(
<
dts
.
ApplicationEventData
>
{
eventName
:
dts
.
exitEvent
,
object
:
androidApp
,
android
:
activity
}
)
;
androidApp
.
startActivity
=
undefined
;
}
androidApp
.
notify
(
<
dts
.
AndroidActivityEventData
>
{
eventName
:
"activityDestroyed"
,
object
:
androidApp
,
activity
:
activity
}
)
;
if
(
androidApp
.
onActivityDestroyed
)
{
androidApp
.
onActivityDestroyed
(
activity
)
;
}
// TODO: This is a temporary workaround to force the V8's Garbage Collector, which will force the related Java Object to be collected.
gc
(
)
;
}
,
onActivityPaused
:
function
(
activity
:
any
)
{
if
(
!
(
activity
instanceof
(
<
any
>
com
)
.
tns
.
NativeScriptActivity
)
)
{
return
;
}
(
<
any
>
androidApp
)
.
paused
=
true
;
if
(
activity
===
androidApp
.
foregroundActivity
)
{
if
(
exports
.
onSuspend
)
{
exports
.
onSuspend
(
)
;
}
exports
.
notify
(
<
dts
.
ApplicationEventData
>
{
eventName
:
dts
.
suspendEvent
,
object
:
androidApp
,
android
:
activity
}
)
;
}
androidApp
.
notify
(
<
dts
.
AndroidActivityEventData
>
{
eventName
:
"activityPaused"
,
object
:
androidApp
,
activity
:
activity
}
)
;
if
(
androidApp
.
onActivityPaused
)
{
androidApp
.
onActivityPaused
(
activity
)
;
}
}
,
onActivityResumed
:
function
(
activity
:
any
)
{
if
(
!
(
activity
instanceof
(
<
any
>
com
)
.
tns
.
NativeScriptActivity
)
)
{
return
;
}
(
<
any
>
androidApp
)
.
paused
=
false
;
if
(
activity
===
androidApp
.
foregroundActivity
)
{
if
(
exports
.
onResume
)
{
exports
.
onResume
(
)
;
}
exports
.
notify
(
<
dts
.
ApplicationEventData
>
{
eventName
:
dts
.
resumeEvent
,
object
:
androidApp
,
android
:
activity
}
)
;
}
androidApp
.
notify
(
<
dts
.
AndroidActivityEventData
>
{
eventName
:
"activityResumed"
,
object
:
androidApp
,
activity
:
activity
}
)
;
if
(
androidApp
.
onActivityResumed
)
{
androidApp
.
onActivityResumed
(
activity
)
;
}
}
,
onActivitySaveInstanceState
:
function
(
activity
:
any
,
bundle
:
any
)
{
if
(
!
(
activity
instanceof
(
<
any
>
com
)
.
tns
.
NativeScriptActivity
)
)
{
return
;
}
androidApp
.
notify
(
<
dts
.
AndroidActivityBundleEventData
>
{
eventName
:
"saveActivityState"
,
object
:
androidApp
,
activity
:
activity
,
bundle
:
bundle
}
)
;
if
(
androidApp
.
onSaveActivityState
)
{
androidApp
.
onSaveActivityState
(
activity
,
bundle
)
;
}
}
,
onActivityStarted
:
function
(
activity
:
any
)
{
if
(
!
(
activity
instanceof
(
<
any
>
com
)
.
tns
.
NativeScriptActivity
)
)
{
return
;
}
androidApp
.
foregroundActivity
=
activity
;
androidApp
.
notify
(
<
dts
.
AndroidActivityEventData
>
{
eventName
:
"activityStarted"
,
object
:
androidApp
,
activity
:
activity
}
)
;
if
(
androidApp
.
onActivityStarted
)
{
androidApp
.
onActivityStarted
(
activity
)
;
}
}
,
onActivityStopped
:
function
(
activity
:
any
)
{
if
(
!
(
activity
instanceof
(
<
any
>
com
)
.
tns
.
NativeScriptActivity
)
)
{
return
;
}
androidApp
.
notify
(
<
dts
.
AndroidActivityEventData
>
{
eventName
:
"activityStopped"
,
object
:
androidApp
,
activity
:
activity
}
)
;
if
(
androidApp
.
onActivityStopped
)
{
androidApp
.
onActivityStopped
(
activity
)
;
}
}
}
)
;
return
lifecycleCallbacks
;
}
app
.
init
(
{
getActivity
:
function
(
activity
:
android
.
app
.
Activity
)
{
var
intent
=
activity
.
getIntent
(
)
return
exports
.
android
.
getActivity
(
intent
)
;
}
,
onCreate
:
function
(
)
{
exports
.
android
.
init
(
this
)
;
}
}
)
;
export
class
AndroidApplication
extends
observable
.
Observable
implements
dts
.
AndroidApplication
{
public
static
activityCreatedEvent
=
"activityCreated"
;
public
static
activityDestroyedEvent
=
"activityDestroyed"
;
public
static
activityStartedEvent
=
"activityStarted"
;
public
static
activityPausedEvent
=
"activityPaused"
;
public
static
activityResumedEvent
=
"activityResumed"
;
public
static
activityStoppedEvent
=
"activityStopped"
;
public
static
saveActivityStateEvent
=
"saveActivityState"
;
public
static
activityResultEvent
=
"activityResult"
;
public
static
activityBackPressedEvent
=
"activityBackPressed"
;
public
nativeApp
:
android
.
app
.
Application
;
public
context
:
android
.
content
.
Context
;
public
currentContext
:
android
.
content
.
Context
;
public
foregroundActivity
:
android
.
app
.
Activity
;
public
startActivity
:
android
.
app
.
Activity
;
public
packageName
:
string
;
public
hasActionBar
:
boolean
;
public
onActivityCreated
:
(
activity
:
android
.
app
.
Activity
,
bundle
:
android
.
os
.
Bundle
)
=>
void
;
public
onActivityDestroyed
:
(
activity
:
android
.
app
.
Activity
)
=>
void
;
public
onActivityStarted
:
(
activity
:
android
.
app
.
Activity
)
=>
void
;
public
onActivityPaused
:
(
activity
:
android
.
app
.
Activity
)
=>
void
;
public
onActivityResumed
:
(
activity
:
android
.
app
.
Activity
)
=>
void
;
public
onActivityStopped
:
(
activity
:
android
.
app
.
Activity
)
=>
void
;
public
onSaveActivityState
:
(
activity
:
android
.
app
.
Activity
,
bundle
:
android
.
os
.
Bundle
)
=>
void
;
public
onActivityResult
:
(
requestCode
:
number
,
resultCode
:
number
,
data
:
android
.
content
.
Intent
)
=>
void
;
private
_eventsToken
:
any
;
public
getActivity
(
intent
:
android
.
content
.
Intent
)
:
Object
{
if
(
intent
&&
intent
.
getAction
(
)
===
android
.
content
.
Intent
.
ACTION_MAIN
)
{
// application's main activity
if
(
exports
.
onLaunch
)
{
exports
.
onLaunch
(
intent
)
;
}
exports
.
notify
(
{
eventName
:
dts
.
launchEvent
,
object
:
this
,
android
:
intent
}
)
;
setupOrientationListener
(
this
)
;
/* In the onLaunch event we expect the following setup, which ensures a root frame:
* var frame = require("ui/frame");
* var rootFrame = new frame.Frame();
* rootFrame.navigate({ pageModuleName: "mainPage" });
*/
}
var
topFrame
=
frame
.
topmost
(
)
;
if
(
!
topFrame
)
{
// try to navigate to the mainEntry/Module (if specified)
var
navParam
=
dts
.
mainEntry
;
if
(
!
navParam
)
{
navParam
=
dts
.
mainModule
;
}
if
(
navParam
)
{
topFrame
=
new
frame
.
Frame
(
)
;
topFrame
.
navigate
(
navParam
)
;
}
else
{
// TODO: Throw an exception?
throw
new
Error
(
"A Frame must be used to navigate to a Page."
)
;
}
}
return
topFrame
.
android
.
onActivityRequested
(
intent
)
;
}
public
init
(
nativeApp
:
any
)
{
this
.
nativeApp
=
nativeApp
;
this
.
packageName
=
nativeApp
.
getPackageName
(
)
;
this
.
context
=
nativeApp
.
getApplicationContext
(
)
;
this
.
_eventsToken
=
initEvents
(
)
;
this
.
nativeApp
.
registerActivityLifecycleCallbacks
(
this
.
_eventsToken
)
;
this
.
_registerPendingReceivers
(
)
;
}
private
_registeredReceivers
=
{
}
;
private
_pendingReceiverRegistrations
=
new
Array
<
(
context
:
android
.
content
.
Context
)
=>
void
>
(
)
;
private
_registerPendingReceivers
(
)
{
if
(
this
.
_pendingReceiverRegistrations
)
{
var
i
=
0
;
var
length
=
this
.
_pendingReceiverRegistrations
.
length
;
for
(
;
i
<
length
;
i
++
)
{
var
registerFunc
=
this
.
_pendingReceiverRegistrations
[
i
]
;
registerFunc
(
this
.
context
)
;
}
this
.
_pendingReceiverRegistrations
=
new
Array
<
(
context
:
android
.
content
.
Context
)
=>
void
>
(
)
;
}
}
public
registerBroadcastReceiver
(
intentFilter
:
string
,
onReceiveCallback
:
(
context
:
android
.
content
.
Context
,
intent
:
android
.
content
.
Intent
)
=>
void
)
{
var
that
=
this
;
var
registerFunc
=
function
(
context
:
android
.
content
.
Context
)
{
var
receiver
=
new
BroadcastReceiver
(
onReceiveCallback
)
;
context
.
registerReceiver
(
receiver
,
new
android
.
content
.
IntentFilter
(
intentFilter
)
)
;
that
.
_registeredReceivers
[
intentFilter
]
=
receiver
;
}
if
(
this
.
context
)
{
registerFunc
(
this
.
context
)
;
}
else
{
this
.
_pendingReceiverRegistrations
.
push
(
registerFunc
)
;
}
}
public
unregisterBroadcastReceiver
(
intentFilter
:
string
)
{
var
receiver
=
this
.
_registeredReceivers
[
intentFilter
]
;
if
(
receiver
)
{
this
.
context
.
unregisterReceiver
(
receiver
)
;
this
.
_registeredReceivers
[
intentFilter
]
=
undefined
;
delete
this
.
_registeredReceivers
[
intentFilter
]
;
}
}
}
class
BroadcastReceiver
extends
android
.
content
.
BroadcastReceiver
{
private
_onReceiveCallback
:
(
context
:
android
.
content
.
Context
,
intent
:
android
.
content
.
Intent
)
=>
void
;
constructor
(
onReceiveCallback
:
(
context
:
android
.
content
.
Context
,
intent
:
android
.
content
.
Intent
)
=>
void
)
{
super
(
)
;
this
.
_onReceiveCallback
=
onReceiveCallback
;
return
global
.
__native
(
this
)
;
}
public
onReceive
(
context
:
android
.
content
.
Context
,
intent
:
android
.
content
.
Intent
)
{
if
(
this
.
_onReceiveCallback
)
{
this
.
_onReceiveCallback
(
context
,
intent
)
;
}
}
}
global
.
__onUncaughtError
=
function
(
error
:
Error
)
{
// TODO: Obsolete this
if
(
types
.
isFunction
(
exports
.
onUncaughtError
)
)
{
exports
.
onUncaughtError
(
error
)
;
}
exports
.
notify
(
{
eventName
:
dts
.
uncaughtErrorEvent
,
object
:
appModule
.
android
,
android
:
error
}
)
;
}
exports
.
start
=
function
(
)
{
dts
.
loadCss
(
)
;
}
exports
.
android
=
new
AndroidApplication
(
)
;
var
currentOrientation
:
number
;
function
setupOrientationListener
(
androidApp
:
AndroidApplication
)
{
androidApp
.
registerBroadcastReceiver
(
android
.
content
.
Intent
.
ACTION_CONFIGURATION_CHANGED
,
onConfigurationChanged
)
;
currentOrientation
=
androidApp
.
context
.
getResources
(
)
.
getConfiguration
(
)
.
orientation
}
function
onConfigurationChanged
(
context
:
android
.
content
.
Context
,
intent
:
android
.
content
.
Intent
)
{
var
orientation
=
context
.
getResources
(
)
.
getConfiguration
(
)
.
orientation
;
if
(
currentOrientation
!==
orientation
)
{
currentOrientation
=
orientation
;
var
newValue
;
switch
(
orientation
)
{
case
android
.
content
.
res
.
Configuration
.
ORIENTATION_LANDSCAPE
:
newValue
=
enums
.
DeviceOrientation
.
landscape
;
break
;
case
android
.
content
.
res
.
Configuration
.
ORIENTATION_PORTRAIT
:
newValue
=
enums
.
DeviceOrientation
.
portrait
;
break
;
default
:
newValue
=
enums
.
DeviceOrientation
.
unknown
;
break
;
}
exports
.
notify
(
<
dts
.
OrientationChangedEventData
>
{
eventName
:
dts
.
orientationChangedEvent
,
android
:
context
,
newValue
:
newValue
,
object
:
exports
.
android
,
}
)
;
}
}
global
.
__onLiveSync
=
function
(
)
{
if
(
exports
.
android
&&
exports
.
android
.
paused
)
{
return
;
}
// Clear file resolver cache to respect newly added files.
fileResolverModule
.
clearCache
(
)
;
// Reload app.css in case it was changed.
appModule
.
loadCss
(
)
;
// Reload current page.
frame
.
reloadPage
(
)
;
}
Back
|
FazBrowse Home
|
New Git URL