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

Commit 84d2265

Browse files
committed
[[ AndroidUtils ]] Add ApplicationContext handler to android utils
1 parent 00f4907 commit 84d2265

File tree

5 files changed

+38
-26
lines changed

5 files changed

+38
-26
lines changed

extensions/libraries/androidbgaudio/androidbgaudio.lcb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ library com.livecode.library.androidbgaudio
2828
use com.livecode.foreign
2929
use com.livecode.java
3030
use com.livecode.engine
31+
use com.livecode.library.androidutils
3132

3233
metadata version is "0.0.0"
3334
metadata author is "LiveCode"
@@ -36,7 +37,6 @@ metadata title is "Android Background Audio"
3637
----
3738

3839
__safe foreign handler _JNI_EngineGet() returns JObject binds to "java:com.runrev.android.Engine>getEngine()Lcom/runrev/android/Engine;!static"
39-
__safe foreign handler _JNI_EngineGetContext(in pEngine as JObject) returns JObject binds to "java:android.view.View>getContext()Landroid/content/Context;"
4040

4141
__safe foreign handler _JNI_ContextGetAssets(in pContext as JObject) returns JObject \
4242
binds to "java:android.content.Context>getAssets()Landroid/content/res/AssetManager;"
@@ -89,13 +89,15 @@ private handler mediaplayerSetDataSource(in pString as String)
8989
return
9090
end if
9191

92+
variable tContext as JObject
93+
put ApplicationContext() into tContext
9294
if pString begins with "asset://" then
9395
variable tAssetFd as JObject
94-
put _JNI_AssetManagerOpenFd(_JNI_ContextGetAssets(_JNI_EngineGetContext(_JNI_EngineGet())), StringToJString(char 9 to -1 of pString)) into tAssetFd
96+
put _JNI_AssetManagerOpenFd(_JNI_ContextGetAssets(tContext), StringToJString(char 9 to -1 of pString)) into tAssetFd
9597
_JNI_MediaPlayerSetDataSourceFd(mMediaPlayer, _JNI_AssetFileDescriptorGetFileDescriptor(tAssetFd))
9698
_JNI_AssetFileDescriptorClose(tAssetFd)
9799
else
98-
_JNI_MediaPlayerSetDataSourceUri(mMediaPlayer, _JNI_EngineGetContext(_JNI_EngineGet()), _JNI_UriParse(StringToJString(pString)))
100+
_JNI_MediaPlayerSetDataSourceUri(mMediaPlayer, tContext, _JNI_UriParse(StringToJString(pString)))
99101
end if
100102
end handler
101103

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,28 @@ public handler StringToAndroidColor(in pString as String) returns Integer
6464

6565
return _JNI_GetColorFromARGB(tA,tR,tG,tB)
6666
end handler
67+
68+
__safe foreign handler _JNI_GetAndroidEngine() returns JObject \
69+
binds to "java:com.runrev.android.Engine>getEngine()Lcom/runrev/android/Engine;!static"
70+
__safe foreign handler _JNI_GetEngineContext(in pEngine as JObject) returns JObject \
71+
binds to "java:android.view.View>getContext()Landroid/content/Context;"
72+
73+
/**
74+
Summary: Get the application Context
75+
76+
Example:
77+
__safe foreign handler _JNI_CreateButton(in pContext as JObject) returns JObject \
78+
binds to "java:android.widget.Button>new(Landroid/content/Context;)?ui"
79+
80+
handler NativeButtonView() returns JObject
81+
return _JNI_CreateButton(ApplicationContext())
82+
end handler
83+
84+
Description:
85+
Use the <ApplicationContext> handler to fetch the current application's
86+
Context object.
87+
*/
88+
public handler ApplicationContext() returns JObject
89+
return _JNI_GetEngineContext(_JNI_GetAndroidEngine())
90+
end handler
6791
end module
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Android Utility Module
22

33
A new android utility module has been added. It currently
4-
contains one handler, StringToAndroidColor, which converts
5-
a string representing a color (optionally with alpha component)
6-
to an Android Color object.
4+
contains two handlers:
5+
6+
* `StringToAndroidColor`: convert a string representing a color (optionally with alpha component) to an Android Color object.
7+
* `ApplicationContext`: returns the current application Context object.

extensions/widgets/androidbutton/androidbutton.lcb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ metadata labelColor.default is "0,0,0"
9292
metadata labelColor.editor is "com.livecode.pi.colorwithalpha"
9393
metadata labelColor.section is "Colors"
9494

