New Hecl Release

I’ve added a new Hecl release to the sourceforge site:

http://sourceforge.net/project/showfiles.php?group_id=122383

Although, as always, you’re probably better off checking it out of subversion. We mainly do releases in order to focus on polishing things up just a bit, and as a way to promote Hecl by creating a reasonably stable release.

Some of the things that we’ve got working pretty well for this release include:

  • Google Android support.

  • Java reflection/integration – call Java directly from Hecl.

  • And of course a lot of numerous other little tweaks and fixes that crop up over time.

An impressive amount of resources

In the TechCrunch coverage of Google IO, I was impressed by this:

Not every team has the resources Google does to build for every platform. Google is betting on browser over time for mobile devices; it will be the entry point for many applications.

But today, the only browsers capable for supporting mobile apps include iPhone and few others. So Google is developing Android, which includes world class Webkit browser.

Here at the world headquarters of DedaSys LLC (which for the time being happens to be “in front of David’s computer, wherever that is”), we too have some suspicions about the browser becoming the platform of choice for mobile phones, although there is a big if:

When will the carriers make always on, cheap internet connections widespread?

Until that happens, client side apps will still probably dominate for many things. And of course for applications requiring hardware access, some kind of local program will be necessary as well for the foreseeable future. (And of course, Hecl is a good choice “for the time being”, too!)

Coming back to the phrase cited above, what blows me away about it is the sheer amount of time and money that they’re investing in something that “over time”, they’re not really betting on, in the sense that they just want browsers everywhere? Jumping into a market with a new “OS”, toolkit, etc… just to spread a platform with a decent browser?

Wow. That is a company with a lot of spare change. Most people and companies in business are doing the best they can to stretch out their dollars or euros as best they can. I guess it really takes a behemoth to pour so much in the way of engineering talent and financial resources into something that’s in some ways a bump in the road in terms of their long term vision. Or maybe I’m just reading too much into one phrase?

Neat Hecl app from newcomer

This is a cool little application, and a helpful tutorial for people trying Hecl for the first time. Since I know the system so intimately, people doing these kinds of pages are always a helpful reality check, pointing out where people new to the language hit stumbling blocks, and providing a guide for people who haven’t “been there and done that”:

http://lauri.ojansivu.googlepages.com/heclcommandlineapp

Thanks Lauri!

Hypothetical Hecl

I did some ‘playing’ with Hecl the other day, to see what it might look like if implemented in a slightly different way, and I thought I’d share what I was thinking about. Keep in mind that

  1. I haven’t thought this through completely:-)

  2. It’s not something I’m going to just rush out and do in the near future. It’s definitely a Hecl 2.0 sort of idea.

My notion was to make use of the ClassCommand feature to have at least simple types be hooked directly to the relevant Java types, and dealt with through methods, rather than commands. For instance:

$i + 10

instead of

+ $i 10

Where $i is, in the first case, a Java Integer, rather than a Hecl type.

What it would be doing internally is something like this:

  1. $i is recognized as the object to act on.

  2. It’s an Integer, so the corresponding ClassCommand is looked up and executed with + and 10 as arguments.

  3. The classcommand for Integer has a + method that accepts an argument, in this case, 10, which is added to the value of $i, and returned as a new Integer.

Without a “real” (where real also means bloated, memory and space hungry) parser, Hecl is never going to have a C-like syntax – you still wouldn’t be able to do foo = 1 + 2 + 3 – but this just might make a significant amount of people more comfortable with Hecl, which would be a good thing. You could do something like this:

set i 1

while { $i < 1000000 } {
    set i [$i + 1]
}

instead of

set i 1

while { < $i 1000000 } {
    set i [+ $i 1]
}

I actually built a really simple HeclInteger class that implements <
and + as methods to Integer ObjectThings, in order to test out this idea, and it seems to run about as fast as the regular Hecl version.

I welcome any thoughts or comments on the idea.

XML handling for Hecl

We received an interesting contribution to Hecl recently: Martin Mainusch put together a Hecl interface to kxml, and also sent in some demo code that fetches and parses weather forecast information. I thought it was pretty cool:

