2013年8月17日 星期六

About Library Dependency Manager on Android Project

You Gave Me Your Wings. 
   開發專案過程中,管理library(不論是3rd party或自製)的問題常常存在,在JAVA的世界可以用ivy、maven或Gradle(有些也包含build script的功能),而在Objective-c可以用CocoPads。

   但最近聽到蠻多人用git的submodlue來管理,連sublime text及vim的plugin都用這招管;就Android開發而言,用Maven及Ivy體系就有點綁手綁腳,因為並不是每個library都會放到repository server(除非你想自己host一個,並且手動up-to-date)。

   而git的submodlue可能會比較合適,畢竟比較好用的library大部分在Github上都有維護,例如:AndroidQueryPullToRefreshActiveAndroid等等。唯一的缺點大概是每次都要自己compile library project,時間會拉的有點長;不過ADT更新到r17時有改變link library project的policy(如下段文章所示),以後R.java及resource都不會bind到library的jar檔,這大概宣示非自己compile source code不可。

Important: If you are still referencing jar libraries manually instead of putting them under libs/ be aware of the following:
  • If the project is a Library project, these jar libraries will not be automatically visible to application projects. You should really move these to libs/
  • If the project is an application, this can work but you must make sure to mark the jar files as exported.

   至於Gradle可以多一些期盼(自己還沒有很認真在玩,但groovy語法我是很熟悉),連web framework: Grails也預計在3.0版(最近在2.3 release)後使用它!


2013年5月26日 星期日

The Free Translate API from Google

   The google translate api is not free in v2. but some way(hack) let it's work. we can sniff the Google Translate. and mimic the request, the example show as follow:

main.py
TRANSLATE_URL_PREFIX='http://translate.google.com.tw/translate_a/t?client=t&hl=zh-TW&sl=en&tl=zh-TW&ie=UTF-8&oe=UTF-8&multires=1&prev=enter&ssel=0&tsel=0&sc=1&q='
# the user agent is neccerry ...
query = "THE WORDS YOU WANT TO TRANSLATE"
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36'
headers = { 'User-Agent' : user_agent }
req = urllib2.Request(self.TRANSLATE_URL_PREFIX+query), None, headers)
res = urllib2.urlopen(req)
r = res.read()
while ',,' in r:
    r = r.replace(',,',',')
content = json.loads(r)
result = content[0][0][0]) 

The most trick is the response JSON is invalid. we should remove the wied tag : ",,".

hope it's help for you :)

2013年5月10日 星期五

[Android] The Bug of Set the different drawable for ProgressBar

   作RPG遊戲時,角色的血量通常會用ProgressBar,而比較精美的遊戲會在角色剩下20%或50%的時候給予不同的提示,例如閃爍、變紅等等這時候可以使用ProgressBar.setProgressDrawable(Drawable),將res/drawable/裡面不同的設定檔設定。

code
code
code

   但實際跑起來會發現,call setProgressDrawable後,整個progressBar就消失;這是Android的bug?!看起來是如此!目前的解決方法也很簡單,用xml裡用色票的start/center/endColor即可!

<?xml version=”1.0″ encoding=”UTF-8″?>
<shape xmlns:android=”http://schemas.android.com/apk/res/android”>
<gradient
   android:endColor=”#fff”
   android:centerColor="#aaa"
   android:startColor=”#999″
   android:type=”linear”
/>
</shape>


Reference:
  http://osdir.com/ml/Android-Developers/2010-06/msg02432.html

Pure Java java.util.zip Implementation

"Zipped"!!:)

   J2ME的世界裡,沒有java.util.zip的lib可以用(弔詭的是明明porting也需要jar decoder,還是要包一個zip lib),對於J2ME的開發環境,Jar size動不動就限制2M或4M(此時覺得nokia的偉大,隨便都16M),遊戲圖片想要多一點、華麗一點就可以宣告死刑,當然有很多種方式可以達成目的,例如:降低圖片解析度、減少code size(這有點...)、全部resource用OTA下載、有些programmer就會利用zip的方式減少Jar Size,就最後一個方案是最不傷遊戲畫面及遊戲邏輯的解決方案,該死的是J2ME沒有這個玩意!

   不過日本的J2ME(DoJa)有com.nttdocomo.util.JarInflater取代之,所以才發想此專案 - Jazzlib2me

