Building Android interfaces using Corona

I've tinkering with this for a while but I never seem to have enough time in the day to make sustain progress.  Rather than keep this to myself I thought I would post up what I've got and let others get involved with ideas, questions, coding and general advise of what they think.

If you have an android device you can download this application (see screenshot below) and try it.  It'll talk to my house livebox iServer so you'll see what I see on my joggler.

Member for

13 years 11 months

AndrewJ

13 years 7 months ago

Thanks Brett, been waiting for this as you know :)

 

Happy Easter to you too

Member for

13 years 11 months

AndrewJ

13 years 7 months ago

Modified for my Dynamic DNS service

working well so far

 

Member for

17 years

derek

13 years 7 months ago

This runs well onm my HTC OneX. However, the app doesn't appear on the list of 'running' applications. This makes it a bit awkward to navigate back to the app or to shut it down/restart it.

Added a 'Quit' button with the following code.

 

 

local widget = require "widget"

local function onButtonRelease( event )

local btn = event.target

    native.requestExit()

end


local button = widget.newButton{
    label = "Quit",
    font = "HelveticaNeue-Bold",
    fontSize = 18,
    yOffset = -2,
    labelColor = { default={ 65 }, over={ 0 } },
    emboss = true,
    onRelease = onButtonRelease
}

button.x = display.contentCenterX
button.y = display.contentCenterY

 

Member for

13 years 11 months

AndrewJ

13 years 7 months ago

Hi Derek,

 

I can see the app running on my Handset (HTC Desire HD) both in the running applications and 3rd party task manager

will add your button code and give it a go

heres the background code - local background = display.newImage( "background.png", true)

I have noticed a few app crashes which appear to be socket releated, im still logging these with logcat to see if i can find a workaround.

 

Andrew

Member for

16 years 10 months

brett

13 years 7 months ago

Andrew,

I'd be interested in any ideas you have about the crash.  I've not experienced one of those but then again I run this on a tablet not a phone.
Are there logs somewhere about where this was generated from?  LUA normally throws a stack trace which I'm hoping is logged.
I'll read up on how corona handle runtime errors - perhaps a splash screen to handle this might be good idea.
Early days.... This is only framework code with a "very simple" main.lua to demonstrate some of what I've been doing, impress that people are running with it thou, this bodes well for getting me motivated   :)

Brett

Member for

17 years

derek

13 years 7 months ago

I have a few different variants of the xAP app on my OneX. Most of the confusion that I've had with the piece seems to be caused by launching a 2nd app whilst another one is still running.

I still can't see the xAP apps in the list of 'running apps'. The only way I can get back to the app is to hit the icon that launched it in the first place (I'm assuming that this just takes me back to the already running instance rather than starting a new one).

I can however, use 'Settings/apps/all - Force Stop' to kill off the app. 

Of course, we are using the 'demo' version of the Corona environment. The production one seems to be rather newer. Some of the issues might be down to bugs that have been fixed.

I've not tried to hookup the USB 'debug log' thing yet. Might give that a spin.

Member for

13 years 11 months

AndrewJ

13 years 7 months ago

Hi Brett & Derek,

 

Brett, - this is the message on screen when the app crashes ""This application encountered a Lua error (see logs)" - this appears to be a very generic error and from what i have read and normally relates to a typo or character case error on a path i.e for an image.png

I was able crash your demo app almost everytime and could only run it up once i had cleared the App data / cache (built in android feature) or restarted my phone

the error logs at the time (I didnt save a copy) mentioned a lua txsocket error which is why i mentioned socket errors above - next time it crashes i will be sure to save a log

I also added the following error handling into the main.lua - is this correct?

local function myUnhandledErrorListener( event )

    local iHandledTheError = true

    if iHandledTheError then
        print( "Handling the unhandled error", event.errorMessage )
    else
        print( "Not handling the unhandled error", event.errorMessage )
    end
   
    return iHandledTheError
end

 


Derek, you can use ADB Logcat via usb "$ adb logcat -s corona or an app, i am using this https://play.google.com/store/apps/details?id=org.jtb.alogcat&hl=en

you can also filter "Corona" events for ease of viewing.

Member for

16 years 10 months

brett

13 years 7 months ago

Andrew, I've not looked into the logs but I was thinking about what happens when the program quits and I know I don't clear up the sockets.  This is bad.

In xap.lua I have a stop() function but its contents is wrong it needs to close all the sockets as well as stopping the network threads.

