<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
<channel>
<title>srplab's blog - Yahoo! Blog</title>
<link>http://blog.yahoo.com/srplab</link>
<description>Discuss about multi language programming of c/c  ,java,c#,python,etc. Distribute object middleware</description>
<language>en-us</language>
<lastBuildDate>Sun, 17 Jun 2012 20:34:00 +0800</lastBuildDate>
<sy:updatePeriod>daily</sy:updatePeriod>
<sy:updateFrequency>3</sy:updateFrequency> <item><category>Multi Language Programming</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Writing android gui using python(7:Activity)]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/645040</guid>
  <link>http://blog.yahoo.com/srplab/articles/645040</link>
  <description><![CDATA[<div><h1>Introduction</h1><br><div>Activity of android application is main entity which likes windows on win32 platform. It has lifecycle, contains gui widgets, and manages them together. Activity can create child activities, with initial parameters, and gets results from them. Here gives an example to operate activities, which code is written with python based on wrapandroid project. The purpose of this article is to tell programmer how to operate activities using python, rather than explain activity’s lifecycle and how to create child activities, which is talked in detail in android sdk documents.&nbsp;<br><div>Examples given in this article is simple. We create two activities, one is root, and another is child. The root activity contains an edit widget, which is used to get input from user, and a button widget. When users press button, child activity is created with input as parameter. Child activity displays the parameter in text widgets, shows an edit widget for user to input result for parent. At last, the text returned from child is shown in parent activity.<br><div><h1>Root Activity</h1><br><div><h2>layout xml file</h2><br><div>We use xml file layout. The xml file contains text view, button and edit view, which is listed below.<br><div>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<div>&lt;LinearLayout<div>android:id=&quot;@+id/widget30&quot;<div>android:layout_width=&quot;fill_parent&quot;<div>android:layout_height=&quot;fill_parent&quot;<div>android:orientation=&quot;vertical&quot;<div>xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<div>&gt;<div>&lt;LinearLayout<div>android:id=&quot;@+id/widget34&quot;<div>android:layout_width=&quot;fill_parent&quot;<div>android:layout_height=&quot;wrap_content&quot;<div>android:gravity=&quot;bottom&quot;<div>android:layout_gravity=&quot;center_vertical&quot;<div>&gt;<div>&lt;TextView<div>android:id=&quot;@+id/widget35&quot;<div>android:layout_width=&quot;wrap_content&quot;<div>android:layout_height=&quot;wrap_content&quot;<div>android:text=&quot;input text :&quot;<div>&gt;<div>&lt;/TextView&gt;<div>&lt;EditText<div>android:id=&quot;@+id/widget38&quot;<div>android:layout_width=&quot;fill_parent&quot;<div>android:layout_height=&quot;wrap_content&quot;<div>android:text=&quot;hello&quot;<div>android:textSize=&quot;18sp&quot;<div>&gt;<div>&lt;/EditText&gt;<div>&lt;/LinearLayout&gt;<div>&lt;Button<div>android:id=&quot;@+id/widget39&quot;<div>android:layout_width=&quot;wrap_content&quot;<div>android:layout_height=&quot;wrap_content&quot;<div>android:text=&quot;send to child&quot;<div>&gt;<div>&lt;/Button&gt;<div>&lt;LinearLayout<div>android:id=&quot;@+id/widget40&quot;<div>android:layout_width=&quot;fill_parent&quot;<div>android:layout_height=&quot;wrap_content&quot;<div>android:gravity=&quot;bottom&quot;<div>android:layout_gravity=&quot;center_vertical&quot;<div>&gt;<div>&lt;TextView<div>android:id=&quot;@+id/widget41&quot;<div>android:layout_width=&quot;wrap_content&quot;<div>android:layout_height=&quot;wrap_content&quot;<div>android:text=&quot;result from child :&quot;<div>&gt;<div>&lt;/TextView&gt;<div>&lt;TextView<div>android:id=&quot;@+id/widget42&quot;<div>android:layout_width=&quot;fill_parent&quot;<div>android:layout_height=&quot;wrap_content&quot;<div>android:text=&quot;&quot;<div>android:textSize=&quot;18sp&quot;<div>&gt;<div>&lt;/TextView&gt;<div>&lt;/LinearLayout&gt;<div>&lt;/LinearLayout&gt;<br><br><div><h2>Code of activity</h2><br><div>The boot code of activity is written in java, which is simple. The function of it is to load python code, which is code.py located in asset directory.<br><div>public class ActivityActivity extends WrapAndroidActivity {<div>&nbsp; &nbsp; /** Called when the activity is first created. */<div>&nbsp; &nbsp; @Override<div>&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {<div>&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);<div>&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.main);<div>&nbsp; &nbsp; &nbsp; &nbsp; StarActivity._Call(&quot;DoAssetsFile&quot;, &quot;python&quot;, &quot;code.py&quot;);<div>&nbsp; &nbsp; }<div>}<br><div><h3>code.py</h3><br><div>“Code.py” is the main code of root activity. It is a python file.&nbsp;<br><div>Step 1:<div>The first step of python code is to get service group object and service object created by java code.&nbsp;<br><div>SrvGroup = libstarpy._GetSrvGroup()<div>Service = SrvGroup._GetService(&quot;&quot;,&quot;&quot;)<div>#--get activity<div>StarActivity = Service.ActivityClass.getCurrent();<br><div>Step 2:<br><div>The step 2 is to obtain widgets defined in the layout file. And set onClick event listener of button. When the event is triggered, we build an intent and create child activity. The child activity is named ChildActivity. It must be declared in AndroidManifest.xml. Otherwise, the call will be failed.<br><div>AndroidManifest.xml:<div>&nbsp; &nbsp; &lt;application<div>&nbsp; &nbsp; &nbsp; &nbsp; android:icon=&quot;@drawable/ic_launcher&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:label=&quot;@string/app_name&quot; &gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &lt;activity<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:label=&quot;@string/app_name&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:name=&quot;.ActivityActivity&quot; &gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;intent-filter &gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;<br><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/intent-filter&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &lt;/activity&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &lt;activity android:name=&quot;.ChildActivity&quot; android:label=&quot;@string/app_name&quot;/&gt;<div>&lt;/application&gt;<br><div>Python Code:<br><div>#--button &nbsp; &nbsp; &nbsp; &nbsp;<div>myEdit = StarActivity.findViewById(&quot;EditTextClass&quot;,StarActivity.getResource(&quot;id/widget38&quot;));<div>myButton = StarActivity.findViewById(&quot;ButtonClass&quot;,StarActivity.getResource(&quot;id/widget39&quot;));<div>def myButton_onClick(self, Ev) :<div>&nbsp; &nbsp; MyIntent = Service.IntentClass._New();&nbsp;<div>&nbsp; &nbsp; MyIntent.setClassName(&quot;ChildActivity&quot;);<div>&nbsp; &nbsp; MyIntent.putStringExtra(&quot;value&quot;,myEdit.getText());<div>&nbsp; &nbsp; StarActivity.startActivityForResult(MyIntent,1);<div>&nbsp; &nbsp; MyIntent._Free();<div>&nbsp; &nbsp; return;<div>myButton.onClick = myButton_onClick;&nbsp;<div>myButton.setOnClickListener();&nbsp;<br><div>Step 3:<br><div>When child activity returns, we can get result from child and show it in text view. To receive result, we should override activity’s function onActivityResult.<br><div>#--receive result<div>myText = StarActivity.findViewById(&quot;EditTextClass&quot;,StarActivity.getResource(&quot;id/widget42&quot;));<div>myText.setTextColor(0xFFFF0000)<div>def StarActivity_onActivityResult(self,requestCode, resultCode, data) :<div>&nbsp; &nbsp; if( requestCode == 1 and data != None ) :<div>&nbsp; &nbsp; &nbsp; &nbsp; myText.setText(data.getStringExtra(&quot;value&quot;))<div>StarActivity.onActivityResult = StarActivity_onActivityResult;<br><div><h1>Child Activity</h1><br><div><h2>layout xml file</h2><br><div>The xml file also contains text view, button and edit view, which is listed below.<br><div>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<div>&lt;LinearLayout<div>android:id=&quot;@+id/widget30&quot;<div>android:layout_width=&quot;fill_parent&quot;<div>android:layout_height=&quot;fill_parent&quot;<div>android:orientation=&quot;vertical&quot;<div>xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<div>&gt;<div>&lt;LinearLayout<div>android:id=&quot;@+id/widget31&quot;<div>android:layout_width=&quot;wrap_content&quot;<div>android:layout_height=&quot;wrap_content&quot;<div>&gt;<div>&lt;TextView<div>android:id=&quot;@+id/widget32&quot;<div>android:layout_width=&quot;wrap_content&quot;<div>android:layout_height=&quot;wrap_content&quot;<div>android:text=&quot;from parent :&quot;<div>&gt;<div>&lt;/TextView&gt;<div>&lt;TextView<div>android:id=&quot;@+id/widget33&quot;<div>android:layout_width=&quot;wrap_content&quot;<div>android:layout_height=&quot;wrap_content&quot;<div>android:text=&quot;&quot;<div>&gt;<div>&lt;/TextView&gt;<div>&lt;/LinearLayout&gt;<div>&lt;LinearLayout<div>android:id=&quot;@+id/widget34&quot;<div>android:layout_width=&quot;fill_parent&quot;<div>android:layout_height=&quot;wrap_content&quot;<div>&gt;<div>&lt;TextView<div>android:id=&quot;@+id/widget35&quot;<div>android:layout_width=&quot;wrap_content&quot;<div>android:layout_height=&quot;wrap_content&quot;<div>android:text=&quot;input text : &quot;<div>&gt;<div>&lt;/TextView&gt;<div>&lt;EditText<div>android:id=&quot;@+id/widget36&quot;<div>android:layout_width=&quot;fill_parent&quot;<div>android:layout_height=&quot;wrap_content&quot;<div>android:text=&quot;EditText&quot;<div>android:textSize=&quot;18sp&quot;<div>&gt;<div>&lt;/EditText&gt;<div>&lt;/LinearLayout&gt;<div>&lt;Button<div>android:id=&quot;@+id/widget37&quot;<div>android:layout_width=&quot;wrap_content&quot;<div>android:layout_height=&quot;wrap_content&quot;<div>android:text=&quot;return to parent&quot;<div>&gt;<div>&lt;/Button&gt;<div>&lt;/LinearLayout&gt;<br><div><h2>Code of child activity</h2><br><div>The boot code of child activity is same as parent activity.<br><div><h3>code.py</h3><br><div>Step 1:<div>The first step of python code is to get service group object and service object created by java code, which is the same as parent activity. Python uses global namespace, which should be pay special attention. If we use same variable name as parent activity, it will replace the variable in parent, which may cause error.<br><div>SrvGroup = libstarpy._GetSrvGroup()<div>Service = SrvGroup._GetService(&quot;&quot;,&quot;&quot;)<br><div>#--get activity, global python name space<div>ChildStarActivity = Service.ActivityClass.getCurrent();<br><div>Step 2:<br><div>First, we get start intent, which is set by parent. Then, obtains text view defined in the layout file, and show string of the intent in the text view. Gets button widget, set it’s onClick event listener. When the event is triggered, we build result intent for parent and call finish function to end the child activity.<br><div>#--button &nbsp; &nbsp;&nbsp;<div>child_intent = ChildStarActivity.getIntent(); &nbsp;&nbsp;<div>ChildText = ChildStarActivity.findViewById(&quot;TextViewClass&quot;,ChildStarActivity.getResource(&quot;id/widget33&quot;));<div>ChildText.setText(child_intent.getStringExtra(&quot;value&quot;))<div>ChildText.setTextColor(0xFFFF0000)<br><div>childEdit = ChildStarActivity.findViewById(&quot;EditTextClass&quot;,ChildStarActivity.getResource(&quot;id/widget36&quot;));<div>childButton = ChildStarActivity.findViewById(&quot;ButtonClass&quot;,ChildStarActivity.getResource(&quot;id/widget37&quot;));<br><div>def childButton_onClick(self, Ev) :<div>&nbsp; &nbsp; MyIntent = Service.IntentClass._New();&nbsp;<div>&nbsp; &nbsp; MyIntent.putStringExtra(&quot;value&quot;,childEdit.getText());<div>&nbsp; &nbsp; ChildStarActivity.setResult1(0,MyIntent);<div>&nbsp; &nbsp; MyIntent._Free();<div>&nbsp; &nbsp; ChildStarActivity.finish();<div>&nbsp; &nbsp; return;<div>childButton.onClick = childButton_onClick;&nbsp;<div>childButton.setOnClickListener();<br><div>We also should capture “BACK” key event. When the key is pressed, we also end the activity.<div>def ChildStarActivity_onKeyDown(self,keyCode,event) :<div>&nbsp; &nbsp; if( keyCode == Service.AndroidConstantClass.getInt(&quot;KeyEvent&quot;,&quot;KEYCODE_BACK&quot;) ) :<div>&nbsp; &nbsp; &nbsp; &nbsp; MyIntent = Service.IntentClass._New();&nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp; MyIntent.putStringExtra(&quot;value&quot;,&quot;press key back&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp; ChildStarActivity.setResult1(0,MyIntent);<div>&nbsp; &nbsp; &nbsp; &nbsp; MyIntent._Free();<div>&nbsp; &nbsp; &nbsp; &nbsp; ChildStarActivity.finish(); &nbsp;&nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp; return True,True;&nbsp;<div>ChildStarActivity.onKeyDown = ChildStarActivity_onKeyDown<br><br>ScreenShot<br><br>Parent Activity:<br><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/12/l/ysNVI971VAcNbKEuOsz6gA.jpg" title="activity_parent_screenshot" id="0"><br>Child Activity<br><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/13/l/HXqcEkyMGzykuOqrxU_QNQ.jpg" title="activity_child_screenshot" id="0"><br>Examples :<br><br>examples can be downloaded from :&nbsp;<br>http://wrapandroid-for-multilanguage.googlecode.com/svn/wiki/examples/pythongui_activity.zip<br><br><br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Sun, 17 Jun 2012 20:34:00 +0800</pubDate>
 </item> <item><category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Writing android gui using c++(3:debug)]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/643945</guid>
  <link>http://blog.yahoo.com/srplab/articles/643945</link>
  <description><![CDATA[<div><h1>Introduction</h1><div>This article talks about how to debug c++ codes using ndk-debug tool, and give some advice about c++ programming on android. If application is written in java, programmers can use eclipse to debug the code, set break points, and trace step by step. When written in c++, how to debug? By now, for android version above 2.2, ndk provides a ndk-debug tool, which runs on linux, can be used to debug the source code.<br><div>Debug environment may vary with platforms. The steps given here is based on environment of my host, which is windows xp, android-ndk-r6b, and cgywin is used to build c++ code for android. How to install android-ndk-r6b and cgywin are out the scope of this article. You can refer to other materials.&nbsp;<br><div>The example code to be debugged is list view holder, which is given in previous article. Suppose you have already prepared the environment, and the c++ code can be built for android successfully.<br><div><h1>changes of the project</h1><br><div>1.&nbsp;&nbsp;&nbsp;&nbsp;change AndroidManifest.xml to enable gdbserver<div>Based on document of ndk, in order to debug the native code, AndroidManifest.xml should be add a flag “android:debuggable=&quot;true&quot; “, as follows.<br><div>&nbsp; &nbsp; &lt;application<div>&nbsp; &nbsp; &nbsp; &nbsp; android:icon=&quot;@drawable/ic_launcher&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:label=&quot;@string/app_name&quot;&nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:debuggable=&quot;true&quot; &gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &lt;activity<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:label=&quot;@string/app_name&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:name=&quot;.DebugActivity&quot; &gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;intent-filter &gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;<br><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/intent-filter&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &lt;/activity&gt;<div>&lt;/application&gt;<br><div>2.&nbsp;&nbsp;&nbsp;&nbsp;recompile the project with debug flag “NDK_DEBUG=1”<div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/85/l/Sap1YcjbLYTNw3dFM5QI5w.jpg" title="debug_step2" id="0"><br><div>3.&nbsp;&nbsp;&nbsp;&nbsp;run the project from eclipse<br><div><h1>debug with ndk-debug</h1><br><div><h2>switch to cgywin console</h2><br><div>1.&nbsp;&nbsp;&nbsp;&nbsp;run ndk-debug as follows<div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/86/l/IGJL2n17pcj1a4E7xc1A4A.jpg" title="debug_step3" id="0"><br><div>There may be many warnings. But need not care, they not affect the debug proecess.<div>2.&nbsp;&nbsp;&nbsp;&nbsp;type list command, you can see the source file<div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/87/l/dvb1KUhMCWcvSDlRprsVfQ.jpg" title="debug_step4" id="0"><br><div>use b command to set the break points.<div>use c command to continue run.<div>3.&nbsp;&nbsp;&nbsp;&nbsp;The mobile may be response slowly, please wait, until mobile can operate normaly.<div>If the mobile has no response for a long time, you can type CTRL+C to exit ndk-debug and repeat the above steps.<div>4.&nbsp;&nbsp;&nbsp;&nbsp;click button, then the source will be broken at the break point set before.<div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/88/l/RaUx4wjK4EsjqDFHvJfBzQ.jpg" title="debug_step5" id="0"><br><div>In cygwin console, when watch variable value, you may see “value optimized out”.<div>In this case, you should add –Oo flags to Android.mk file, rebuild the project and restart.<br><br><div>LOCAL_CFLAGS += -Wno-write-strings -O0 -DENV_ANDROID<div>LOCAL_CPPFLAGS += -Wno-write-strings -O0 -fexceptions -DENV_ANDROID<div>LOCAL_LDFLAGS += -Wno-write-strings -O0 -DENV_ANDROID<br><br><div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/89/l/1.vJjcyxCmBj4iKoqerUMQ.jpg" title="debug_step6" id="0"><br><div>Debug on simulator may be less efficient for slow response. The better choice is to buy a cheaper mobile or palm, and debug using real device. Debug c++ code on android is not an easy work, no matter you are familiar with gdb or not. I think, migrate mature code from other platform to android is a good choice, or debug on other platform using more convenient tools other than on android directly.&nbsp;<div><br><h1>debug on win32</h1><div>CLE also supports windows and linux, therefore we can debug program logic on windows. Lets &nbsp;discuss in deeper. To debug on windows, there are lots of work to do, not technology, but is the work to setup stubs for android classes. The work is not started by now, so this article only gives the steps to illustrate we can debug android c++ code on windows. You can also write stubs by self following the examples in this article. The examples is also includes in the download package. For the built result of c++ code is share library, on windows platform, it can only be built into a dynamic library. So the first step is to create a loader to init environment and load it.&nbsp;<br><div>Before start, cle for win32 should be installed from http://www.srplab.com/data/starcore_win32.1.5.1.exe<br><div><h2>dynamic library loader</h2><br><div>The loader init cle environment, import wrapandroid service description file “SRPWrapAndroidEngine.xml’, and create a service for the share library. The loader can be created with java, python, lua, c#, c++, or other languages supported by CLE. Here we create it using c++. The code is shown below.&nbsp;<br><div>1.&nbsp;&nbsp;&nbsp;&nbsp;init CLE environment<br><div>&nbsp;&nbsp;&nbsp;&nbsp;class ClassOfBasicSRPInterface *BasicSRPInterface;<div>&nbsp;&nbsp;&nbsp;&nbsp;VS_CORESIMPLECONTEXT Context;<div>&nbsp;&nbsp;&nbsp;&nbsp;<div>// init CLE environment. The first parameter Context is used to get the return value.<div>&nbsp;&nbsp;&nbsp;&nbsp;BasicSRPInterface = VSCore_InitSimpleEx(&Context,0,0,NULL,0,NULL);<div>&nbsp;&nbsp;&nbsp;&nbsp;if( BasicSRPInterface == NULL ){<div><span class="Apple-tab-span" style="white-space:pre;">		</span>printf(&quot;init starcore fail&#92;n&quot;);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>return -1;<div>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<div>&nbsp; &nbsp; // import wrapandroid service description file. This file contains id and name of android classe.<div>&nbsp;&nbsp;&nbsp;&nbsp;{<div><span class="Apple-tab-span" style="white-space:pre;">		</span>FILE *hFile;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>int FileSize;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>char *xmlBufer;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>hFile = fopen(&quot;SRPWrapAndroidEngine.xml&quot;,&quot;rt&quot;);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>fseek(hFile,0,SEEK_END);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>FileSize = ftell(hFile);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>fseek(hFile,0,SEEK_SET);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>xmlBufer = (char *)malloc(FileSize+1);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>FileSize = fread(xmlBufer,1,FileSize,hFile);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>fclose(hFile);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>xmlBufer[FileSize] = 0;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>if( BasicSRPInterface -&gt;ImportServiceFromXmlBuf(xmlBufer,false) == VS_FALSE ){<div><span class="Apple-tab-span" style="white-space:pre;">			</span>printf(&quot;parse [SRPWrapAndroidEngine.xml] failed&#92;n&quot;);<div><span class="Apple-tab-span" style="white-space:pre;">			</span>return -1;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>}<div><span class="Apple-tab-span" style="white-space:pre;">		</span>free(xmlBufer);<div>&nbsp;&nbsp;&nbsp;&nbsp;}<div>&nbsp; &nbsp; // create a service for share library.<div>&nbsp;&nbsp;&nbsp;&nbsp;BasicSRPInterface -&gt; CreateService( &quot;&quot;,&quot;wrapandroid&quot;, NULL, &quot;123&quot;,5,0,0,0,0,0 );<div>&nbsp;&nbsp;&nbsp;&nbsp;// obtain the service interface.<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface = BasicSRPInterface -&gt;GetSRPInterface(&quot;wrapandroid&quot;,&quot;root&quot;,&quot;123&quot;);<div>// set not check password flag for share library.<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt;CheckPassword(VS_FALSE);<br><div>2.&nbsp;&nbsp;&nbsp;&nbsp;Create root activity<br><div>Share library needs a pre-created activity. The following is used to create it. And write stub code for getCurrent function of the activity. Then this function can be called from share library.<br><div>The stub function of getCurrent.<div>void *RootActivity;<br><div>static void *ActivityClass_getCurrent(void *Object){<div>&nbsp; &nbsp; return RootActivity;<div>}<br><div>The code of creating root activity.<div>&nbsp;&nbsp;&nbsp;&nbsp;//---get ActivityClass defined in wrapandroid service description file.<div>&nbsp;&nbsp;&nbsp;&nbsp;void *ActivityClass = SRPInterface -&gt; GetObjectEx(NULL,&quot;ActivityClass&quot;);<div>&nbsp; &nbsp; //---change to atomic object<div>void *AtomicActivityClass = SRPInterface -&gt;ObjectToAtomic(ActivityClass);<div>//---create function stub for getCurrent<div>&nbsp;&nbsp;&nbsp;&nbsp;void *AtomicActivityClassFunction_getCurrent = SRPInterface -&gt;CreateAtomicFunctionSimple(AtomicActivityClass,&quot;getCurrent&quot;,&quot;VS_OBJPTR getCurrent();&quot;,NULL,NULL,VS_FALSE,VS_FALSE);<div>//---set the function address<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt; SetAtomicFunction(AtomicActivityClassFunction_getCurrent,(void *)ActivityClass_getCurrent);<div>&nbsp; &nbsp;&nbsp;<div>&nbsp;&nbsp;&nbsp;&nbsp;//alloc root activity<div>&nbsp;&nbsp;&nbsp;&nbsp;RootActivity = SRPInterface -&gt;MallocObjectL(&VSOBJID_ActivityClass,0,NULL);<br><div>3.&nbsp;&nbsp;&nbsp;&nbsp;Load share library<div>Load share library is simple. We call cle interface DoFile function to do this, more like script interface.<br><div>&nbsp;&nbsp;&nbsp;&nbsp;if( SRPInterface -&gt;DoFile(&quot;&quot;,&quot;../libcode.dll&quot;,NULL, NULL, VS_FALSE) == VS_FALSE ){<div><span class="Apple-tab-span" style="white-space:pre;">		</span>printf(&quot;load library file&#92;n&quot;);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>return -1;<div>&nbsp;&nbsp;&nbsp;&nbsp;}<br><div>When compile, starlib_vcm.lib should be add to the project, as shown below:<br><div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/90/l/bCZSHJxhfgzcd.zVnfeN6w.jpg" title="debugc_pic1" id="0"><br><br><div>add header files to the project:<div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/91/l/lpbJj5rbbOVSg1JmFBr8wg.jpg" title="debugc_pic2" id="0"><br><div>Set break points on code.cpp, and run.<div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/92/l/ePWhXOm70dlgektx_szQPQ.jpg" title="debugc_pic3" id="0"><br><div><h2>create stubs for android classes<br></h2><div>We create stub for android classes, in order to debug or others goal. It will enable to debug android c++ codes on windows platform. How to create stub? is simple, like this,<br><div>define function<div>static void *ActivityClass_getCurrent(void *Object){<div>&nbsp; &nbsp; …<div>}<div>create stub function<div>void *AtomicActivityClassFunction_getCurrent = SRPInterface -&gt;CreateAtomicFunctionSimple(AtomicActivityClass,&quot;getCurrent&quot;,&quot;VS_OBJPTR getCurrent();&quot;,NULL,NULL,VS_FALSE,VS_FALSE);<br><div>and assign address to the stub function<div>SRPInterface -&gt; SetAtomicFunction(AtomicActivityClassFunction_getCurrent,(void *)ActivityClass_getCurrent);<br><div>If you create stubs of all function of android classes, your application can be migrated to windows.<br><br><h1>Examples</h1>examples can be download from&nbsp;<br><br>http://wrapandroid-for-multilanguage.googlecode.com/svn/wiki/examples/cgui_debug.zip<br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Wed, 13 Jun 2012 19:26:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Writing android gui using c++(2:list view holder)]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/643501</guid>
  <link>http://blog.yahoo.com/srplab/articles/643501</link>
  <description><![CDATA[<div><h1>Introduction</h1><div>Wrapandroid and CLE project gives a choice to write android gui applications with multiple languages, such as python, lua, or c++. This article continues to talk about writing programming using c++. For gui applications, list view control is the most commonly used control, which is to present information to users to perform some action. For list view, to speed up scroll speed, a view holder is often used. We can find many materials talking about it in java. But how to write holder using c++?&nbsp;<div>&nbsp;&nbsp;&nbsp;&nbsp;Android gui object is wrapped with a cle object. For each cle object, cle presents functions “MallocPrivateBuf” and “GetPrivateBuf” to malloc it private buf, which can be used to contain objects pointers. The buffer will be freed with object automatically. Like this:<div>&nbsp;&nbsp;&nbsp;&nbsp;struct Holder{<div><span class="Apple-tab-span" style="white-space:pre;">		</span>void *Object1;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>void *Object2;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>…<div>&nbsp;&nbsp;&nbsp;&nbsp;}*holder;<div>holder = (struct Holder *)SRPInterface -&gt; MallocPrivateBuf(…);<div>and then get the holder,<div>holder = (struct Holder *)SRPInterface -&gt; GetPrivateBuf(…);<br><div><h1>Begin programming</h1><div>Using eclipse and NDK to development application. How to install them, please refer to other related articles. Android version should be above 2.2. Wrapandroid has updated to version 0.9.0, please download from http://code.google.com/p/wrapandroid-for-multilanguage. CLE has update to version r7, website is http://www.code.google.com/p/cle-for-android.<br><br><div><h2>Steps :</h2><div>a. Open eclipse, create a new android project named “listviewholder”<div>b. cle may be installed from network when application started, in this case, the following permission should be added:<div>&nbsp; &nbsp; &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;<div>&nbsp; &nbsp; &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;&gt;&lt;/uses-permission&gt;<div>c. Copy java library starcore_android_r7.jar and wrapandroid.jar to project directory, and add them into project:<div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/82/l/w659gpVVQNHIr7g45_azNQ.jpg" title="listviewholder_libs" id="0"><br><div>d. edit ListviewholderActivity.java，changed as follow，loading native share library.<br><div>import com.srplab.wrapandroid.*;<br><div>public class ListviewholderActivity extends WrapAndroidActivity {<div>&nbsp; &nbsp; /** Called when the activity is first created. */<div>&nbsp; &nbsp; @Override<div>&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {<div>&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);<div>&nbsp; &nbsp; &nbsp; &nbsp; //setContentView(R.layout.main);<div>&nbsp; &nbsp; &nbsp; &nbsp; StarActivity._Call(&quot;DoFile&quot;,&quot;&quot;,&quot;/data/data/&quot;+getPackageName()+&quot;/lib/libCode.so&quot;);<div>&nbsp; &nbsp; }<div>}<br><div>d. cle may also be included in the project, in this case, you should copy share libraries of cle to the directory of the project:&nbsp;<div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/83/l/0l6anbt4jm6OQTcu9nlwNQ.jpg" title="listviewholder_workspace" id="0"><br><div>And change download flag to false<br><div>public class ListviewholderActivity extends WrapAndroidActivity {<div>&nbsp; &nbsp; /** Called when the activity is first created. */<div>&nbsp; &nbsp; @Override<div>&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {<div>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;DownloadFromNetFlag = false;<div>&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);<div>&nbsp; &nbsp; &nbsp; &nbsp; //setContentView(R.layout.main);<div>&nbsp; &nbsp; &nbsp; &nbsp; StarActivity._Call(&quot;DoFile&quot;,&quot;&quot;,&quot;/data/data/&quot;+getPackageName()+&quot;/lib/libCode.so&quot;);<div>&nbsp; &nbsp; }<div>}<br><div>e. edit listview layout：vlist2.xml.<br><div>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<div>&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<div>&nbsp;&nbsp;&nbsp;&nbsp;android:orientation=&quot;horizontal&quot;&nbsp;<div>&nbsp;&nbsp;&nbsp;&nbsp;android:layout_width=&quot;fill_parent&quot;<div>&nbsp;&nbsp;&nbsp;&nbsp;android:layout_height=&quot;fill_parent&quot;&gt;<div>&nbsp;&nbsp;&nbsp;&nbsp;&lt;ImageView android:id=&quot;@+id/img&quot;&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:layout_width=&quot;wrap_content&quot;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:layout_height=&quot;wrap_content&quot;&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:layout_margin=&quot;5px&quot;/&gt;<div>&nbsp;&nbsp;&nbsp;&nbsp;&lt;LinearLayout android:orientation=&quot;vertical&quot;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:layout_width=&quot;wrap_content&quot;&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:layout_height=&quot;wrap_content&quot;&gt;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>&lt;TextView android:id=&quot;@+id/title&quot;&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">			</span>android:layout_width=&quot;wrap_content&quot;<div><span class="Apple-tab-span" style="white-space:pre;">			</span>android:layout_height=&quot;wrap_content&quot;&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">			</span>android:textColor=&quot;#FFFFFFFF&quot;<div><span class="Apple-tab-span" style="white-space:pre;">			</span>android:textSize=&quot;22px&quot; /&gt;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>&lt;TextView android:id=&quot;@+id/info&quot;&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">			</span>android:layout_width=&quot;wrap_content&quot;<div><span class="Apple-tab-span" style="white-space:pre;">			</span>android:layout_height=&quot;wrap_content&quot;&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">			</span>android:textColor=&quot;#FFFFFFFF&quot;<div><span class="Apple-tab-span" style="white-space:pre;">			</span>android:textSize=&quot;13px&quot; /&gt;<div>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/LinearLayout&gt;<div>&nbsp;&nbsp;&nbsp;&nbsp;&lt;Button android:id=&quot;@+id/view_btn&quot;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:layout_width=&quot;wrap_content&quot;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:layout_height=&quot;wrap_content&quot;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:text=&quot;more&quot;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:layout_gravity=&quot;bottom|right&quot; /&gt;<div>&lt;/LinearLayout&gt;<br><div>f. Create jni directory under project, copy header files of wrap android and cle into jni directory. create new file named code.cpp. and Android.mk, edit Android.mk as follow<br><div>LOCAL_PATH := $(call my-dir)<div>include $(CLEAR_VARS)<br><div># Here we give our module name and sourcefile(s)<div>LOCAL_CFLAGS += -Wno-write-strings -DENV_ANDROID<div>LOCAL_CPPFLAGS += -Wno-write-strings -fexceptions -DENV_ANDROID<div>LOCAL_LDFLAGS += -Wno-write-strings -DENV_ANDROID<br><div>LOCAL_C_INCLUDES += cle_files/include<br><div>#--------source file<div>MODULE_CXXSRCS := Code.cpp SRPWrapAndroidEngine_UUIDDef.cpp<br><div>LOCAL_SRC_FILES := ${MODULE_CXXSRCS}<div>LOCAL_LDLIBS := cle_files/libs/armeabi/libstarlib.a<br><div>LOCAL_MODULE &nbsp;:= Code<br><div>include $(BUILD_SHARED_LIBRARY) &nbsp;<br><br><div>#------------------------<div>include $(CLEAR_VARS)<br><div>LOCAL_SRC_FILES := cle_files/so/armeabi/libstarcore.so<div>LOCAL_MODULE &nbsp;:= starcore<br><div>include $(PREBUILT_SHARED_LIBRARY) &nbsp;<br><div>#------------------------<div>include $(CLEAR_VARS)<br><div>LOCAL_SRC_FILES := cle_files/so/armeabi/libstar_java.so<div>LOCAL_MODULE &nbsp;:= star_java<br><div>include $(PREBUILT_SHARED_LIBRARY)&nbsp;<br><div><h2>code.cpp</h2><br><div>Write native code in android, we should compile source file into share library. The share library expose two functions :&nbsp;<div>VS_BOOL StarCoreService_Init(class ClassOfStarCore *starcore)<div>void StarCoreService_Term(class ClassOfStarCore *starcore)<br><div>the first function will be call when the library is loaded, which can be used to do some initialize. StarCoreService_Term will be called before the share library is unloaded.<br><div>The code of code.cpp is list below. We explain in detail;<br><div>#include &quot;SRPWrapAndroidEngine_VSDHeader.h&quot;<br><div>static class ClassOfSRPInterface *SRPInterface;<div>static void *StarActivity;<div>static void *mInflater;<div>// event listener function of button click,&nbsp;<div>static VS_INT32 MyButton_onClick(VS_ULONG FunctionChoice,void *EventPara)<div>{<div>&nbsp;&nbsp;&nbsp;&nbsp;//create a toast and show some information<div>&nbsp;&nbsp;&nbsp;&nbsp;void *toast = SRPInterface-&gt;MallocObjectL(&VSOBJID_ToastClass,0,NULL);<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt; ScriptCall(toast,NULL,&quot;makeText&quot;,&quot;(si)&quot;,&quot;Button is click&quot;, 0);<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt; ScriptCall(toast,NULL,&quot;show&quot;,&quot;()&quot;);<div>&nbsp; &nbsp; return 0;<div>}<br><div>static VS_INT32 SRPAPI MyAdapter_getCount(void *Object)<div>{<div>&nbsp;&nbsp;&nbsp;&nbsp;return 20; &nbsp;// the number list view item;<div>}<br><div>static VS_INT32 SRPAPI MyAdapter_getItem(void *Object,VS_INT32 Position)<div>{<div>&nbsp;&nbsp;&nbsp;&nbsp;return Position;<div>}&nbsp;&nbsp;&nbsp;&nbsp;<div>static VS_LONG SRPAPI MyAdapter_getItemId(void *Object,VS_INT32 Position)<div>{<div>&nbsp;&nbsp;&nbsp;&nbsp;return Position;<div>}&nbsp;&nbsp;&nbsp;&nbsp;<div>&nbsp;&nbsp;&nbsp;&nbsp;<div>static VS_OBJPTR SRPAPI MyAdapter_getView(void *Object,VS_INT32 position,VS_OBJPTR convertView,VS_OBJPTR parent)<div>{<div>&nbsp;&nbsp;&nbsp;&nbsp;void *i;<div>&nbsp;&nbsp;&nbsp;&nbsp;// define holder, which contains image, title, info, and button objects. The objects are c pointers.<div>&nbsp;&nbsp;&nbsp;&nbsp;struct MyHolder{<div><span class="Apple-tab-span" style="white-space:pre;">		</span>void *img;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>void *title;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>void *info;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>void *view_btn;<div>&nbsp;&nbsp;&nbsp;&nbsp;}*holder;<div>&nbsp;&nbsp;&nbsp;&nbsp;<div>&nbsp;&nbsp;&nbsp;&nbsp;if( convertView == NULL ){<span class="Apple-tab-span" style="white-space:pre;">		</span><div><span class="Apple-tab-span" style="white-space:pre;">		</span>// get resource id contains in the project.<div><span class="Apple-tab-span" style="white-space:pre;">		</span>int vlist2 = SRPInterface -&gt; ScriptCall(StarActivity,NULL,&quot;getResource&quot;,&quot;(s)i&quot;,&quot;layout/vlist2&quot;);<div>// inflate widgets from resource, using the inflate function of mInflater<div>&nbsp; &nbsp; &nbsp; &nbsp; convertView = (void *)SRPInterface -&gt; ScriptCall(mInflater,NULL,&quot;inflate&quot;,&quot;(sio)o&quot;,&quot;LinearLayoutClass&quot;,vlist2, NULL);<div>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;// Alloc object’s private buf which is used as holder.<div>&nbsp; &nbsp; &nbsp; &nbsp; holder = (struct MyHolder *)SRPInterface -&gt; MallocPrivateBuf(convertView,SRPInterface -&gt; GetLayer(convertView),0,sizeof(struct MyHolder));<div>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; // get image resource id contains in the layout.<div><span class="Apple-tab-span" style="white-space:pre;">		</span>int img = SRPInterface -&gt; ScriptCall(StarActivity,NULL,&quot;getResource&quot;,&quot;(s)i&quot;,&quot;id/img&quot;);<div>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; // get image widgets, and store the pointer to holder.<div>&nbsp; &nbsp; &nbsp; &nbsp; holder -&gt; img = (void *)SRPInterface -&gt; ScriptCall(convertView,NULL,&quot;findViewById&quot;,&quot;(si)o&quot;,&quot;ImageViewClass&quot;, img);<div>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; // get text resource id contains in the layout.<div><span class="Apple-tab-span" style="white-space:pre;">		</span>int title = SRPInterface -&gt; ScriptCall(StarActivity,NULL,&quot;getResource&quot;,&quot;(s)i&quot;,&quot;id/title&quot;);<div>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; // get textview widgets, and store the pointer to holder.<div>&nbsp; &nbsp; &nbsp; &nbsp; holder -&gt; title = (void *)SRPInterface -&gt; ScriptCall(convertView,NULL,&quot;findViewById&quot;,&quot;(si)o&quot;,&quot;TextViewClass&quot;, title);<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; // get text resource id contains in the layout.<div><span class="Apple-tab-span" style="white-space:pre;">		</span>int info = SRPInterface -&gt; ScriptCall(StarActivity,NULL,&quot;getResource&quot;,&quot;(s)i&quot;,&quot;id/info&quot;);<div>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; // get textview widgets, and store the pointer to holder.<div>&nbsp; &nbsp; &nbsp; &nbsp; holder -&gt; info = (void *)SRPInterface -&gt; ScriptCall(convertView,NULL,&quot;findViewById&quot;,&quot;(si)o&quot;,&quot;TextViewClass&quot;, info);<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; // get button resource id contains in the layout.<div><span class="Apple-tab-span" style="white-space:pre;">		</span>int view_btn = SRPInterface -&gt; ScriptCall(StarActivity,NULL,&quot;getResource&quot;,&quot;(s)i&quot;,&quot;id/view_btn&quot;);<div>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; // get button widgets, and store the pointer to holder.<div>&nbsp; &nbsp; &nbsp; &nbsp; holder -&gt; view_btn = (void *)SRPInterface -&gt; ScriptCall(convertView,NULL,&quot;findViewById&quot;,&quot;(si)o&quot;,&quot;ButtonClass&quot;, view_btn);<div>&nbsp;&nbsp;&nbsp;&nbsp;}else{<div><span class="Apple-tab-span" style="white-space:pre;">		</span>// if convertVew is not null, then we can fetch the holder stored in the object private buf.<div><span class="Apple-tab-span" style="white-space:pre;">		</span>holder = (struct MyHolder *)SRPInterface -&gt; GetPrivateBuf(convertView,SRPInterface -&gt; GetLayer(convertView),0,NULL);<div>&nbsp;&nbsp;&nbsp;&nbsp;}<div>&nbsp;&nbsp;&nbsp;&nbsp;int resid;<div>&nbsp;&nbsp;&nbsp;&nbsp;switch( position % 3 ){<div>&nbsp;&nbsp;&nbsp;&nbsp;case 0 :&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>// get resource id “drawable/i1” in the project&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>resid = SRPInterface -&gt; ScriptCall(StarActivity,NULL,&quot;getResource&quot;,&quot;(s)i&quot;,&quot;drawable/i1&quot;);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>// set the image to image widget<div><span class="Apple-tab-span" style="white-space:pre;">		</span>SRPInterface -&gt; ScriptCall(holder -&gt; img,NULL,&quot;setBackgroundResource&quot;,&quot;(i)&quot;,resid);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>// set text<div><span class="Apple-tab-span" style="white-space:pre;">		</span>SRPInterface -&gt; ScriptCall(holder -&gt; title,NULL,&quot;setText&quot;,&quot;(s)&quot;,&quot;G1&quot;);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>// set text<div><span class="Apple-tab-span" style="white-space:pre;">		</span>SRPInterface -&gt; ScriptCall(holder -&gt; info,NULL,&quot;setText&quot;,&quot;(s)&quot;,&quot;google 1&quot;);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>break;<div>&nbsp;&nbsp;&nbsp;&nbsp;case 1 :&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>resid = SRPInterface -&gt; ScriptCall(StarActivity,NULL,&quot;getResource&quot;,&quot;(s)i&quot;,&quot;drawable/i2&quot;);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>SRPInterface -&gt; ScriptCall(holder -&gt; img,NULL,&quot;setBackgroundResource&quot;,&quot;(i)&quot;,resid);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>SRPInterface -&gt; ScriptCall(holder -&gt; title,NULL,&quot;setText&quot;,&quot;(s)&quot;,&quot;G2&quot;);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>SRPInterface -&gt; ScriptCall(holder -&gt; info,NULL,&quot;setText&quot;,&quot;(s)&quot;,&quot;google 2&quot;);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>break;<div>&nbsp;&nbsp;&nbsp;&nbsp;case 2 :&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>resid = SRPInterface -&gt; ScriptCall(StarActivity,NULL,&quot;getResource&quot;,&quot;(s)i&quot;,&quot;drawable/i3&quot;);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>SRPInterface -&gt; ScriptCall(holder -&gt; img,NULL,&quot;setBackgroundResource&quot;,&quot;(i)&quot;,resid);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>SRPInterface -&gt; ScriptCall(holder -&gt; title,NULL,&quot;setText&quot;,&quot;(s)&quot;,&quot;G3&quot;);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>SRPInterface -&gt; ScriptCall(holder -&gt; info,NULL,&quot;setText&quot;,&quot;(s)&quot;,&quot;google 3&quot;);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>break;<div>&nbsp;&nbsp;&nbsp;&nbsp;}<div>&nbsp; &nbsp; &nbsp;// set onClick event listener<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt; RegEventFunction(holder -&gt; view_btn,&VSOUTEVENTID_ViewClass_onClick,holder -&gt; view_btn,(void *)MyButton_onClick,0);<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt; ScriptCall(holder -&gt; view_btn,NULL,&quot;setOnClickListener&quot;,&quot;()&quot;);<div>&nbsp; &nbsp; return convertView; &nbsp; &nbsp;<div>}<br><div>// init function<div>VS_BOOL StarCoreService_Init(class ClassOfStarCore *starcore)<div>{<div>&nbsp;&nbsp;&nbsp;&nbsp;class ClassOfBasicSRPInterface *BasicSRPInterface;<div>&nbsp;&nbsp;&nbsp;&nbsp;<div>&nbsp;&nbsp;&nbsp;&nbsp;//--init star core<div>&nbsp;&nbsp;&nbsp;&nbsp;BasicSRPInterface = starcore -&gt;GetBasicInterface();&nbsp;&nbsp;&nbsp;&nbsp;<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface = BasicSRPInterface -&gt;GetSRPInterface(BasicSRPInterface-&gt;QueryActiveService(NULL),&quot;&quot;,&quot;&quot;);<div><span class="Apple-tab-span" style="white-space:pre;">		</span><div>&nbsp;&nbsp;&nbsp;&nbsp;void *ActivityClass;<div>&nbsp;&nbsp;&nbsp;&nbsp;ActivityClass = SRPInterface -&gt; GetObjectEx(NULL,&quot;ActivityClass&quot;);<div>&nbsp;&nbsp;&nbsp;&nbsp;// get current activity<div>&nbsp;&nbsp;&nbsp;&nbsp;StarActivity = (void *)SRPInterface -&gt; ScriptCall(ActivityClass,NULL,&quot;getCurrent&quot;,&quot;()O&quot;);<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt; Print(&quot;Get Main Activity = %s&quot;, SRPInterface -&gt; GetName(StarActivity));&nbsp;&nbsp;&nbsp;&nbsp;<div>&nbsp;&nbsp;&nbsp;&nbsp;<div>&nbsp;&nbsp;&nbsp;&nbsp;<div>&nbsp;&nbsp;&nbsp;&nbsp;//--create AbsoluteLayout &nbsp; &nbsp; &nbsp; &nbsp;<div>&nbsp; &nbsp; void *MyLayout = SRPInterface-&gt;MallocObject(StarActivity,VSATTRINDEX_ACTIVITYCLASS_VIEWGROUPQUEUE,&VSOBJID_AbsoluteLayoutClass,0,NULL); &nbsp; &nbsp;<div>&nbsp; &nbsp; // create a layout inflater<div>&nbsp; &nbsp; mInflater = SRPInterface-&gt;MallocObjectL(&VSOBJID_LayoutInflaterClass,0,NULL);<div>&nbsp; &nbsp; // create an adapter<div>void *MyAdapter = SRPInterface-&gt;MallocObjectL(&VSOBJID_BaseAdapterClass,0,NULL);<div>// set getCount function of the adapter<div>SRPInterface -&gt; CreateOVLFunction(MyAdapter,&VSFUNCID_BaseAdapterClass_getCount,(void *)MyAdapter_getCount,NULL);<div>// set getItem function of the adapter<div>SRPInterface -&gt; CreateOVLFunction(MyAdapter,&VSFUNCID_BaseAdapterClass_getItem,(void *)MyAdapter_getItem,NULL);<div>// set getItemId function of the adapter<div>SRPInterface -&gt; CreateOVLFunction(MyAdapter,&VSFUNCID_BaseAdapterClass_getItemId,(void *)MyAdapter_getItemId,NULL);<div>// set getView function of the adapter<div>&nbsp; &nbsp; SRPInterface -&gt; CreateOVLFunction(MyAdapter,&VSFUNCID_BaseAdapterClass_getView,(void *)MyAdapter_getView,NULL);<div>// Create a listview<div>void *MyListView = SRPInterface-&gt;MallocObject(MyLayout,VSATTRINDEX_VIEWGROUPCLASS_VIEWQUEUE,&VSOBJID_ListViewClass,0,NULL);<div>// set listview layout parameter<div>SRPInterface -&gt; ScriptCall(MyListView,NULL,&quot;setAbsoluteLayoutParams&quot;,&quot;(iiii)&quot;,FILL_PARENT,FILL_PARENT,0,0);<div>// set listview’s adapter<div>&nbsp; &nbsp; SRPInterface -&gt; ScriptCall(MyListView,NULL,&quot;setAdapter&quot;,&quot;(o)&quot;,MyAdapter);<br><div>&nbsp;&nbsp;&nbsp;&nbsp;return VS_TRUE;<div>}<br><div>void StarCoreService_Term(class ClassOfStarCore *starcore)<div>{<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt; Release();<div>&nbsp;&nbsp;&nbsp;&nbsp;return;<div>}<br><br><h1>Screenshot</h1><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/84/l/bkVWIXnlDrrFLmxKhGYQdA.jpg" title="listviewholder_screenshot" id="0"><br><br><br>Examples:<br><br>download from http://wrapandroid-for-multilanguage.googlecode.com/svn/wiki/examples/cgui_listviewholder.rar<br><br><br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Sun, 10 Jun 2012 19:53:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Writing android gui using c++(introduction)]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/642103</guid>
  <link>http://blog.yahoo.com/srplab/articles/642103</link>
  <description><![CDATA[<div><h1>Introduction</h1><br>For some reason, we might want to write android applications using native code. In this case, NDK will be used. But only NDK does not enough. Because android only exports java interface to programmers, so applications can not call android classes directly. How to solve this problem? Don’t worry about. &nbsp;Using the result of cle and wrapandroid project, programmers can call android class through interfaces of cle more easily.<br><br><div>The article is an introduction to write android gui applications using cle and wrapandroid. CLE is a middleware for programming using multiple languages, which supports java, python,c/c++,lua, etc. and can be extended to support other languages. Objects, for example, instance of class, will be maintained in the kernel. And then, cle provides common interface to multiple languages. We can call object’s function, get or set object’s attributes, capture object’s events.<div><br>Wrapandroid wraps android classes with cle objects, which enables programmers to use android classes in their applications. In normal case, the steps is illustrated below.<br><div>1.&nbsp;&nbsp;&nbsp;&nbsp;create an object of android class using MallocObjectL of cle interface.<div>2.&nbsp;&nbsp;&nbsp;&nbsp;call object’s functions using ScriptCall method.<div>3.&nbsp;&nbsp;&nbsp;&nbsp;Override object’s function using CreateOvlFunction method<div>4.&nbsp;&nbsp;&nbsp;&nbsp;Hook object’s event using RegEventFunction method<br><div><h1>Step 1 : Prepare environment</h1><div>a: CLE may install from network by application automatically, you need only include &nbsp;starcore_android_r6.jar in the project. The file is in starcore_devfiles_r6.zip, which can be download from http://code.google.com/p/cle-for-android<br><br><div>b: Wrapandroid has a lib files: wrapandroid.jar, which can be download from http:/code.google.com/p/wrapandroid-for-multilaguage/download/wrapandroid_devfiles_0_8_5.rar<br><div><h1>Step 2 : Begin programming&nbsp;</h1><br><div>Using eclipse and NDK to development application. How to install them, please refer to other related articles. Android version should be above 2.2.<br><div>a. Open eclipse, create a new android project named “introduction”<br><br><div>b. cle may be installed from network when application started, in this case, the following permission should be added:<br><br><div><div>&nbsp; &nbsp; <span style="text-decoration:underline;"><div>&nbsp; &nbsp; <span style="text-decoration:underline;"><br><br><div><div><br><div>c. Copy java library starcore_android_r6.jar and wrapandroid.jar to project directory, and add them into project:<br><br><div class="img-wrap "><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/7/l/uo53CuUJGNJQHel3rpT59A.jpg" title="introduction_workspace1" id="0"><br><br><div>d. edit IntroductionActivity.java，changed as follow，loading native share library.<br><div>import com.srplab.wrapandroid.*;<br><div>public class IntroductionActivity extends WrapAndroidActivity {<div>&nbsp; &nbsp; /** Called when the activity is first created. */<div>&nbsp; &nbsp; @Override<div>&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {<div>&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);<div>&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.main);<div>&nbsp; &nbsp; &nbsp; &nbsp; StarActivity._Call(&quot;DoFile&quot;,&quot;&quot;,&quot;/data/data/&quot;+getPackageName()+&quot;/lib/libCode.so&quot;);<div>&nbsp; &nbsp; }<div>}<br><div>e cle may also be included in the project, in this case, you should copy share libraries of cle to the directory of the project:&nbsp;<div><br><div class="img-wrap "><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/8/l/wz1FUZJSBMln7y8Mfo.ELQ.jpg" title="introduction_workspace2" id="0"><br><div>And change download flag to false<br><div>public class IntroductionActivity extends WrapAndroidActivity {<div>&nbsp; &nbsp; /** Called when the activity is first created. */<div>&nbsp; &nbsp; @Override<div>&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {<div>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;DownloadFromNetFlag = false;<div>&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);<div>&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.main);<div>&nbsp; &nbsp; &nbsp; &nbsp; StarActivity._Call(&quot;DoFile&quot;,&quot;&quot;,&quot;/data/data/&quot;+getPackageName()+&quot;/lib/libCode.so&quot;);<div>&nbsp; &nbsp; }<div>}<br><div>f. edit layout：main.xml.<br><br><div><!--?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?--><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><!--?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?--><div><br><div><!--?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?--><div> &nbsp; &nbsp; android:id=&quot;@+id/widget73&quot;<div>&nbsp; &nbsp; android:layout_width=&quot;fill_parent&quot;<div>&nbsp; &nbsp; android:layout_height=&quot;fill_parent&quot;<div>&nbsp; &nbsp; android:orientation=&quot;vertical&quot; &gt;<div><br><div>&nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; android:id=&quot;@+id/widget45&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width=&quot;fill_parent&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height=&quot;wrap_content&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:text=&quot;@string/hello&quot; /&gt;&nbsp;<div>&nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; android:id=&quot;@+id/widget74&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width=&quot;220dp&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height=&quot;48dp&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:text=&quot;thank for your use&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:typeface=&quot;serif&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:textStyle=&quot;bold&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:textColor=&quot;#ffff0000&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:layout_x=&quot;284dp&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:layout_y=&quot;220dp&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:textSize=&quot;16dp&quot;<div>&nbsp; &nbsp; /&gt; &nbsp; &nbsp; &nbsp; &nbsp;<div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><br><div>g. Create jni directory under project, copy header files of wrap android and cle into jni directory. create new file named code.cpp. and Android.mk, edit Android.mk as follow<br><br><div>LOCAL_PATH := $(call my-dir)<div>include $(CLEAR_VARS)<br><div># Here we give our module name and sourcefile(s)<div>LOCAL_CFLAGS += -Wno-write-strings -DENV_ANDROID<div>LOCAL_CPPFLAGS += -Wno-write-strings -fexceptions -DENV_ANDROID<div>LOCAL_LDFLAGS += -Wno-write-strings -DENV_ANDROID<br><div>LOCAL_C_INCLUDES += cle_files/include<br><div>#--------source file<div>MODULE_CXXSRCS := Code.cpp SRPWrapAndroidEngine_UUIDDef.cpp<br><div>LOCAL_SRC_FILES := ${MODULE_CXXSRCS}<div>LOCAL_LDLIBS := cle_files/libs/armeabi/libstarlib.a<br><div>LOCAL_MODULE &nbsp;:= Code<br><div>include $(BUILD_SHARED_LIBRARY) &nbsp;<br><br><div>#------------------------<div>include $(CLEAR_VARS)<br><div>LOCAL_SRC_FILES := cle_files/so/armeabi/libstarcore.so<div>LOCAL_MODULE &nbsp;:= starcore<br><div>include $(PREBUILT_SHARED_LIBRARY) &nbsp;<br><div>#------------------------<div>include $(CLEAR_VARS)<br><div>LOCAL_SRC_FILES := cle_files/so/armeabi/libstar_java.so<div>LOCAL_MODULE &nbsp;:= star_java<br><div>include $(PREBUILT_SHARED_LIBRARY)&nbsp;<br><div><h1>Step 3. Code.cpp</h1><br><div>Write native code in android, we should compile source file into share library. The share library expose two functions :&nbsp;<div>VS_BOOL StarCoreService_Init(class ClassOfStarCore *starcore)<div>void StarCoreService_Term(class ClassOfStarCore *starcore)<br><div>the first function will be call when the library is loaded, which can be used to do some initialize. StarCoreService_Term will be called before the share library is unloaded.<br><div>The code of code.cpp is list below. We explain in detail;<br><div>//--header file of wrap android<div>#include &quot;SRPWrapAndroidEngine_VSClass.h&quot;<br><div>//--service interface, other function calls will be done through this interface.<div>static class ClassOfSRPInterface *SRPInterface;<div>//--Activity object of android, which is created by previous java code.<div>static void *StarActivity;<br><div>//--button event function. Each event has the same prototype.<div>static VS_INT32 MyButton_onClick(VS_ULONG FunctionChoice,void *EventPara)<div>{<div>&nbsp;&nbsp;&nbsp;&nbsp;//--when the event is triggered, we show some information using android Toast.<div>&nbsp; &nbsp; &nbsp; &nbsp; //--Create instance of toast.<div>&nbsp;&nbsp;&nbsp;&nbsp;void *toast = SRPInterface-&gt;MallocObjectL(&VSOBJID_ToastClass,0,NULL);<div>&nbsp; &nbsp; &nbsp; &nbsp; //--call toast function “makeText”, (si) is input parameter type and return type. please refer to cle documents.<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt; ScriptCall(toast,NULL,&quot;makeText&quot;,&quot;(si)&quot;,&quot;Button is click&quot;, 0);<div>&nbsp; &nbsp; &nbsp; &nbsp; //--call toast function “show”<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt; ScriptCall(toast,NULL,&quot;show&quot;,&quot;()&quot;);<div>&nbsp; &nbsp; return 0;<div>}<br><div>static VS_INT32 MyButton1_onClick(VS_ULONG FunctionChoice,void *EventPara)<div>{<div>&nbsp;&nbsp;&nbsp;&nbsp;void *toast = SRPInterface-&gt;MallocObjectL(&VSOBJID_ToastClass,0,NULL);<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt; ScriptCall(toast,NULL,&quot;makeText&quot;,&quot;(si)&quot;,&quot;Button is click&quot;, 0);<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt; ScriptCall(toast,NULL,&quot;show&quot;,&quot;()&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp; return 0;<div>}<br><br><div>//--share library init function.<div>VS_BOOL StarCoreService_Init(class ClassOfStarCore *starcore)<div>{<div>&nbsp;&nbsp;&nbsp;&nbsp;class ClassOfBasicSRPInterface *BasicSRPInterface;<div>&nbsp;&nbsp;&nbsp;&nbsp;<div>&nbsp;&nbsp;&nbsp;&nbsp;//--get basic interface of cle<div>&nbsp;&nbsp;&nbsp;&nbsp;BasicSRPInterface = starcore -&gt;GetBasicInterface();&nbsp;&nbsp;&nbsp;&nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp; //---get current service interface, the service is create by java code.<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface = BasicSRPInterface -&gt;GetSRPInterface(BasicSRPInterface-&gt;QueryActiveService(NULL),&quot;&quot;,&quot;&quot;);<div>&nbsp;&nbsp;&nbsp;&nbsp;<div>&nbsp;&nbsp;&nbsp;&nbsp;void *ActivityClass;<div>&nbsp; &nbsp; &nbsp; &nbsp; //---get wrapped acvitity<div>&nbsp;&nbsp;&nbsp;&nbsp;ActivityClass = SRPInterface -&gt; GetObjectEx(NULL,&quot;ActivityClass&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp; //--call getCurrent function to get current activity, which is created by java code. ()O means the return type is cle object;<div>&nbsp;&nbsp;&nbsp;&nbsp;StarActivity = (void *)SRPInterface -&gt; ScriptCall(ActivityClass,NULL,&quot;getCurrent&quot;,&quot;()O&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp; //--show some infotmation.<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt; Print(&quot;Get Main Activity = %s&quot;, SRPInterface -&gt; GetName(StarActivity));&nbsp;&nbsp;&nbsp;&nbsp;<div>&nbsp;&nbsp;&nbsp;&nbsp;//--call activity function getResource to obtained resource id of above layout. Input parameter is a string and output is an integer, so here we use (s)I tag.<div>int widget45 = SRPInterface -&gt; ScriptCall(StarActivity,NULL,&quot;getResource&quot;,&quot;(s)i&quot;,&quot;id/widget45&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp; //--Call findViewById function of activity, which is a textview. For this function, we should provide class name as input parameter, which is little different from android function. Input parameter is string and integer and return type is cle object. So, here uses tag (si)o.<div>void *MyText = (void *)SRPInterface -&gt; ScriptCall(StarActivity,NULL,&quot;findViewById&quot;,&quot;(si)o&quot;,&quot;TextViewClass&quot;,widget45);<div>&nbsp; &nbsp; &nbsp; &nbsp; //--Call setText of textview object.<div>&nbsp; &nbsp; &nbsp; &nbsp; SRPInterface -&gt; ScriptCall(MyText,NULL,&quot;setText&quot;,&quot;(s)&quot;,&quot;TextViewClass&quot;,&quot;from layout&quot;);<br><div>&nbsp; &nbsp; &nbsp; &nbsp; int widget74 = SRPInterface -&gt; ScriptCall(StarActivity,NULL,&quot;getResource&quot;,&quot;(s)i&quot;,&quot;id/widget74&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp; //--Call findViewById function of activity to get button object.<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;void *MyButton = (void *)SRPInterface -&gt; ScriptCall(StarActivity,NULL,&quot;findViewById&quot;,&quot;(si)o&quot;,&quot;ButtonClass&quot;,widget74);<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;//--Call setText function of button object.<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;SRPInterface -&gt; ScriptCall(MyButton,NULL,&quot;setText&quot;,&quot;(s)&quot;,&quot;click me&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;//--Call setOnClickListener function of button object.<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;SRPInterface -&gt; ScriptCall(MyButton,NULL,&quot;setOnClickListener&quot;,&quot;()&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;//--register event function of button object.<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;SRPInterface -&gt; RegEventFunction(MyButton,&VSOUTEVENTID_ViewClass_onClick,MyButton,(void *)MyButton_onClick,0);<div>&nbsp; &nbsp;&nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;int widget73 = SRPInterface -&gt; ScriptCall(StarActivity,NULL,&quot;getResource&quot;,&quot;(s)i&quot;,&quot;id/widget73&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;//--Call findViewById function of activity to get LinearLayout.<div>&nbsp;&nbsp;&nbsp;&nbsp;void *MyLinearLayout = (void *)SRPInterface -&gt; ScriptCall(StarActivity,NULL,&quot;findViewById&quot;,&quot;(si)o&quot;,&quot;LinearLayoutClass&quot;,widget73);<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;//--Dynamically create a button, which as a child of linear out.<div>&nbsp;&nbsp;&nbsp;&nbsp;void *MyDynaButton = SRPInterface-&gt;MallocObject(MyLinearLayout,VSATTRINDEX_VIEWGROUPCLASS_VIEWQUEUE,&VSOBJID_ButtonClass,0,NULL);<div>&nbsp; &nbsp; &nbsp; &nbsp; SRPInterface -&gt; ScriptCall(MyDynaButton,NULL,&quot;setText&quot;,&quot;(s)&quot;,&quot;created dynamically&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp; SRPInterface -&gt; ScriptCall(MyDynaButton,NULL,&quot;setOnClickListener&quot;,&quot;()&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;SRPInterface -&gt; RegEventFunction(MyDynaButton,&VSOUTEVENTID_ViewClass_onClick,MyDynaButton,(void *)MyButton1_onClick,0);<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;//--set layout parameters of button created.<div>&nbsp; &nbsp; &nbsp; &nbsp; SRPInterface -&gt; ScriptCall(MyDynaButton,NULL,&quot;setLinearLayoutParams&quot;,&quot;(ii)&quot;,100,50);<br><div>&nbsp;&nbsp;&nbsp;&nbsp;return VS_TRUE;<div>}<br><div>void StarCoreService_Term(class ClassOfStarCore *starcore)<div>{<div>&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt; Release();<div>&nbsp;&nbsp;&nbsp;&nbsp;return;<div>}<br><br><h1>Step 4. Compile to share library<br><div style="font-size:12px;font-weight:normal;line-height:14px;"><br><br>Result:<br><br><div class="img-wrap "><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/9/l/oupsU1TzBH7c2rPcFJx0ow.jpg" title="wrapandroid_screenshot" id="0"><br><br>Example can be download from :&nbsp;<br><br> http://wrapandroid-for-multilanguage.googlecode.com/svn/wiki/examples/c_introduction.zip<br><br><br><br></div><br><br></div></h1></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></span></div></span></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Wed, 30 May 2012 21:17:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Writing android gui using python(6:layout)]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/641836</guid>
  <link>http://blog.yahoo.com/srplab/articles/641836</link>
  <description><![CDATA[<div><h1>Introduction</h1><br><div>Wrapandroid 0.8.5 is uploaded, programmers can download from http:/code.google.com/p/wrapandroid-for-multilanguage. In this version, a method is presented which can be used to unzip files to directory on sdcard or package path. If path does not exist, it will be created automatically. The method name is “unzipAssetFile”. Programmers can call it through activity object. The prototype of the functions is “boolean unzipAssetFile(StarObjectClass self,String zipFileName, String outputDirectory,Boolean OverWriteFlag)”. The zipFileName is located on assets directory of the project.<br><div>From version 0.8.5, layout is supported. You can use “findViewById” or “inflate” function to retrieve widgets defined in xml file. The two functions are little different from android version. “ClassName” is added as an input parameter; because program does not know which class the widgets belong to. The classname is widget name with a “Class” postfix. For example, LinearLayout should be used as LinearLayoutClass.<br><div><h1>first example</h1><br><div>This example is shown how to get widgets defined in layout.<br><div><h2>layout file main.xml, which includes a linearlayout, textview and a button.</h2><br><div>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<div>&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<div>&nbsp; &nbsp; android:id=&quot;@+id/widget73&quot;<div>&nbsp; &nbsp; android:layout_width=&quot;fill_parent&quot;<div>&nbsp; &nbsp; android:layout_height=&quot;fill_parent&quot;<div>&nbsp; &nbsp; android:orientation=&quot;vertical&quot; &gt;<br><div>&nbsp; &nbsp; &lt;TextView<div>&nbsp; &nbsp; &nbsp; &nbsp; android:id=&quot;@+id/widget45&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width=&quot;fill_parent&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height=&quot;wrap_content&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:text=&quot;@string/hello&quot; /&gt;&nbsp;<div>&nbsp; &nbsp; &lt;Button<div>&nbsp; &nbsp; &nbsp; &nbsp; android:id=&quot;@+id/widget74&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width=&quot;220dp&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height=&quot;48dp&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:text=&quot;thank for your use&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:typeface=&quot;serif&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:textStyle=&quot;bold&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:textColor=&quot;#ffff0000&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:layout_x=&quot;284dp&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:layout_y=&quot;220dp&quot;<div>&nbsp; &nbsp; &nbsp; &nbsp; android:textSize=&quot;16dp&quot;<div>&nbsp; &nbsp; /&gt; &nbsp; &nbsp; &nbsp; &nbsp;<div>&lt;/LinearLayout&gt;<br><div><h2>python code</h2><br><div>SrvGroup = libstarpy._GetSrvGroup()<div>Service = SrvGroup._GetService(&quot;&quot;,&quot;&quot;)<br><div>#--get activity<div>StarActivity = Service.ActivityClass.getCurrent();<br><div>#--get textview defined in layout<div>MyText = StarActivity.findViewById(&quot;TextViewClass&quot;,StarActivity.getResource(&quot;id/widget45&quot;));<div>#--set text<div>MyText.setText(&quot;from layout&quot;);<br><div>#--get button defined in layout<div>MyButton = StarActivity.findViewById(&quot;ButtonClass&quot;,StarActivity.getResource(&quot;id/widget74&quot;));<div>#--set onClick function of the button<div>def MyButton_onClick(self, Ev) :<div>&nbsp; &nbsp; Service.ToastClass._New().makeText(&quot;Button is click&quot;, 0).show();<div>MyButton.onClick = MyButton_onClick; &nbsp; &nbsp;<div>MyButton.setOnClickListener();<div>#--set text<div>MyButton.setText(&quot;click me&quot;);<br><div>#--get linearlayout defined in layout<div>MyLinearLayout = StarActivity.findViewById(&quot;LinearLayoutClass&quot;,StarActivity.getResource(&quot;id/widget73&quot;));<br><div>#--create a button dynamically<div>MyDynaButton = Service.ButtonClass._New(MyLinearLayout);<div>#--set onClick function of the button<div>def MyDynaButton_onClick(self, Ev) :<div>&nbsp; &nbsp; Service.ToastClass._New().makeText(&quot;Button is click&quot;, 0).show();<div>MyDynaButton.onClick = MyDynaButton_onClick;<div>MyDynaButton.setOnClickListener();<div>#--set text<div>MyDynaButton.setText(&quot;created dynamically&quot;);<div>#--set layout parameter<div>MyDynaButton.setLinearLayoutParams(100,50);<br><div><h2>Screenshot</h2><div class="img-wrap "><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/60/l/juqKlqiYBrIWXT_5Q0ixTA.jpg" title="wrapandroid_screenshot" id="0"><br><br><br><div><h1>second example(view holder)</h1><br><div>&nbsp;&nbsp;&nbsp;&nbsp;Viewholder is often talked and used in list view to speed up scroll process. Using python, we can also use method similar to view holder. For python is dynamic language, this is easy to realize.&nbsp;<br><div><h2>layout file, which will be used in list view</h2><br><div>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<div>&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<div>&nbsp;&nbsp;&nbsp;&nbsp;android:orientation=&quot;horizontal&quot;&nbsp;<div>&nbsp;&nbsp;&nbsp;&nbsp;android:layout_width=&quot;fill_parent&quot;<div>&nbsp;&nbsp;&nbsp;&nbsp;android:layout_height=&quot;fill_parent&quot;&gt;<br><br><div>&nbsp;&nbsp;&nbsp;&nbsp;&lt;ImageView android:id=&quot;@+id/img&quot;&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:layout_width=&quot;wrap_content&quot;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:layout_height=&quot;wrap_content&quot;&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:layout_margin=&quot;5px&quot;/&gt;<br><div>&nbsp;&nbsp;&nbsp;&nbsp;&lt;LinearLayout android:orientation=&quot;vertical&quot;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:layout_width=&quot;wrap_content&quot;&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:layout_height=&quot;wrap_content&quot;&gt;<br><div><span class="Apple-tab-span" style="white-space:pre;">		</span>&lt;TextView android:id=&quot;@+id/title&quot;&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">			</span>android:layout_width=&quot;wrap_content&quot;<div><span class="Apple-tab-span" style="white-space:pre;">			</span>android:layout_height=&quot;wrap_content&quot;&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">			</span>android:textColor=&quot;#FFFFFFFF&quot;<div><span class="Apple-tab-span" style="white-space:pre;">			</span>android:textSize=&quot;22px&quot; /&gt;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>&lt;TextView android:id=&quot;@+id/info&quot;&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">			</span>android:layout_width=&quot;wrap_content&quot;<div><span class="Apple-tab-span" style="white-space:pre;">			</span>android:layout_height=&quot;wrap_content&quot;&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">			</span>android:textColor=&quot;#FFFFFFFF&quot;<div><span class="Apple-tab-span" style="white-space:pre;">			</span>android:textSize=&quot;13px&quot; /&gt;<br><div>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/LinearLayout&gt;<br><br><div>&nbsp;&nbsp;&nbsp;&nbsp;&lt;Button android:id=&quot;@+id/view_btn&quot;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:layout_width=&quot;wrap_content&quot;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:layout_height=&quot;wrap_content&quot;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:text=&quot;more&quot;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>android:layout_gravity=&quot;bottom|right&quot; /&gt;<div>&lt;/LinearLayout&gt;<br><div><h2>Python code</h2><br><div>SrvGroup = libstarpy._GetSrvGroup()<div>Service = SrvGroup._GetService(&quot;&quot;,&quot;&quot;)<br><div>#--get activity<div>StarActivity = Service.ActivityClass.getCurrent();<br><div>#--create AbsoluteLayout &nbsp; &nbsp; &nbsp; &nbsp;<div>MyLayout = Service.AbsoluteLayoutClass._New(StarActivity);<br><div>#--create an inflater, which will be used to inflate the layout file.<div>mInflater = Service.LayoutInflaterClass._New();<br><div>#--create adapter for list view<div>MyAdapter = Service.BaseAdapterClass._New();<br><br><div>#--override getCount function<div>def MyAdapter_getCount(self) :<div>&nbsp; &nbsp; return 20;<div>MyAdapter.getCount = MyAdapter_getCount; &nbsp;<br><br><div>#--override getItem function<div>def MyAdapter_getItem(self,position) :<div>&nbsp; &nbsp; return position;<div>MyAdapter.getItem = MyAdapter_getItem; &nbsp;<br><br><div>#--override getItemId function<div>def MyAdapter_getItemId(self,position) &nbsp;:<div>&nbsp; &nbsp; return position;<div>MyAdapter.getItemId = MyAdapter_getItemId; &nbsp;<br><div>#--override getView function<div>def MyAdapter_getView(self,position,convertView,parent) :<div>&nbsp; &nbsp; global Service,mInflater,StarActivity;<div>&nbsp; &nbsp; if(convertView == None) :<div>&nbsp; &nbsp; &nbsp; &nbsp; #--if input convertView is empty, then we inflate from layout<div>&nbsp; &nbsp; &nbsp; &nbsp; #--define holder for python, here we use list<div>&nbsp; &nbsp; &nbsp; &nbsp; holder={}; &nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp; convertView = mInflater.inflate(&quot;LinearLayoutClass&quot;,StarActivity.getResource(&quot;layout/vlist2&quot;),None); &nbsp; &nbsp;<div>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;#--get ImageView from layout<div>&nbsp; &nbsp; &nbsp; &nbsp; holder[&quot;img&quot;] = convertView.findViewById(&quot;ImageViewClass&quot;,StarActivity.getResource(&quot;id/img&quot;));<div>&nbsp; &nbsp; &nbsp; &nbsp; #--get TextView from layout<div>&nbsp; &nbsp; &nbsp; &nbsp; holder[&quot;title&quot;] = convertView.findViewById(&quot;TextViewClass&quot;,StarActivity.getResource(&quot;id/title&quot;));<div>&nbsp; &nbsp; &nbsp; &nbsp; #--get TextView from layout<div>&nbsp; &nbsp; &nbsp; &nbsp; holder[&quot;info&quot;] = convertView.findViewById(&quot;TextViewClass&quot;,StarActivity.getResource(&quot;id/info&quot;));<div>&nbsp; &nbsp; &nbsp; &nbsp; #--get Button from layout<div>&nbsp; &nbsp; &nbsp; &nbsp; holder[&quot;viewBtn&quot;] = convertView.findViewById(&quot;ButtonClass&quot;,StarActivity.getResource(&quot;id/view_btn&quot;));<div>&nbsp;&nbsp;&nbsp;&nbsp;#--assign holder to convertView, here variable Tag is defined dynamically<div>&nbsp; &nbsp; &nbsp; &nbsp; convertView.Tag = holder;<div>&nbsp;&nbsp;&nbsp;&nbsp;#--call _SLockGC, hold convertView in python space.<div>&nbsp; &nbsp; &nbsp; &nbsp; convertView._SLockGC(); &nbsp; &nbsp;<span class="Apple-tab-span" style="white-space:pre;">				</span><div>&nbsp; &nbsp; else :<div>&nbsp; &nbsp; &nbsp; &nbsp; #--if input convertView is not null, then we get holder from convertView<div>&nbsp; &nbsp; &nbsp; &nbsp; holder = convertView.Tag;<div>&nbsp; &nbsp;&nbsp;<div>&nbsp; &nbsp; #--assign values for each widgets in the holder.<div>&nbsp; &nbsp; if( position % 3 == 0 ) :<div>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;holder[&quot;img&quot;].setBackgroundResource(StarActivity.getResource(&quot;drawable/i1&quot;));<div>&nbsp; &nbsp; &nbsp; &nbsp; holder[&quot;title&quot;].setText(&quot;G1&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp; holder[&quot;info&quot;].setText(&quot;google 1&quot;);<div>&nbsp; &nbsp; elif( position % 3 &nbsp;== 1 ):<div>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;holder[&quot;img&quot;].setBackgroundResource(StarActivity.getResource(&quot;drawable/i2&quot;));<div>&nbsp; &nbsp; &nbsp; &nbsp; holder[&quot;title&quot;].setText(&quot;G2&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp; holder[&quot;info&quot;].setText(&quot;google 2&quot;);<div>&nbsp; &nbsp; elif( position % 3 &nbsp;== 2 ):<div>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;holder[&quot;img&quot;].setBackgroundResource(StarActivity.getResource(&quot;drawable/i3&quot;));<div>&nbsp; &nbsp; &nbsp; &nbsp; holder[&quot;title&quot;].setText(&quot;G3&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp; holder[&quot;info&quot;].setText(&quot;google 3&quot;);<br><div>&nbsp; &nbsp; #--set onClick event function for viewBtn.<div>&nbsp; &nbsp; def holder_viewBtn_onClick(self, Ev) :<div>&nbsp; &nbsp; &nbsp; &nbsp; Service.ToastClass._New().makeText(&quot;Button is click&quot;, 0).show();<div>&nbsp; &nbsp; holder[&quot;viewBtn&quot;].onClick = holder_viewBtn_onClick;<div>&nbsp; &nbsp; holder[&quot;viewBtn&quot;].setOnClickListener();<div>&nbsp; &nbsp; return convertView;<br><div>MyAdapter.getView = MyAdapter_getView; &nbsp; &nbsp;<br><div>#--create a list view and assign adapter to it.<div>MyListView = Service.ListViewClass._New(MyLayout);<div>MyListView.setAbsoluteLayoutParams(Service.FILL_PARENT,Service.FILL_PARENT,0,0);<div>MyListView.setAdapter(MyAdapter);<br><div><h2></h2><h2>Screenshot</h2><div class="img-wrap "><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/70/l/QEvoIE.oH7U7HaRT6Qotcg.jpg" title="layout_screenshot" id="0"><br><br><h2>example can be download from link :&nbsp;wrapandroid-for-multilanguage.googlecode.com/svn/wiki/examples/pythongui_layout.rar</h2><br><br><br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Mon, 28 May 2012 22:07:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Writing android gui using python(menu scroll view and popup window)]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/638322</guid>
  <link>http://blog.yahoo.com/srplab/articles/638322</link>
  <description><![CDATA[<div><h1>Introduction</h1><br>The examples in this article will create menu, scroll view and popup window. For menu widget, programmer should override android functions of activity, which are “onCreateOptionsMenu”, “onPrepareOptionsMenu”, “onOptionsItemSelected”. Using python, the same functions should be defined and assigned to activity object. Popup window is often used to show information or perform some interactions with customer. Python permits define functions in another function, which is convenient to define widgets in popup window.&nbsp;<br><div><h1>Scroll View</h1><div>&nbsp; &nbsp; Creating scroll view is more directly and simple.&nbsp;<div><h2>Create scroll view object</h2><div>MyScroll = Service.ScrollViewClass._New(MyLayout);<br><div>MyScroll.setLinearLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);<br><div><h2>Create root layout in the scroll view</h2><br><div>MyScrollLayout = Service.LinearLayoutClass._New(MyScroll);<div>MyScrollLayout.setOrientation(&quot;VERTICAL&quot;);<div>MyScrollLayout.setFrameLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);<br><div><h2>Create a button, when is clicked, we create a new button in scroll view.</h2><br><div>MyButtonIndex = 1;<div>//#set onClick event listener<div>def MyButton_onClick(self,Ev) :<div>global MyButtonIndex;<div>//#Create a new button in scroll view<div>b = Service.ButtonClass._New(MyScrollLayout);<div>//#Set text content and color<div>&nbsp; &nbsp; b.setText(&quot;Button&quot; + str(MyButtonIndex));<div>&nbsp; &nbsp; b.setTextColor(0xFFFF0000); &nbsp;&nbsp;<div>&nbsp; &nbsp; b.setLinearLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);<div>&nbsp; &nbsp; MyButtonIndex = MyButtonIndex + 1;<div>return;<div>//#assign event listener to object<div>MyButton.onClick = MyButton_onClick;<br><div><h1>Menu</h1><div>For menu, we should override functions of activity object. which are “onCreateOptionsMenu”, “onPrepareOptionsMenu”,and “onOptionsItemSelected”.<br><div>The activity object is obtained using code “StarActivity = Service.ActivityClass.getCurrent();”<br><div><h2>Override function “onCreateOptionsMenu”</h2><div>In this function, we create menu items.<br><br><div>def StarActivity_onCreateOptionsMenu(self,menu) :<div>&nbsp; &nbsp; menu.add1(0, 1, 1, &quot;open&quot;);<div>&nbsp; &nbsp; menu.add1(0, 2, 2, &quot;edit&quot;);<div>&nbsp; &nbsp; menu.add1(0, 3, 3, &quot;update&quot;);<div>&nbsp; &nbsp; menu.add1(0, 4, 4, &quot;clode&quot;);<div>&nbsp; &nbsp; return True;<div>StarActivity.onCreateOptionsMenu = StarActivity_onCreateOptionsMenu;<br><div><h2>Override function “onPrepareOptionsMenu”</h2><br><div>The function simply returns true to enable menu to show.<br><br><div>def StarActivity_onPrepareOptionsMenu(self,menu) :<div>&nbsp; &nbsp; return True;<div>StarActivity.onPrepareOptionsMenu = StarActivity_onPrepareOptionsMenu;<br><div><h2>Override function “onOptionsItemSelected”</h2><br><div>def StarActivity_onOptionsItemSelected(self,menuitem) : &nbsp; &nbsp;<div>&nbsp; &nbsp; id = menuitem.getItemId();<div>&nbsp; &nbsp; if( id == 1 ) :<div>&nbsp; &nbsp; &nbsp; &nbsp; self.setTitle(&quot;Open Text!&quot;);<div>&nbsp; &nbsp; if( id == 2 ) :<div>&nbsp; &nbsp; &nbsp; &nbsp; self.setTitle(&quot;Edit Text!&quot;);<div>&nbsp; &nbsp; if( id == 3 ) :<div>&nbsp; &nbsp; &nbsp; &nbsp; self.setTitle(&quot;Update Text!&quot;);<div>&nbsp; &nbsp; if( id == 4 ) :<div>&nbsp; &nbsp; &nbsp; &nbsp; self.setTitle(&quot;Close Text!&quot;);<div>&nbsp; &nbsp; return True;&nbsp;<div>StarActivity.onOptionsItemSelected = StarActivity_onOptionsItemSelected;<br><div><h1>Popup window</h1><div>Popup window is often used to show information or perform some interactions with customer.<br><br><div><h2>Create a button, when is clicked, we create a popup window.</h2><div>OpenPopWindowButton = Service.ButtonClass._New(ButtonLayout);<div>OpenPopWindowButton.setText(&quot;Open popup window&quot;);<div>OpenPopWindowButton.setLinearLayoutParams(Service.WRAP_CONTENT,Service.WRAP_CONTENT); &nbsp;<div>def OpenPopWindowButton_onClick(self, Ev):<div>&nbsp; &nbsp;…<div>&nbsp; &nbsp; return;<div>OpenPopWindowButton.onClick = OpenPopWindowButton_onClick<br><div><h2>In onClick event function, a popup window is created</h2><br><div>Because python permits to define function in another function, we can create popup window and create functions for it.<br><div>def OpenPopWindowButton_onClick(self, Ev):<div>&nbsp; &nbsp; //#create popup window<div>&nbsp; &nbsp; MyPopupWindow = Service.PopupWindowClass._New()<div>&nbsp; &nbsp; //#set onDismiss event listener<div>&nbsp; &nbsp; def MyPopupWindow_onDismiss(self, Ev):<div>&nbsp; &nbsp; &nbsp; &nbsp; Service.ToastClass._New().makeText(&quot;PopupWindow is dismiss&quot;,0).show();<div>&nbsp; &nbsp; &nbsp; &nbsp; return;<div>&nbsp; &nbsp; MyPopupWindow.onDismiss = MyPopupWindow_onDismiss;<div>&nbsp; &nbsp;&nbsp;<div>&nbsp; &nbsp; //#create root layout for popup window<div>&nbsp; &nbsp; PopupLayout = Service.LinearLayoutClass._New();<div>&nbsp; &nbsp; PopupLayout.setBackgroundColor(0xFF0000FF) #--blue<div>&nbsp; &nbsp; //#create a button<div>&nbsp; &nbsp; MyButton = Service.ButtonClass._New(PopupLayout);<div>&nbsp; &nbsp; def MyButton_onClick(self, Ev) :<div>&nbsp; &nbsp; &nbsp; &nbsp; //#when is clicked, we show information and close the popup window<div>&nbsp; &nbsp; &nbsp; &nbsp; Service.ToastClass._New().makeText(&quot;Button is click&quot;,0).show();<div>&nbsp; &nbsp; &nbsp; &nbsp; MyPopupWindow.dismiss();<div>&nbsp; &nbsp; &nbsp; &nbsp; return;<div>&nbsp; &nbsp; MyButton.onClick = MyButton_onClick;<div>&nbsp; &nbsp; MyButton.setText(&quot;CloseWindow&quot;);<div>&nbsp; &nbsp; MyButton.setLinearLayoutParams(Service.WRAP_CONTENT,Service.WRAP_CONTENT);&nbsp;<div>&nbsp; &nbsp; //#assign layout to popup window.<div>&nbsp; &nbsp;MyPopupWindow.setContentView(PopupLayout);<div>&nbsp; &nbsp; MyPopupWindow.setWidth(200);<div>&nbsp; &nbsp; MyPopupWindow.setHeight(200);<div>&nbsp; &nbsp; MyPopupWindow.setFocusable(True); &nbsp; &nbsp; &nbsp;<div>&nbsp; &nbsp; MyPopupWindow.showAtLocation(self,17,0,0);<div>&nbsp; &nbsp; //#prevent garbage collected by python.<div>&nbsp; &nbsp; MyPopupWindow._LockGC();<div>&nbsp; &nbsp; return;<br><div>Screenshot:<div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/64/l/vUUwYUPlLnGfrKrYqq_CpA.jpg" title="menu_scrollview_popupwindow_screenshot" id="0"><br><br><div>Examples download:<br><br>http://www.srplab.com/android/pythongui_menu_scrollview_popupwindow.rar</div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Wed, 2 May 2012 21:09:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Writing android gui using lua(introduction)]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/637698</guid>
  <link>http://blog.yahoo.com/srplab/articles/637698</link>
  <description><![CDATA[<div><h1>Introduction</h1><br><div>Lua is more like python. Both are all dynamic languages, with little difference in syntax. Therefore these series of articles are similar to &quot;write android gui using python&quot; series. <br>Using CLE and wrapandroid project, programmers can also write android gui applications with lua. CLE supports interaction between lua and java, gives a common interface for multiple programming languages. Wrapandroid project encapsulates android java class with cle objects. This article is an introduction. There will have series of articles to further explain how to programming android applications using lua.<br><div><h1>Preparing environment</h1><br><div>Unlike python, for lua engine has been embedded in CLE, you do not need to install other support packages.<br><div>a: CLE may install from network by application automatically, you need only include &nbsp;starcore_android_r5.jar in the project. The file is in starcore_devfiles_r5.zip, which can be download from http://code.google.com/p/cle-for-android<br><br><div>b: Wrapandroid has two files: wrapandroid.jar and SRPWrapAndroidEngine.xml, which can be download from http:/code.google.com/p/wrapandroid-for-multilaguage/download/wrapandroid_devfiles_0_8_2.rar<br><div><h2>Begin programming</h2><div>a. Open eclipse, create a new android project, for example, “introduction”<div>b. Add Permission, which is used to download and install cle for the application<div>&nbsp; &nbsp; &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;<div>&nbsp; &nbsp; &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;&gt;&lt;/uses-permission&gt;<div>c. copy files : starcore_android_r5.jar and wrapandroid.jar into the project directory, and add them to java build path, as shown below.<div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/10/l/QHnlWqm9KeC0z0sVsIh6Jg.jpg" title="library" id="0"><br><div>d. copy file : SRPWrapAndroidEngine.xml to assets directory.<div>e. edit IntroductionActivity.java<div>import com.srplab.wrapandroid.*;<br><div>public class IntroductionActivity extends WrapAndroidActivity {<div>&nbsp; &nbsp; /** Called when the activity is first created. */<div>&nbsp; &nbsp; @Override<div>&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {<div>&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);<div>&nbsp; &nbsp; &nbsp; &nbsp; //setContentView(R.layout.main);<div>&nbsp; &nbsp; &nbsp; &nbsp; StarActivity._Call(&quot;DoAssetsFile&quot;, &quot;lua&quot;, &quot;code.lua&quot;);<div>&nbsp; &nbsp; }<div>}<div>f. create new text file code.lua in assets directory.<br><div><h1>code.lua</h1><br><div><h2>get current service maintained by cle</h2><div>SrvGroup = libstarcore._GetSrvGroup()<div>Service = SrvGroup:_GetService(&quot;&quot;,&quot;&quot;)<br><div><h2>get current activity, which is created by wrapandroid at init stage</h2><div>StarActivity = Service.ActivityClass:getCurrent();<br><div>from now on, we can create gui elements. The first element should be layout element, which may be linear layout, absolute layout, etc. In this example, we create linear layout, which contains an edit text and buttons.&nbsp;<br><div><h2>create root layout</h2><br><div>MyLayout = Service.LinearLayoutClass:_New(StarActivity);<div>MyLayout:setOrientation(&quot;VERTICAL&quot;);<br><div><h2>create title layout and gui elements</h2><br><div>MyTitleLayout = Service.LinearLayoutClass:_New(MyLayout);<div>MyTitleLayout:setLinearLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);<br><div>UrlEdit = Service.EditTextClass:_New(MyTitleLayout);<div>UrlEdit:setText(&quot;http://www.google.com&quot;);<div>UrlEdit:setLinearLayoutParams(StarActivity:getWidth()-200,Service.WRAP_CONTENT);<br><div>GoButton = Service.ButtonClass:_New(MyTitleLayout);<div>GoButton:setText(&quot;go&quot;);<div>GoButton:setLinearLayoutParams(100,Service.FILL_PARENT);<div>function GoButton:onClick(ev)<div>&nbsp; &nbsp; MyWebView:loadUrl(UrlEdit:getText());<div>end &nbsp; &nbsp;<div>&nbsp;<div>ExitButton = Service.ButtonClass:_New(MyTitleLayout);<div>ExitButton:setText(&quot;exit&quot;);<div>ExitButton:setLinearLayoutParams(100,Service.FILL_PARENT);<div>function ExitButton:onClick(ev)<div>&nbsp; &nbsp; StarActivity:exit(0);<div>end &nbsp; &nbsp;&nbsp;<br><div><h2>create webview layout and webview instance</h2><br><div>MyTitleLayout1 = Service.LinearLayoutClass:_New(MyLayout);<div>MyTitleLayout1:setLinearLayoutParams(Service.FILL_PARENT,Service.FILL_PARENT);<br><div>MyWebView = Service.WebViewClass:_New(MyTitleLayout1)<div>MyWebView:setLinearLayoutParams(Service.FILL_PARENT,Service.FILL_PARENT);<div>MyWebSettings = MyWebView:getSettings();<div>MyWebSettings:setJavaScriptEnabled(true);<div>MyWebSettings:_Free();<br><div>The screenshot is as shown below:<br><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/11/l/egZKRTUvzvOXaSaRwKFiUQ.jpg" title="introduction_screenshot" id="0"><br>Example can be downloaded from http://www.srplab.com/android/luagui_introduction.rar</div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Fri, 27 Apr 2012 23:51:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Writing android gui using python(list view and custom view)]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/637557</guid>
  <link>http://blog.yahoo.com/srplab/articles/637557</link>
  <description><![CDATA[<div><h1>Introduction</h1><br>The examples in this article will create a listview and a custom view. Android listview often uses adapter such as “ArrayAdapter&lt;String&gt;(…)”. The syntax is java generic, which may have no corresponding types in dynamic script languages, such as python, lua, etc. Therefore, we have to give specific class, such as StringArrayAdaper, for it can be used in these dynamic languages. For StringArrayAdapter, we can override function getView to return little more complex view for listview. For custom view, we can create instance of View, and override its function onDraw. In function onDraw, we may draw text or bitmaps using function of canvas.<br><div><h1>List View</h1>We first create an instance of StringArrayAdapterClass, which is defined wrapandroid.jar package. And then create a list view.<div><h2>Create StringArrayAdapter and override its function getView</h2><div>//#create instance of StringArrayAdapterClass<div>MyStringArrayAdapter = Service.StringArrayAdapterClass._New();<div>//#override function getView<div>def MyStringArrayAdapter_getView(self,position, convertView, parent) :<div>&nbsp; &nbsp; global Service;<div>&nbsp; &nbsp;&nbsp;<div>&nbsp; &nbsp; //#create linearlayout<div>&nbsp; &nbsp; i = Service.LinearLayoutClass._New();<div>&nbsp; &nbsp; px = i.dp2px(5);<div>&nbsp; &nbsp; //#set padding<div>&nbsp; &nbsp; i.setPadding(px,px,px,px);<div>&nbsp; &nbsp; //#set layout parameters, here we should uses function “setAbsListViewLayoutParams”<div>&nbsp; &nbsp; i.setAbsListViewLayoutParams(Service.WRAP_CONTENT,Service.WRAP_CONTENT);<div>&nbsp; &nbsp; //#create an imageview<div>&nbsp; &nbsp; imageView = Service.ImageViewClass._New(i);<div>&nbsp; &nbsp; //#set padding<div>&nbsp; &nbsp; imageView.setPadding(5,5,5,5);<div>&nbsp; &nbsp; //#set layout parameters. Because imageView is child of LinearLayout, we should use function “setLinearLayoutParams”.<div>&nbsp; &nbsp; imageView.setLinearLayoutParams(i.dp2px(24),i.dp2px(24));<div>&nbsp; &nbsp; //#create TextView<div>&nbsp; &nbsp; itextview = Service.TextViewClass._New(i);<div>&nbsp; &nbsp; //#set layout parameter<div>&nbsp; &nbsp; itextview.setLinearLayoutParams(Service.WRAP_CONTENT,Service.WRAP_CONTENT);<div>&nbsp; &nbsp; //#set size of font.<div>&nbsp; &nbsp; itextview.setTextSize(i.sp2px(18))<div>&nbsp; &nbsp; //#set content of textview and imageview based on position<div>&nbsp; &nbsp; if( position == 0 ) :<div>&nbsp; &nbsp; &nbsp; &nbsp; itextview.setText(&quot;Android&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp; imageView.setImageResource(StarActivity.getResource(&quot;drawable/android_logo&quot;));<div>&nbsp; &nbsp; if( position == 1 ) :<div>&nbsp; &nbsp; &nbsp; &nbsp; itextview.setText(&quot;WindowsMobile&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp; imageView.setImageResource(StarActivity.getResource(&quot;drawable/windowsmobile_logo&quot;));<div>&nbsp; &nbsp; if( position == 2 ) :<div>&nbsp; &nbsp; &nbsp; &nbsp; itextview.setText(&quot;iOS&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp; imageView.setImageResource(StarActivity.getResource(&quot;drawable/ios_logo&quot;));<div>&nbsp; &nbsp; if( position == 3 ) :<div>&nbsp; &nbsp; &nbsp; &nbsp; itextview.setText(&quot;Blackberry&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp; imageView.setImageResource(StarActivity.getResource(&quot;drawable/blackberry_logo&quot;));<div>&nbsp; &nbsp; //#call _LockGC function before function returns to prevent the object GC by python.<div>&nbsp; &nbsp; i._LockGC();<div>&nbsp; &nbsp; return i;<div>MyStringArrayAdapter.getView = MyStringArrayAdapter_getView; &nbsp; &nbsp;<br><div>#add string values to StringArrayAdapter.<div>MyStringArrayAdapter.add(&quot;Android&quot;);<div>MyStringArrayAdapter.add(&quot;WindowsMobile&quot;);<div>MyStringArrayAdapter.add(&quot;iOS&quot;);<div>MyStringArrayAdapter.add(&quot;Blackberry&quot;);<br><div><h2>Create ListView and set its adapter</h2><br><div>//#create ListView<div>MyListView = Service.ListViewClass._New(MyLayout);<div>//#set its onItemClick event listener.<div>def MyListView_onItemClick(self, Ev,objid,position,id) :<div>&nbsp; &nbsp; Service.ToastClass._New().makeText(&quot;[MyListView] event on click is onItemClick &quot;+objid,0).show();<div>MyListView.onItemClick = MyListView_onItemClick; &nbsp; &nbsp;<div>//#set layout prameter<div>MyListView.setLinearLayoutParams(Service.FILL_PARENT,150);<div>//#set adapter<div>MyListView.setAdapter(MyStringArrayAdapter);<br><div><h1>Custom View</h1><div>For custom view, we can create instance of View, and override its function onDraw. In function onDraw, we may draw text or bitmaps using function of canvas.&nbsp;<br><div><h2>Create a Paint and BitmapFactory for using in next step.</h2><br><div>MyPaint = Service.PaintClass._New(); &nbsp;<div>MyBitmapFactory = Service.BitmapFactoryClass._New();<br><div><h2>Create View and override its function onDraw</h2><br><div>def myView_onDraw(self,canvas) :<div>&nbsp; &nbsp; global MyBitmapFactory,MyPaint<div>&nbsp; &nbsp;&nbsp;<div>&nbsp; &nbsp; //#call parent onDraw function<div>&nbsp; &nbsp; self.onSuperDraw(canvas); &nbsp; &nbsp;<div>&nbsp; &nbsp; //#set color of Paint &nbsp; &nbsp;<span class="Apple-tab-span" style="white-space:pre;">		</span><div>&nbsp; &nbsp; MyPaint.setColor(0xFFFF0000); &nbsp;&nbsp;<div>&nbsp; &nbsp; //#draw a rect on canvas<div>&nbsp; &nbsp; canvas.drawRect(10, 20, 100, 100, MyPaint);&nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<div>&nbsp; &nbsp; //#get bitmap from resources. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div>&nbsp; &nbsp; MyBitmap = MyBitmapFactory.decodeResource(StarActivity.getResource(&quot;drawable/aqua02&quot;));<div>&nbsp; &nbsp; //#draw the bitmap on canvas<div>&nbsp; &nbsp; canvas.drawBitmap(MyBitmap, 100, 100, None); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div>&nbsp; &nbsp;&nbsp;<div>&nbsp; &nbsp; //#create an matrix object &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div>&nbsp; &nbsp; matrix=Service.MatrixClass._New();<div>&nbsp; &nbsp; //#set rotation parameter of matrix<div>&nbsp; &nbsp; matrix.postScale(0.8, 0.8);<div>&nbsp; &nbsp; matrix.postRotate(45);<div>&nbsp; &nbsp; //create a new bitmap and set to the result of previous bitmap with matris<div>&nbsp; &nbsp; dstbmp=Service.BitmapClass._New();<div>&nbsp; &nbsp; dstbmp.createBitmap0(MyBitmap,0,0,MyBitmap.getWidth(),MyBitmap.getHeight(),matrix,True);<div>&nbsp; &nbsp; //#draw the bitmap<div>&nbsp; &nbsp; canvas.drawBitmap(dstbmp, 300, 100, None);<div>&nbsp; &nbsp;&nbsp;<div>&nbsp; &nbsp; //#free object created locally<div>&nbsp; &nbsp; matrix._Free();<div>&nbsp; &nbsp; dstbmp._Free();<div>&nbsp; &nbsp; MyBitmap._Free();<div>myView.onDraw = myView_onDraw; &nbsp; &nbsp;<div>//#set layout parameter.<div>myView.setLinearLayoutParams(Service.FILL_PARENT,Service.FILL_PARENT);<br><br><div>Screenshot:<div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/23/l/nvV9qH5aa5lOuWbBq.Pltg.jpg" title="listandcustomview_screenshot" id="0"><br><br><div>Examples download:<div><br>http://www.srplab.com/android/listandcustomview.rar</div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Thu, 26 Apr 2012 20:00:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Writing android gui using python(gallery)]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/636595</guid>
  <link>http://blog.yahoo.com/srplab/articles/636595</link>
  <description><![CDATA[<div><h1>Introduction</h1><br><div>Wrapandroid version is updated to 0.8.2. Please uses this version. <br><br>We might write python code outside eclipse. In this case, we have no logcat window to show print message from python. Do not worry about, cle supports syslog. The message can be output to syslog server. Using the following sentence to open syslog client:<br><blockquote>SrvGroup._SetOutputPort(&quot;192.168.0.129&quot;,514)<br>The ipaddress should be adjusted based on your environment.</blockquote><br><div>Output likes this:<br><br><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/70/l/31N0cIDYL664EgrESsIQFA.jpg" title="gallery_syslog" id="0"><br><br><div>Android gallery is a little more complicate gui element. In order to use gallery, we should first create an adapter, override its functions. And then, gallery object uses the adapter to obtain the content to be drawn.<br><div><h1>Create adapter object</h1><div>&nbsp;&nbsp;&nbsp;&nbsp;<div>&nbsp;&nbsp;&nbsp;&nbsp;Wrapandroid’s adpterClass encapsulates android java class adapter. It has functions can be override, such as getCount, getItem, getItemId, getView,etc. Detailed explains of these functions, please refer to android documents.&nbsp;<br><div>1.&nbsp;&nbsp;&nbsp;&nbsp;Create bitmap object<blockquote>Bitmap0 = StarActivity.getBitmapDrawable(StarActivity.getResource(&quot;drawable/aqua02&quot;)).getBitmap();<br>Bitmap1 = StarActivity.getBitmapDrawable(StarActivity.getResource(&quot;drawable/aqua03&quot;)).getBitmap();<br>Bitmap2 = StarActivity.getBitmapDrawable(StarActivity.getResource(&quot;drawable/aqua04&quot;)).getBitmap();<br>Bitmap3 = StarActivity.getBitmapDrawable(StarActivity.getResource(&quot;drawable/aqua05&quot;)).getBitmap();</blockquote><div>Bitmap resources locates in project directory res&#92;drawable-hdpi.<br><div>2.&nbsp;&nbsp;&nbsp;&nbsp;getCount, getItem,and getItemId function<br><blockquote>MyAdapter = Service.AdapterClass._New()<br>def MyAdapter_getCount(self) :<br>&nbsp; &nbsp; return 4;<br>MyAdapter.getCount = MyAdapter_getCount; &nbsp;<br>def MyAdapter_getItem(self,position) :<br>&nbsp; &nbsp; return position;<br>MyAdapter.getItem = MyAdapter_getItem; &nbsp;<br>def MyAdapter_getItemId(self,position) &nbsp;:<br>&nbsp; &nbsp; return position;<br>MyAdapter.getItemId = MyAdapter_getItemId;</blockquote><br><div>3.&nbsp;&nbsp;&nbsp;&nbsp;getView function<br><blockquote>def MyAdapter_getView(self,position,convertView,parent) :<br>&nbsp; &nbsp; global Service;<br>&nbsp; &nbsp; i = Service.ImageViewClass._New();<br>&nbsp; &nbsp; if( position == 0 ) :<br>&nbsp; &nbsp; &nbsp; &nbsp; i.setImageBitmap(Bitmap0);<br>&nbsp; &nbsp; if( position == 1 ) :<br>&nbsp; &nbsp; &nbsp; &nbsp; i.setImageBitmap(Bitmap1);<br>&nbsp; &nbsp; if( position == 2 ) :<br>&nbsp; &nbsp; &nbsp; &nbsp; i.setImageBitmap(Bitmap2);<br>&nbsp; &nbsp; if( position == 3 ) :<br>&nbsp; &nbsp; &nbsp; &nbsp; i.setImageBitmap(Bitmap3); &nbsp;<br>&nbsp; &nbsp; i.setGalleryLayoutParams(Service.FILL_PARENT,Service.FILL_PARENT);<br>&nbsp; &nbsp; i.setScaleType(&quot;FIT_XY&quot;);<br>&nbsp; &nbsp; i.setBackgroundResource(StarActivity.getResource(&quot;mGalleryItemBackground&quot;));<br>&nbsp; &nbsp; i._LockGC();<br>&nbsp; &nbsp; return i; &nbsp; &nbsp;<br>MyAdapter.getView = MyAdapter_getView;</blockquote><br><div>Because imageview object is created by python, when function returns. the object will be freed by GC. So, we should call _LockGC before function return.<br><div><h1>Create Gallery object</h1><br><blockquote>MyGallery = Service.GalleryClass._New(MyLayout)<br>def MyGallery_onItemClick(self,event,objid,position,id ):<br>&nbsp; &nbsp; Service.ToastClass._New().makeText(&quot;[MyGallery] event on onItemClick is trigger &quot;+objid+str(position)+str(id),0).show();<br>MyGallery.onItemClick = MyGallery_onItemClick;<br>MyGallery.setAdapter(MyAdapter);<br>MyGallery.setLinearLayoutParams(Service.FILL_PARENT,Service.FILL_PARENT);</blockquote><br><div>Screenshot:<div><br><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/71/l/5OUMpTGvV8P.Q7C0iAy5zg.jpg" title="gallery_screenshot" id="0"><br><br><div>Examples download:<br><br><a href="http://www.srplab.com/android/pythongui_gallery.rar" title="http://www.srplab.com/android/pythongui_gallery.rar"></a><a href="http://www.srplab.com/android/pythongui_gallery.rar" title="http://www.srplab.com/android/pythongui_gallery.rar"></a>http://www.srplab.com/android/pythongui_gallery.rar</div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Sun, 22 Apr 2012 19:39:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Writing android gui using python(basic gui elements)]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/636007</guid>
  <link>http://blog.yahoo.com/srplab/articles/636007</link>
  <description><![CDATA[<div>&nbsp;&nbsp;&nbsp;&nbsp;Let’s continue the topic. In this article, we gives example of basic android gui elements created and controlled using python, which may be buttons, textviews, checkboxs, radiobuttons.<div>&nbsp;&nbsp;&nbsp;&nbsp;Before the topic, we discuss how to create timer using python. Python script code runs in the thread context of android main activity. Therefore, we can not use local loop statement for example, “while”, to wait some actions, such as waiting response from remote server. In this case, a timer may be used. Based on the support of CLE, create a timer using python is simple, there are three steps:<div>1.&nbsp;&nbsp;&nbsp;&nbsp;create an object. &nbsp;<div>2.&nbsp;&nbsp;&nbsp;&nbsp;define a timer function and assign to the object.<div>3.&nbsp;&nbsp;&nbsp;&nbsp;create an object’s timer.<div>The detailed code example is as follow<br><br><div>timerobj = Service._New();<div>def timerobj _timerfunc(self,arg1,arg2) :<div>print(“timer is trigger”);<div>timerobj.timerfunc = timerobj _timerfunc;<div>timerobj._SetTimer(100, timerobj.timerfunc,0,0); &nbsp;# timer triggered per second.<br><br><div>&nbsp;&nbsp;&nbsp;&nbsp;For basic android gui elements, the corresponding cle class name are “ButtoClass”, “TextViewClass”, “EditClass”, “CheckBoxClass”, “RadioGroupClass”, “RadioButtonClass”. The naming rules is android java class name with suffix “Class”, which is easy to remember. Functions and events are similar to android class. So, for detailed information about the class, you can refer to android development kit documents. By now, wrapandroid project is in progress, it wrap general used functions and events of the class. Which are wrapped, please refer to document wrapandroid.chm.<div>&nbsp;&nbsp;&nbsp;&nbsp;In order to place gui elements on the screen, we first should create layout, which may by Linear Layout or Absolute Layout. In the example, we use Linear Layout.<br><br><div>1.&nbsp;&nbsp;&nbsp;&nbsp;Create LinearLayout<br><br><div>MyLayout = Service.LinearLayoutClass._New(StarActivity);<div>MyLayout.setOrientation(&quot;VERTICAL&quot;);<br><br><div>2.&nbsp;&nbsp;&nbsp;&nbsp;Create a button, set text, and textcolor<br><br><div>Button = Service.ButtonClass._New(MyLayout);<div>Button.setText(&quot;hello button&quot;);<div>Button.setTextColor(0xFF0000FF);<div>Button.setLinearLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);<div>def Button_onClick(self,ev) :<div>&nbsp; &nbsp; Service.ToastClass._New().makeText(&quot;button is clicked&quot;,0).show();<div>Button.onClick = Button_onClick;<br><br><div>3.&nbsp;&nbsp;&nbsp;&nbsp;Create a textview<br><br><div>Text = Service.TextViewClass._New(MyLayout);<div>Text.setText(&quot;hello text&quot;);<div>Text.setTextColor(0xFFFF0000);&nbsp;<div>Text.setTextSize(30);<div>Text.setLinearLayoutParams(Service.WRAP_CONTENT,Service.WRAP_CONTENT);<br><br><div>4.&nbsp;&nbsp;&nbsp;&nbsp;Create an EditText<br><br><div>MyEdit = Service.EditTextClass._New(MyLayout);<div>MyEdit.setLinearLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);<br><br><div>5.&nbsp;&nbsp;&nbsp;&nbsp;Create an RadioGroup<br><br><div>MyRadioGroup = Service.RadioGroupClass._New(MyLayout);<div>MyRadioGroup.setLinearLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);<div>def MyRadioGroup_onCheckedChanged(self,Event,objid) :<div>&nbsp; &nbsp; Service.ToastClass._New().makeText(&quot;[MyRadioGroup] event on click is trigger&quot;+objid,0).show();<div>&nbsp; &nbsp; return;<div>MyRadioGroup.onCheckedChanged = MyRadioGroup_onCheckedChanged;<br><br><div>6.&nbsp;&nbsp;&nbsp;&nbsp;Create RadioButton in the group<br><br><div>MyRadioButton1 = Service.RadioButtonClass._New(MyRadioGroup);<div>MyRadioButton1.setText(&quot;RadioButton1&quot;);<div>MyRadioButton2 = Service.RadioButtonClass._New(MyRadioGroup);<div>MyRadioButton2.setText(&quot;RadioButton2&quot;);<br><br><div>7.&nbsp;&nbsp;&nbsp;&nbsp;Create a timer, in timer, we update content of textview.<br><br><div>Index = 1;&nbsp;<div>def Text_timerfunc(self,TimerID,arg1,arg2) :<div>&nbsp; &nbsp; global Index<div>&nbsp; &nbsp; self.setText(&quot;hello text &nbsp;&quot;+str(Index));<div>&nbsp; &nbsp; Index = Index + 1;&nbsp;<div>Text.timerfunc = Text_timerfunc;<div>Text._SetTimer(1, Text.timerfunc,0,0);<br><br><div>Screenshot:<div><div class="img-wrap "><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/89/l/8WSOv7vFn4eVGv_GjPBYNw.jpg" title="basicguielements_screenshot" id="0"><br><br><div>Examples download:<br>http://www.srplab.com/android/pythongui_basicguielements.rar</div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Wed, 18 Apr 2012 21:28:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Writing android gui using python(introduction)]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/635401</guid>
  <link>http://blog.yahoo.com/srplab/articles/635401</link>
  <description><![CDATA[<div>&nbsp;&nbsp;&nbsp;&nbsp;PythonForAndroid provides support for python script language on android. CLE project supports interaction between python and java, gives a common interface for multiple programming languages. And wrapandroid project encapsulates android java class with cle objects. Using the three components, programmers can write android gui programs with python directly. This article is an introduction. There will have series of articles to further explain how to programming android applications using python.<br><br><div>1. Preparing environment.<br><br><div>a: install PythonForAndroid from http://code.google.com/p/android-scripting<div>b: CLE may install from network by application automatically, you need only include &nbsp;starcore_android_r5.jar in the project. The file is in starcore_devfiles_r5.zip, which can be download from http://code.google.com/p/cle-for-android<div>c: Wrapandroid has two files: wrapandroid.jar and SRPWrapAndroidEngine.xml, which can be download from http:/code.google.com/p/wrapandroid-for-multilaguage/download/wrapandroid_devfiles_0_8_1.rar<br><br><div>2. Begin programming<br><br><div>a. Open eclipse, create a new android project, for example, “introduction”<div>b. Add Permission, which is used to download and install cle for the application<br><br><div>&nbsp; &nbsp; &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;<div>&nbsp; &nbsp; &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;&gt;&lt;/uses-permission&gt;<br><br><div>c. copy files : starcore_android_r5.jar and wrapandroid.jar into the project directory, and add them to java build path, as shown below.<br><br><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/94/l/V8R0WaDXg7XcYm6ouvld3w.jpg" title="library" id="0"><br><div>d. copy file : SRPWrapAndroidEngine.xml to assets directory.<div>e. edit IntroductionActivity.java<br><br><div>import com.srplab.wrapandroid.*;<br><div>public class IntroductionActivity extends WrapAndroidActivity {<div>&nbsp; &nbsp; /** Called when the activity is first created. */<div>&nbsp; &nbsp; @Override<div>&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {<div>&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);<div>&nbsp; &nbsp; &nbsp; &nbsp; //setContentView(R.layout.main);<div>&nbsp; &nbsp; &nbsp; &nbsp; StarActivity._Call(&quot;DoAssetsFile&quot;, &quot;python&quot;, &quot;code.py&quot;);<div>&nbsp; &nbsp; }<div>}<br><br><div>f. create new text file code.py in assets directory.<br><br><div>3. code.py<br><br><div>a. get current service maintained by cle<div><br>SrvGroup = libstarpy._GetSrvGroup()<div>Service = SrvGroup._GetService(&quot;&quot;,&quot;&quot;)<br><div><br>b. get current activity, which is created by wrapandroid at init stage<div><br>StarActivity = Service.ActivityClass.getCurrent();<br><div><br>from now on, we can create gui elements. The first element should be layout element, which may be linear layout, absolute layout, etc. In this example, we create linear layout, which contains an edit text and buttons.&nbsp;<br><div><br>c. create root layout<br><div><br>MyLayout = Service.LinearLayoutClass._New(StarActivity);<div>MyLayout.setOrientation(&quot;VERTICAL&quot;);<br><div><br>d. create title layout and gui elements<br><div><br>MyTitleLayout = Service.LinearLayoutClass._New(MyLayout);<div>MyTitleLayout.setLinearLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);<br><div>UrlEdit = Service.EditTextClass._New(MyTitleLayout);<div>UrlEdit.setText(&quot;http://www.google.com&quot;);<div>UrlEdit.setLinearLayoutParams(StarActivity.getWidth()-200,Service.WRAP_CONTENT);<br><div>GoButton = Service.ButtonClass._New(MyTitleLayout);<div>GoButton.setText(&quot;go&quot;);<div>GoButton.setLinearLayoutParams(100,Service.FILL_PARENT);<div>def GoButton_onClick(self,ev) :<div>&nbsp; &nbsp; global MyWebView;<div>&nbsp; &nbsp; MyWebView.loadUrl(UrlEdit.getText());<div>GoButton.onClick = GoButton_onClick; &nbsp; &nbsp;<div>&nbsp;<div>ExitButton = Service.ButtonClass._New(MyTitleLayout);<div>ExitButton.setText(&quot;exit&quot;);<div>ExitButton.setLinearLayoutParams(100,Service.FILL_PARENT);<div>def ExitButton_onClick(self,ev) :<div>&nbsp; &nbsp; global StarActivity;<div>&nbsp; &nbsp; StarActivity.exit(0);<div>ExitButton.onClick = ExitButton_onClick; &nbsp;&nbsp;<br><div><br>e. create webview layout and webview instance<br><div><br>MyTitleLayout1 = Service.LinearLayoutClass._New(MyLayout);<div>MyTitleLayout1.setLinearLayoutParams(Service.FILL_PARENT,Service.FILL_PARENT);<br><div>MyWebView = Service.WebViewClass._New(MyTitleLayout1)<div>MyWebView.setLinearLayoutParams(Service.FILL_PARENT,Service.FILL_PARENT);<div>MyWebSettings = MyWebView.getSettings();<div>MyWebSettings.setJavaScriptEnabled(True);<div>MyWebSettings._Free();<br><br><div>The screenshot is as shown below:<br><br><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/95/l/vP_CmXL9OQyYuL3RnCyMjw.jpg" title="introduction_screenshot" id="0"><br><br>examples can be download from : http://www.srplab.com/android/pythongui_introduction.rar<br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Sun, 15 Apr 2012 17:05:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Let android support multiple programming languages]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/634643</guid>
  <link>http://blog.yahoo.com/srplab/articles/634643</link>
  <description><![CDATA[<div><div style="text-align:left;">&nbsp; &nbsp; Java is used as main language on android to write applications. Although ndk is provided, but it is difficult to write native GUI applications. Can we write programs with other languages? The answer is yes. We can wrap android java classes to expose to special languages, and then uses that language to develop applications. The work should be done once instead of doing it for each language again and again. Using middleware named CLE(common language extension) is able to achieve this. Using CLE, all the work needed to do is encapsulated android class into cle objects, then, multiple languages are supported through cle interface.<div><div style="text-align:left;">&nbsp; &nbsp; Using a figure to illustrate, as shown below:<div><div style="text-align:left;">&nbsp;<div class="img-wrap " style="text-align:center;"><div style="text-align:left;"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/55/l/dXlFBF6AfLrFtBf_Z3JDLg.jpg" title="wrapandroid_pic1" id="0"><div><div style="text-align:left;"><strong style="color:#0000bf;">Wrapping an android class into cle object, there are four steps:</strong><div><div style="text-align:left;">1.&nbsp;&nbsp;&nbsp;&nbsp;Create CLE service and object description<div><div style="text-align:left;">2.&nbsp;&nbsp;&nbsp;&nbsp;Implement Object’s OnCreate and OnDestroy event. In procedure of OnCreate event, we create and record a corresponding instance of android class, and OnDestroy event, we free the instance.<div><div style="text-align:left;">3.&nbsp;&nbsp;&nbsp;&nbsp;In the handler of OnCreate event, after created the android instance, we add event listeners to it. When the listener is triggered, we generate cle event, and the event will be routed by cle to application layer.<div><div style="text-align:left;">4.&nbsp;&nbsp;&nbsp;&nbsp;Write interface functions, for each function, we first get the recorded android instance, and then calls the corresponding functions of the instance.<div><div style="text-align:left;"><br><div style="text-align:left;">&nbsp; &nbsp; &nbsp; &nbsp; Based on the support of cle, the work is relatively simple. In this process, we can use ecllipse and java language to write and test. After finish, the result can be called with lua, python, or native c++.<div><div style="text-align:left;">&nbsp; &nbsp; Here uses a simple example to explain the above procedure in details. In this example, we encapsulate a button class.<div><div style="text-align:left;">1.&nbsp;&nbsp;&nbsp;&nbsp;Create CLE service and object description, we can do this with srpdebug tools.<div><div><span style="background-color:#d0d0d0;"><!--?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?--></span><div><span style="background-color:#d0d0d0;"></span><div><div style="text-align:left;"><br><div><span style="background-color:#d0d0d0;"><!--?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?--></span><div><span style="background-color:#d0d0d0;"></span><div><div>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;<div>&lt;service ID=&quot;0d0af7b9-32f3-4708-ab91-c32f57e15dca&quot; Password=&quot;123&quot; Name=&quot;WrapAndroidService&quot;&gt;<div>&nbsp; &nbsp; &lt;sysrootitem&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &lt;BasicServiceItem ID=&quot;c15c5dfa-253f-4198-b1c3-34473487fb90&quot; NameID=&quot;cb0338e8-e22f-48c0-931e-01b97502e374&quot;&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;object&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;AndroidBaseClass ID=&quot;e51b7626-160e-485e-b4ee-1f37686736dd&quot;&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;attribute&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;AndroidRefCount Type=&quot;VS_INT32&quot; SyncFlag=&quot;1&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/attribute&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;function&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;incAndroidRef ID=&quot;b4b43eb0-ec45-44aa-8af7-93b3f09a847c&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;decAndroidRef ID=&quot;d7f5f007-b86f-4fd9-bc8f-86e1e5285e3b&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;getActivity ID=&quot;e183d80f-1544-447a-928a-83702f475e0d&quot;&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;output Type=&quot;VS_OBJPTR&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/getActivity&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/function&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/AndroidBaseClass&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ViewClass ID=&quot;2f669713-d0e6-484b-9473-5c50afd8bbd2&quot; Class=&quot;AndroidBaseClass&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ActivityClass ID=&quot;16371687-dc7e-4177-a7cc-76fd118c9c3c&quot; Class=&quot;AndroidBaseClass&quot;&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;attribute&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ViewGroupQueue Type=&quot;void *&quot; Class=&quot;AbsoluteLayoutClass&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/attribute&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;function&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;runScript ID=&quot;1d7668e4-b7e2-4c85-99cd-3cdb11ce5438&quot;&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input Name=&quot;scriptInterface&quot; Type=&quot;VS_CHAR *&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input Name=&quot;script&quot; Type=&quot;VS_CHAR *&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;output Type=&quot;VS_BOOL&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/runScript&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;DoFile ID=&quot;542be0a5-bd21-4765-b004-5e336587367c&quot;&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input Name=&quot;scriptInterface&quot; Type=&quot;VS_CHAR *&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input Name=&quot;path&quot; Type=&quot;VS_CHAR *&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;output Type=&quot;VS_BOOL&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/DoFile&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;DoAssetsFile ID=&quot;e894f038-1776-47c3-a6ec-802e5b86403c&quot;&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input Name=&quot;scriptInterface&quot; Type=&quot;VS_CHAR *&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input Name=&quot;path&quot; Type=&quot;VS_CHAR *&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;output Type=&quot;VS_BOOL&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/DoAssetsFile&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;pushActivity ID=&quot;b8b252ec-e3e7-4194-a557-4f98a5e43c22&quot;&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input Name=&quot;activity&quot; Type=&quot;VS_OBJPTR&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/pushActivity&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;popActivity ID=&quot;e024762e-cd83-4032-917b-9984bd91d725&quot;&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;output Type=&quot;VS_OBJPTR&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/popActivity&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;getCurrent ID=&quot;cfcabd6e-028d-45b4-9e34-ebe7dc4c56b6&quot;&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;output Type=&quot;VS_OBJPTR&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/getCurrent&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/function&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/ActivityClass&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;AbsoluteLayoutClass ID=&quot;155920e1-fdaa-489f-b4e9-4e3ac4cf5d7e&quot; Class=&quot;ViewClass&quot;&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;attribute&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ViewQueue Type=&quot;void *&quot; Class=&quot;ViewClass&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/attribute&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/AbsoluteLayoutClass&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ButtonClass ID=&quot;f12ab1a2-2694-4165-8b4f-889b8f9aa872&quot; Class=&quot;ViewClass&quot;&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;function&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;setText ID=&quot;1c6e8c3b-42db-41b9-988d-b7fb540b93be&quot;&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input Name=&quot;text&quot; Type=&quot;VS_CHAR *&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/setText&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/function&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;outevent&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;onClick ID=&quot;328adf46-b65a-49d4-a10a-bd308552de05&quot; /&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/outevent&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/ButtonClass&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/object&gt;<div>&nbsp; &nbsp; &nbsp; &nbsp; &lt;/BasicServiceItem&gt;<div>&nbsp; &nbsp; &lt;/sysrootitem&gt;<div>&lt;/service&gt;<div><div><div><div><div><span style="background-color:#d0d0d0;"></span><br><div><div><div><div><div><span style="background-color:#d0d0d0;"><div style="text-align:left;"><br><div><div style="text-align:left;">2.&nbsp;&nbsp;&nbsp;&nbsp;OnCreate event of button<div><div style="text-align:left;">/* _OnCreate Event */<div><div style="text-align:left;"><span style="background-color:#d0d0d0;">public Object[] _OnCreate(Hashtable Ev){</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">StarObjectClass self = (StarObjectClass)Ev.get(&quot;_DesObject&quot;);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; StarObjectClass parent = (StarObjectClass)self._Get(&quot;_Parent&quot;);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">StarObjectClass activity = (StarObjectClass)self._Call(&quot;getActivity&quot;); &nbsp;&nbsp;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">/* create android button instance */</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">StarCLEButton button = new StarCLEButton((Activity)WrapAndroidClass.GetAndroidObject(activity,&quot;AndroidObject&quot;),self); &nbsp;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">/* record android button instance */</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">WrapAndroidClass.SetAndroidObject(self,&quot;AndroidObject&quot;,(Object)button);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">/* set android button instance’s parent */</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">if( parent != null ){</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; if( activity == parent ){</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Activity android_activity = (Activity)WrapAndroidClass.GetAndroidObject(parent,&quot;AndroidObject&quot;);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android_activity.setContentView(button);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; }else{</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">ViewGroup android_viewgroup = (ViewGroup)WrapAndroidClass.GetAndroidObject(parent,&quot;AndroidObject&quot;);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">android_viewgroup.addView(button);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; }</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; self._LockGC();</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; self._Call(&quot;decAndroidRef&quot;); &nbsp;// release with parent</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">}</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">/* Add event listener */</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; button.setOnClickListener(new View.OnClickListener(){&nbsp;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">public void onClick(View v) {</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">StarObjectClass self = ((BasicAndroidInterface)v).getBasicAndroid().getStarObject();</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">/* trigger CLE object’s event onClick */</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">self._ProcessEvent(&quot;onClick&quot;);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">}&nbsp;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">}); &nbsp; &nbsp; &nbsp; &nbsp;</span><span class="Apple-tab-span" style="background-color:#d0d0d0;white-space:pre;">		</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">return null;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">} &nbsp;</span><div><div style="text-align:left;">3.&nbsp;&nbsp;&nbsp;&nbsp;set Text function of Button.<div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">public void setText(StarObjectClass self,String Text){</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; /*get recorded android instance*/</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">TextView textview = (TextView)WrapAndroidClass.GetAndroidObject(self,&quot;AndroidObject&quot;);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">if( textview != null )</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; /*call android instance’s function setText*/</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">textview.setText(Text);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">}</span><div><div style="text-align:left;">4.&nbsp;&nbsp;&nbsp;&nbsp;export to jar files: “wrapandroid.jar”<div><div style="text-align:left;">Finish the above steps, we can obtain jar files “wrapandroid.jar” and service description files “WrapAndroidService.xml”. And then, we can write programs with other languages using eclipse.<div><div style="text-align:left;"><strong style="color:#800000;">5.	Programming with lua.</strong><strong><div style="text-align:left;"><span style="color:#800000;"><br></span><div><div style="text-align:left;">a.&nbsp;&nbsp;&nbsp;&nbsp;Create android project named “myluaexample”<div><div style="text-align:left;">b.&nbsp;&nbsp;&nbsp;&nbsp;copy “wrapandroid.jar” and “starcore_android_r5.jar” to your project directory. add jar library to your project.<div><div style="text-align:left;">&nbsp;<div><div style="text-align:left;">c.&nbsp;&nbsp;&nbsp;&nbsp;Copy “WrapAndroidService.xml” to assets directory of your project<div><div style="text-align:left;">d.&nbsp;&nbsp;&nbsp;&nbsp;Add Permission<br><br><div><div style="text-align:left;"><div>&nbsp; &nbsp; &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;<div>&nbsp; &nbsp; &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;&gt;&lt;/uses-permission&gt;<br><br><div><div style="text-align:left;"><div><div style="text-align:left;">e.&nbsp;&nbsp;&nbsp;&nbsp;Create code.lua file in asserts directory, input text<div><div style="text-align:left;"><span style="background-color:#d0d0d0;">SrvGroup = libstarcore._GetSrvGroup()</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">Service = SrvGroup:_GetService(&quot;&quot;,&quot;&quot;)</span><div><div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">--get activity</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">StarActivity = Service.ActivityClass.getCurrent();</span><div><div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">--create AbsoluteLayout &nbsp; &nbsp; &nbsp; &nbsp;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">MyLayout = Service.AbsoluteLayoutClass:_New(StarActivity);</span><div><div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">--create Button</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">MyButton = Service.ButtonClass:_New(MyLayout);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">MyButton:setText(&quot;Hello&quot;);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">function MyButton:onClick(Event)</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; self:setText(&quot;Is Clicked&quot;);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">end</span><div><div style="text-align:left;">f.&nbsp;&nbsp;&nbsp;&nbsp;Edit “MyluaexampleActivity”, as follow:<div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; @Override</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">// &nbsp; &nbsp; &nbsp; &nbsp;setContentView(R.layout.main);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; StarActivity._Call(&quot;DoAssetsFile&quot;, &quot;lua&quot;, &quot;code.lua&quot;);&nbsp;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">}</span><div><div style="text-align:left;"><span style="color:#800000;">6.&nbsp;&nbsp;&nbsp;&nbsp;Programming with python.</span><div><div style="text-align:left;">a.&nbsp;&nbsp;&nbsp;&nbsp;Install SL4A<div><div style="text-align:left;">b.&nbsp;&nbsp;&nbsp;&nbsp;Create android project named “mypythonexample”<div><div style="text-align:left;">c.&nbsp;&nbsp;&nbsp;&nbsp;Create code.py file in asserts directory, input text<div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">SrvGroup = libstarpy._GetSrvGroup()</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">Service = SrvGroup._GetService(&quot;&quot;,&quot;&quot;)</span><div><div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">#--get activity</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">StarActivity = Service.ActivityClass.getCurrent();</span><div><div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">#--create AbsoluteLayout &nbsp; &nbsp; &nbsp; &nbsp;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">MyLayout = Service.AbsoluteLayoutClass._New(StarActivity);</span><div><div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">#--create Button</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">MyButton = Service.ButtonClass._New(MyLayout);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">MyButton.setText(&quot;Hello&quot;);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">def MyButton_onClick(self,Event) :</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; self.setText(&quot;Is Clicked&quot;);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">MyButton.onClick = MyButton_onClick</span><span style="background-color:#d0d0d0;"><div style="text-align:left;"><br><div><div style="text-align:left;">d.&nbsp;&nbsp;&nbsp;&nbsp;Edit “MypythonexampleActivity”, as follow:<div><div style="text-align:left;"><br><span style="background-color:#d0d0d0;"><div style="text-align:left;">public class MypythonexampleActivity extends WrapAndroidActivity {<div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; /** Called when the activity is first created. */</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; @Override</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; //setContentView(R.layout.main);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; StarActivity._Call(&quot;DoAssetsFile&quot;, &quot;python&quot;, &quot;code.py&quot;);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; }</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">}</span><div><div style="text-align:left;">other steps are same as lua example.<div><div style="text-align:left;"><span style="color:#800000;">7.&nbsp;&nbsp;&nbsp;&nbsp;Programming with native c.</span><div><div style="text-align:left;">a.&nbsp;&nbsp;&nbsp;&nbsp;Create android project named “mycexample”<div><div style="text-align:left;">b.&nbsp;&nbsp;&nbsp;&nbsp;Create jni directory, enter jni, and create header files from xml file, using command<div><div style="text-align:left;"><span style="color:#6000bf;">star2h ..&#92;assets&#92;WrapAndroidService.xml</span><div><div style="text-align:left;">c.&nbsp;&nbsp;&nbsp;&nbsp;create Code.cpp source file<div><div style="text-align:left;"><span style="background-color:#d0d0d0;">#include &quot;WrapAndroidService_VSDHeader.h&quot;</span><div><div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">static class ClassOfSRPInterface *SRPInterface;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">static void *StarActivity;</span><div><div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">static VS_INT32 MyButton_onClick(VS_ULONG FunctionChoice,void *EventPara)</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">{</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; VS_EVENTPARAM *EventParam;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp;&nbsp;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; EventParam = (VS_EVENTPARAM *)EventPara; &nbsp; &nbsp;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; SRPInterface -&gt; ScriptCall(EventParam-&gt;SrcObject,NULL,&quot;setText&quot;,&quot;(s)&quot;,&quot;Is Clicked&quot;);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; return 0;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">}</span><div><div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">VS_BOOL StarCoreService_Init(class ClassOfStarCore *starcore)</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">{</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; class ClassOfBasicSRPInterface *BasicSRPInterface;</span><div><span style="background-color:#d0d0d0;">	</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; //--init star core</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; BasicSRPInterface = starcore -&gt;GetBasicInterface(); &nbsp; &nbsp;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; SRPInterface = BasicSRPInterface -&gt;GetSRPInterface(BasicSRPInterface-&gt;QueryActiveService(NULL),&quot;&quot;,&quot;&quot;);</span><div><span style="background-color:#d0d0d0;">		</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; void *ActivityClass;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; ActivityClass = SRPInterface -&gt; GetObjectEx(NULL,&quot;ActivityClass&quot;);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; StarActivity = (void *)SRPInterface -&gt; ScriptCall(ActivityClass,NULL,&quot;getCurrent&quot;,&quot;()O&quot;);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; SRPInterface -&gt; Print(&quot;Get Main Activity = %s&quot;, SRPInterface -&gt; GetName(StarActivity)); &nbsp; &nbsp;</span><div><span style="background-color:#d0d0d0;">	</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; //--create AbsoluteLayout &nbsp; &nbsp; &nbsp; &nbsp;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; void *MyLayout = SRPInterface-&gt;MallocObject(StarActivity,VSATTRINDEX_ACTIVITYCLASS_VIEWGROUPQUEUE,&VSOBJID_AbsoluteLayoutClass,0,NULL);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; //--create Button</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; void *MyButton = SRPInterface-&gt;MallocObject(MyLayout,VSATTRINDEX_ABSOLUTELAYOUTCLASS_VIEWQUEUE,&VSOBJID_ButtonClass,0,NULL);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; SRPInterface -&gt; ScriptCall(MyButton,NULL,&quot;setText&quot;,&quot;(s)&quot;,&quot;Hello&quot;);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; SRPInterface -&gt; RegEventFunction(MyButton,&VSOUTEVENTID_ButtonClass_onClick,MyButton,(void *)MyButton_onClick,0);</span><div><span style="background-color:#d0d0d0;">	</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; return VS_TRUE;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">}</span><div><div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">void StarCoreService_Term(class ClassOfStarCore *starcore)</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">{</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; SRPInterface -&gt; Release();</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; return;</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">}</span><span style="background-color:#d0d0d0;"><div style="text-align:left;"><br><div><div style="text-align:left;">d.&nbsp;&nbsp;&nbsp;&nbsp;create Android.mk.<div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">LOCAL_PATH := $(call my-dir)</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">include $(CLEAR_VARS)</span><div><div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;"># Here we give our module name and sourcefile(s)</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">LOCAL_CFLAGS += -Wno-write-strings -DENV_ANDROID</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">LOCAL_CPPFLAGS += -Wno-write-strings -fexceptions -DENV_ANDROID</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">LOCAL_LDFLAGS += -Wno-write-strings -DENV_ANDROID</span><div><div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">LOCAL_C_INCLUDES += cle_files/include</span><div><div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">#--------source file</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">MODULE_CXXSRCS := Code.cpp WrapAndroidService_UUIDDef.cpp</span><div><div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">LOCAL_SRC_FILES := ${MODULE_CXXSRCS}</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">LOCAL_LDLIBS := cle_files/libs/armeabi/libstarlib.a</span><div><div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">LOCAL_MODULE &nbsp;:= Code</span><div><div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">include $(BUILD_SHARED_LIBRARY) &nbsp;</span><div><div style="text-align:left;">e.&nbsp;&nbsp;&nbsp;&nbsp;Edit “MycexampleActivity”, as follow:<div style="text-align:left;"><br><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">public class MycexampleActivity extends WrapAndroidActivity {</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; /** Called when the activity is first created. */</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; @Override</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; //setContentView(R.layout.main);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; StarActivity._Call(&quot;DoFile&quot;,&quot;&quot;,&quot;/data/data/com.srplab.mycexample/lib/libCode.so&quot;);</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">&nbsp; &nbsp; }</span><div><div style="text-align:left;"><span style="background-color:#d0d0d0;">}</span><div><div class="img-wrap "><div style="text-align:left;"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/56/l/tDCnQT2HUhucrJUfBiCj5Q.jpg" title="device-2012-04-11-204325" id="0"><div style="text-align:left;"><br><div><div style="text-align:left;">The example can be download from :<div><div style="text-align:left;">http://www.srplab.com/android/wrapandroid.rar<div style="text-align:left;">An open source project http://code.google.com/p/wrapandroid-for-multilanguage is in progress. Anyone who has interest can join the project.&nbsp;</div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></span></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></span></div></div></div></div></div></span></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></strong></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></span></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Wed, 11 Apr 2012 20:58:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Calling C/C++ from java android using CLE]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/627801</guid>
  <link>http://blog.yahoo.com/srplab/articles/627801</link>
  <description><![CDATA[&nbsp; &nbsp; &nbsp; &nbsp; There are lots of resources written in C/C++ languages. But on android platform, Java is major language. Developers have to using JNI to wrap the interface of c/c++ codes. JNI is suitable for simple case. For more complicate application, programmers have to maintain references of c++ objects to java objects, process callback functions from c/c++ to java, etc, which are not easy. Using CLE, these works will be done by CLE, programmers only need to focus on specific functions.<div>&nbsp;&nbsp;&nbsp;&nbsp;For java calls c/c++ using CLE, method is not directly as java calls scripts, such as lua, python, etc. Because, script languages are all have reflection mechanism. We can get functions and attributes definition dynamically. But for C/C++, there has no similar mechanism. All definitions in source code will be compiled into binary code. Therefore, we need descript functions and attributes of objects first, if CLE is used.<div>&nbsp;&nbsp;&nbsp;&nbsp;There are two methods to descript object’s attributes and functions.<br><br><div><strong style="color:#0060bf;">First method, using interface functions of CLE.</strong><br><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;void *AtomicClass,*Add_AtomicFunction,*Print_AtomicFunction;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;AtomicClass = SRPInterface -&amp;gt;CreateAtomicObjectSimple(&quot;TestItem&quot;,&quot;TestClass&quot;,&quot;VS_INT32 CValue&quot;,NULL,NULL);</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;Add_AtomicFunction = SRPInterface -&amp;gt;CreateAtomicFunctionSimple(AtomicClass,&quot;CAdd&quot;,&quot;VS_INT32 CAdd(VS_INT32 x,VS_INT32 y);&quot;,NULL,NULL,VS_FALSE,VS_FALSE);</span><div><span style="background-color:#e1c4a8;"><span style="background-color:#e6e6e6;"><span style="background-color:#b9b9b9;">&nbsp;&nbsp;&nbsp;&nbsp;Print_AtomicFunction = SRPInterface -&amp;gt;CreateAtomicFunctionSimple(AtomicClass,&quot;CPrint&quot;,&quot;void CPrint(VS_INT32 x,VS_INT32 y);&quot;,NULL,NULL,VS_FALSE,VS_FALSE);</span>&nbsp;&nbsp;&nbsp;&nbsp;</span><br></span><br><div><strong style="color:#0060bf;">Second method, using xml string generates object’s attributes and functions.<br></strong><br><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;void *AtomicClass,*Add_AtomicFunction,*Print_AtomicFunction;<br></span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;class ClassOfSRPSXMLInterface *SXMLInterface;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;SXMLInterface = BasicSRPInterface -&amp;gt; GetSXMLInterface();</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;SXMLInterface -&amp;gt; LoadFromBuf(</span><div><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &amp;lt;?xml version=&#92;&quot;1.0&#92;&quot; encoding=&#92;&quot;utf-8&#92;&quot; ?&amp;gt;&#92;n&quot;&nbsp;&nbsp;&nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &amp;lt;sysrootitem&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;TestItem&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;object&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;TestClass ID=&#92;&quot;ed51e615-e548-442e-8802-a99b2fa8fe15&#92;&quot;&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;attribute&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;CValue Type=&#92;&quot;VS_INT32&#92;&quot; /&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;/attribute&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;function&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;CAdd ID=&#92;&quot;f402fd47-8bea-4136-b7f7-45bb7fb51b14&#92;&quot;&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;input Name=&#92;&quot;x&#92;&quot; Type=&#92;&quot;VS_INT32&#92;&quot; /&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;input Name=&#92;&quot;y&#92;&quot; Type=&#92;&quot;VS_INT32&#92;&quot; /&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;output Type=&#92;&quot;VS_INT32&#92;&quot; /&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;/CAdd&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;CPrint ID=&#92;&quot;da44c24e-f20a-46e3-8d37-49c3b90c6c05&#92;&quot;&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;input Name=&#92;&quot;x&#92;&quot; Type=&#92;&quot;VS_INT32&#92;&quot; /&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;input Name=&#92;&quot;y&#92;&quot; Type=&#92;&quot;VS_INT32&#92;&quot; /&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;/CPrint&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;/function&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;/TestClass&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;/object&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &nbsp; &nbsp; &nbsp;&amp;lt;/TestItem&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&quot; &nbsp; &amp;lt;/sysrootitem&amp;gt;&#92;n&quot;</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; ,NULL);</span><div><div><div><span style="background-color:#e6e6e6;"><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&amp;gt; XmlToSysRootItem(SXMLInterface,NULL,&quot;&quot;,NULL,0);</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; AtomicClass = SRPInterface -&amp;gt;GetAtomicObject(_UUIDPTR(&quot;ed51e615-e548-442e-8802-a99b2fa8fe15&quot;));</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;Add_AtomicFunction = SRPInterface -&amp;gt;GetAtomicFunction(_UUIDPTR(&quot;f402fd47-8bea-4136-b7f7-45bb7fb51b14&quot;));</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;Print_AtomicFunction = SRPInterface -&amp;gt;GetAtomicFunction(_UUIDPTR(&quot;da44c24e-f20a-46e3-8d37-49c3b90c6c05&quot;));</span><br><div><br><strong style="color:#0060bf;">After finish object’s attributes and functions, we can attach functions body address:</strong><br><div><br><span style="background-color:#e6e6e6;">&nbsp; &nbsp; //---Set Function Address</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&amp;gt; SetAtomicFunction(Add_AtomicFunction,(void *)CAdd);</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&amp;gt; SetAtomicFunction(Print_AtomicFunction,(void *)CPrint);</span><br><div><br><strong style="color:#0060bf;">By now, the functions and attributes can be accessed from java.</strong><br><div><br><strong style="color:#00bfbf;">java code:</strong><div>StarObjectClass a = Service._GetObject(&quot;TestClass&quot;)._New();<div>a._Call(&quot;CAdd&quot;,12,34));<br><br><div><strong style="color:#00bfbf;">Callback of c/c++ to java:</strong><br><br><div>java call back function:<div>StarObjectClass a = Service._GetObject(&quot;TestClass&quot;)._New()._Assign(new StarObjectClass(){<div><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;public int JavaAdd(StarObjectClass self,int x,int y){<div><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return x+y;<div><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;}<div><span class="Apple-tab-span" style="white-space:pre;">		</span>});<br><div>c code:<br><div>&nbsp;&nbsp;&nbsp;&nbsp;VS_INT32 ScriptStack;<div>&nbsp;&nbsp;&nbsp;&nbsp;<div>&nbsp;&nbsp;&nbsp;&nbsp;ScriptStack = SRPInterface -&amp;gt; ScriptGetStack();&nbsp;&nbsp;&nbsp;&nbsp; //----save script stack<div>&nbsp; &nbsp; SRPInterface -&amp;gt; Print( &quot;Function result from java %d&quot;,SRPInterface -&amp;gt; ScriptCall(Object,NULL,&quot;JavaAdd&quot;,&quot;(ii)i)&quot;,x,y) );<div>&nbsp; &nbsp; SRPInterface -&amp;gt; ScriptSetStack(ScriptStack);<br><br><div><strong style="color:#0060bf;">source code ( integrate CLE with your project):</strong><div>1.&nbsp;&nbsp;&nbsp;&nbsp;Download devfiles from http://code.google.com/p/cle-for-android, and then Open Eclipse<div>2.&nbsp;&nbsp;&nbsp;&nbsp;Create project for android.<div>3.&nbsp;&nbsp;&nbsp;&nbsp;Add CLE jar&nbsp;
&nbsp;to project as follows:<div class="img-wrap "><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/43/l/KDrI8xaYtZTGySn_tQuIIA.jpg" title="calling_c_from_java_2" id="0"><div><br><div>4.&nbsp;&nbsp;&nbsp;&nbsp;c++ codes<br><br><div><span style="background-color:#e6e6e6;">#include &quot;vsopenapi.h&quot;<br></span><div><span style="background-color:#e6e6e6;">class ClassOfSRPInterface *SRPInterface;<br></span><div><span style="background-color:#e6e6e6;">struct StructOfTestClass{</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;VS_INT32 CValue;</span><div><span style="background-color:#e6e6e6;">};&nbsp;&nbsp;&nbsp;&nbsp;<br></span><div><span style="background-color:#e6e6e6;">static VS_INT32 CAdd(void *Object,VS_INT32 x,VS_INT32 y)</span><div><span style="background-color:#e6e6e6;">{</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;struct StructOfTestClass *TestClass;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;TestClass = (struct StructOfTestClass *)Object;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;TestClass -&amp;gt; CValue = 200;</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; SRPInterface -&amp;gt; Print(&quot;Call c function...&quot;);</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; return x+y;&nbsp;&nbsp;&nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">}<br></span><div><span style="background-color:#e6e6e6;">static void CPrint(void *Object,VS_INT32 x,VS_INT32 y)</span><div><span style="background-color:#e6e6e6;">{</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;VS_INT32 ScriptStack;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;ScriptStack = SRPInterface -&amp;gt; ScriptGetStack();&nbsp;&nbsp;&nbsp;&nbsp; //----save script stack</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; SRPInterface -&amp;gt; Print( &quot;Value defined in java is %d&quot;,SRPInterface -&amp;gt; ScriptGetInt(Object,&quot;JavaValue&quot;) );</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; SRPInterface -&amp;gt; Print( &quot;Function result from java %d&quot;,SRPInterface -&amp;gt; ScriptCall(Object,NULL,&quot;JavaAdd&quot;,&quot;(ii)i)&quot;,x,y) );</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; SRPInterface -&amp;gt; ScriptSetStack(ScriptStack); &nbsp; &nbsp; //----restore script stack</span><div><span style="background-color:#e6e6e6;">}<br></span><div><span style="background-color:#e6e6e6;">VS_BOOL StarCoreService_Init(class ClassOfStarCore *starcore)</span><div><span style="background-color:#e6e6e6;">{</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;void *AtomicClass,*Add_AtomicFunction,*Print_AtomicFunction;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;class ClassOfBasicSRPInterface *BasicSRPInterface;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;//--init star core</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;BasicSRPInterface = starcore -&amp;gt;GetBasicInterface();&nbsp;&nbsp;&nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface = BasicSRPInterface -&amp;gt;GetSRPInterface(BasicSRPInterface-&amp;gt;QueryActiveService(NULL),&quot;&quot;,&quot;&quot;);</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;//---Create Atomic Class, for define function, &nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;AtomicClass = SRPInterface -&amp;gt;CreateAtomicObjectSimple(&quot;TestItem&quot;,&quot;TestClass&quot;,&quot;VS_INT32 CValue&quot;,NULL,NULL);</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;Add_AtomicFunction = SRPInterface -&amp;gt;CreateAtomicFunctionSimple(AtomicClass,&quot;CAdd&quot;,&quot;VS_INT32 CAdd(VS_INT32 x,VS_INT32 y);&quot;,NULL,NULL,VS_FALSE,VS_FALSE);</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;Print_AtomicFunction = SRPInterface -&amp;gt;CreateAtomicFunctionSimple(AtomicClass,&quot;CPrint&quot;,&quot;void CPrint(VS_INT32 x,VS_INT32 y);&quot;,NULL,NULL,VS_FALSE,VS_FALSE);&nbsp;&nbsp;&nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; //---Set Function Address</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&amp;gt; SetAtomicFunction(Add_AtomicFunction,(void *)CAdd);</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&amp;gt; SetAtomicFunction(Print_AtomicFunction,(void *)CPrint);</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;return VS_TRUE;</span><div><span style="background-color:#e6e6e6;">}<br></span><div><span style="background-color:#e6e6e6;">void StarCoreService_Term(class ClassOfStarCore *starcore)</span><div><span style="background-color:#e6e6e6;">{</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&amp;gt; Release();</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp;return;</span><div><span style="background-color:#e6e6e6;">}<br></span><br><div><strong style="color:#00bfbf;">Compile code into share library using NDK, the Android.mk is:</strong><br><br><div><span style="background-color:#e6e6e6;">LOCAL_PATH := $(call my-dir)</span><div><span style="background-color:#e6e6e6;">include $(CLEAR_VARS)<br></span><div><span style="background-color:#e6e6e6;"># Here we give our module name and sourcefile(s)</span><div><span style="background-color:#e6e6e6;">LOCAL_CFLAGS += -Wno-write-strings -DENV_ANDROID</span><div><span style="background-color:#e6e6e6;">LOCAL_CPPFLAGS += -Wno-write-strings -fexceptions -DENV_ANDROID</span><div><span style="background-color:#e6e6e6;">LOCAL_LDFLAGS += -Wno-write-strings -DENV_ANDROID<br></span><div><span style="background-color:#e6e6e6;">LOCAL_C_INCLUDES += ../../cle_files/include<br></span><div><span style="background-color:#e6e6e6;">#--------source file</span><div><span style="background-color:#e6e6e6;">MODULE_CXXSRCS := AddFunction.cpp<br></span><div><span style="background-color:#e6e6e6;">LOCAL_SRC_FILES := ${MODULE_CXXSRCS}</span><div><span style="background-color:#e6e6e6;">LOCAL_LDLIBS := ../../cle_files/libs/armeabi/libstarlib.a<br></span><div><span style="background-color:#e6e6e6;">LOCAL_MODULE &nbsp;:= AddFunction<br></span><div><span style="background-color:#e6e6e6;">include $(BUILD_SHARED_LIBRARY) &nbsp;</span><br><div><br>5.&nbsp;&nbsp;&nbsp;&nbsp;Java Codes:<br><br><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><span style="background-color:#e6e6e6;"><div>package com.cle.cfromjava;<div><br><div>import android.app.Activity;<div>import android.os.Bundle;<div><br><div>import com.srplab.www.starcore.*;<div>import com.srplab.netinst.*;<div><br><div>public class CfromjavaActivity extends Activity {<div>&nbsp; &nbsp; /** Called when the activity is first created. */<div>&nbsp; &nbsp; @Override<div>&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {<div>&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);<div>&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.main);<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp; //--init CLE<div>&nbsp; &nbsp; &nbsp; &nbsp; starcore_net_inst.InstallZipFile(this,&quot;libstarcore.so&quot;,&quot;http://www.srplab.com/android/starcore_armeabi_r3.zip&quot;, &quot;/data/data/com.cle.cfromjava/files&quot;);<div>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;StarCoreFactoryPath.StarCoreShareLibraryPath = &quot;/data/data/com.cle.cfromjava/files&quot;;<div>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;StarCoreFactoryPath.StarCoreOperationPath = &quot;/data/data/com.cle.cfromjava/files&quot;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>StarCoreFactory starcore= StarCoreFactory.GetFactory();<div><span class="Apple-tab-span" style="white-space:pre;">		</span>StarServiceClass Service=starcore._InitSimple(&quot;test&quot;,&quot;123&quot;,0,0);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>Service._CheckPassword(false);<div>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp; Service._DoFile(&quot;&quot;,&quot;/data/data/com.cle.cfromjava/lib/libAddFunction.so&quot;,&quot;&quot;);<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>StarObjectClass a = Service._GetObject(&quot;TestClass&quot;)._New()._Assign(new StarObjectClass(){<div><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;public int JavaAdd(StarObjectClass self,int x,int y){<div><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;Call java function...&quot;);<div><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return x+y;<div><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;}<div><span class="Apple-tab-span" style="white-space:pre;">		</span>});<div>&nbsp; &nbsp; &nbsp; &nbsp; a._Set(&quot;JavaValue&quot;,100);<div>&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(a._Call(&quot;CAdd&quot;,12,34)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(a._Get(&quot;CValue&quot;));<span class="Apple-tab-span" style="white-space:pre;">						</span><div>&nbsp; &nbsp; &nbsp; &nbsp; a._Call(&quot;CPrint&quot;,56,78); &nbsp;&nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp; starcore._ModuleExit();<div>&nbsp; &nbsp; }<div>}<br><span style="color:#0000bf;"><strong>examples can be downloaded from 
http://www.srplab.com/android/calling_c_from_java.rar</strong></span><br><br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></span></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></span></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Sun, 4 Mar 2012 16:14:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Calling lua from java android using CLE]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/627048</guid>
  <link>http://blog.yahoo.com/srplab/articles/627048</link>
  <description><![CDATA[<div>&nbsp; &nbsp; &nbsp; &nbsp; Java is major language to develop applications on android platform. But, in some case, programmers want to call lua code from java to perform some functions. There are many articles discussing this topic. Here, we present another method, which uses CLE middleware to call lua from java application on android.<div>&nbsp; &nbsp; &nbsp; &nbsp; CLE(Common Language Extension) is developed by srplab which presents functions to aid multi-language calls between scripts, including java, python, lua, etc. A lua engine has been compiled into cle core library. Using CLE, java calls lua code becomes very easy. Architecture of CLE is as follow:&nbsp;<div><br><div class="img-wrap "><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/86/l/LPrmC_57.VjutQrFgVeeDA.jpg" title="calling_lua_from_java_1" id="0"><div><span style="color:#c00000;"><strong>Java calls lua functions:</strong></span><br><br><div><strong>lua function:</strong><div><span style="background-color:#d0d0d0;">Obj=Service:_New(&quot;TestClass&quot;);</span><div><span style="background-color:#d0d0d0;">function Obj:LuaAdd(x,y)</span><div><span style="background-color:#d0d0d0;">return x+y;</span><div><span style="background-color:#d0d0d0;">end</span><br><div><strong>java code:</strong><div><span style="background-color:#e6e6e6;">StarObjectClass a = Service._GetObject(&quot;TestClass&quot;)._New();</span><div><span style="background-color:#e6e6e6;">a._Call(&quot;LuaAdd&quot;,12,34));</span><br><div><strong style="color:#c00000;">Callback of lua to java:</strong><br><div><strong>java call back function:</strong><div><span style="background-color:#e6e6e6;">StarObjectClass a = Service._GetObject(&quot;TestClass&quot;)._New()._Assign(new StarObjectClass(){</span><div><span style="background-color:#e6e6e6;"><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;public int JavaAdd(StarObjectClass self,int x,int y){</span><div><span style="background-color:#e6e6e6;"><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return x+y;</span><div><span style="background-color:#e6e6e6;"><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;}</span><div><span style="background-color:#e6e6e6;"><span class="Apple-tab-span" style="white-space:pre;">		</span>});</span><br><div><span style="color:#111111;"><strong>lua code:</strong></span><br><div><span style="background-color:#e6e6e6;">Obj:JavaAdd(x,y)</span><br><div><strong style="color:#c00000;">Java gets object’s attributes defined in lua</strong><br><div><span style="background-color:#d0d0d0;">Lua: &nbsp; &nbsp; Obj.LuaValue = 200;</span><div><span style="background-color:#d0d0d0;">Java: &nbsp; &nbsp;a._Get(&quot;LuaValue&quot;)</span><br><div><span style="color:#c00000;"><strong>Lua gets object’s attributes defined in java</strong></span><br><div><span style="background-color:#e6e6e6;">Java &nbsp;: a._Set(&quot;JavaValue&quot;,100);</span><div><span style="background-color:#e6e6e6;">Lua &nbsp; : self.JavaValue</span><br><div><span style="color:#0000bf;"><strong>Method1 source code ( intergrate CLE with your project):</strong></span><div>1.&nbsp;&nbsp;&nbsp;&nbsp;Download devfiles from http://code.google.com/p/cle-for-android, and then Open Eclipse<div>2.&nbsp;&nbsp;&nbsp;&nbsp;Create project for android.<div>3.&nbsp;&nbsp;&nbsp;&nbsp;Add CLE libraries to project as follows:<div><div class="img-wrap "><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/87/l/Ja2k33D9PRCS760mlVzMXA.jpg" title="calling_lua_from_java_2" id="0"><br><div class="img-wrap "><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/88/l/cRNsI9hjnJ6rPj6dnmNK4Q.jpg" title="calling_lua_from_java_3" id="0"><br><br><div>4.&nbsp;&nbsp;&nbsp;&nbsp;Lua codes<br><div><span style="background-color:#e6e6e6;">SrvGroup = libstarcore._GetSrvGroup()</span><div><span style="background-color:#e6e6e6;">Service = SrvGroup:_GetService(&quot;&quot;,&quot;&quot;)</span><div><span style="background-color:#e6e6e6;"><br></span><div><span style="background-color:#e6e6e6;">--Create objects</span><div><span style="background-color:#e6e6e6;">Obj=Service:_New(&quot;TestClass&quot;);</span><div><span style="background-color:#e6e6e6;"><br></span><div><span style="background-color:#e6e6e6;">--Define functions</span><div><span style="background-color:#e6e6e6;">function Obj:LuaAdd(x,y)</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; print(&quot;Call lua function...&quot;);</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; return x+y;</span><div><span style="background-color:#e6e6e6;">end &nbsp; &nbsp;</span><div><span style="background-color:#e6e6e6;"><br></span><div><span style="background-color:#e6e6e6;">--Call java functions</span><div><span style="background-color:#e6e6e6;">function Obj:LuaPrint(x,y)</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; print( &quot;Value defined in java is &quot;,self.JavaValue );</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; print( &quot;Function result from java &quot;,self:JavaAdd(x,y) );</span><div><span style="background-color:#e6e6e6;">end</span><div><span style="background-color:#e6e6e6;"><br></span><div><span style="background-color:#e6e6e6;">--define Attributes</span><div><span style="background-color:#e6e6e6;">Obj.LuaValue = 200;</span><br><div>5.&nbsp;&nbsp;&nbsp;&nbsp;Java Codes:<br><div><span style="background-color:#e6e6e6;">package com.cle.luafromjava;</span><div><span style="background-color:#e6e6e6;"><br></span><div><span style="background-color:#e6e6e6;">import android.app.Activity;</span><div><span style="background-color:#e6e6e6;">import android.os.Bundle;</span><div><span style="background-color:#e6e6e6;">import android.content.res.AssetManager;</span><div><span style="background-color:#e6e6e6;"><br></span><div><span style="background-color:#e6e6e6;">import java.io.IOException;</span><div><span style="background-color:#e6e6e6;">import java.io.InputStream;</span><div><span style="background-color:#e6e6e6;"><br></span><div><span style="background-color:#e6e6e6;">import com.srplab.www.starcore.*;</span><div><span style="background-color:#e6e6e6;"><br></span><div><span style="background-color:#e6e6e6;">public class LuafromjavaActivity extends Activity {</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; /** Called when the activity is first created. */</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; @Override</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.main);</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; //--init CLE</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; StarCoreFactoryPath.InitDefault(Runtime.getRuntime(),&quot;/data/data/com.cle.luafromjava&quot;);</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;"><span class="Apple-tab-span" style="white-space:pre;">		</span>StarCoreFactory starcore= StarCoreFactory.GetFactory();</span><div><span style="background-color:#e6e6e6;"><span class="Apple-tab-span" style="white-space:pre;">		</span>StarServiceClass Service=starcore._InitSimple(&quot;test&quot;,&quot;123&quot;,0,0);</span><div><span style="background-color:#e6e6e6;"><span class="Apple-tab-span" style="white-space:pre;">		</span>Service._CheckPassword(false);</span><div><span style="background-color:#e6e6e6;">		</span><div><span style="background-color:#e6e6e6;"><span class="Apple-tab-span" style="white-space:pre;">		</span>AssetManager assetManager = getAssets(); &nbsp; &nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">		</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; try{</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;String luabuf;</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;InputStream dataSource = assetManager.open(&quot;code.lua&quot;);</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;int size=dataSource.available();</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;byte[] buffer=new byte[size];&nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;dataSource.read(buffer);&nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;dataSource.close(); &nbsp; &nbsp; &nbsp; &nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;luabuf=new String(buffer);</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Service._RunScript(&quot;lua&quot;,luabuf,&quot;cmd&quot;,&quot;&quot;);</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; }</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;catch(IOException e ){</span><div><span style="background-color:#e6e6e6;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;} &nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;"><span class="Apple-tab-span" style="white-space:pre;">		</span>StarObjectClass a = Service._GetObject(&quot;TestClass&quot;)._New()._Assign(new StarObjectClass(){</span><div><span style="background-color:#e6e6e6;"><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;public int JavaAdd(StarObjectClass self,int x,int y){</span><div><span style="background-color:#e6e6e6;"><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;Call java function...&quot;);</span><div><span style="background-color:#e6e6e6;"><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return x+y;</span><div><span style="background-color:#e6e6e6;"><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;}</span><div><span style="background-color:#e6e6e6;"><span class="Apple-tab-span" style="white-space:pre;">		</span>});</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; a._Set(&quot;JavaValue&quot;,100);</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(a._Get(&quot;LuaValue&quot;));<span class="Apple-tab-span" style="white-space:pre;">						</span></span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(a._Call(&quot;LuaAdd&quot;,12,34)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; a._Call(&quot;LuaPrint&quot;,56,78); &nbsp; &nbsp; &nbsp; &nbsp;</span><div><span style="background-color:#e6e6e6;">&nbsp; &nbsp; }</span><div><span style="background-color:#e6e6e6;">}</span><br><div><span style="color:#0000bf;"><strong>Method2 source code ( install cle from http://code.google.com/p/cle-for-android independently):</strong></span><br><div>1.&nbsp;&nbsp;&nbsp;&nbsp;Install CLE from http://code.google.com/p/cle-for-android/downloads/list<div>2.&nbsp;&nbsp;&nbsp;&nbsp;Download devfiles from http://code.google.com/p/cle-for-android, and then Open Eclipse<div>3.&nbsp;&nbsp;&nbsp;&nbsp;Create project for android.<div>4.&nbsp;&nbsp;&nbsp;&nbsp;Add starcore_android_r3.jar to project<div>5.<span class="Apple-tab-span" style="white-space:pre;">	</span>Lua codes<br><div><span style="color:#c00000;">same as method 1</span><div>6.&nbsp;&nbsp;&nbsp;&nbsp;Java codes<div><span style="color:#c00000;">delete the following code line. others are same as method1<br></span><div><span style="color:#c00000;">StarCoreFactoryPath.InitDefault(Runtime.getRuntime(),&quot;/data/data/com.cle.luafromjava&quot;);<br><br>examples can be downloaded from http://www.srplab.com/android/calling_lua_from_java.rar</span></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Tue, 28 Feb 2012 22:51:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Calling python from java for android using CLE and SL4A]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/626882</guid>
  <link>http://blog.yahoo.com/srplab/articles/626882</link>
  <description><![CDATA[<div>&nbsp; &nbsp; &nbsp; &nbsp; Java is major language to develop applications on android platform. But, in some case, programmers want to call python code from java to perform some functions. By now, there is no direct method to write program with python.<div>&nbsp; &nbsp; &nbsp; &nbsp; SL4A is an open project, which supports script languages including python, lua, etc. But calling python from java has not been supported directly. Programmers have to use JNI method, and native code to write interface for python. This is more difficult, you have to manage relationship between java objects and python objects, kinds of references, interface parameters conversion, and callback functions from python to java.&nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp; CLE(Common Language Extension) is a middleware which presents functions to aid multi-language calls between scripts, including java and python. Using CLE, the above problems can be solved easily. Architecture of CLE is as follow:&nbsp;<div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/81/l/l255Ec9IdfhQY9xtehdI_g.jpg" title="calling_python_from_java_1" id="0"><br><div><strong style="color:#c00000;">Java calls python functions:</strong><br><div><strong>python function:</strong><div><span style="background-color:#d0d0d0;">Obj=Service._New(&quot;TestClass&quot;);</span><div><span style="background-color:#d0d0d0;">def Obj_PythonAdd(self,x,y) :</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; return x+y;</span><div><span style="background-color:#d0d0d0;">Obj.PythonAdd = Obj_PythonAdd;</span><br><div><strong>java code:</strong><div><span style="background-color:#d0d0d0;">StarObjectClass a = Service._GetObject(&quot;TestClass&quot;)._New();</span><div><span style="background-color:#d0d0d0;">a._Call(&quot;PythonAdd&quot;,12,34));</span><br><div><strong style="color:#c00000;">Callback of python to java:</strong><br><div><strong>java call back function:</strong><div><span style="background-color:#d0d0d0;">StarObjectClass a = Service._GetObject(&quot;TestClass&quot;)._New()._Assign(new StarObjectClass(){</span><div><span style="background-color:#d0d0d0;"><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;public int JavaAdd(StarObjectClass self,int x,int y){</span><div><span style="background-color:#d0d0d0;"><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return x+y;</span><div><span style="background-color:#d0d0d0;"><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;}</span><div><span style="background-color:#d0d0d0;"><span class="Apple-tab-span" style="white-space:pre;">		</span>});</span><br><div><strong>python code:</strong><br><div><span style="background-color:#d0d0d0;">Obj.JavaAdd(x,y)</span><br><div><span style="color:#c00000;">Java gets object’s attributes defined in python</span><br><div><strong>Python</strong>: &nbsp;Obj.PythonValue = 200;<div><strong>Java</strong>: &nbsp; &nbsp;a._Get(&quot;PythonValue&quot;)<br><div><span style="color:#c00000;">Python gets object’s attributes defined in java</span><br><div><strong>Java &nbsp;</strong>: a._Set(&quot;JavaValue&quot;,100);<div><strong>Python</strong>: self.JavaValue<br><div><span style="color:#c00000;"><strong>Method1 source code ( intergrate CLE with your project):</strong></span><div>1.&nbsp;&nbsp;&nbsp;&nbsp;Install SL4A from <span style="text-decoration:underline;">http://code.google.com/p/android-scripting/downloads/list</span><div>2.&nbsp;&nbsp;&nbsp;&nbsp;Download devfiles from <span style="text-decoration:underline;">http://code.google.com/p/cle-for-android</span>, and then Open Eclipse<div>3.&nbsp;&nbsp;&nbsp;&nbsp;Create project for android.<div>4.&nbsp;&nbsp;&nbsp;&nbsp;Add CLE libraries to project as follows:<div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/82/l/IW.8QD7mpUcAX1BCS5U7Dg.jpg" title="calling_python_from_java_2" id="0"><br><br><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/83/l/tcygfdvtBmsmiCA4HB35jw.jpg" title="calling_python_from_java_3" id="0"><br><br><div>5.&nbsp;&nbsp;&nbsp;&nbsp;Python codes<br><div><span style="background-color:#d0d0d0;">SrvGroup = libstarpy._GetSrvGroup()</span><div><span style="background-color:#d0d0d0;">Service = SrvGroup._GetService(&quot;&quot;,&quot;&quot;)</span><div><span style="background-color:#d0d0d0;"><br></span><div><span style="background-color:#d0d0d0;">#Create objects</span><div><span style="background-color:#d0d0d0;">Obj=Service._New(&quot;TestClass&quot;);</span><div><span style="background-color:#d0d0d0;"><br></span><div><span style="background-color:#d0d0d0;">#Define functions</span><div><span style="background-color:#d0d0d0;">def Obj_PythonAdd(self,x,y) :</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; print(&quot;Call python function...&quot;);</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; return x+y;</span><div><span style="background-color:#d0d0d0;">Obj.PythonAdd = Obj_PythonAdd;</span><div><span style="background-color:#d0d0d0;"><br></span><div><span style="background-color:#d0d0d0;">#Call java functions</span><div><span style="background-color:#d0d0d0;">def Obj_PythonPrint(self,x,y) :</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; print( &quot;Value defined in java is &quot;,self.JavaValue );</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; print( &quot;Function result from java &quot;,self.JavaAdd(x,y) );</span><div><span style="background-color:#d0d0d0;">Obj.PythonPrint = Obj_PythonPrint;</span><div><span style="background-color:#d0d0d0;"><br></span><div><span style="background-color:#d0d0d0;">#define Attributes</span><div><span style="background-color:#d0d0d0;">Obj.PythonValue = 200;</span><br><div>6.&nbsp;&nbsp;&nbsp;&nbsp;Java Codes:<br><div><span style="background-color:#d0d0d0;">package com.cle.pythonfromjava;</span><div><span style="background-color:#d0d0d0;"><br></span><div><span style="background-color:#d0d0d0;">import android.app.Activity;</span><div><span style="background-color:#d0d0d0;">import android.os.Bundle;</span><div><span style="background-color:#d0d0d0;">import android.content.res.AssetManager;</span><div><span style="background-color:#d0d0d0;"><br></span><div><span style="background-color:#d0d0d0;">import java.io.IOException;</span><div><span style="background-color:#d0d0d0;">import java.io.InputStream;</span><div><span style="background-color:#d0d0d0;"><br></span><div><span style="background-color:#d0d0d0;">import com.srplab.www.starcore.*;</span><div><span style="background-color:#d0d0d0;"><br></span><div><span style="background-color:#d0d0d0;">public class PytonfromjavaActivity extends Activity {</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; /** Called when the activity is first created. */</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; @Override</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.main);</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; //--init CLE</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; StarCoreFactoryPath.InitDefault(Runtime.getRuntime(),&quot;/data/data/com.cle.pythonfromjava&quot;);</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><div><span style="background-color:#d0d0d0;"><span class="Apple-tab-span" style="white-space:pre;">		</span>StarCoreFactory starcore= StarCoreFactory.GetFactory();</span><div><span style="background-color:#d0d0d0;"><span class="Apple-tab-span" style="white-space:pre;">		</span>StarServiceClass Service=starcore._InitSimple(&quot;test&quot;,&quot;123&quot;,0,0);</span><div><span style="background-color:#d0d0d0;"><span class="Apple-tab-span" style="white-space:pre;">		</span>Service._CheckPassword(false);</span><div><span style="background-color:#d0d0d0;">		</span><div><span style="background-color:#d0d0d0;"><span class="Apple-tab-span" style="white-space:pre;">		</span>AssetManager assetManager = getAssets(); &nbsp; &nbsp;&nbsp;</span><div><span style="background-color:#d0d0d0;">		</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; try{</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;String pythonbuf;</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;</span><div><span style="background-color:#d0d0d0;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;InputStream dataSource = assetManager.open(&quot;code.py&quot;);</span><div><span style="background-color:#d0d0d0;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;int size=dataSource.available();</span><div><span style="background-color:#d0d0d0;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;byte[] buffer=new byte[size];&nbsp;</span><div><span style="background-color:#d0d0d0;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;dataSource.read(buffer);&nbsp;</span><div><span style="background-color:#d0d0d0;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;dataSource.close(); &nbsp; &nbsp; &nbsp; &nbsp;</span><div><span style="background-color:#d0d0d0;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;pythonbuf=new String(buffer);</span><div><span style="background-color:#d0d0d0;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Service._RunScript(&quot;python&quot;,pythonbuf,&quot;cmd&quot;,&quot;&quot;);</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; }</span><div><span style="background-color:#d0d0d0;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;catch(IOException e ){</span><div><span style="background-color:#d0d0d0;">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;} &nbsp;&nbsp;</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><div><span style="background-color:#d0d0d0;"><span class="Apple-tab-span" style="white-space:pre;">		</span>StarObjectClass a = Service._GetObject(&quot;TestClass&quot;)._New()._Assign(new StarObjectClass(){</span><div><span style="background-color:#d0d0d0;"><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;public int JavaAdd(StarObjectClass self,int x,int y){</span><div><span style="background-color:#d0d0d0;"><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;Call java function...&quot;);</span><div><span style="background-color:#d0d0d0;"><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return x+y;</span><div><span style="background-color:#d0d0d0;"><span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp; &nbsp;}</span><div><span style="background-color:#d0d0d0;"><span class="Apple-tab-span" style="white-space:pre;">		</span>});</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; a._Set(&quot;JavaValue&quot;,100);</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(a._Get(&quot;PythonValue&quot;));<span class="Apple-tab-span" style="white-space:pre;">						</span></span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(a._Call(&quot;PythonAdd&quot;,12,34)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; &nbsp; &nbsp; a._Call(&quot;PythonPrint&quot;,56,78);</span><div><span style="background-color:#d0d0d0;">&nbsp; &nbsp; }</span><div><span style="background-color:#d0d0d0;">}</span><br><div><span style="color:#c00000;"><strong>Method2 source code ( install cle from http://code.google.com/p/cle-for-android independently):</strong></span><br><div>1.&nbsp;&nbsp;&nbsp;&nbsp;Install SL4A from http://code.google.com/p/android-scripting/downloads/list<div>2.&nbsp;&nbsp;&nbsp;&nbsp;Install CLE from http://code.google.com/p/cle-for-android/downloads/list<div>3.&nbsp;&nbsp;&nbsp;&nbsp;Download devfiles from http://code.google.com/p/cle-for-android, and then Open Eclipse<div>4.&nbsp;&nbsp;&nbsp;&nbsp;Create project for android.<div>5.&nbsp;&nbsp;&nbsp;&nbsp;Python codes<div>same as method 1<div>6.&nbsp;&nbsp;&nbsp;&nbsp;Java codes<br><div>delete the following code line. others are same as method1<div><span style="background-color:#d0d0d0;">StarCoreFactoryPath.InitDefault(Runtime.getRuntime(),&quot;/data/data/com.cle.pythonfromjava&quot;);<br><br>Examples may be downloaded from http://www.srplab.com/android/calling_python_from_java.rar</span></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Mon, 27 Feb 2012 23:45:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Wrapping Irrlicht  For Android]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/625094</guid>
  <link>http://blog.yahoo.com/srplab/articles/625094</link>
  <description><![CDATA[<div>&nbsp; &nbsp; &nbsp; &nbsp; Irrlicht is well-known open source 3D engine, which supports multiple platforms. It has been ported to android by Laurent Mallet, source code can be obtained from https://gitorious.org/irrlichtandroid/. The code is written in c++ language, but android mainly supports the java language. Using native c++ on android to develop applications is more difficult, and not convenient, especially to debug. For developer to use irrlicht easily, it is best to provide java interfaces.<div>&nbsp; &nbsp; &nbsp; &nbsp; The work here is wrapping irrlicht engine to enable it to provide the java interface. Traditional method for java calling native code is using the jni mechanism. But irrlicht engine is big and has many render objects. Using jni directly to wrap irrlicht will be a hard work. We have to define classes in java, create c++ code skeleton, maintain global reference of java object to irrlicht render object, process callback, etc. In order to simplify programming, CLE is used as middleware to help java to call native code.<div>&nbsp; &nbsp; &nbsp; &nbsp; CLE(common language extension) is developed by srplab( http://www.srplab.com ), which presents a general method for mixed language calls. It supports distributed object technique, which objects as medium to implement the mixed call between languages. Programming with cle, you need not care about details of special script languages, and the wrapping supports many languages such as java, lua, python, etc.<div>&nbsp; &nbsp; &nbsp; &nbsp; Wrapping irrlicht with cle, the first step is to create interface description using xml language, as follows:<blockquote><span style="background-color:#d0d0d0;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;<br>&lt;service ID=&quot;d52a1620-6a1c-401b-9c31-826793086683&quot; Password=&quot;123&quot; Name=&quot;SRPIrrlichtES2Engine&quot;&gt;<br>&nbsp;&lt;import&gt;<br>&nbsp; &lt;SRPFSEngine /&gt;<br>&lt;/import&gt;<br>&nbsp;&lt;module&gt;<br>&nbsp; &lt;SRPIrrlichtES2EngineBasicModule ID=&quot;73e4254e-1fed-4d15-970a-0756a39521ea&quot; /&gt;<br>&nbsp;&lt;/module&gt;<br>&lt;macro&gt;<br>……<br>&lt;/macro&gt;<br>&lt;struct&gt;<br>&lt;SRP3DVector ID=&quot;e41ba4f5-ef8f-477d-9c03-dafe387f04f9&quot;&gt;<br>&lt;X Type=&quot;VS_FLOAT&quot; /&gt;<br>&lt;Y Type=&quot;VS_FLOAT&quot; /&gt;<br>&lt;Z Type=&quot;VS_FLOAT&quot; /&gt;<br>&lt;/SRP3DVector&gt;<br>&lt;/struct&gt;<br>&lt;sysrootitem&gt;<br>&lt;BasicServiceItem ID=&quot;66d95cfa-ab44-481c-af86-101339687bf0&quot; NameID=&quot;71f6f33e-04b2-47cb-9487-092870c8e201&quot;&gt;<br>&lt;object&gt;<br>&lt;IrrDeviceClass ID=&quot;bb11872b-ae5e-45b7-8fd6-a0d009d1bb5d&quot; SysEvent=&quot;true&quot;&gt;<br>&lt;attribute&gt;<br>&lt;Color Type=&quot;VS_COLOR&quot; Default=&quot;&quot; EditType=&quot;3&quot; /&gt;<br>&lt;Width Type=&quot;VS_INT32&quot; Default=&quot;800&quot; /&gt;<br>&lt;Height Type=&quot;VS_INT32&quot; Default=&quot;600&quot; /&gt;<br>&lt;RenderWnd Type=&quot;VS_ULONG&quot; SyncFlag=&quot;1&quot; Default=&quot;&quot; /&gt;<br>&nbsp; &lt;/attribute&gt;<br>&lt;function&gt;<br>&lt;GetCurDevice ID=&quot;fb76adbd-13ec-423b-a7ea-f8434bf6823c&quot;&gt;<br>&lt;output Type=&quot;void *&quot; /&gt;<br>&lt;/GetCurDevice&gt;<br>&lt;Lua_GetCurDevice Type=&quot;luafunc&quot; Desc=&quot;Driver=Lua_GetCurDevice()&quot; ID=&quot;28d4732e-e36e-434a-a838-b9b29f17dc1a&quot; /&gt;<br>……<br>&lt;/function&gt;<br>&lt;/IrrDeviceClass&gt;<br>……<br>&lt;/object&gt;<br>&lt;/BasicServiceItem&gt;<br>&lt;/sysrootitem&gt;<br>&lt;/service&gt;</span></blockquote><div>&nbsp; &nbsp; &nbsp; &nbsp; The above xml description can also be created using tools srpdebug, which can be downloaded from srplab web site. After finishing the description, uses srpdebug tool or star2c command line tool to create c/c++ code skeleton.<div>&nbsp;<div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/6/l/B7LXEjk4KsD3d1eKg.RkHg.jpg" title="srpdebug" id="0"><div>&nbsp; &nbsp; &nbsp; &nbsp; Each object will be associated with one or more irrlicht objects. For above “IrrDeviceClass”, when is activated, an irrlicht device object will be created, as follows :<br><br><div>SRPIrrlichtES2EngineBasicModule_IrrDeviceClass.cpp:<div>….<div>&nbsp; &nbsp; case VSEVENT_SYSTEMEVENT_ONACTIVATE:&nbsp;<div>&nbsp; &nbsp; &nbsp; &nbsp; //TriggerForLoadFlag = LocalEventParaPtr -&gt; RequestParam -&gt; LParam;<div><span class="Apple-tab-span" style="white-space:pre;">		</span>{<div><span class="Apple-tab-span" style="white-space:pre;">			</span>IrrlichtDeviceClassLocalBuf = (struct StructOfIrrlichtDeviceClassLocalBuf *)pSRP -&gt; GetPrivateBuf( IrrDeviceClassPtr, IrrlichtDeviceClass_ClassLayer,IRRLICHTDEVICECLASS_LOCALVARINDEX, NULL );<div><span class="Apple-tab-span" style="white-space:pre;">			</span>if( IrrlichtDeviceClassLocalBuf -&gt; SRPEventReceiver == NULL )<div><span class="Apple-tab-span" style="white-space:pre;">				</span>IrrlichtDeviceClassLocalBuf -&gt; SRPEventReceiver = new class ClassOfSRPEventReceiver();<div><span class="Apple-tab-span" style="white-space:pre;">			</span>if( IrrlichtDeviceClassLocalBuf -&gt;IrrlichtDevice == NULL ){<div><span class="Apple-tab-span" style="white-space:pre;">				</span>irr::SIrrlichtCreationParameters param;&nbsp;<br><div><span class="Apple-tab-span" style="white-space:pre;">				</span>param.DriverType = video::EDT_OGLES2;<div><span class="Apple-tab-span" style="white-space:pre;">				</span>param.WindowId = (void *)IrrDeviceClassPtr -&gt;RenderWnd;<div><span class="Apple-tab-span" style="white-space:pre;">				</span>param.WindowSize = core::dimension2d&lt;s32&gt;(IrrDeviceClassPtr-&gt;Width, IrrDeviceClassPtr-&gt;Height);<div><span class="Apple-tab-span" style="white-space:pre;">				</span>param.Bits = 16;<div><span class="Apple-tab-span" style="white-space:pre;">				</span>param.Fullscreen = false;<div><span class="Apple-tab-span" style="white-space:pre;">				</span>param.Stencilbuffer = false;<div><span class="Apple-tab-span" style="white-space:pre;">				</span>param.Vsync = false;<div><span class="Apple-tab-span" style="white-space:pre;">				</span>param.AntiAlias = false;<div><span class="Apple-tab-span" style="white-space:pre;">				</span>param.HighPrecisionFPU = false;<div><span class="Apple-tab-span" style="white-space:pre;">			</span> &nbsp; &nbsp;param.EventReceiver = IrrlichtDeviceClassLocalBuf -&gt; SRPEventReceiver;<div><span class="Apple-tab-span" style="white-space:pre;">			</span> &nbsp; &nbsp;IrrlichtDeviceClassLocalBuf -&gt;IrrlichtDevice = irr::createDeviceEx(param);<div>#if( VS_OS_TYPE == VS_OS_WINDOWS)<div><span class="Apple-tab-span" style="white-space:pre;">				</span>_control87( _MCW_EM,0x801f );<div>#endif<div><span class="Apple-tab-span" style="white-space:pre;">			</span>}<span class="Apple-tab-span" style="white-space:pre;">			</span><div><span class="Apple-tab-span" style="white-space:pre;">		</span>}<div><span class="Apple-tab-span" style="white-space:pre;">		</span>pSRP -&gt;GetID(IrrDeviceClassPtr,&g_DeviceObjectID);<div><span class="Apple-tab-span" style="white-space:pre;">		</span>break;<br><div>Finish the c++ code, create project to generate SRPIrrlichtES2EngineBasicModule.dll for windows, or libSRPIrrlichtES2EngineBasicModule_android.so for android.<br><div>For java to call above library, code is as follow:<br><div>&nbsp;&nbsp;&nbsp;&nbsp;starcore= StarCoreFactory.GetFactory();<span class="Apple-tab-span" style="white-space:pre;">		</span><div>&nbsp; &nbsp; &nbsp;Service=starcore._InitSimple(&quot;test&quot;,&quot;123&quot;,0,0,&quot;SRPIrrlichtES2Engine.xml&quot;); &nbsp; &nbsp; &nbsp; &nbsp;<div>&nbsp; &nbsp; &nbsp;SrvGroup = (StarSrvGroupClass)Service._Get(&quot;_ServiceGroup&quot;);<div>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;Device=Service._GetObject(&quot;IrrDeviceClass&quot;)._New();<div>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;Device._Set(&quot;Width&quot;,800);<div>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;Device._Set(&quot;Height&quot;,480);<div>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;Device._Set(&quot;Color&quot;,0x7F7F7F7F);<div>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;Device._Active();<br><div>When Device._Active() is called, the c++ event VSEVENT_SYSTEMEVENT_ONACTIVATE will be generated, then, the corresponding irrlicht device is created.<div>&nbsp; &nbsp; An example Helloworld:<div>&nbsp;<div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/7/l/_H5kaS_x6huytVCKG3a7Qg.jpg" title="warpirrlicht_HelloWorld" id="0"><br><div>The wrapping work is in progress, you can download full source code from http://code.google.com/p/wrapirrlicht-for-android</div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Sun, 19 Feb 2012 19:37:00 +0800</pubDate>
 </item> <item><category>CodeProject</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[A simple opengl example of android using cle]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/199562</guid>
  <link>http://blog.yahoo.com/srplab/articles/199562</link>
  <description><![CDATA[<div>&nbsp;&nbsp;&nbsp;&nbsp;Using common language extension(cle) as interface middleware, you need not care about JNI. The programming may be simple. This example is copied from ndk example hello-gl2 with small change to use cle.<br><div>&nbsp;&nbsp;&nbsp;&nbsp;c code is as follows:<div><font style="background-color:#e6e6e6;">++ #include &quot;vsopenapi.h&quot;</font><div><font style="background-color:#e6e6e6;">…</font><div><font style="background-color:#e6e6e6;">-- bool setupGraphics(int w, int h) {</font><div><font style="background-color:#e6e6e6;">++ bool setupGraphics(void *object,int w, int h) {</font><div><font style="background-color:#e6e6e6;">…</font><div><font style="background-color:#e6e6e6;">}</font><div><font style="background-color:#e6e6e6;"><br></font><div><font style="background-color:#e6e6e6;">-- void renderFrame() {</font><div><font style="background-color:#e6e6e6;">++ void renderFrame(void *object) {</font><div><font style="background-color:#e6e6e6;">…</font><div><font style="background-color:#e6e6e6;">}</font><div><font style="background-color:#e6e6e6;"><br></font><div><font style="background-color:#e6e6e6;">-- extern &quot;C&quot; {</font><div><font style="background-color:#e6e6e6;">-- &nbsp; &nbsp;JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_init(JNIEnv * env, jobject obj, &nbsp;jint width, jint height);</font><div><font style="background-color:#e6e6e6;">-- &nbsp; &nbsp;JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_step(JNIEnv * env, jobject obj);</font><div><font style="background-color:#e6e6e6;">--};</font><div><font style="background-color:#e6e6e6;"><br></font><div><font style="background-color:#e6e6e6;">-- JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_init(JNIEnv * env, jobject obj, &nbsp;jint width, jint height)</font><div><font style="background-color:#e6e6e6;">-- {</font><div><font style="background-color:#e6e6e6;">-- &nbsp; &nbsp; setupGraphics(width, height);</font><div><font style="background-color:#e6e6e6;">-- }</font><div><font style="background-color:#e6e6e6;"><br></font><div><font style="background-color:#e6e6e6;">-- JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_step(JNIEnv * env, jobject obj)</font><div><font style="background-color:#e6e6e6;">-- {</font><div><font style="background-color:#e6e6e6;">-- &nbsp; &nbsp; renderFrame();</font><div><font style="background-color:#e6e6e6;">-- }</font><div><font style="background-color:#e6e6e6;"><br></font><div><font style="background-color:#e6e6e6;">++ VS_BOOL StarCoreService_Init(class ClassOfStarCore *starcore)</font><div><font style="background-color:#e6e6e6;">++ {</font><div><font style="background-color:#e6e6e6;">++&nbsp;&nbsp;&nbsp;&nbsp;void *AtomicClass,*step_AtomicFunction,*init_AtomicFunction;</font><div><font style="background-color:#e6e6e6;">++&nbsp;&nbsp;&nbsp;&nbsp;class ClassOfBasicSRPInterface *BasicSRPInterface;</font><div><font style="background-color:#e6e6e6;">++ &nbsp; &nbsp; class ClassOfSRPInterface *SRPInterface;</font><div><font style="background-color:#e6e6e6;">++&nbsp;&nbsp;&nbsp;&nbsp;</font><div><font style="background-color:#e6e6e6;">++&nbsp;&nbsp;&nbsp;&nbsp;//--init star core</font><div><font style="background-color:#e6e6e6;">++&nbsp;&nbsp;&nbsp;&nbsp;BasicSRPInterface = starcore -&gt;GetBasicInterface();&nbsp;&nbsp;&nbsp;&nbsp;</font><div><font style="background-color:#e6e6e6;">++&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface = BasicSRPInterface -&gt;GetSRPInterface(BasicSRPInterface-&gt;QueryActiveService(NULL),&quot;&quot;,&quot;&quot;);</font><div><font style="background-color:#e6e6e6;">++&nbsp;&nbsp;&nbsp;&nbsp;</font><div><font style="background-color:#e6e6e6;">++&nbsp;&nbsp;&nbsp;&nbsp;//---Create Atomic Class, for define function, no attribute&nbsp;</font><div><font style="background-color:#e6e6e6;">++&nbsp;&nbsp;&nbsp;&nbsp;AtomicClass = SRPInterface -&gt;CreateAtomicObjectSimple(&quot;TestItem&quot;,&quot;GLTestClass&quot;,NULL,NULL,NULL);</font><div><font style="background-color:#e6e6e6;">++&nbsp;&nbsp;&nbsp;&nbsp;step_AtomicFunction = SRPInterface -&gt;CreateAtomicFunctionSimple(AtomicClass,&quot;step&quot;,&quot;void step();&quot;,NULL,NULL,VS_FALSE,VS_FALSE);</font><div><font style="background-color:#e6e6e6;">++&nbsp;&nbsp;&nbsp;&nbsp;init_AtomicFunction = SRPInterface -&gt;CreateAtomicFunctionSimple(AtomicClass,&quot;init&quot;,&quot;VS_BOOL init(VS_INT32 width,VS_INT32 height);&quot;,NULL,NULL,VS_FALSE,VS_FALSE);</font><div><font style="background-color:#e6e6e6;">++ &nbsp; &nbsp; //---Set Function Address</font><div><font style="background-color:#e6e6e6;">++&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt; SetAtomicFunction(init_AtomicFunction,(void *)setupGraphics);</font><div><font style="background-color:#e6e6e6;">++&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt; SetAtomicFunction(step_AtomicFunction,(void *)renderFrame);</font><div><font style="background-color:#e6e6e6;">++&nbsp;&nbsp;&nbsp;&nbsp;SRPInterface -&gt; Release();</font><div><font style="background-color:#e6e6e6;">++&nbsp;&nbsp;&nbsp;&nbsp;return VS_TRUE;</font><div><font style="background-color:#e6e6e6;">++ }</font><div><font style="background-color:#e6e6e6;"><br></font><div><font style="background-color:#e6e6e6;">++ void StarCoreService_Term(class ClassOfStarCore *starcore)</font><div><font style="background-color:#e6e6e6;">++ {</font><div><font style="background-color:#e6e6e6;">++&nbsp;&nbsp;&nbsp;&nbsp;return;</font><div><font style="background-color:#e6e6e6;">++ }</font><br><br><div>Android.mk：<br><div><font style="background-color:#e6e6e6;">LOCAL_PATH := $(call my-dir)</font><div><font style="background-color:#e6e6e6;">include $(CLEAR_VARS)</font><div><font style="background-color:#e6e6e6;"><br></font><div><font style="background-color:#e6e6e6;"># Here we give our module name and sourcefile(s)</font><div><font style="background-color:#e6e6e6;">LOCAL_CFLAGS += -Wno-write-strings -DENV_ANDROID</font><div><font style="background-color:#e6e6e6;">LOCAL_CPPFLAGS += -Wno-write-strings -fexceptions -DENV_ANDROID</font><div><font style="background-color:#e6e6e6;">LOCAL_LDFLAGS += -Wno-write-strings -DENV_ANDROID</font><div><font style="background-color:#e6e6e6;"><br></font><div><font style="background-color:#e6e6e6;">LOCAL_C_INCLUDES += ../../../../../android/workspace/include</font><div><font style="background-color:#e6e6e6;"><br></font><div><font style="background-color:#e6e6e6;">#--------source file</font><div><font style="background-color:#e6e6e6;">MODULE_CXXSRCS := gl_code.cpp</font><div><font style="background-color:#e6e6e6;"><br></font><div><font style="background-color:#e6e6e6;">LOCAL_SRC_FILES := ${MODULE_CXXSRCS}</font><div><font style="background-color:#e6e6e6;">LOCAL_LDLIBS := ../../../../../android/workspace/libs/armeabi/libstarlib.a -llog -lGLESv2</font><div><font style="background-color:#e6e6e6;"><br></font><div><font style="background-color:#e6e6e6;">LOCAL_MODULE &nbsp;:= gltest</font><div><font style="background-color:#e6e6e6;"><br></font><div><font style="background-color:#e6e6e6;">include $(BUILD_SHARED_LIBRARY) &nbsp;</font><br><div>java code:<br><div><font style="background-color:#e6e6e6;">…</font><div><font style="background-color:#e6e6e6;">++ import com.srplab.www.starcore.*;</font><div><font style="background-color:#e6e6e6;">…</font><div><font style="background-color:#e6e6e6;">class GL2JNIView extends GLSurfaceView {</font><div><font style="background-color:#e6e6e6;">…</font><div><font style="background-color:#e6e6e6;">&nbsp; &nbsp;&nbsp;</font><div><font style="background-color:#e6e6e6;">++ &nbsp; &nbsp; StarCoreFactory starcore;</font><div><font style="background-color:#e6e6e6;">++ &nbsp; &nbsp; static StarObjectClass GlObject;</font><div><font style="background-color:#e6e6e6;">…</font><div><font style="background-color:#e6e6e6;"><br></font><div><font style="background-color:#e6e6e6;">&nbsp; &nbsp; private void init(boolean translucent, int depth, int stencil) {</font><div><font style="background-color:#e6e6e6;"><br></font><div><font style="background-color:#e6e6e6;">…</font><div><font style="background-color:#e6e6e6;">++<span class="Apple-tab-span" style="white-space:pre;">		</span> &nbsp;StarCoreFactory starcore= StarCoreFactory.GetFactory();</font><div><font style="background-color:#e6e6e6;">++ &nbsp; &nbsp; &nbsp; &nbsp;StarServiceClass Service=starcore._InitSimple(&quot;test&quot;,&quot;123&quot;,0,0,&quot;&quot;); &nbsp; &nbsp; &nbsp; &nbsp;</font><div><font style="background-color:#e6e6e6;">++ &nbsp; &nbsp; &nbsp; &nbsp;Service._CheckPassword(false);</font><div><font style="background-color:#e6e6e6;">++ &nbsp; &nbsp; &nbsp; &nbsp;Service._DoFile(&quot;&quot;,&quot;/data/data/com.srplabgl.gltest/lib/libgltest.so&quot;,&quot;&quot;); &nbsp; &nbsp; &nbsp; &nbsp;</font><div><font style="background-color:#e6e6e6;">++ &nbsp; &nbsp; &nbsp; &nbsp;GlObject = Service._GetObject(&quot;GLTestClass&quot;)._New();</font><div><font style="background-color:#e6e6e6;">… &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><div><font style="background-color:#e6e6e6;">&nbsp; &nbsp; }</font><div><font style="background-color:#e6e6e6;">…</font><div><font style="background-color:#e6e6e6;">&nbsp; &nbsp; private static class Renderer implements GLSurfaceView.Renderer {</font><div><font style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; public void onDrawFrame(GL10 gl) {</font><div><font style="background-color:#e6e6e6;">++ &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GlObject._Call(&quot;step&quot;);</font><div><font style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; }</font><div><font style="background-color:#e6e6e6;"><br></font><div><font style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; public void onSurfaceChanged(GL10 gl, int width, int height) {</font><div><font style="background-color:#e6e6e6;">++ &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GlObject._Call(&quot;init&quot;,width,height);</font><div><font style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; }</font><div><font style="background-color:#e6e6e6;"><br></font><div><font style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; public void onSurfaceCreated(GL10 gl, EGLConfig config) {</font><div><font style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Do nothing.</font><div><font style="background-color:#e6e6e6;">&nbsp; &nbsp; &nbsp; &nbsp; }</font><div><font style="background-color:#e6e6e6;">}<br></font><br>example code can be downloaded from : &nbsp;<a href="http://www.srplab.com/android/gltest.rar">http://www.srplab.com/android/gltest.rar</a> &nbsp;&nbsp;<br><br><div>&nbsp; &nbsp; In more complicated case, using jni method will be much more difficult. Because programmer have to manager references, callbacks, parameter translations. cle provides powerful and flexible method for java call other languages such as c/c++.<div>&nbsp; &nbsp; The follow picture is a wrap library for android of anti-grain geomet, which will be released soon after.<div><div class="img-wrap "><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/90/l/bzMKDyJwHtouv50_QdGlQg.jpg" title="example" id="0"><br><br><div>please visited <span><a href=" http://code.google.com/p/cle-for-android">http://code.google.com/p/cle-for-android</a></span> to get more information about cle.<br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Mon, 21 Nov 2011 21:49:00 +0800</pubDate>
 </item> <item><category>CodeProject</category>
<category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Common Language Extension : For Android]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/180613</guid>
  <link>http://blog.yahoo.com/srplab/articles/180613</link>
  <description><![CDATA[<div><div class="img-wrap ">&nbsp; &nbsp; For android version, CLE is complete free, and current version supports java in calling lua, c/c++, and python(need SL4A). CLE is very easy to use and greatly simplify the programming for java with different other languages. With the help of CLE, programmers can written c/c++ share modules without any knowledge about JNI, can call lua or python and provide callback functions of java to these languages easily.<br><div><br><div><div>&nbsp; &nbsp; CLE supports android version android 2.1/2.2/3.0/3.2/4.0. The install package includes a simple lua console.<br><br>* Main interface<br><div class="img-wrap "><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/30/l/ruvANuRdgR0F6GvRmdPXIw.jpg" title="main" id="0"><br><div>* Lua console<br><div class="img-wrap "><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/32/l/xYx0w0MpBdXTM0XHMKwzEA.jpg" title="luaconsole" id="0"><br><br><div>Project Link:&nbsp;&nbsp; <a href="http://code.google.com/p/cle-for-android">http://code.google.com/p/cle-for-android</a><br><br><br></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Tue, 15 Nov 2011 22:00:00 +0800</pubDate>
 </item> <item><category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Common Language Extension : Architecture]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/180610</guid>
  <link>http://blog.yahoo.com/srplab/articles/180610</link>
  <description><![CDATA[<div>&nbsp; &nbsp; Common Language Extension(CLE) is a realization of common object environment. It manages interface objects and presents interface to c/c++, lua, java, python, php, c#, etc. Through these interface, special language can create, define, and control interface objects, define their functions and events, or call their functions.<div>&nbsp; &nbsp; Architecture of objects in CLE is as follows,<div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/27/l/EDcPrtMBzm3boxPSttlDmA.jpg" title="Common Language Extension_Architecture_pic1" id="0"><br><div class="img-wrap">&nbsp; &nbsp; Objects are grouped into four kinds: service group object, service object, service item object, object, where service item object may be not existed if you do not develop distributed applications.&nbsp;<div class="img-wrap">&nbsp; &nbsp; Applications based on CLE, mainly is create and init the above four kinds of object. Specific functions or events are provided by object.<div class="img-wrap">The following code is a simple example for java calling lua.<div class="img-wrap"><br><div class="img-wrap"><font style="background-color:#a2a2a2;">&nbsp;&nbsp;&nbsp;&nbsp;String LuaString;<span class="Apple-tab-span" style="white-space:pre;">		</span></font><div class="img-wrap"><font style="background-color:#a2a2a2;">&nbsp;&nbsp;&nbsp;&nbsp;LuaString = &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;SrvGroup = libstarcore._GetSrvGroup()&#92;n&quot;;</font><div class="img-wrap"><font style="background-color:#a2a2a2;">&nbsp;&nbsp;&nbsp;&nbsp;LuaString = LuaString + &quot;Service = SrvGroup:_GetService(&#92;&quot;&#92;&quot;,&#92;&quot;&#92;&quot;)&#92;n&quot;;</font><div class="img-wrap"><font style="background-color:#a2a2a2;">&nbsp;&nbsp;&nbsp;&nbsp;LuaString = LuaString + &quot;Obj=Service:_New(&#92;&quot;TestClass&#92;&quot;);&#92;n&quot;;</font><div class="img-wrap"><font style="background-color:#a2a2a2;">&nbsp;&nbsp;&nbsp;&nbsp;LuaString = LuaString + &quot;function Obj:Add(x,y)&#92;n&quot;;</font><div class="img-wrap"><font style="background-color:#a2a2a2;">&nbsp;&nbsp;&nbsp;&nbsp;LuaString = LuaString + &quot; &nbsp;return x+y;&#92;n&quot;;</font><div class="img-wrap"><font style="background-color:#a2a2a2;">&nbsp;&nbsp;&nbsp;&nbsp;LuaString = LuaString + &quot;end&#92;n&quot;; &nbsp;</font><div class="img-wrap"><span class="Apple-tab-span" style="white-space:pre;"><font style="background-color:#a2a2a2;">		</font></span><div class="img-wrap"><font style="background-color:#a2a2a2;">&nbsp; &nbsp; &nbsp; &nbsp; Service._CheckPassword(false);</font><div class="img-wrap"><font style="background-color:#a2a2a2;">&nbsp;&nbsp;&nbsp;&nbsp;SrvGroup._RunScript(&quot;lua&quot;,LuaString,&quot;&quot;);</font><div class="img-wrap"><font style="background-color:#a2a2a2;">&nbsp;&nbsp;&nbsp;&nbsp;StarObjectClass Obj = Service._GetObject(&quot;TestClass&quot;)._New();</font><div class="img-wrap"><font style="background-color:#a2a2a2;">&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(Obj._Call(&quot;Add&quot;,234,567));</font>&nbsp;&nbsp;&nbsp;&nbsp;<div class="img-wrap"><br><div class="img-wrap">&nbsp;&nbsp;&nbsp;&nbsp;In above example, Interface object “TestClass” is defined by lua, for java to call its function “Add”, a new instance of TestClass is created using java language.&nbsp;<br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Tue, 15 Nov 2011 21:53:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
  <author>srplab</author>
  <title><![CDATA[Multi-Language Programming : Distributed Object]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/123492</guid>
  <link>http://blog.yahoo.com/srplab/articles/123492</link>
  <description><![CDATA[&nbsp; &nbsp; If common object enivronment is deployed on different computers, it will become a distributed object environment. For the environment holds all information and parameters of interface objects, it can act as a proxy for distributed functions of objects. As a result, applications access these distributed objects as if they are located at local, and programmers do not need face with kinds of standards or interface of different languages. For environment supports multi-language, you can developed service using java, c/c++,lua,python,etc, and call it from client side with other languages.<br><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/43/l/IPI6lgi_Mje2VHkNFBdJeQ.jpg" title="Multi-Language Programming_Distributed Object_pic1" id="0"><br>&nbsp; &nbsp; Above figure is a normal function call. If we distribute the environment on two computers, it will be a distributed environment, which is shown as follow:<br><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/52/l/cYr149aHuLe7g_qRrk4uKg.jpg" title="Multi-Language Programming_Distributed Object_pic2" id="0"><br><div class="img-wrap">&nbsp; &nbsp; The changes only occur on the environment. For applications, no matter they are client or server, may be not aware of these changes. For programmers, the environment helps them to solve many problems such as how to select standards and interfaces, or how to programming, especially in the case when client side needs to communicates with different servers of different languages.<div class="img-wrap">&nbsp; &nbsp; &nbsp;We put objects together, describe them, manage them and provide an environment to them. More and more functions can be added to the environment, rather than special languages realizing these objects. Common interface, common architecture, and all of these will simply programming and freeing programmers from meaningless work.&nbsp;<div class="img-wrap">&nbsp; &nbsp; Think about: you write a library in c/c++, then you have to rewrite it for java,python,etc at some time later. Is it meaningful?<br></div></div></div></div></div>]]></description>
  <pubDate>Sun, 30 Oct 2011 22:18:00 +0800</pubDate>
 </item> </channel>
</rss><!-- w307.asb.tw1.yahoo.com compressed/chunked Sun May 19 00:01:17 UTC 2013 -->
