<?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>Wed, 2 May 2012 21:09:00 +0800</lastBuildDate>
<sy:updatePeriod>daily</sy:updatePeriod>
<sy:updateFrequency>3</sy:updateFrequency> <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> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
  <author>srplab</author>
  <title><![CDATA[Multi-Language Programming : Simplifying web service programming]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/92563</guid>
  <link>http://blog.yahoo.com/srplab/articles/92563</link>
  <description><![CDATA[<div>&nbsp; &nbsp; Let’s go deeper into common object environment. The environment manages interface objects of multiple different languages and provides interface to these languages to define object’s attributes, functions and events. It holds all information and parameters about the objects. &nbsp; &nbsp; &nbsp; Therefore, it can act as a proxy to other applications outside to complete some functions or function calls.&nbsp;<div>&nbsp; &nbsp; Web service is a popular method of remote call, which uses SOAP as interface standard. Common object environment can implement SOAP interface, and thus become a proxy for web service. This will simply the development of web service. The applications, components or libraries developed based on the environment may not aware of web service call. There are no difference between web service calls and normal calls. Let’s use a diagram to further illustrate.<br><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/83/l/J7dvvkW8XEOPNwxmlbjthg.jpg" title="Multi-Language Programming_Simplifying web service programming_pic1" id="0"><br><div class="img-wrap">&nbsp; &nbsp; In above figure, a class object defined in java language has a function “Add”. For its c/c++ instance, application can call “Add” function through environment as previously talked about. The “Add” function of c/c++ instance can also be used as web service. In this case, the WSDL file can be generated automatically by environment proxy. Other application can get WSDL of the instance by http post request. Then, they can initiate web service call, and the proxy routing the call to the function of the class object. The class object does not know whether the call is web service call or normal function call. It only needs to implement the “Add” function body using java. Is it simple?<div class="img-wrap">&nbsp; &nbsp; Common object environment holds all information of interface. Thus, it can act as a proxy and generate WSDL file to provide web service function to outside applications, which will simply the development of web services.&nbsp;<br></div></div></div></div></div>]]></description>
  <pubDate>Wed, 26 Oct 2011 19:27:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
  <author>srplab</author>
  <title><![CDATA[Multi-Language Programming : Common Object’s Event_6]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/86478</guid>
  <link>http://blog.yahoo.com/srplab/articles/86478</link>
  <description><![CDATA[<div>&nbsp; &nbsp; &nbsp;Object may generate or handle events. This feature should continue to be supported in common object environment. Event is not an entity. It is a set of information. If the event information is described by special language, then it can not be triggered or handled by other languages. Therefore, environment should present a common model to describe events and provide a dispatcher to schedule these events.&nbsp;<div>&nbsp; &nbsp; &nbsp;In the environment, event may be identified by UUID or name. UUID uniquely corresponds to an event. Different languages can use interface of environment to find the event defined, and then trigger the event. Event may also have parameters or have response results from the handler. These parameters should be converted by environment when passed to different languages.<div>&nbsp; &nbsp; &nbsp;Common object environment should support to define object’s event and event handler. Object’s event may be handled by itself. For example, we click button, trigger a click event, then the button’s click function is called to process the event. Object’s event may also be handled by other objects. These objects can register handler function to capture events interested. These two kinds of event handling method should be supported.<div>&nbsp; &nbsp; &nbsp;Using UUID to identify an event enables events defined in one language to be found or triggered by other defined languages. For events, environment is responsible for parameter conversion between different languages, and dispatching event to objects which may be defined by different languages.<br><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/41/l/h8d86bNTeLQziW3SnM5s3w.jpg" title="Multi-Language Programming_Common Object&#39;s Event_pic1" id="0"><br><div class="img-wrap">&nbsp; &nbsp; In above figure, Click event and its handler are defined for class object of java. If a c/c++ module needs to trigger the event, it finds the event first. Then it triggers “OnClick” event. Event dispatcher is responsible for routing the event to correct handler, which will call &nbsp;OnClick_Handler of java. The handler returns results. The dispatcher translates results to c/c++ language format, and then return the results to c/c++ module.<div class="img-wrap">&nbsp; &nbsp; Summary: common object environment supports defining object attributes, functions, and events. It also supports function overloading, event dispatching, and parameters/arguments conversion between different languages. It is an object-oriented runtime environment.&nbsp;<br></div></div></div></div></div></div></div>]]></description>
  <pubDate>Sun, 23 Oct 2011 09:57:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
  <author>srplab</author>
  <title><![CDATA[Multi-Language Programming : More Complicated Function Call Between Languages_5]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/76836</guid>
  <link>http://blog.yahoo.com/srplab/articles/76836</link>
  <description><![CDATA[<div>&nbsp; &nbsp; Components or libraries provide some functions. Other modules may access these functions through their interface. In some cases, the interface functions may be simple. The caller constructs input arguments, calls the function, and gets the return value. These type interfaces are easy to define and no more restrictions. But in other cases, interface functions may be more complicated. Let’s use an example to explain in detail.&nbsp;<div>&nbsp; &nbsp; Supposing there is a c/c++ component. It provides a function to draw and fill a circle into memory pixel buffer. If the component uses a simple interface, it should take parameters as input arguments, such as line type, color, width, fill color, fill pattern, fill bitmap, etc. We define an interface object contains this function in common object environment. The function may be called by other languages, for example java.<br><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/57/l/q8EqOmMQia9ilVbtCJZzeQ.jpg" title="Multi-Language Programming_More Complicated Function Call Between Languages_pic1" id="0"><br><div class="img-wrap">&nbsp; &nbsp; In this case, there are some disadvantages. First, interface function may have many input arguments, and for each call, these arguments should be input although their values do not change. Second, if a new parameter is added, all related functions must be changed. This will increase the burden of software maintenance.<div class="img-wrap">&nbsp; &nbsp; In general, we often define two other objects: pen and brush objects. The draw function takes pen and brush object as input arguments. The pen object has attributes color, type and width, and brush object has attributes color, pattern, image, etc. We also define an interface object contains the draw function in common object environment. As result, there are three objects defined by c/c++ language in environment, which are pen, brush, and object containing the draw function. For other languages using the function, for example java, it should create instances of the three interface class objects. And the instances can be accessed by c/c++ and java language base on the support of the environment.<br><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/58/l/9aSlrxnVah9d9gnskJ8z5A.jpg" title="Multi-Language Programming_More Complicated Function Call Between Languages_pic2" id="0"><br><div class="img-wrap">&nbsp; &nbsp; In this case, the function call is more complicated. The caller needs to create instances of objects defined in c/c++ languages. The instances can be accessed by the two languages. They are used as input arguments of draw function. Without the support of common object environment, it is difficult to realize in this way, especially to support the call from applications programmed with multiple different languages.<div class="img-wrap">&nbsp; &nbsp; It is clear that the c/c++ service can be call by any other languages. Is it easy and concise? You can compare it with traditional method using JNI interface.<br><br></div></div></div></div></div></div></div></div>]]></description>
  <pubDate>Wed, 19 Oct 2011 19:22:00 +0800</pubDate>
 </item> <item><category>Multi Language Programming</category>
<category>CodeProject</category>
  <author>srplab</author>
  <title><![CDATA[Multi-Language Programming : Accessing Interface Objects Defined In Other Language_4]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/73810</guid>
  <link>http://blog.yahoo.com/srplab/articles/73810</link>
  <description><![CDATA[<div>&nbsp; &nbsp; Common object environment provides run-time support. Based on support of the environment, applications can access interface objects defined in other language. There are two methods, we introduce them one by one in detail<div>&nbsp; &nbsp; First method, application can find interface object defined in other language using the function of environment. And then, it can access object’s attributes or call object’s functions. Interface objects may be identified by its UUID or name. Object’s name should be used carefully because there may have multiple objects with the same name.<div><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/67/l/4tKTKhTKmV33eqIUr85M0Q.jpg" title="Multi-Language Programming_Accessing Objects Defined In Other Language_1" id="0"><br>&nbsp; &nbsp; Second method, for environment supporting object-oriented programming, application can create an instance of entity class object of other language as talking about previously. The instance can be accessed by the two languages simultaneously. This method is more flexible. Application can create overloading functions of class object to support callback. For examples, if an object with function “Add” is defined in python language, application in java can create an instance of it and overloading the function “Add”. Then, python can callback to java through the “Add” function.<br><div class="img-wrap"><img src="http://blog.yimg.com/3/dSj22lt7s59iDOlzFLgsqDWMsb0fx0AH6FuUauXDV.dD2QNoQNvweA--/68/l/xaOFwgutm_zv5zae.wXFow.jpg" title="Multi-Language Programming_Accessing Objects Defined In Other Language_2" id="0"><br>&nbsp; &nbsp; In above figure, python class object defines two functions “Add” and “Dec”. The “Add” function is overridden by java instance. The instance is managed by environment, and is visible to java and python. Python can call “Add” function of java instance to perform callback function. For “Dec” function, because instance does not override this function, the call will be redirected to class object of python.<br><br><br></div></div></div></div></div>]]></description>
  <pubDate>Mon, 17 Oct 2011 21:51:00 +0800</pubDate>
 </item> <item><category>Common Language Extension Programming</category>
  <author>srplab</author>
  <title><![CDATA[Common Language Extension Programming, Starting here]]></title>
  <guid isPermaLink="true">http://blog.yahoo.com/srplab/articles/67209</guid>
  <link>http://blog.yahoo.com/srplab/articles/67209</link>
  <description><![CDATA[&nbsp; &nbsp; &nbsp; Common Language Extension(CLE) is a realization of common object environment by srplab. Its idea, interface, problems, and how to use will be discussed in this category.<br>&nbsp; &nbsp; &nbsp; Please visit our site : http://www.srplab.com.<br>&nbsp; &nbsp; &nbsp;Thanks for supporting.]]></description>
  <pubDate>Sun, 16 Oct 2011 11:26:00 +0800</pubDate>
 </item> </channel>
</rss><!-- w301.asb.tw1.yahoo.com compressed/chunked Mon May 21 09:31:53 UTC 2012 -->