function Server:stop()
    self:close()
    for _,t in pairs(self.threads) do
        timer.cancel(t)
        t = nil
    end
end

We also need to create a close() method for the UDPServer class as its missing.

function UDPServer:close()
    self.txSocket:close()
    self.rxSocket:close()
end

In the main.lua you need to capture the ApplicationExit event and make sure we STOP the xap engine.  This will close the sockets and stop the timers.  Append this to the end.  Failure to close the sockets will leave them open and when the application attempts start again it will find the in use causing it to break.  One of your symptoms.

local onSystem = function( event )
    if event.type == "applicationExit" then
        xap_server:stop()
    end
end
Runtime:addEventListener( "system", onSystem )

See if that gives you any joy.  In the meantime I'll play with adb and figure out where these logs are what the "real" cause was, however I think my hunch is on the right track.

This also brings up the issue of suspend/resume - if you leave a socket suspended for too long the other side may close it.   I'll need to investigate, might have to do something with these system events too.

Brett

Member for

15 years 7 months

garrydwilms

13 years 7 months ago

Cheers guys,

Yet more flexibility! I am trying to use a cheap tablet mounted in a frame (see pic) as my central controller, to keep swmbo happy. This runs xapflash but I find it takes an age to refresh from standby meaning it isn't yet as useful as I had hoped. Am definitely interested in giving this a go when I get a few hours!

Keep up the good work folks!

Garry

Attachments
image.jpg (16.53 KB)

Member for

13 years 11 months

AndrewJ

13 years 7 months ago

Brett,

 

Thanks that could indeed explain the socket error - it has worked flawlessly the past 2 days with not a single crash which is strange as i haven't actually made any changes as yet.

I will append your stop code to the end of the main.lua and will give it a test going foward, where do i need to add the UDPServer class code?

 

Andrew

Member for

16 years 10 months

brett

13 years 7 months ago

XAP UI demo screenshot"

I modified the code a little.  The design is based on a grid and you specify things that need to goto into each cell.  I've also corrected the design so that it works on Droid-Phone dimensions.   You'll see I've added a large quit button, thanks Derek.  Slightly reworked from the code he pasted so it looks nicer and uses the UI library that I have built in.

The program should also clean up its sockets when it quits, I've not into any issues with the error previously reported with it. 

I'm not trying to make this fully blown just a concept demonstrator showing key features of how things might work.

I'm thinking what would cool is if the UI could be built like the xAPFlash program, Data Driven, but all its content would be servered up by the iServer.   This would allow you to change the look and feel without any code changes and recompiles.

Anyway just thoughts - trying to get the UI, XAP, BSC frameworks hanging together first.

Brett

Screenshot of how the demo code that is attached now looks.

 

PS: Andrew just merge over all the .lua files except main.lua into your branch and take the last bit for the application exit from my main.lua - that should sync you up.


UPDATE:  Corrected the code base.

  • If an error occurs during connection you'll get a message indicating what went wrong and you'll be able to quit out.
  • The read only buttons labelled LOBAT now work (bug in bsc.lua)  
  • The demo code http://www.dbzoo.com/public/xap-ui-demo.apk now reflect this code base.
  • The NAG dialog is done now I'm building with the latest Corona SDK 2013.

Brett

Attachments
xAPUI.zip (186.92 KB)

Member for

14 years 5 months

mark_baldwin

13 years 7 months ago

Something else to play with :)

Member for

13 years 11 months

AndrewJ

13 years 7 months ago

Hi Brett,

 

Thanks for the updated code, the Grid method is a huge improvement :)

Can i ask which resolution this is currently based on?

A few comments and suggestions that could enchance this apps awesomeness even further :)

Grid to adjust to landscape rotation

Detect phone resolution and scale accordingly

Multiple grid pages

Connection Status

Cosm graph feeds

 

Member for

16 years 10 months

brett

13 years 7 months ago

As I still had an old version of the app on my device.  I got a command line into my android device and I saw this runtime error.

I/Corona  (21574): Lua Runtime Error: lua_pcall failed with status: 2, error message is: ...\Users\brett\Documents\Corona Projects\xAPUI\xap.lua:215: attempt to index field 'txSocket' (a nil value)

This is trying to add a filter when the txSocket has not been initialized.

Sidebar:  Install an SSHD server and putty in from windows.   Its just a linux device with busybox after all.  The command you want is "logcat -C".  Much easier to see what is going on this way.