Hecl Weather Forecast

Note that it’s not compiled into the default build, so you have to change a property from 0 to 1 and recompile, and…being new there might be some issues, but still, it’s nice to have that to let people play around with simple “web service” type things that use XML. Of course, another strategy might be to have a ‘proxy’ server that fetches the actual XML, and returns a tagged Hecl list so that Hecl can parse it directly, but that requires a server and has more overhead.

Hecl Jacta Est

I don’t know if androids dream of electric sheep, but I’ve certainly been losing a lot of sleep over Google’s Android, and am glad that the Android Developer Challenge is winding up, as I’ve been doing all Android, all the time for the past month or so.

I have greatly enjoyed working with Android, as it’s much more capable than Java ME. It’s still very much a work in progress, and only time will tell if Google and their partners are able to best current mobile programming systems… or if the web will simply obviate the whole thing, as it’s doing in many other areas. Win or lose, I will continue to support Android as a platform that Hecl runs on, but in the meantime, I hope to go back and look at some less future-oriented code and get back to some Java ME work. More on that soon…

In any case, here’s hoping that the contest judges see the value in bringing a scripting language to mobile phones! I also hope to see actual hardware soon, as I’m really looking forward to getting my own Android phone.

Mass customization of Android applications

As I mentioned in an earlier article, one of the goals for the Android port of Hecl is to make it as easy as it is to create Java ME applications:

  1. Write the script.
  2. Wrap it up and send it to the phone.

No “compiling” (well, none that’s obvious or explicitly asked of the user, at least, as we’ll see) or fiddling with build files or writing new Java classes or any of that.

We’re still not there yet 100%, but I did managed to hack together a system that, from an original Hecl.apk, creates a new application that can be installed on the emulator. This is tricky, because Android looks at the uniqueness of the package and class utilized for the application, meaning you can’t just rename Hecl.apk to Foo.apk and install it as a separate app. The emulator will overwrite your old copy of Hecl.

Here’s what we go through to make this work. Be forewarned – big, ugly hacks that are not for the faint of heart follow. The command line isn’t exactly beautiful either, but it beats all the individual steps that it replaces, as we’ll see.

java -jar /home/davidw/workshop/hecl/jars/AndroidBuilder.jar -android 
    /opt/android-sdk_m5-rc15_linux-x86/ -class Example -label "Hecl Example" 
    -package my.example -permissions ACCESS_LOCATION,ACCESS_GPS
  1. The first thing that needs to be done is to replace the AndroidManifest.xml that is contained within our “template” application, that resides in Hecl.apk, a copy of which is contained within the AndroidBuilder jar file. Since the compiled version of the xml file is a binary format that hasn’t been reverse engineered or documented yet, the best way to do this was to simply:
  2. Write out a new AndroidManifest.xml in our temporary work directory that uses our provided package name, label and class.
  3. Using the aapt command in the SDK, create a new, empty apk file, specifying the newly created xml file as the manifest file to use.
  4. Now we can extract the compiled version of the xml file, for later use.
  5. At this point, we extract a version of Hecl.jar contained within AndroidBuilder.jar to the temp directory.
  6. Now we need to generate some Java classes that correspond to the new package and class names that we’ve passed to AndroidBuilder. Ideally, I would have done this with some tools to write the byte code out directly, since the classes are so simple: all they do is subclass the Hecl and SubHecl classes so that methods fall through to those.
  7. But instead, I just write out the .java code and compile it.
  8. The two new classes get stashed inside the copy of Hecl.jar that we’re working with.
  9. We now use the dx command to compile the entire jar into the classes.dex file that Android uses.
  10. Since Hecl.apk is just a zip file, we can replace the AndroidManifest.xml file and classes.dex file with the new files we have created.
  11. At this point, we rename the whole works, and move it out of the temporary working directory to the current directory.

Phew! That’s a lot of ugly work. It’s obvious that a lot of this should and could be handled in a less “brute force” way, but for now it gets the job done. The only thing left to do is replace the Hecl script, which is very easy:

