Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 8f1ab59

Browse files
committed
[[ Bug 22950 ]] Add StartActivityForResult to Android Utils
This patch adds new handler to facilitate starting activities by Intent and handling results. The `StartActivityForResult` handler registers a listener to handle the `onActivityResult` method of the engine activity and when handled calls a callback handler with the request code, result code and Intent object.
1 parent a134b6f commit 8f1ab59

2 files changed

Lines changed: 153 additions & 1 deletion

File tree

extensions/modules/android-utils/android-utils.lcb

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use com.livecode.java
2626
use com.livecode.canvas
2727
use com.livecode.library.widgetutils
2828

29-
metadata version is "1.0.0"
29+
metadata version is "1.1.0"
3030
metadata author is "LiveCode"
3131
metadata title is "Android Utilities"
3232
metadata os is "android"
@@ -90,6 +90,152 @@ public handler ApplicationContext() returns JObject
9090
return _JNI_GetEngineContext(_JNI_GetAndroidEngine())
9191
end handler
9292

93+
private variable sResultListeners as Array
94+
95+
public handler type OnActivityResultHandler(\
96+
in pRequestCode as JInt, \
97+
in pResultCode as JInt, \
98+
in pIntent as optional JObject) returns nothing
99+
100+
handler _OnActivityResultListener(\
101+
in pRequestCode as JObject, \
102+
in pResultCode as JObject, \
103+
in pIntent as optional JObject) returns nothing
104+
105+
variable tContext as JObject
106+
put ApplicationContext() into tContext
107+
108+
variable tRequestCode as JInt
109+
put _JNI_NumberIntValue(pRequestCode) into tRequestCode
110+
variable tResultCode as JInt
111+
put _JNI_NumberIntValue(pResultCode) into tResultCode
112+
113+
_JNI_LiveCodeActivityRemoveOnActivityResultListener(tContext, tResultCode)
114+
115+
variable tRequestCodeString as String
116+
put tRequestCode formatted as string into tRequestCodeString
117+
118+
if tRequestCodeString is among the keys of sResultListeners then
119+
variable tHandler as OnActivityResultHandler
120+
put sResultListeners[tRequestCodeString] into tHandler
121+
delete sResultListeners[tRequestCodeString]
122+
tHandler(tRequestCode, tResultCode, pIntent)
123+
end if
124+
end handler
125+
126+
__safe foreign handler _JNI_LiveCodeActivityOnActivityResultListener( \
127+
in pCallbacks as Array) returns JObject \
128+
binds to "java:com.runrev.android.LiveCodeActivity$OnActivityResultListener>interface()"
129+
130+
__safe foreign handler _JNI_LiveCodeActivitySetOnActivityResultListener( \
131+
in pEngine as JObject, \
132+
in pListener as JObject, \
133+
in pRequestCode as JInt) returns nothing \
134+
binds to "java:com.runrev.android.LiveCodeActivity>setOnActivityResultListener(Lcom/runrev/android/LiveCodeActivity$OnActivityResultListener;I)V"
135+
136+
__safe foreign handler _JNI_LiveCodeActivityRemoveOnActivityResultListener( \
137+
in pEngine as JObject, \
138+
in pRequestCode as JInt) returns nothing \
139+
binds to "java:com.runrev.android.LiveCodeActivity>removeOnActivityResultListener(I)V"
140+
141+
__safe foreign handler _JNI_ActivityStartActivityForResult( \
142+
in pActivity as JObject, \
143+
in pIntent as JObject, \
144+
in pRequestCode as JInt) \
145+
returns nothing \
146+
binds to "java:android.app.Activity>startActivityForResult(Landroid/content/Intent;I)V"
147+
148+
__safe foreign handler _JNI_NumberIntValue(in pNumber as JObject) returns JInt \
149+
binds to "java:java.lang.Number>intValue()I"
150+
151+
/**
152+
Summary: Start an activity by Intent
153+
154+
Example:
155+
156+
constant kIntentACTION_SEND is "android.intent.action.SEND"
157+
constant kIntentEXTRA_TEXT is "android.intent.extra.TEXT"
158+
constant kActivityRESULT_CANCELED is 0
159+
constant kShareStringRequestCode is 123
160+
161+
__safe foreign handler _JNI_IntentNew(in pAction as JString) \
162+
returns JObject \
163+
binds to "java:android.content.Intent>new(Ljava/lang/String;)"
164+
165+
__safe foreign handler _JNI_IntentSetType(in pIntent as JObject, \
166+
in pType as JString) \
167+
returns JObject \
168+
binds to "java:android.content.Intent>setType(Ljava/lang/String;)Landroid/content/Intent;"
169+
170+
__safe foreign handler _JNI_IntentPutExtraString(in pIntent as JObject, \
171+
in pType as JString, \
172+
in pValue as JString) \
173+
returns JObject \
174+
binds to "java:android.content.Intent>putExtra(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;"
175+
176+
handler _ShareStringResultListener( \
177+
in pRequestCode as JInt, \
178+
in pResultCode as JInt, \
179+
in pIntent as optional JObject) returns nothing
180+
181+
if pResultCode is kActivityRESULT_CANCELED then
182+
post "shareStringCancelled"
183+
else
184+
post "shareStringComplete"
185+
end if
186+
end handler
187+
188+
public handler ShareString(in pString as String) returns nothing
189+
variable tIntent as JObject
190+
put _JNI_IntentNew(StringToJString(kIntentACTION_SEND)) into tIntent
191+
192+
_JNI_IntentSetType(tIntent, StringToJString("text/plain"))
193+
194+
_JNI_IntentPutExtraString(tIntent, StringToJString(kIntentEXTRA_TEXT), \
195+
StringToJString(pString))
196+
197+
StartActivityForResult(tIntent, kShareStringRequestCode, _ShareStringResultListener)
198+
end handler
199+
200+
Parameters:
201+
pIntent: An Intent JObject to use to start an activity
202+
pRequestCode: A positive integer used to identify the request when handling
203+
`onActivityResult`.
204+
pHandler: A handler that conforms to the `OnActivityResultHandler` type
205+
206+
Description:
207+
Start an activity by Intent and receive a callback to the specified handler when
208+
the LiveCode activity receives the result via the `onActivityResult` method.
209+
210+
The callback must conform to the `OnActivityResultHandler` type which returns
211+
nothing and has parameters:
212+
213+
- in pRequestCode as JInt
214+
- in pResultCode as JInt
215+
- in pIntent as optional JObject
216+
217+
*/
218+
219+
public handler StartActivityForResult( \
220+
in pIntent as JObject, \
221+
in pRequestCode as JInt, \
222+
in pHandler as OnActivityResultHandler) returns nothing
223+
224+
variable tContext as JObject
225+
put ApplicationContext() into tContext
226+
227+
variable tListener as JObject
228+
put _JNI_LiveCodeActivityOnActivityResultListener(\
229+
{"onActivityResult" : _OnActivityResultListener }) into tListener
230+
231+
_JNI_LiveCodeActivitySetOnActivityResultListener(tContext, tListener, pRequestCode)
232+
233+
put pHandler into sResultListeners[pRequestCode formatted as string]
234+
235+
_JNI_ActivityStartActivityForResult(tContext, pIntent, pRequestCode)
236+
end handler
237+
238+
93239
private foreign handler MCAndroidCheckRuntimePermission(in pPermission as String) \
94240
returns CBool binds to "<builtin>"
95241

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Starting Activities and Handling Results
2+
3+
A new handler has been added to facilitate starting activities by Intent and
4+
handling results. The `StartActivityForResult` handler registers a listener to
5+
handle the `onActivityResult` method of the engine activity and when handled
6+
calls a callback handler with the request code, result code and Intent object.

0 commit comments

Comments
 (0)