After I updated to the new version that closes the engine I've not been able to induce this failure again.  So I guess its gone (for now).  If I can induce the error I stand a good chance of being able to figure out why (next time).

To answer your question Andrew - the device resolution is controlled by the config.lua file and yes all those suggestions for improvements would be cool in the fullness of time.   My next step pull out the "grid" configuration and have this uploaded from the iServer along with all the assets (images).

Brett

Member for

13 years 11 months

AndrewJ

13 years 7 months ago

Brett,

 

Thanks for the Resolution info, i took a look at Config.lua

My Desire HD screen res is 800 x 480 (800 / 480 = 1.66) therefore the second scaling method applies

I was puzzled how four 80 pixel images could fill my 480 pixel screen, the config now explains that :)

A small issue i did have was to the move the "Quit button" down from -54 to -40 to fit on my screen - other than that everything seems to fit nicely

just on a side note - the Temp / Lightbulb images are currently 100x100 despire your code stating 80 x 80, not a major issue, just pointing it out.

 

Regarding the crashes - i am now running the new code with your fixes and it does appear to still crash but only on two occasions

1 - If the App has been running in the background and i attempt to go back into it, i may receive the lua error mentioned before (closing the app via a task manager and obviously using the quit button before exiting the app seems to get around this - is the app possibly attempting to reconnect but already connected to the iserver?)

2 - I have noticed that the app does occasionally crash my livebox - i've yet debug this but no xap flash access is avaliable this is also noticed using the xapflash app on my jogglers

Somthing else noticed - If i have been outside of the app for a while but it is still running and it lets me back in without displaying a lua error i see a quick flash of updates to the endpoints as if its playing catchup - again not an issue but something to mention

One final thing, i cant seem to get the lobat and moved indicators working using the following, prehaps you havn't implemented this yet despite your screenshots.

        { -- ROW 5
         {c=toggle, text="R2-PWR", xapSource="dbzoo.livebox.jeenode:bedroom2.lobat", readOnly=true},
         {c=toggle, text="R2-PIR", xapSource="dbzoo.livebox.jeenode:bedroom2.moved", readOnly=true},

 

thanks

 

Andrew

Member for

14 years 5 months

mark_baldwin

13 years 7 months ago

I can see this really catching on Brett

SWMBO likes it, so it must be good

Member for

13 years 11 months

AndrewJ

13 years 7 months ago

Love the custom icons Mark, a real nice layout with the Red, Green and Blue too.

Member for

16 years 10 months

brett

13 years 7 months ago

Woo just noticed a new version of the SDK was released.

Corona SDK on Windows Build 2013.1076

Member for

13 years 11 months

AndrewJ

13 years 7 months ago

Got an email earlier announcing the "Free" starter sdk...

i guess this gets rid of the nag screen :)

Member for

17 years

derek

13 years 7 months ago

Yup. Running the latest from Brett & the nag is gone. Also decent handling of errors when no Wifi or 3G connection can be established.

Member for

15 years 10 months

g7pkf

13 years 7 months ago

Been watching this thread for a while as most of you know i am not a coder.

but downloaded the demo and it is excellent.

Just wish it could pull screen layout from iServer like my 3 jogglers do.

Oh well can only hope someone crack's it.

Been playing on a 7" android tab i got for free for buying some toner cartridges for my work printer.

Dean

Member for

13 years 11 months

AndrewJ

13 years 7 months ago

Hi Dean,

 

Im pretty sure thats on Bretts list of things to do.

Regarding the coding aspect you really dont have to do much to get this running

1, change the domain for your own i.e home.dbzoo.com to your own dynamic service

2, set your endpoints up using Bretts as a guide

these are all done in the main.lua file provided by Brett

you then fire up the simulator and build for Android - this will generate an APK (Android installer package) which you can copy / email over to your device and install.

Andrew

Member for

15 years 10 months

g7pkf

13 years 7 months ago

downloading now.

 

you do realize you just caused a flood of potentially stupid questions from me :)

 

havent coded since zeap (Z80 assembly protocol)

Member for

13 years 11 months

AndrewJ

13 years 7 months ago

That means your a seasoned pro! maybe a bit rusty but the principles are the same :)

Member for

15 years 10 months

g7pkf

13 years 7 months ago

sent app via email and my android device says no installed app can open?

 

huh

 

stupid android

Member for

13 years 11 months

AndrewJ

13 years 7 months ago

no installed app can open?

Mark on here was having trouble installing the app, that was displayed as app not installed

your issue sounds different.

Member for

