forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathandroid-utils.lcb
More file actions
340 lines (275 loc) · 15.6 KB
/
Copy pathandroid-utils.lcb
File metadata and controls
340 lines (275 loc) · 15.6 KB
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
/*
Copyright (C) 2018 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
/**
A library of utility handlers for functions commonly needed by Android widgets.
*/
module com.livecode.library.androidutils
use com.livecode.foreign
use com.livecode.java
use com.livecode.canvas
use com.livecode.library.widgetutils
metadata version is "1.0.0"
metadata author is "LiveCode"
metadata title is "Android Utilities"
metadata os is "android"
private handler ColorComponentToInt(in pComponent as Real) returns Integer
multiply pComponent by 255
round pComponent
return pComponent
end handler
__safe foreign handler _JNI_GetColorFromARGB(in pA as JInt, in pR as JInt, in pG as JInt, in pB as JInt) returns JInt \
binds to "java:android.graphics.Color>argb(IIII)I!static"
/**
Summary: Convert a color string to an integer for Android
Parameters:
pString: The color string
Description:
Use the <StringToAndroidColor> handler to convert a string representing
a color to an integer that can be used with Android color APIs.
*/
public handler StringToAndroidColor(in pString as String) returns Integer
variable tColor as Color
put stringToColor(pString) into tColor
variable tA as Integer
variable tR as Integer
variable tG as Integer
variable tB as Integer
put ColorComponentToInt(the alpha of tColor) into tA
put ColorComponentToInt(the red of tColor) into tR
put ColorComponentToInt(the green of tColor) into tG
put ColorComponentToInt(the blue of tColor) into tB
return _JNI_GetColorFromARGB(tA,tR,tG,tB)
end handler
__safe foreign handler _JNI_GetAndroidEngine() returns JObject \
binds to "java:com.runrev.android.Engine>getEngine()Lcom/runrev/android/Engine;!static"
__safe foreign handler _JNI_GetEngineContext(in pEngine as JObject) returns JObject \
binds to "java:android.view.View>getContext()Landroid/content/Context;"
/**
Summary: Get the application Context
Example:
__safe foreign handler _JNI_CreateButton(in pContext as JObject) returns JObject \
binds to "java:android.widget.Button>new(Landroid/content/Context;)?ui"
handler NativeButtonView() returns JObject
return _JNI_CreateButton(ApplicationContext())
end handler
Description:
Use the <ApplicationContext> handler to fetch the current application's
Context object.
*/
public handler ApplicationContext() returns JObject
return _JNI_GetEngineContext(_JNI_GetAndroidEngine())
end handler
private foreign handler MCAndroidCheckRuntimePermission(in pPermission as String) \
returns CBool binds to "<builtin>"
/**
Summary:
Displays a dialog showing a permission request for <pPermission>. If a user
has already granted permission for <pPermission>, this function does nothing.
Returns true if permission <pPermission> has been granted by the user.
Parameters:
pPermission (enum):
The name of the permission to request.
- "android.permission.READ_CALENDAR": permission to allow an application to read the device's calendar.
- "android.permission.WRITE_CALENDAR": permission to allow an application to write to the device's calendar.
- "android.permission.CAMERA": permission to allow an application to access the device's camera.
- "android.permission.ACCESS_COARSE_LOCATION": permission to allow an application to access the device's coarse location.
- "android.permission.ACCESS_FINE_LOCATION": permission to allow an application to access the device's fine location.
- "android.permission.READ_CONTACTS": permission to allow an application to read data from the device's contacts.
- "android.permission.WRITE_CONTACTS": permission to allow an application to write date to the device's contacts.
- "android.permission.GET_ACCOUNTS": permission to allow an application to access to the list of accounts in the Accounts Service.
- "android.permission.RECORD_AUDIO": permission to allow an application to allow an application to record audio.
- "android.permission.READ_EXTERNAL_STORAGE": permission to allow an application to read data from the device's external storage.
- "android.permission.WRITE_EXTERNAL_STORAGE": permission to allow an application to write data to the device's external storage.
- "android.permission.READ_PHONE_STATE": permission to allow an application to access phone state, including the phone number of the device, current cellular network information, the status of any ongoing calls, and a list of any PhoneAccounts registered on the device.
- "android.permission.READ_PHONE_NUMBERS": permission to allow an application to access the device's phone number(s).
- "android.permission.CALL_PHONE": permission to allow an application to initiate a phone call without going through the Dialer user interface for the user to confirm the call.
- "android.permission.ANSWER_PHONE_CALLS": permission to allow an application to answer an incoming phone call.
- "android.permission.READ_CALL_LOG": permission to allow an application to read the user's call log.
- "android.permission.WRITE_CALL_LOG": permission to allow an application to write to the user's call log.
- "android.permission.ADD_VOICEMAIL": permission to allow an application to add voicemails into the system.
- "android.permission.USE_SIP": permission to allow an application to use SIP service.
- "android.permission.PROCESS_OUTGOING_CALLS": permission to allow an application to see the number being dialed during an outgoing call with the option to redirect the call to a different number or abort the call altogether.
- "android.permission.SEND_SMS": permission to allow an application to send SMS messages.
- "android.permission.RECEIVE_SMS": permission to allow an application to receive SMS messages
- "android.permission.READ_SMS": permission to allow an application to read SMS messages.
- "android.permission.RECEIVE_WAP_PUSH": permission to allow an application to receive WAP push messages.
- "android.permission.RECEIVE_MMS": permission to allow an application to receive MMS messages.
- "android.permission.BODY_SENSORS": permission to allow an application to access data from sensors that the user uses to measure what is happening inside his/her body, such as heart rate.
Returns(boolean):
True if permission has been granted, false otherwise.
Description:
Use the <AndroidRequestPermission> command to request permission for
<pPermission> from the user.
>*Note:* Permission names are case sensitive.
*/
public handler AndroidRequestPermission(in pPermission as String) returns Boolean
variable tHasPermission as Boolean
unsafe
MCAndroidCheckRuntimePermission(pPermission)
put MCAndroidHasPermission(pPermission) into tHasPermission
end unsafe
return tHasPermission
end handler
private foreign handler MCAndroidCheckPermissionExists(in pPermission as String) \
returns CBool binds to "<builtin>"
/**
Summary:
Returns true if <pPermission> is a valid Android permission name
Parameters:
pPermission (string):
The name of the permission to check.
Returns(boolean):
True if <pPermission> is a valid Android permission name, false otherwise.
Description:
Use the <AndroidPermissionExists> function to check if <pPermission> is a valid
Android permission name.
>*Note:* Permission names are case sensitive.
*/
public handler AndroidPermissionExists(in pPermission as String) returns Boolean
variable tPermissionExists as Boolean
unsafe
put MCAndroidCheckPermissionExists(pPermission) into tPermissionExists
end unsafe
return tPermissionExists
end handler
private foreign handler MCAndroidHasPermission(in pPermission as String) \
returns CBool binds to "<builtin>"
/**
Summary:
Returns true if permission <pPermission> has been granted by the user.
Parameters:
pPermission (enum):
The name of the permission to request.
- "android.permission.READ_CALENDAR": permission to allow an application to read the device's calendar.
- "android.permission.WRITE_CALENDAR": permission to allow an application to write to the device's calendar.
- "android.permission.CAMERA": permission to allow an application to access the device's camera.
- "android.permission.ACCESS_COARSE_LOCATION": permission to allow an application to access the device's coarse location.
- "android.permission.ACCESS_FINE_LOCATION": permission to allow an application to access the device's fine location.
- "android.permission.READ_CONTACTS": permission to allow an application to read data from the device's contacts.
- "android.permission.WRITE_CONTACTS": permission to allow an application to write date to the device's contacts.
- "android.permission.GET_ACCOUNTS": permission to allow an application to access to the list of accounts in the Accounts Service.
- "android.permission.RECORD_AUDIO": permission to allow an application to allow an application to record audio.
- "android.permission.READ_EXTERNAL_STORAGE": permission to allow an application to read data from the device's external storage.
- "android.permission.WRITE_EXTERNAL_STORAGE": permission to allow an application to write data to the device's external storage.
- "android.permission.READ_PHONE_STATE": permission to allow an application to access phone state, including the phone number of the device, current cellular network information, the status of any ongoing calls, and a list of any PhoneAccounts registered on the device.
- "android.permission.READ_PHONE_NUMBERS": permission to allow an application to access the device's phone number(s).
- "android.permission.CALL_PHONE": permission to allow an application to initiate a phone call without going through the Dialer user interface for the user to confirm the call.
- "android.permission.ANSWER_PHONE_CALLS": permission to allow an application to answer an incoming phone call.
- "android.permission.READ_CALL_LOG": permission to allow an application to read the user's call log.
- "android.permission.WRITE_CALL_LOG": permission to allow an application to write to the user's call log.
- "android.permission.ADD_VOICEMAIL": permission to allow an application to add voicemails into the system.
- "android.permission.USE_SIP": permission to allow an application to use SIP service.
- "android.permission.PROCESS_OUTGOING_CALLS": permission to allow an application to see the number being dialed during an outgoing call with the option to redirect the call to a different number or abort the call altogether.
- "android.permission.SEND_SMS": permission to allow an application to send SMS messages.
- "android.permission.RECEIVE_SMS": permission to allow an application to receive SMS messages
- "android.permission.READ_SMS": permission to allow an application to read SMS messages.
- "android.permission.RECEIVE_WAP_PUSH": permission to allow an application to receive WAP push messages.
- "android.permission.RECEIVE_MMS": permission to allow an application to receive MMS messages.
- "android.permission.BODY_SENSORS": permission to allow an application to access data from sensors that the user uses to measure what is happening inside his/her body, such as heart rate.
Returns(boolean):
True if permission has been granted, false otherwise.
Description:
Use the <AndroidHasPermission> function to find out if permission
<pPermission> has been granted by the user.
>*Note:* Permission names are case sensitive.
*/
public handler AndroidHasPermission(in pPermission as String) returns Boolean
variable tHasPermission as Boolean
unsafe
put MCAndroidHasPermission(pPermission) into tHasPermission
end unsafe
return tHasPermission
end handler
public handler type OnPauseHandler() returns nothing
public handler type OnResumeHandler() returns nothing
__safe foreign handler _JNI_Engine_LifecycleListener(in pListener as Array) returns JObject \
binds to "java:com.runrev.android.Engine$LifecycleListener>interface()"
__safe foreign handler _JNI_Engine_RegisterLifecycleListener(in pSelf as JObject, in pListener as JObject) returns JBoolean \
binds to "java:com.runrev.android.Engine>registerLifecycleListener(Lcom/runrev/android/Engine$LifecycleListener;)Z"
__safe foreign handler _JNI_Engine_UnregisterLifecycleListener(in pSelf as JObject, in pListener as JObject) returns JBoolean \
binds to "java:com.runrev.android.Engine>unregisterLifecycleListener(Lcom/runrev/android/Engine$LifecycleListener;)Z"
/**
Summary:
Register handlers to be called when on application lifecycle events.
Parameters:
pPauseHandler: The handler to be called when the application is paused
pResumeHandler: The handler to be called when the application is resumed
Returns:
A listener object that wraps the registered handlers.
Example:
private variable mLifecycleListener as optional JObject
public handler OnOpen() returns nothing
put AndroidRegisterLifecycleListener(OnMyWidgetPaused, OnMyWidgetResumed) into mLifecycleListener
end handler
public handler OnClose() returns nothing
AndroidUnregisterLifecycleListener(mLifecycleListener)
put nothing into mLifecycleListener
end handler
private handler OnMyWidgetPaused() returns nothing
-- Perform pause actions
end handler
private handler OnMyWidgetResumed() returns nothing
-- Perform resume actions
end handler
Description:
Use the <AndroidRegisterLifecycleListener> handler to register handlers to
application lifecycle events. The handler <pPauseHandler> will be called when
the application is paused and enters into the background. The handler
<pResumeHandler> will be called when the application is resumed and returns from
the background.
A listener object will be returned that wraps the registered handlers and can be
used with <AndroidUnregisterLifecycleListener> to cancel the registration.
*/
public handler AndroidRegisterLifecycleListener(in pPauseHandler as OnPauseHandler, in pResumeHandler as OnResumeHandler) returns JObject
variable tEngine as JObject
put _JNI_GetAndroidEngine() into tEngine
variable tLifecycleListenerHandler as Array
put { \
"OnPause": pPauseHandler, \
"OnResume": pResumeHandler \
} into tLifecycleListenerHandler
variable tLifecycleListener as JObject
put _JNI_Engine_LifecycleListener(tLifecycleListenerHandler) \
into tLifecycleListener
_JNI_Engine_RegisterLifecycleListener(tEngine, tLifecycleListener)
return tLifecycleListener
end handler
/**
Summary:
Unregister an object that is listening on application lifecycle events.
Parameters:
pListener: The listener object to be unregistered
Returns:
True if the listener object was previously registered and has been successfully
unregistered.
Example:
private variable mLifecycleListener as optional JObject
public handler OnOpen() returns nothing
put AndroidRegisterLifecycleListener(OnMyWidgetPaused, OnMyWidgetResumed) into mLifecycleListener
end handler
public handler OnClose() returns nothing
AndroidUnregisterLifecycleListener(mLifecycleListener)
put nothing into mLifecycleListener
end handler
Description:
Use the <AndroidUnregisterLifecycleListener> handler to unregister an object
created with <AndroidRegisterLifecycleListener> that is listening to application
lifecycle events. This will have the effect that any handlers that the listener
object is wrapping will no longer be called.
*/
public handler AndroidUnregisterLifecycleListener(in pListener as JObject) returns Boolean
variable tEngine as JObject
put _JNI_GetAndroidEngine() into tEngine
return _JNI_Engine_UnregisterLifecycleListener(tEngine, pListener)
end handler
end module