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

Commit 896e9ee

Browse files
Merge pull request #6371 from livecodeali/feature-android_utils
[[ AndroidUtils ]] Add android utility library
2 parents 019b8ac + b57aad2 commit 896e9ee

File tree

8 files changed

+120
-94
lines changed

8 files changed

+120
-94
lines changed

Installer/package.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ component Extensions
427427
rfolder macosx:packaged_extensions/com.livecode.library.androidbgaudio
428428
into [[ToolsFolder]]/Extensions place
429429
rfolder macosx:packaged_extensions/com.livecode.library.androidaudiorecorder
430+
into [[ToolsFolder]]/Extensions place
431+
rfolder macosx:packaged_extensions/com.livecode.library.androidutils
430432
into [[ToolsFolder]]/Extensions place
431433
rfolder macosx:packaged_extensions/com.livecode.library.iconsvg
432434
into [[ToolsFolder]]/Extensions place

extensions/extensions.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
'sources':
140140
[
141141
'modules/widget-utils/widget-utils.lcb',
142+
'modules/android-utils/android-utils.lcb',
142143
'modules/scriptitems/scriptitems.lcb',
143144

144145
'libraries/androidbgaudio/androidbgaudio.lcb',

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Copyright (C) 2018 LiveCode Ltd.
3+
4+
This file is part of LiveCode.
5+
6+
LiveCode is free software; you can redistribute it and/or modify it under
7+
the terms of the GNU General Public License v3 as published by the Free
8+
Software Foundation.
9+
10+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
11+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
17+
18+
/**
19+
A library of utility handlers for functions commonly needed by Android widgets.
20+
*/
21+
22+
module com.livecode.library.androidutils
23+
24+
use com.livecode.foreign
25+
use com.livecode.java
26+
use com.livecode.canvas
27+
use com.livecode.library.widgetutils
28+
29+
metadata version is "1.0.0"
30+
metadata author is "LiveCode"
31+
metadata title is "Android Utilities"
32+
33+
private handler ColorComponentToInt(in pComponent as Real) returns Integer
34+
multiply pComponent by 255
35+
round pComponent
36+
return pComponent
37+
end handler
38+
39+
__safe foreign handler _JNI_GetColorFromARGB(in pA as JInt, in pR as JInt, in pG as JInt, in pB as JInt) returns JInt \
40+
binds to "java:android.graphics.Color>argb(IIII)I!static"
41+
42+
/**
43+
Summary: Convert a color string to an integer for Android
44+
45+
Parameters:
46+
pString: The color string
47+
48+
Description:
49+
Use the <StringToAndroidColor> handler to convert a string representing
50+
a color to an integer that can be used with Android color APIs.
51+
*/
52+
public handler StringToAndroidColor(in pString as String) returns Integer
53+
variable tColor as Color
54+
put stringToColor(pString) into tColor
55+
56+
variable tA as Integer
57+
variable tR as Integer
58+
variable tG as Integer
59+
variable tB as Integer
60+
put ColorComponentToInt(the alpha of tColor) into tA
61+
put ColorComponentToInt(the red of tColor) into tR
62+
put ColorComponentToInt(the green of tColor) into tG
63+
put ColorComponentToInt(the blue of tColor) into tB
64+
65+
return _JNI_GetColorFromARGB(tA,tR,tG,tB)
66+
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
91+
end module
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Android Utility Module
2+
3+
A new android utility module has been added. It currently
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: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use com.livecode.widget
4242
use com.livecode.canvas
4343
use com.livecode.engine
4444
use com.livecode.library.widgetutils
45+
use com.livecode.library.androidutils
4546

4647
metadata version is "1.0.0"
4748
metadata author is "LiveCode"
@@ -91,16 +92,9 @@ metadata labelColor.default is "0,0,0"
9192
metadata labelColor.editor is "com.livecode.pi.colorwithalpha"
9293
metadata labelColor.section is "Colors"
9394

94-
__safe foreign handler _JNI_GetAndroidEngine() returns JObject \
95-
binds to "java:com.runrev.android.Engine>getEngine()Lcom/runrev/android/Engine;!static"
96-
__safe foreign handler _JNI_GetEngineContext(in pEngine as JObject) returns JObject \
97-
binds to "java:android.view.View>getContext()Landroid/content/Context;"
98-
9995
// Handlers for creating and attaching view
10096
__safe foreign handler _JNI_CreateButton(in pContext as JObject) returns JObject \
10197
binds to "java:android.widget.Button>new(Landroid/content/Context;)?ui"
102-
__safe foreign handler _JNI_AddButtonView(in pParentView as JObject, in pChildView as JObject) returns nothing \
103-
binds to "java:android.view.ViewGroup>addView(Landroid/view/View;)V?ui"
10498

10599
// Handlers for adding click listener
106100
handler type ClickCallback(in pView as JObject) returns nothing
@@ -114,8 +108,7 @@ __safe foreign handler _JNI_SetTextViewText(in pView as JObject, in pValue as JS
114108
binds to "java:android.widget.TextView>setText(Ljava/lang/CharSequence;)V?ui"
115109
__safe foreign handler _JNI_SetTextViewTextColor(in pView as JObject, in pValue as JInt) returns nothing \
116110
binds to "java:android.widget.TextView>setTextColor(I)V?ui"
117-
__safe foreign handler _JNI_GetColorFromARGB(in pA as JInt, in pR as JInt, in pG as JInt, in pB as JInt) returns JInt \
118-
binds to "java:android.graphics.Color>argb(IIII)I!static"
111+
119112
__safe foreign handler _JNI_SetTextViewEnabled(in pView as JObject, in pValue as JBoolean) returns nothing \
120113
binds to "java:android.view.View>setEnabled(Z)V?ui"
121114
__safe foreign handler _JNI_SetTextViewTypeface(in pView as JObject, in pValue as JObject) returns nothing \
@@ -147,24 +140,8 @@ end handler
147140

148141
private handler InitButtonView()
149142
// 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
156-
157-
// put my native window into tParent
158-
variable tParent as Pointer
159-
MCWidgetGetMyStackNativeView(tParent)
160-
161-
// wrap the parent pointer
162-
variable tParentObj as JObject
163-
put PointerToJObject(tParent) into tParentObj
164-
165-
// add the view
166-
_JNI_AddButtonView(tParentObj, mButton)
167-
143+
put _JNI_CreateButton(ApplicationContext()) into mButton
144+
168145
// get the pointer from the view and set the native layer
169146
variable tButtonPointer as Pointer
170147
put PointerFromJObject(mButton) into tButtonPointer
@@ -271,23 +248,8 @@ private handler ColorComponentToInt(in pComponent as Real) returns Integer
271248
return pComponent
272249
end handler
273250

274-
private handler SetStringAndroidColor(in pString as String)
275-
variable tColor as Color
276-
put stringToColor(pString) into tColor
277-
278-
variable tA as Integer
279-
variable tR as Integer
280-
variable tG as Integer
281-
variable tB as Integer
282-
put ColorComponentToInt(the alpha of tColor) into tA
283-
put ColorComponentToInt(the red of tColor) into tR
284-
put ColorComponentToInt(the green of tColor) into tG
285-
put ColorComponentToInt(the blue of tColor) into tB
286-
287-
variable tColorInt as Integer
288-
put _JNI_GetColorFromARGB(tA,tR,tG,tB) into tColorInt
289-
290-
_JNI_SetTextViewTextColor(mButton, tColorInt)
251+
private handler SetStringAndroidColor(in pString as String)
252+
_JNI_SetTextViewTextColor(mButton, StringToAndroidColor(pString))
291253
end handler
292254

293255
end widget

extensions/widgets/androidfield/androidfield.lcb

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,20 @@ use com.livecode.widget
121121
use com.livecode.canvas
122122
use com.livecode.engine
123123
use com.livecode.library.widgetutils
124+
use com.livecode.library.androidutils
124125
use com.livecode.library.scriptitems
125126

126127
metadata version is "1.0.0"
127128
metadata author is "LiveCode"
128129
metadata title is "Android Native Field"
129130
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"
130131

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

138135
// Handlers for creating and attaching view
139136
__safe foreign handler _JNI_CreateView(in pContext as JObject) returns JObject \
140137
binds to "java:android.widget.EditText>new(Landroid/content/Context;)V?ui"
141-
__safe foreign handler _JNI_AddView(in pParentView as JObject, in pChildView as JObject) returns nothing \
142-
binds to "java:android.view.ViewGroup>addView(Landroid/view/View;)V?ui"
143138

144139
private variable mNativeObj as optional JObject
145140
private variable mIMMObj as optional JObject
@@ -622,24 +617,10 @@ constant INPUT_METHOD_SERVICE is "input_method"
622617

623618
private handler InitView()
624619
// Create an android button using the Engine Context
625-
variable tEngine as JObject
626-
put _JNI_GetAndroidEngine() into tEngine
627-
628620
variable tContext as JObject
629-
put _JNI_GetContext(tEngine) into tContext
621+
put ApplicationContext() into tContext
630622
put _JNI_CreateView(tContext) into mNativeObj
631623

632-
// put my native window into tParent
633-
variable tParent as Pointer
634-
MCWidgetGetMyStackNativeView(tParent)
635-
636-
// wrap the parent pointer
637-
variable tParentObj as JObject
638-
put PointerToJObject(tParent) into tParentObj
639-
640-
// add the view
641-
_JNI_AddView(tParentObj, mNativeObj)
642-
643624
// get the pointer from the view and set the native layer
644625
variable tPointer as Pointer
645626
put PointerFromJObject(mNativeObj) into tPointer
@@ -1357,31 +1338,8 @@ public handler GetText() returns String
13571338
return mText
13581339
end handler
13591340

1360-
__safe foreign handler _JNI_GetColorFromARGB(in pA as JInt, in pR as JInt, in pG as JInt, in pB as JInt) returns JInt \
1361-
binds to "java:android.graphics.Color>argb(IIII)I!static?ui"
1362-
private handler SetStringAndroidColor(in pString as String)
1363-
variable tColor as Color
1364-
put stringToColor(pString) into tColor
1365-
1366-
variable tA as Integer
1367-
variable tR as Integer
1368-
variable tG as Integer
1369-
variable tB as Integer
1370-
put ColorComponentToInt(the alpha of tColor) into tA
1371-
put ColorComponentToInt(the red of tColor) into tR
1372-
put ColorComponentToInt(the green of tColor) into tG
1373-
put ColorComponentToInt(the blue of tColor) into tB
1374-
1375-
variable tColorInt as Integer
1376-
put _JNI_GetColorFromARGB(tA,tR,tG,tB) into tColorInt
1377-
1378-
_JNI_TextView_setTextColor(mNativeObj, tColorInt)
1379-
end handler
1380-
1381-
private handler ColorComponentToInt(in pComponent as Real) returns Integer
1382-
multiply pComponent by 255
1383-
round pComponent
1384-
return pComponent
1341+
private handler SetStringAndroidColor(in pString as String)
1342+
_JNI_TextView_setTextColor(mNativeObj, StringToAndroidColor(pString))
13851343
end handler
13861344

13871345
public handler SetTextColor(in pColor as String)
@@ -1414,6 +1372,8 @@ handler SetTypeface(in pFont as Font)
14141372
PointerToJObject(tTypefacePtr))
14151373
end handler
14161374

1375+
__safe foreign handler _JNI_ViewGetContext(in pView as JObject) returns JObject \
1376+
binds to "java:android.view.View>getContext()Landroid/content/Context;"
14171377
__safe foreign handler _JNI_GetResources(in pContext as JObject) returns JObject \
14181378
binds to "java:android.content.Context>getResources()Landroid/content/res/Resources;"
14191379
__safe foreign handler _JNI_GetDisplayMetrics(in pResources as JObject) returns JObject \
@@ -1425,7 +1385,7 @@ public handler GetTextSize() returns Integer
14251385
put TextView_getTextSize(mNativeObj) into tTextSize
14261386

14271387
variable tContext as JObject
1428-
put _JNI_GetContext(mNativeObj) into tContext
1388+
put _JNI_ViewGetContext(mNativeObj) into tContext
14291389

14301390
variable tResources as JObject
14311391
put _JNI_GetResources(tContext) into tResources

extensions/widgets/androidfield/tests/properties.livecodescript

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
1919
on TestSetup
2020
TestLoadExtension "com.livecode.library.widgetutils"
2121
TestLoadExtension "com.livecode.library.scriptitems"
22+
TestLoadExtension "com.livecode.library.androidutils"
2223
TestLoadExtension "com.livecode.widget.native.android.field"
2324
end TestSetup
2425

0 commit comments

Comments
 (0)