Tiliman’s Weblog

January 2, 2013

Google Cloud Messaging with Erlang

I have been working on Facebook matchmaking code for Android/Unity to mimic iOS Game Center like flow of match making where other player receives notification on phone. Server side is coded with Mochiweb in Erlang. Here is simple code to send GCM Notification.

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
send_gcm_msg(ApiKey, Pnstoken, Message, Game) ->
        Baseurl = "http://android.googleapis.com/gcm/send",
        ApiKey1 = string:concat("key=",ApiKey),
        %% Create Json struct
        Body = lists:flatten(mochijson:encode({struct, [{registration_ids,{array, [Pnstoken]}},
                        {data,{struct, [{message,Message},{game,Game}]}},
                        {time_to_live,3600},
                        {collapse_key,game_invite}]})),
        try httpc:request(post, {Baseurl,[{"Authorization",ApiKey1}],"application/json",Body},[],[]) of
                {ok, {{_,200,_},_,RespBody}} ->
                        {ok, mochijson:decode(RespBody)};
                {error, Reason } ->
                        {error, Reason};
                {ok, {{StatusLine,_,_},_,RespBody}} ->
                        {error, {StatusLine, RespBody}};
                BigError -> {error, BigError}
        catch
                Throw -> {error, caught, Throw}
        end.
Advertisements

February 10, 2010

Android Development with Scala

Filed under: Android, Mobile Software Development — Tags: , , , , , — tiliman @ 4:33 pm

I learned a few weeks back about Scala programming language. I never liked Java and thought to try Scala as I would learn functional language and be able to still use JVMs.
I also wanted to learn Android development as I have done previously Windows Mobile, Symbian and iPhone development. Using Scala with Android was my idea to hit 2 birds with one stone.

Android Decelopment using Eclipse and Scala language

Android Decelopment using Eclipse and Scala language

All the information that I could find about using Scala with Android was for Android 1.6/1.7 but not with Android 2.0.1. Today finally I got Scala-Android development working for me. I am using Eclipse for development on OSX.

The steps involved are documented in detail here which can be followed till Step 4 as after that I will explain the trick to use Scala . If you are an advanced programmer then you should be able to follow my brief guide below.

Increasing memory of Eclipse

Edit eclipse.ini file which can be found in OSX at /Applications/Eclipse/Eclipse.app/Contents/MacOS/eclipse.ini
If you have not made changes to an app contents before in OSX then for your information right-click Show Package Contents displays all the internal directories in .app bundle.
Make sure that these parameters are there in config file as below


256m
-vmargs
-Xms256m
-Xmx1024m
-XX:PermSize=64m

Goto android sdk tools directory at <ANDORID_SDK>/platforms/android-2.0.1/tools/dx.sh and edit it and make sure this param is there


defaultMx="-Xmx512M"

It can be different on windows as windows uses batch file. You can find it as -Xmx<NNN>M where NNN is the memory size.

Note: mine was setup as 1024M so I kept it like that. 512M should work fine as well.

Install Android 2.x.x SDK

Download and install Android SDK and Tools from this link http://developer.android.com/sdk/eclipse-adt.html
I had to use HTTP instead of HTTPS for installing ADT plugin on Eclipse.

Install Scala

Download and install Scala plugin for Eclipse
You can put this URL directly in Install New Software feature of Eclipse
Scala – http://www.scala-lang.org/scala-eclipse-plugin
More details can be found at http://www.scala-lang.org/node/94

Download and install full Scala installer as well to get more control on Scala as well as get sbaz support.
http://www.scala-lang.org/downloads/distrib/files/scala-2.7.6.final-installer.jar or find whatever is the latest version at the time of reading.

Install scala-android library for development

Download and install scala-android.jar (library)


sbaz install scala-android

You can compile it yourself by visiting this link http://robey.lag.net/2009/01/19/scala-on-android.html

Create an Android project

Add Scala Nature by right clicking on project and then Scala->Add Scala Nature

Fix Project settings to use Scala

Scala Eclipse Settings

Scala Eclipse Settings

Goto Properties->Builders tab and make Scala Builder as 2nd last item in list.

Goto Java Build Path->Libraries and Remove Scala Library Version 2.x
In same Java Build Path->Libraries, Add External Jar and locate scala-android.jar in lib directory of Scala SDK.

Extending Scala class from Java

I always had issue compiling Android app written in Scala with class not being found as per manifest file. The issue was somewhere between Java expecting .java file as starting point and not being able to find it. So, I found a solution to leave original .java file as entry point and make a new file for Scala. Now the Java startup/main class extends Scala class which allows me to write rest of application in Scala.
My project was called ScalaTest and the ScalaTest.java was boilerplate code generated by android new project wizard.
The file looks like this after changes made for Scala