17 years

derek

13 years 7 months ago

I had an issue with using yahoo mail. Gmail worked - just click on the attachment & select 'install'.

Member for

14 years 5 months

mark_baldwin

13 years 7 months ago

I ended up just hooking the phone up to the computer and copying the file across.

I must admit it only worked on my works phone (once I added an SD card) and not on my own phone.

Member for

13 years 11 months

AndrewJ

13 years 7 months ago

Derek, sounds great regarding the comms fixes will Brett be releasing this?

Member for

16 years 10 months

brett

13 years 7 months ago

Andrew i did - the update in the middle of this thread with the .zip attachment has its code bundle lifted.

Member for

14 years 6 months

magill

13 years 7 months ago

Hi Brett

This is great stuff, wish I'd got this before embarking down the Java road. (Pretty heavyweight) I'm not too savvy with Lua, is there an easy way to send an alias message with slight mod to the code?

John

Member for

16 years 10 months

brett

13 years 7 months ago

The entire Framework was lifted from the HAH with some minor modifications to make it iServer compliant.
Anything you can do on the HAH will be pretty much an identical call from Android.

For example:

function aliasSend(body)
   local msg = string.format([[
xap-header
{
target=%s
class=alias
}
command
{
text=%s
}]], body.target, body.text)
  xap_server:sendShort(msg)    -- On the HAH we would say  xap.sendShort(msg) see it's VERY close.
end

aliasSend{target="dbzoo.livebox.twitter", text="Hello twitter"}

You can almost write all the code on the HAH get it running here then make minor edits when transferring it.

I wanted to make sure everybody could leverage all the lovely skills they have been building up on the HAH with LUA and spend very little time writing code to get something quite functional in a small amount of time.

I believe the Corona API + the bits I'm working on do this.  Intially I thought about writing this in Java too, but it just didn't feel right.

Brett

Member for

14 years 6 months

magill

13 years 7 months ago

Any guidance as to how to get a button (read only) to trigger this event?

On my Java app I send an alias eg "bedroom boost 2" which the HAH interprets.

thanks

John

Member for

16 years 10 months

brett

13 years 7 months ago

Screenshot

Screenshot

Really the target doesn't matter with an alias message

function aliasSend(body)
   local msg = string.format([[
xap-header
{
class=alias
}
command
{
text=%s
}
]], body.text)
  xap_server:sendShort(msg)
end

And a button might look like this.

-- Create SEND ALIAS button
ui.newButton{
    text = "Boost", size=18,
    defaultSrc="glossy_button_green.png", defaultX = 80, defaultY=80,
    overSrc = "glossy_button_dkgreen.png", overX = 80, overY=80,
    x = display.contentCenterX,
    y = display.contentCenterY,
    onRelease = function() aliasSend{text="bedroom boost 2"} end
}

defaultSrc is how the button looks, overSrc is how it looks when pressed down by your finger.

See attachment for "REALLY SIMPLE" example - its not a lot of code.

 

xap_server = xap.TCPServer("home.dbzoo.com")

Can be an IP address if you are only going to use this internally or a DYNDNS name if you want to find your house whilst you are out and about.

xap_server = xap.TCPServer("192.168.1.15")

Attachments
aliasApp.zip (165.26 KB)

Member for

15 years 10 months

g7pkf

13 years 7 months ago

Well i now have a nearly all working android app.

 

not as flash as some but i am getting there.

Member for

16 years 10 months

brett

13 years 7 months ago

g7pkf that's a pretty congested UI but hey it works!
I really need to post some examples using multiple screen pages.
I'll work on a slightly more complex UI demo which can be picked up as a template.  Its a time thing.

I can feel this thread getting long.....
Might have consider creating a new CORONA top level forum entry to collect all the threads about this development kit.

Brett

Member for

13 years 11 months

AndrewJ

13 years 7 months ago

Sorry Brett,

Completely missed that :)

up and running now

Member for

15 years 3 months

EJ-Ambient

13 years 6 months ago

Hi Mark....

The icon/graphics are really nice.... any chance you could post a zip of the components and the main.lua so I can have a go at building my own....thanks in advance...

Elliot

Member for

14 years 5 months

mark_baldwin

13 years 4 months ago

Hi Elliot

Did you get my email?

I've been out of the loop for a while and have so much to catch up on.

New house so chnace to start from scratch on the home automation, I have so much to do :)

Member for

14 years

leighton.olds

13 years ago

Wow, this is simply awesome!!!