https://github.com/bearprada/jazzlib2me

   主要還是改自jazzlib這個J2SE的solution,歡迎大家享用(不過話說寫J2ME App的Programmer應該不多了:( )




2013年5月5日 星期日

Virtual JoyStick on Android : Use AndEngine Framework



   天臨時被抓去寫Android的UI,主要是做一個藍芽控制器的界面,有點像任天堂的手把;之前自製Virtual Joystick時,一直迷失在操作搖桿的State Control以及G-sensor數值的處理(使用者體驗總是主觀阿!)。

   後來覺得,用Game Engine裡面的實作比較迅速,所以我選了AndEngine,類似的還有e3roid(後者Performance較弱,用起來會卡卡)。 AndEngine大概是一年前想寫天蠶變遊戲所接觸,直到現在也跟著進版到Android 4.0.x的Support,不過Joystick在1.6版就可執行!

最後還是要小小抱怨一下,AndEngine近版速度算快,但最不能接受的是API不能夠相容!(後來Nicolas Gramlich跑去Zynga Mobile Team,還在網站上徵人


Reference:

UI Framework in JAVA Swing


   什麼要討論已經過時以久的UI系統Swing呢?除了詭異帶點醜陋的UI元件、有點難以理解的 Layout系統、殘缺不堪的元件模組(想找個類似Android的ListView卻找不到);但它還是JAVA Desktop Application的首選(JDK/JRE Default就有的功能,相較於JavaFX,需要額外下載一包Runtime);很不幸的,我們只是需要一個皮(View)替演算法的細節視覺化,同時能夠做簡單的操作達到說明的效果,最後只好乖乖使用Swing這種old style的framework。

   用Swing拉Layout可以用Netbean IDE,但對於動態生成的情境卻是弱的可以(Android的ADT在11版就有支援),但Netbean說起來也只是幫您自動長出那些噁心的Layout Code,換句話說之後微調或修改還是需要直接碰到Source Code,而不是修改Configuration(Android的話就是layout.xml)就能直接使用;好在心灰意冷之餘,同事推薦了JavaBuilders,它把描述UI這件事情變成Yaml,並且引入別牌的LayoutManager : MigLayout,以下是官方的範例Code

 JPanel:  
   - JSplitPane(name=split1):  
     - JPanel:  
       - JPanel(name=p1,groupTitle=Titled Border): [JLabel(text=Groupbox border)]  
       - JPanel(name=p2,border=raisedEtched): [JLabel(text=Raised etched border)]  
       - JPanel(name=p3,border=loweredEtched): [JLabel(text=Lowered etched border)]  
       - JPanel(name=p4,border=raisedBevel): [JLabel(text=Raised bevel border)]  
       - JPanel(name=p5,border=loweredBevel): [JLabel(text=Lowered bevel border)]  
       - JPanel(name=p6,border=1): [JLabel(text=1 px wide border with default color)]  
       - JPanel(name=p7,border=4): [JLabel(text=4 px wide border with default color)]  
       - JPanel(name=p8,border=olive 4): [JLabel(text=4 px wide border with olive color)]  
       - JPanel(name=p9,border=orange 4): [JLabel(text=4 px wide border with orange color)]  
       - MigLayout(layoutConstraints="wrap 1"): [p1,p2,p3,p4,p5,p6,p7,p8,p9]  
     - JPanel(name=sourcePanel):  
       - JLabel(name=sourceLbl,text="YAML source:")  
       - JScrollPane(name=scroll1): JTextArea(name=source,editable=false,font=Monospaced)  
       - MigLayout: |  
         [grow]  
         sourceLbl  
         scroll1  [grow]  
   - MigLayout: |  
      [grow]  
      split1 [grow]    
 bind:  
   - source.text: this.yaml  

   JFrame/JPanel就會對應到檔案名稱相同的Yaml檔,雖然無法臻至完美,但也算是個區域最佳解,為Swing的MVC架構向前一大步。


(迷之聲:那麼難用的UI Framework不如直接寫Android!)

2013年4月29日 星期一

Mobile Backend Services


The Administrator Page on Helios Server

   開發App的同時,需要一些Server的功能來輔助,例如:Bug report、Push Message、In-app-purchase(包含購買即下載的功能)、使用者資料分析(好像有點違法)、雲端記錄檔等等。話說技術上難度並不是太高,反而多的是冗於的重工,又或者API或資料格式定義不好,導致實作的SDK有點難以理解。

通常尋求這類型解答有幾種思考模式:


第一、花錢消災型:多用第三方服務以利於快速達陣,花錢好辦事,問題有人檔,根據以上需求分別有:

Bug report : BugsenseTestFightCrashlytics
Push Message : Parse(最近被Facebook買走了)UrbanAirShip
使用者資料分析:FlurryMixpanel

   這類型服務通常各種平台的SDK都要有,後端的資料儲存有多Big Data就多Big Data,當然好看的後台管理介面同樣只是基本需求,不過每種服務都可以相互跨越、取代(能存使用者資訊當然就能夠存Bug訊息)。

第二、硬幹型:這類型的思考通常覺得,東西是別人的就沒有安全感(怕很客製化的東西沒辦法加上去),不過如果太客製的需求是否代表著這是一個荒謬的需求?

第三、Open Source型:還是有些好心人會把類似的服務包成一個Open Source專案(例如: Helios或者OpenKitOpenKit的前身的OpenFeint),自己就可以架一台Server囉!


2013年4月27日 星期六

Facebook : Login for Desktop Apps



   果想要讓一個小盒子(例如:Embedded System or Raspberry Pi)能夠拿到facebook的access token來做一些事情,例如:控制GPIO顯示未讀訊息(NOTICE : Physical Facebook Notifier)、抓臉書的相片當做數位像框、當關注的異性感情狀態變更時會有鬧鈴提醒等等。

   盒子通常不會有實體IP(而且也不切實際,就算有IP,幫每個盒子都必須申請一個特別的Facebook ID也很噁心),此時就可以使用Desktop版本的登入方式,操作概念如下:

1. 將redirect_url改成 https://www.facebook.com/connect/login_success.html

    https://www.facebook.com/dialog/oauth?
        client_id=YOUR_APP_ID
       &redirect_uri=https://www.facebook.com/connect/login_success.html
       &scope=COMMA_SEPARATED_LIST_OF_PERMISSION_NAMES
       &response_type=token


2. 偵測redirect的event,在把redirect Url上的access token變數拿下來用

https://www.facebook.com/connect/login_success.html#
    access_token=USER_ACCESS_TOKEN...

   偵測的方式可以用WebView掛Listener來完成,看起來還是要提供螢幕讓使用者登入:(,不過之前在hack Google Play Console的API時,有參考Andlytics模仿Google登入的request內容,應該還是有機會辦到純Program的方式拿到access token


----------- 2013/4/29 修正 -------------

看起來NOTICE : Physical Facebook Notifier作法比較簡單,只把七段顯示器直接透過serial port來操作,所有認證的畫面及流程都用桌面軟體搞定。



2013年4月18日 星期四

Test Framework on Android

   In the test case, we can divide into unit and integration test(also call it is white and black box test). we always   use the unit test to make sure the correctness. and integration test to make sure it in user's way. Android provide the framwork's to finish this work:

Test Project : Include the mock object for Android's object, and also test the key objects : Activity and Service. in the other way, it's support emma(the code coverage tool for test case). the usage step show as follow:
  1. android create test-project -m ../MyAndroidProject-n MyTestProject -p MyActivityTest
  2. ant emma debug install test #Run the test case on debug build with code coverage analysis
UI Test : It's new feature in Android SDK 16 and high. it can write the script(Java) to control the UI Components( for example: click button, swipe list view and press the back key). looks like it's not support dragging touch event. the usage step show as follow: 

  1. android create uitest-project -n my_ui_test_project -t 1 -p my_ui_test_project_folder
  2. ant build
  3. adb push bin/my_ui_test_project.jar /usr/local/tmp/
  4. adb shell uiautomator my_ui_test_project.jar -c com.yourtestcasepackagename.app.Main
Monkey : Send bunch of key/touch event into Application.
example : adb shell monkey -p com.yourpakcagename.app -v 50000 (send the number of events)


In the future work, we can integration it into the Jenkins service.


2013年4月3日 星期三

Hack the Google's play store console api




   The feature is not publish in today, but we want to re-design the data visualization on our service. some Third-party service(like AppFigures) can organize it after you offer the play and  app store account, but it has some problem for sync Google account ( you will recv alert for change password ). So we decide to hack the api.

   The android application : Andlytics can handle those data and show it on. but it's hack way, mimic the console from Google's ajax request. so we need to take the two problem : authorization and data parsing.

Authorization : you can reference Developer-Console-v2 (Google say it will update at April 15. 2013 :( ).
Data parsing : the Json format looks like Proguard. so you need to login the console and try to mapping the data with dashboard :((

Future work:
if the Google still without release the API in the short time. i will publish the implementation on Grails framework :)

2013年3月17日 星期日

Who Most Like You?

   天花了些時間把之前的作品重新粉飾一般。Who Most Like You專門統計整理所有關於誰按你讚的系統,登入臉書後就可以開始找出誰最愛按您讚了!

備註:本專案採用FQL Multi-query的方式以降低多次request的麻煩,但是單次request的時間還是超久:(

網址:http://who-most-like-you.herokuapp.com/

2013年2月22日 星期五

The Customized Android Activity on Unity3D

   The Unity3D is not accept you to change the launch flow.but some time we need to insert the special flowchart( for example : login, play the opening video, native menu...etc). So it's not a function in current version(4.5.6). but we still can finish it.

In Android, we can insert the special Activity to be launch point. and launch the Unity activity later. the principle is that, when you put the Manifest in project folder(Assets/Plugin/Android/), the release will use it. you should put res/ and assets/ too. finally the src/ should compile to jar file, and put it in bin/.

//TODO add source code

The iOS solution will discuss later.

Reference : 

http://lancelotdiary.blogspot.tw/2012/05/unity3d-plug-in-for-android-activity.html
http://randomactsofdev.wordpress.com/2011/08/19/accessing-the-android-compass-through-unity-3d/


2013年2月19日 星期二

Data Mining for Social Media : Hurricane Sandy Tweetbeat

   Some guy use data mining for social media when the Hurricane Sandy is comed. it's call Hurricane Sandy Tweetbeat.




   It's my favor to analysis the data in social media. and try to find out the story behind the data. we build a project(call refresh bpm) to found out the definition for any kind of emotions in the world.

2013年2月16日 星期六

Httpd on a App(Android and iOS)

    We found a lot of app contain http/ftp(the ftp protocol is not discuss today) server to upload file/video/music(like GPlayer). that's let me interest how to implement it on a app. according to my survey, if you want to do it on Android. you can use NanoHttp(implement by java.net.ServerSocket). on the iOS, you can use CocoaHTTPServer(implement by sys/socket). Btw those lib is not make sure fully support HTTP spec.

picture: the screenshot of upload page of GPlayer (support drop-n-upload feature!)


Reference: 
http://stackoverflow.com/questions/6804650/ios-devices-as-web-server

2013年2月14日 星期四

The Shiro and Quartz2 Plugin's Confliction

We always install Shiro(for security feature) and Quartz2(for offline worker) plugins when we develop Grails Application.

but the Shiro plugin contains Quartz-1.x jar, so we can not run application by command run-app. 

Solution: download the newest quartz jar from official site, and put it into /lib/

the problem still occur when deploy the war file on the web container(for example : tomcat)

Solution: remove the useless jar file in the war at the last step of build war file

add some code at /grails-app/conf/BuildConfig.groovy

//TODO add the source code in here

finally we can work perfectly. but it's show some problem for java dependency manager(the grails use ivy by default), it can not handle the dependency perfectly between each plugin.


2013年2月5日 星期二

Pull Down to Refresh UI Design

   The action of pull down and refresh is normal feature in mobile app. but the official SDK isn't support it. so we find some  open source  :


i use the Android-PullToRefresh. because it support :
  • ListView
  • ExpandableListView
  • GridView
  • WebView
  • ScrollView
  • HorizontalScrollView
  • ViewPager

we just replace my GridView into PullToRefreshGridView, and setOnRefreshListener().

public static interface OnRefreshListener<V extends View>{
    public void onRefresh(final PullToRefreshBase<V> refreshView);
}

Remember, you should call onRefreshComplete() let the UI stop the animation.

2013年1月31日 星期四

How to Handle The long and large Image Resource on Android's ListView

   Use listView with item that have ImageView is normal situation for Application. but the android will catch OutOfMemoryExceptions when the list is too long even if you use the SimpleAdapter. the BaseAdapter interface without method like dispose() let the item to release the resource when it's invisible.

   so the official site post a article : Displaying Bitmaps Efficiently. looks like the Caching Bitmaps can solve the problem. but the LruCache work on API 12. I think the key issue is we can not get the heap size on a Bitmap(but the interface is open in API 12 Bitmap.getByteCount()).
 
   finally we found the open source projects to handle this kind of problem. The ImageLoader and Android-Universal-Image-Loader. but it's only for resource from Internet and File(that's means the resource in assets can't support). the implementation use View.setTag(java.lang.Object) to link the ImageLoader and View.


OpenCV Scaling Image on Android

  Today i try to integrate the opencv on my android project(i need the image scaling alg. for my midp adapter's canvas). 

The purposes :  

  • Study the Open CV on Android platform 
  • Import static Open CV lib(without OpenCV Manager) 
  • Image process the Bitmap  (resize algorithm : INTER_NEAREST , INTER_LINEAR , INTER_AREA , INTER_CUBIC and INTER_LANCZOS4) 


source code : https://github.com/bearprada/android_opencv_sample_resize

2013年1月28日 星期一

HQ2X on Android

之前寫了一個midp2android的adapter,當中的Midp的Canvas是直接對應到Android的SurfaceView,然後直接用matrix根據螢幕實際大小硬將原本只有240x260的midp game撐大(到平板是悲劇)。

前些日子同事有分享HQ2x(當然還有HQ3x跟HQ4x)的演算法,適合用在Run-time Scaling 圖像,處理過後的圖片不再那麼鋸齒,於是我找到一個Android版的HQ2X(Perfomance是悲劇,大概只剩下 1 FPS)。

最後終於找到一個NDK版本的實作(範例),不過跑起來並不是那麼理想(只剩下10 FPS左右)。

9-patch in Unity3D

   Unity3D上開發GUI程式在3.5.6版比較推薦NGUI這套系統(雖然還是很多BUG),不過據說在4.x之後就會有官方support的版本。今天碰到一個節省GUI資源的問題,在Android上有9-patch能節省GUI所花費記憶體的大小,但NGUI的Altas似乎沒辦法看懂它,但NGUI提供另外一套類9-patch的解法。用Sliced Sprite搭配Altas內的圖片增加Board的寬度即可以達成。

2013年1月18日 星期五

JInjector - Code coverage tool for J2ME


Overview

Concept

  1. injector
  2. make html report

Step-by-Step

1. checkout the code(include jinjector_tool and jinjector_mobile)
2. get the related lib from web by script
./adddextrapackagesandifiles.sh (copy from jinjector_mobile/)
requirement tool on lib/
google-collect-snapshot-20090211.zip
junit3.8.1.zip
easymock2.4.zip
easymockclassextension2.4.zip
clib-2.2.jar
3. modify the build.xml
build-yourprojectname.xml -> build.xml:
  • <property name="WTK" value ="c:\wtk2.6" /> -> change the value into your WTK folder
  • <property name="emulator.device" value="DefaultColorPhone" /> -> you can change device by setting this value
jinjector.yourprojectname.properties:
  • jars: c:/wtk2.6/lib/cldcapi10.jar... -> your_WTK_folder/lib/cldcapi10.jar...
4. put your source and test case
jinjector_mobile\src : your source
jinjector_mobile\tests_to_inject : your test case
also you can change the value of srcdir:
<target name="injectiontests" depends="unittests"  description="Compiles the tests to inject">
<javac srcdir="${basedir}/tests_to_inject" ...
...
...
<target name="compile" depends="debug_ant, extract"  description="Compiles the source">
<javac srcdir="${basedir}/src" ...
5. build test case with jinjector
ant build
  • it can auto run the emulator from WTK

    figure. auto run the emulator from WTK

figure. end of test and output the log of coverage
hint: the log will put in here in experiential
lcov file place on Windows 7
direction: {UserDir}/j2mewtk/{WTK_Version}/appdb/{Device_Name}/filesystem/root1/{Log_Name}
example: C:/Users/prada.hsiung/j2mewtk/2.5.2/appdb/DefaultColorPhone/filesystem/root1/qq.lcov
lcov file place on Ubuntu 10
direction: ~/j2mewtk/{WTK_Version}/appdb/{Device_Name}/filesystem/roo
example: /home/prada/j2mewtk/2.5.2/appdb/DefaultColorPhone/filesystem/root1/qq.lcov
6. generate report html
  • copy the lcov file into the "/src" where is your source folder
  • generating the report html by genhtml command
  • genhtml qq.lcov*
  • open the index.html in the same folder
    figure. the main page show the percentage of code coverage for each class
figure. under light the code by different color. orange=coverage, red=not coverage yet in the test case

Reference

Study Note : HAProxy


Concept

Related Technology

Nginx
*HAProxy
Apache2
Squid
Linux Virtual Servers (LVS)
Pure 負載均衡器(PLB)
Pound
Pen
Varnish

HAProxy

HAProxy 是一套快速以及靈活的load balance套件,透過haproxy可以快速達到server分流、判斷存活、連線數量等功能。當流量達到一定的程度後load balance是一大重點,如何讓各server平均去負擔流量是相當重要的,透過haproxy可以簡單快速的達成load balance。
pros
1.支援session keep alive
2.透過指定的url檢查後端伺服器的狀態
3.支援tcp協定的負載平衡,譬如:可以給mysql伺服器、mail server郵件伺服器做負載平衡
cons
1.不支援虛擬主機 (更改為支援虛擬主機)
2.目前沒有支援 nagios(註2) 和 cacti (註3)的網路監控功能
註2:Nagios 為提高效能和準確性的網路監控軟體(詳細)
註3:Cacti 是一套類似 MRTG 的snmp流量監控跟系統資訊監控軟體,除此 Cacti 還可以外掛 Script 及 Templates作出各種的監控圖 ,有興趣的網友可以參考 CaCti官網 的說明

Installation

tar -xvf haproxy-1.3.15.7.tar.gz
cd haproxy-1.3.15.7
make TARGET=linux26
cp haproxy /usr/local/sbin/
setting the rule for HAProxy (for example:our the Hinet server)
"haproxy.conf"
global
        log 127.0.0.1   local0
        maxconn 1500
        #hroot  C:\cygwin\home\prada.hsiung\haproxy-1.3.15.1\
        uid haproxy
        gid haproxy
        #deamon
        nbproc  2
        pidfile C:\cygwin\home\prada.hsiung\haproxy-1.3.15.1\haproxy.pid
 
defaults
        log     127.0.0.1       local3
        mode    http
        option  httplog
        option  httpclose
        option  dontlognull
        option  forwardfor
        option  redispatch
        retries 2
        maxconn 2000
        balance roundrobin
        stats enable
        stats uri /hastats
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000
 
frontend web-balancer
        bind    *:80 #test:8000
        #acl is_bbs path_beg /bbs
        acl is_opensource hdr_dom(host) -i opensource
 
        use_backend backend_opensource if is_opensource
 
        default_backend backend_yahoo
 
backend backend_yahoo
        server          web01 tw.yahoo.com weight 5 check
backend backend_opensource
        server          web04 192.168.92.35:80 weight 5 check
running
./haproxy -f haproxy.conf

Scaling Design

we find suggestion on internet:
nginx (for HTTP compression) --> Varnish cache (for caching) --> HTTP level load balancer (HAProxy, or nginx, or the Varnish built-in) --> webservers.
Component:
Front-end-Server(Nginx)
Back-end-Server (Apache|Tomcat)
Cacher (Varnish)
Load balancer (HAProxy)
System Structure
...coming soon...

Reference