package android.ScalaTest;

import android.app.Activity;
import android.os.Bundle;

public class ScalaTest extends MainScala {}

The MainScala.scala looks like this


class MainScala extends Activity {
    override def onCreate(savedInstanceState: Bundle) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main)
    }
}




First Scala Application

First Scala Application

Thats it. Scala can be used to do rest of Android Development as well as Debugging. Now I am off to learn Scala with Android.

January 2, 2010

Android 1.5 on HTC

Filed under: Android — Tags: , , — tiliman @ 10:09 am

Installing Android on HTC was much easier than I had expected.
I only tried installing Android on HTC Diamond. Kaiser,Vogue and Touch Dual are also possible.

To get started with Android on HTC Diamond download the android build from
http://connect-utb.com/index.php?option=com_rokdownloads&view=file&Itemid=68&id=53:android-1

The steps to install are simple. Just extract the bundle package and copy it to root of your Internal Storage.
Note: Make sure you replace the STARTUP.TXT with diam100.txt from startup-configs directory. The original STARTUP.TXT is for RALPH100
Note: Disable PINCode for your SIM if you have any before using it with Android on HTC.

Disconnect phone from PC and simply run Haret.

Related links

http://glemsom.anapnea.net/android/htc-msm-android/
http://www.androidonhtc.com/

January 1, 2010

AirplaySDK for Cross Mobile OS Game Development

I am totally astonished as to how well AirplaySDK works. The guys at Ideaworks Labs have done it right.

They have made all the right choices when developing an SDK for cross mobile OS game development.

The issue with mobile game development is dealing with all the different Mobile OS plus dealing with different hardware using same OS. Game developers are somewhat lucky if there is OpenGLES implementation available as then they don’t have to worry too much about the rendering part. The sound and file IO and network features still mostly remain as an issue. OpenAL is not supported on most phones for sound. EdgeLib appeared a few years back to solve the problem of IO, Sound and Network features plus to allow software renderer in case OpenGLES is not available. More on EdgeLib in another post.

I tested the kartz sample from AirplaySDK on N82, N80, HTC Diamond, Samsung Omnia.

It is much easier to just list the feature pros and cons of AirplaySDK that I figured out so far by reading and trying out.

PROS

  • Native binary without any VM.
  • Good packaging tools for all OS.
  • Fixed point computation as well as floating point available.
  • Speedy Software rendering engine
  • Auto fallback to software rendering when no hardware accelerator present.
  • Sticking to opensource libraries.
  • One Dynamic Engine (ODE) for rigid body physics.
  • Support for multiple Mobile OS including Symbian, iPhone, Windows Mobile, Android,BREW and Linux). Edgelib doesn’t support Anrdoid and BREW.
  • Highly optimised compression of assets for games. Not just simple zip compression but usage of Derbh which uses shared buffer across files. Claims on site say that Metal Gear Solid came down to 1.5MB.
  • Arm debugger/emulator available that runs on Windows. Debugging is much easier this way.
  • Windows emulator allows testing of software in various different scenerios (tilt/compass/keypad/touch/resolution)
  • VS Studio programmers would love it as they can use VS with this SDK. Very tight integration.
  • UI development possible for softwares other than games. This is interesting feature but as the Core System API is not extensive, it won’t be possible for creating full applications. Only simple apps can be built. There is possibilty of extensions that can do OS dependent API calls but this system is not exposed to developers to create their own extensions. There seems to be discussion of this opening up on AirplaySDK Forum.
  • Extensions available (only for iPhone) for some of iPhone specific API
  • Phone Orientation awareness and auto rotation of GUI.
  • Human Interface limitations/possiblities awareness. Can detect touch screen and keypad and control based on that.
  • Handling of phone events (incoming call/notifications) without extra code.
  • UI code for input is intelligent enough to display touch keypad on touch UI systems without any extra work. MoSync can’t do that. EdgeLib has no support for UI controls/widgets.

CONS

  • No support for OSX/XCode for development yet. It is planned though.
  • Integrates best with visual studio which can be costly. Although express edition is free and should be enough
  • Not very extensive API for core OS like System API.

My Test Results

Omnia Kartz Running in Portrait at 20 fps. Left/Right sides of screen can be used to steer.

OMNIA Kartz Running in Landscape at 18.65 fps. Left/Right sides of screen can be used to steer.

N82 running Kartz sample at 20 fps and steering with keypads. N82 has vfp

HTC Diamond running Kartz sample with 11.4 fps. HTC Diamond has no vfp/hardware acceleration

N80 running Kartz sample at 9.7 fps and steering is with keypad. N80 has no hardware accelration/vfp.

Blog at WordPress.com.