95-
__safe foreign handler _JNI_GetAndroidEngine() returns JObject \
96-
binds to "java:com.runrev.android.Engine>getEngine()Lcom/runrev/android/Engine;!static"
97-
__safe foreign handler _JNI_GetEngineContext(in pEngine as JObject) returns JObject \
98-
binds to "java:android.view.View>getContext()Landroid/content/Context;"
99-
10095
// Handlers for creating and attaching view
10196
__safe foreign handler _JNI_CreateButton(in pContext as JObject) returns JObject \
10297
binds to "java:android.widget.Button>new(Landroid/content/Context;)?ui"
@@ -147,12 +142,7 @@ end handler
147142

148143
private handler InitButtonView()
149144
// Create an android button using the Engine Context
150-
variable tEngine as JObject
151-
put _JNI_GetAndroidEngine() into tEngine
152-
153-
variable tContext as JObject
154-
put _JNI_GetEngineContext(tEngine) into tContext
155-
put _JNI_CreateButton(tContext) into mButton
145+
put _JNI_CreateButton(ApplicationContext()) into mButton
156146

157147
// put my native window into tParent
158148
variable tParent as Pointer

extensions/widgets/androidfield/androidfield.lcb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,6 @@ metadata author is "LiveCode"
129129
metadata title is "Android Native Field"
130130
metadata svgicon is "M28.06,4.95a.34.34,0,1,1,.34.34A.34.34,0,0,1,28.06,4.95Zm-3.45.34a.34.34,0,1,0-.34-.34A.34.34,0,0,0,24.6,5.29ZM36.25,2V17.24a2,2,0,0,1-2,2H2a2,2,0,0,1-2-2V2A2,2,0,0,1,2,0h32.3A2,2,0,0,1,36.25,2ZM22.39,6.8h8.23a3.76,3.76,0,0,0-2.1-3.31l.65-1.17a.13.13,0,0,0-.05-.18l-.06,0a.13.13,0,0,0-.11.07l-.66,1.19a4.43,4.43,0,0,0-3.56,0l-.66-1.19a.13.13,0,1,0-.23.13l.65,1.17A3.76,3.76,0,0,0,22.39,6.8ZM9.57,2.57H5v1.5H6.53v11H5v1.5H9.57v-1.5H8v-11H9.57ZM22,7.89a.92.92,0,0,0-1.84,0v3.84a.92.92,0,1,0,1.84,0Zm8.57-.76H22.39v6a1,1,0,0,0,1,1H24v2a.92.92,0,1,0,1.84,0v-2h1.23v2a.92.92,0,1,0,1.84,0v-2h.66a1,1,0,0,0,1-1Zm2.19.76a.92.92,0,0,0-1.84,0v3.84a.92.92,0,1,0,1.84,0Z"
131131

132-
__safe foreign handler _JNI_GetAndroidEngine() returns JObject \
133-
binds to "java:com.runrev.android.Engine>getEngine()Lcom/runrev/android/Engine;!static"
134-
__safe foreign handler _JNI_GetContext(in pView as JObject) returns JObject \
135-
binds to "java:android.view.View>getContext()Landroid/content/Context;"
136132
__safe foreign handler _JNI_GetSystemService(in pContext as JObject, in pService as JString) returns JObject \
137133
binds to "java:android.content.Context>getSystemService(Ljava/lang/String;)Ljava/lang/Object;"
138134

@@ -623,11 +619,8 @@ constant INPUT_METHOD_SERVICE is "input_method"
623619

624620
private handler InitView()
625621
// Create an android button using the Engine Context
626-
variable tEngine as JObject
627-
put _JNI_GetAndroidEngine() into tEngine
628-
629622
variable tContext as JObject
630-
put _JNI_GetContext(tEngine) into tContext
623+
put ApplicationContext() into tContext
631624
put _JNI_CreateView(tContext) into mNativeObj
632625

633626
// put my native window into tParent
@@ -1394,6 +1387,8 @@ handler SetTypeface(in pFont as Font)
13941387
PointerToJObject(tTypefacePtr))
13951388
end handler
13961389

1390+
__safe foreign handler _JNI_ViewGetContext(in pView as JObject) returns JObject \
1391+
binds to "java:android.view.View>getContext()Landroid/content/Context;"
13971392
__safe foreign handler _JNI_GetResources(in pContext as JObject) returns JObject \
13981393
binds to "java:android.content.Context>getResources()Landroid/content/res/Resources;"
13991394
__safe foreign handler _JNI_GetDisplayMetrics(in pResources as JObject) returns JObject \
@@ -1405,7 +1400,7 @@ public handler GetTextSize() returns Integer
14051400
put TextView_getTextSize(mNativeObj) into tTextSize
14061401

14071402
variable tContext as JObject
1408-
put _JNI_GetContext(mNativeObj) into tContext
1403+
put _JNI_ViewGetContext(mNativeObj) into tContext
14091404

14101405
variable tResources as JObject
14111406
put _JNI_GetResources(tContext) into tResources

0 commit comments

Comments
 (0)