zip -r Example.apk res/raw/script.hcl

This makes it possible to whip out new Android Hecl scripts without carrying around a lot of project infrastructure – all you really need is AndroidBuilder.jar and your script.

Naturally, all of this is very much a work in progress, so it may not work as advertised, and, given the mess that we go through, bugs are likely as well. Do report them if you find any, and have fun! In the future, it will be necessary to make it so that it’s possible to add resources to the apk file with zip, and have them be available to Hecl applications, which was actually the focus of the previous article.

Bypassing Android’s “Resources”

Android relies heavily on something called “resources” to access portions of the system that aren’t directly in the Java code. For instance, if you had an ogg file that you wanted to use to play a sound with, you would add it as res/raw/foo.ogg. Then, during compilation, the aapt tool transforms all your resources into a Java file called R.java, which is basically a series of integers that can be used to reference the actual resources. For instance, to set up our hypothetical ogg file, we would do:

MediaPlayer mp = MediaPlayer.create(context, R.raw.foo);

That’s all well and good as far as it goes, and if you don’t have any reason not to use it, you should.

However, with Hecl, one of my goals is to make it very easy to create applications. This means:

  • No complex directory layout structure.

  • No big long build files that require tools like ant.

  • No complex compilation steps.

I’m not there yet, on the Android platform, which is a bit more complex than Java ME (where it is indeed so easy to create new Hecl .jars that it’s possible to do it on the fly via this web site: http://builder.hecl.org), but I am making progress.

One thing that a user might want to do is add their own files to the .apk file that actually gets installed on the phone. Since it’s just a zip file, that’s pretty easy:

zip -r Hecl.apk res/raw/foo.ogg

However, without a resource, there is no way to access that file, so the question is: how can we get ahold of it somehow?

The first piece of the puzzle is Java’s ZipFile class, which lets us open up a zip compressed file, such as the .apk file that contains the ogg file we’re interested in in this case.

The critical passage is getting ahold of the actual filename on the Android filesystem of the code that’s currently running. It turns out to not be too difficult, if a bit obscure:

String filename = this.getPackageManager().getApplicationInfo(packagename, 0).sourceDir;

With that information in hand, we can feed it to ZipFile, and then access the contents at run time.

It’s not a perfect nor complete solution, yet. So many of the Android API’s require a resource ID, so it takes some extra coding to do things from the InputStream that we can get out of the zip file, but there may be ways to make this easier. For instance, we could simply dump the contents of raw/ into an application specific data directory the first time an application runs, in order to at least be able to access the files as files, rather than InputStreams connected to ZipEntries. In any case, at least it’s possible to access files added to the .apk without requiring the compilation step.

I’m not sure how many other use cases there might be for direct access to the .apk file, but if other people need to do this, hopefully this information will be helpful.

iPhone – nice, but not for me

Being fairly interested in this whole mobile phone software thing, it’s pretty hard not to sit up and take notice of the iPhone SDK that was just announced. A few thoughts:

  • Of course it’s nice, polished, and lets you do some interesting things with the phone. That was not too surprising though.

  • The “investment fund” seems like something of a response to the Android Developer Challenge, and is certainly an interesting move on Apple’s part. Strategically, it certainly alters the focus of the money flowing into the equation. Comparing the two, the dollar amounts in the ADC are much smaller, but they are also more “concrete”: N people will get X dollars in time frame T. As an independent developer, that works fairly well for me as something to have a go at and then move on from, win or lose. An investment fund backed by Kleiner Perkins is on a whole different scale and level. The amount of money is huge (they won’t even bother with less than 100K), but who knows how that works out in practice, or how much of that is actually 100% committed to iPhone companies. KP aren’t the sort of people that are going to just hand that out to any old company that does an iPhone app. They’re also going to want to be involved in the companies, which means there won’t be too many of them. In short, it brings in all the good and bad aspects of venture capital. In short: in one corner, lots of money for relatively big, organized players, and in the other, small, quick incentives to do cool things that anyone has a shot at. We won’t really be able to judge the results until we see how it plays out. (As an aside, I still have some bad memories of the management that KP foisted off on Linuxcare).

  • The system for selling applications is smart. You will have one place to go to to get your apps, and as a user you’ll have some assurance that they’re up to certain quality standards. People will like that kind of thing, and is far nicer than anything JavaME ever had. Instead of multiple, fractured marketplaces, you get one big one. Of course, it also keeps Apple in control of everything, which also has some negative implications as well.

  • 37 Signals seem to be wildly ecstatic about Apple’s potential, but I’m a bit less sanguine. I don’t doubt that the iPhone has a bright future, that much is pretty obvious. However… dominate? First of all, if it’s so widespread, it loses some of its cachet, doesn’t it? I don’t think Apple wants that. I don’t see them licensing the system so that other phone manufacturers can use it, so it will only be adapted as widely as Apple sees fit. Cheaper Nokia models are very popular over here in Europe. Will iPhones at those price points (less than 100 euro) be available and competitive with Nokia’s offerings? Open and adaptable aren’t necessarily things that Apple is known for, which might be the difference between “strong presence” and “dominate”, as we saw with Mac vs Windows. Getting 10-15% towards the high end of the market might be what they aspire to, rather than ubiquity.

  • I could, however, very easily envision Apple getting into some kind of business niche and dominating that completely, as they did with education for a while. That might not extend to the broader market, though.

  • Moving on to other considerations, this one is important to me: apparently, according to the license agreement, you can’t run an interpreter that fetches code and executes it, amongst the various other restrictions! That’s kind of a deal breaker for Hecl, which counts that ability as one of its strengths. I don’t think a port was in the works in any case since Hecl is written in Java. Still, though, that’s the kind of annoying restriction that reaffirms my commitment to open source.

  • As an aside, I wonder what Sun is up to at this point – between this and Android, they have to be under enormous pressure to release a successor to Java ME, which was a good effort for its time, but is beginning to look dated and limited, even though it is very widely deployed.

I’m an open source guy at heart. I love openness and source code and the freedom to tinker… and while I’ll certainly give Apple their due for making some fine products, closed and controlled is simply not for me. Others may feel differently – it’s a matter of taste and preferences.

So for the moment, my efforts will be focused on Android, which I see as the best, most open thing out there at the moment.

Remote Android Application Shell with Hecl

The latest component of Android Hecl that I have been working on is a simple shell server that lets you interact with Hecl remotely, in order to be able to telnet in, run commands and see what they do. In order to give it a whirl, fetch Hecl from svn:

svn co http://hecl.svn.sourceforge.net/svnroot/hecl/trunk/hecl hecl

Then install the Hecl.apk:

adb install android/bin/Hecl.apk

Select the Hecl Server demo, at the bottom of the list:

Hecl Server

You also need to make sure that port forwarding is set up:

adb forward tcp:7405 tcp:7405

At this point, you can telnet to port 7405 on localhost, and run Hecl commands:

> puts hi
hi
> androidlog "hello world"

> java android.telephony.TelephonyManager telman
telman
> set phoneinfo [[activity] getSystemService phone]
android.telephony.TelephonyManager@435c8f80
> $phoneinfo getLine1Number
15555218135

Since this simple server runs in its own thread/interpreter, you can’t control the GUI from it. To do that, there is a ‘maineval’ function that sends code back to the main interpreter:

> maineval {set layout  [linearlayout -new [activity]]}
1
> maineval {$layout setorientation VERTICAL}
1
> maineval {set layoutparams [linearlayoutparams -new {FILL_PARENT WRAP_CONTENT}]}
1
> maineval {$layout addview [textview -new [activity] -text "Hi!" -layoutparams $layoutparams]}
1
> maineval {[activity] setcontentview $layout}
1

Gets us this very, very simple screen:

Hecl Hi

Hecl’s Java reflection support is still fairly alphaish, but you can use it to query classes about their methods and constructors, and call them most of the time; errors should be sent to me or the mailing list.

I found this little server particularly useful when fiddling around with some of the undocumented android classes, as a way to experiment in a running system.