Archive

Tag Archives: android

Some time ago I designed and implemented mobile solution to browse/search legal information actual in the Netherlands. There are a lot of legal institutions, rules, etc. in the Netherlands, so, the idea of application is to provide the roadmap for regular user to solve specified law question/problem.

There is a client-server solution. From one hand there is free android application which can be used as offline manual. As it’s mostly content oriented application, I created it by PhoneGap + HTML5 + jQueryMobile technology. But it’s not pure HTML5/JS/CSS solution because it needs several PhoneGap plugins implemented by Java.

Server side of the solution is a simple PHP+MySQL CMS implemented as single-page backbone.js web application to compose and correct the content of the manual. I also used Slim MVC, Twig templates and idiorm ORM to MySQL frameworks.

The workflow is:

  1. Content manager edits the content (CRUD) and deploy new version available via HTTP online;
  2. The clients automatically update (check, download and install) the latest version of content from that HTTP interface when they are online;
  3. Then the clients may use new content even offline.

There are also such useful features:

  • search by tags
  • feedback form
  • share the link/content via email/facebook/twitter/etc.

Image

From time to time we need to translate some application from one platform/language to another. For example, we need to implement the same mobile/social game for web/flash, iOS and Android. Of course, we can use some multi-platform framework like Corona to build such system, but it’s not so power like native software development tools.

The idea is to design and implement the core (model) of the game in some universal module in Lua programming language. The model may be pretty complicated for the games like chess or poker. I implemented the game ‘Guess The Number’ in Lua as an example. Then I separated the model:

GuesGame = {}GuesGame.WIN = 1GuesGame.LOSE = 2GuesGame.GREATER = 3GuesGame.LESS = 4GuesGame.MIN = 1GuesGame.MAX = 10GuesGame.MAX_TRY = 3function GuesGame:new()        return setmetatable({                _counter = GuesGame.MAX_TRY        }, {                __index = GuesGame        })endfunction GuesGame:start()        self._x = math.random(GuesGame.MIN, GuesGame.MAX)        self._counter = 0endfunction GuesGame:play(a)        if self._counter >= GuesGame.MAX_TRY then                return GuesGame.LOSE        end        self._counter = self._counter + 1        if a == self._x then                return GuesGame.WIN        else                if self._counter >= GuesGame.MAX_TRY then                        return GuesGame.LOSE                elseif self._x > a then                        return GuesGame.GREATER                else                        return GuesGame.LESS                end        endend

As you can see, there is just classic ‘guess the number’ game. The program chooses integer number from 1 to 10 randomly. And the player is trying to guess it in 3 tries.

Next steps were implementation of the game as console applications in pure lua, Objective-C and Java. Lua engine is built as C library, so, there are not problems with Objective-C. In case of Java I used LuaJ. Keep in mind, I used exactly the same lua code of the game model for all implementations. Generally, it was not only that lua code. I need to add some support code for initialization and interface with UI side implemented in native language specified for the appropriate case.

E:ProjectsGuesGamepure_Lua>lua GuesGame.luaq - quit, s - start new game, 1-10 - guesTry to gues a number between 1 and 10?5Less3Win! :-)q

After that I implemented iOS version as iPhone application in the same way. There were no problems at all.

Ios_lua_gues_the_numver_game

To do flash (action script) game I used lua via alchemy. It costs me some performance. Alchemy is going slow at line of calling it from action script side because of huge serialization. So, if you are going to touch you game model often, then you better avoid such scenario and port the model to action script.

Flash_alchemy_lua_gues_the_number_game

We can’t use LuaJava on Android as for pure Java because of differences in JVM. So, I used Android NDK to compile lua engine as C module for Android game.

Android_ndk_lua_gues_the_number_game

For new multiplayer social game I created simple TCP client as Android application. It connects to special echo server (.NET) and sends/receives packets to/from other clients. Packets are UTF-8 text messages prefixed by string length 4-bytes integer as a header of the packet.

Android-tcp-client

I used Java NIO to implement network layer with second worker thread to wait for connection establishment and further reading of incoming packets. Network layer is separated of GUI, so, ready to be used in other Android application. Eclipse project, .NET server and client for tests are here.

 

Today I’m working on TCP/IP socket client module for android java application.
There are 2 threads: worker thread to receive messages from server and initial UI thread.
Unfortunately, it’s impossible to change UI elements from a non-UI thread.
I use android.os.Handler to send some message from non-UI (worker) thread to UI.

import android.os.Handler;

private Handler _handler;

// in UI thread
_handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        // process message from worker thread
        if(msg.what == ADD_MESSAGE) {
            _addMessage((String) msg.obj);
        }
        super.handleMessage(msg);
    }
};

// send message from worker thread to UI
Message msg = EchoClientActivity.this._handler.obtainMessage();
msg.what = ADD_MESSAGE;
msg.obj = message;
EchoClientActivity.this._handler.sendMessage(msg);