Announcing Hecl Android Alpha and Hecl Android Developer Challenge Entry

Alpha Android Hecl

I’m a little bit nervous about the idea, but I decided that I might as well release early, because for the foreseeable future, there’s always going to be “one more feature” I could add.

So, without further ado, I’m going to announce that I have a version of Hecl running on Android that has a access to a number of Android wigets. It is available here:

http://www.hecl.org/Hecl.apk

Hecl Android Demo

However: this is an alpha release, for those interested in hacking on it. If you are bothered by broken code, don’t like to report bugs, don’t want to think about how to improve it, complain loudly about missing features, then this code is not for you yet. Soon, I hope, it will be stable and useful for many more people, but for the moment, a lot of things are up in the air:

  • Hecl’s java reflection interaction code, which lets us interact with Android’s API without writing wrappers for all of it, is very new. It was written specifically to facilitate the Android port. So it might change. For hackers, the opportunity is to be able to take part in shaping it.

  • More work needs to go into the threading model. Wolfgang Kechel built a nice system of timers and events into Hecl, but work will be needed to make those play nicely with Android.

  • More API coverage is needed. We have a number of the basic widgets, but more are needed.

  • Storage is still something being worked on. I want to have a Hecl script Provider so that other applications can access Hecl scripts, as well as Hecl commands to access the various types of permanent storage that Android makes available.

  • Phone features – we want to have callbacks and commands to deal with phone features like sms, phone calls, the camera, and so on.

  • Needless to say, none of what currently exists is documented besides some comments in the code!

Of course… Hecl itself is a pretty cool language, I like to think, and the fact that it’s interpreted means that you don’t have to recompile anything, you just

  1. Edit your script.
  2. Replace the script in the zip file: zip -r bin/Hecl.apk res/raw/script.hcl
  3. Load it up on the emulator: /opt/android_sdk_linux_m3-rc37a/tools/adb install bin/Hecl.apk

You can do other cool things too, like downloading bits of code over http and running them.

Hecl @ Android Developer Challenge

I hope that people interested in hacking on Hecl will do so because it’s fun and interesting. Being an early adopter means you have a lot of influence in shaping the future of this programming language, and the API’s to interact with Android (or Java ME if that’s your thing).

However, seeing as how Google has put together this contest:

http://code.google.com/android/adc.html

I have decided to submit the Android port of Hecl, and split the prize money, should we win any, with those who have contributed to Hecl. That includes any and all contributions to Hecl, whether or not it’s directly related to Android or not. So, with documentation, Java code, Hecl code, tests, and even improvements to the web page as valid things, there’s room for everyone to get involved. Of course, in order not to paint myself into a corner, I reserve the right to make final decisions about percentages, but it should be roughly equivalent to lines of code added, deleted or modified (yes, deleted counts too – if you make the project that much slimmer without sacrificing functionality, that’s wonderful!).

Also more than welcome are people who want to submit their own projects to the developer challenge based on Android. We make no claim to your winnings, and we’ll be happy to give you a hand.

Interested?

Join the Hecl mailing list, that’s where all our discussions take place:

http://sourceforge.net/mailarchive/forum.php?forum_name=hecl-devel

Hope to see you there!

Android Bug: Radio Groups/Buttons

This was informally confirmed by someone at Google:

Given this layout code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  id="@+id/ll">
</LinearLayout>

And this Java code:

public void onCreate(Bundle icicle)
{
    super.onCreate(icicle);
    setContentView(R.layout.main);

    LinearLayout ll = (LinearLayout)findViewById(R.id.ll);

    RadioGroup rg = new RadioGroup(this);
    rg.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));

    ll.addView(rg);

    RadioGroup.LayoutParams rlp = new RadioGroup.LayoutParams(
        RadioGroup.LayoutParams.WRAP_CONTENT,
        RadioGroup.LayoutParams.WRAP_CONTENT);

    RadioButton foo = new RadioButton(this);
    foo.setText("foo");
    foo.setLayoutParams(rlp);
    rg.addView(foo);

    RadioButton bar = new RadioButton(this);
    bar.setText("bar");
    bar.setLayoutParams(rlp);
    rg.addView(bar);

    RadioButton baz = new RadioButton(this);
    baz.setText("baz");
    baz.setLayoutParams(rlp);
    rg.addView(baz);
}

The resulting radio buttons fail to be mutually exclusive. This is a bit frustrating, as Hecl doesn’t utilize the XML layout system. Or rather, you can use that if you want, or you can create everything in Hecl.

All radiobuttons selected

The individual at Google says the problem will be fixed in a future SDK release, so hopefully things will work in the near future. I also hope it’s open sourced soonish, so that problems like this can be fixed without requiring the intervention of “the hand of Google”. Of course, in this market, there’s also the issue of things being shipped on hardware, where open source or not doesn’t matter much if the bug is out there, because you simply can’t fix it on millions of devices. Hopefully the product that ships will be very solid.

Hecl Java Integration

Yesterday, I took the Java integration I’d been building into the Hecl Android port, and moved it into its own directory, so now it’s built in to the command line/j2se (non JavaME) version of Hecl for people to experiment with. Here’s a slightly modified version of my posting on the subject:

I created a java/ subdirectory with an org.hecl.java package that does a few things. The core of the system is in JavaCmd and Reflection. The first creates commands out of Java classes. It utilizes, obviously, the reflection stuff in Reflection. Both are, at the moment, sort of hacky and tentative, but work. For instance, I checked in this bit of test suite today:

test java-2 {
   java java.util.Hashtable ht
   set hasht [ht -new {}]
   $hasht put foo "bar"
   $hasht get foo
} {bar}

The ‘java’ command takes a class, and string, and creates a new Hecl command ‘ht’ that is tied to the Hashtable class.

ht -new {} (could have been -new [list]) is how we call the constructor. After this, $hasht points to a Hashtable, and we can utilize its methods. Up to a point, at least – there have to be mappings in Reflection to deal correctly with types. For instance, there isn’t a mapping for Enumeration right now, so calling the
.keys() method wouldn’t do the right thing.

There are some more bits and pieces of magic:

  • Static methods are called like this:

      java java.lang.System sys
      sys gc
    

    Which calls System.gc().

  • Static fields, for now, are called like this, although once again, I’m still thinking about the syntax:

      sys -field err
    

    Which is of course System.err

  • In addition to -new, when you instantiate an object, you can pass in things like -text "foo", and that will attempt to call setText("foo"). This is a convenient way, in Android, at least, of setting up an object from within Hecl, without requiring the use of the XML layout files.

  • Furthermore, in order to deal with constants in a convenient way, something that’s reasonably common in certain types of API’s (like Android), the following pieces of code are equivalent:

      layout.setOrientation(LinearLayout.VERTICAL);
      $layout setorientation VERTICAL
    

    What’s happening in the Hecl code is that VERTICAL is looked up in the context of the class that’s calling it, so as to avoid having to do [layout -field VERTICAL] to fetch the value. This won’t work everywhere, and I suppose it could be criticized as too much ‘magic’. Naturally, these sorts of things will evolve as the code does, and survive if they’re useful and don’t create problems, or die out if they cause bugs and aren’t utilized.

There are some open issues with the ongoing work:

  • I like the API for already instantiated objects, but I’m not 100% wedded to the idea of -new for instantiation. The fact that it takes a list is also a bit tricky, because if you want to do something like construct an argument to be passed in, you might end up with something like this:

      callback -new [list [list SelectDemo $spinner]]
    

    which I don’t find all that pleasant. You can’t just do -new [list
    SelectDemo $spinner]
    because then it thinks it’s got two arguments,
    when it really only has one, which happens to be a list.

  • The APIs created like this tend to follow Java pretty closely, for obvious reasons, and can be a bit verbose at times. I’m still thinking about this. One idea might be to provide Hecl wrappers that make common operations easier.

Android, Hecl and scripting

Aside from my hard drive crash, I’ve been working to figure out the best way to integrate Android and Hecl. Since Hecl has been a small, simple, humble language suitable for J2ME (or JavaME or whatever), that has meant in the past that it hasn’t really been able to utilize more complex or advanced Java features like reflection, because they aren’t present in the J2ME API. Android’s more complete implementation of “Java” (yes, I know about the VM, but the programming language I’m writing in is still Java) opens up a number of possibilities.

First on my list of things to do was aim for a generic access layer for Java objects using reflection, so that you can do things like this:

$button settext "blah blah"

Now that we have that working, I also wanted to be able to create new Android widgets from Hecl, which is where things get tricky. The “problem” is that I want something that feels like a scripting language, not simply a reflection of the underlying Java. For instance, the XML description of a button looks like this:

<Button id="@+id/execute"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Execute"
/>

Which is actually not so bad in terms of verbosity. The Java code is uglier, in my opinion:

Button button = new Button(activity);
button.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
button.setText("Execute");

Especially the LayoutParams business, that is nowhere near as clear as the XML, where it’s very obvious what values you’re giving to what parameters. You wouldn’t know that from the Java code without looking up the constructor for the LinearLayout.LayoutParams class.

Now, with Hecl I could of course write the code by hand to make things ‘friendlier’, but I’m worried that if I have to do that too much, it will take a long time to cover the rather extensive API, and it will also bloat the code. The ideal solution would be subvert the XML stuff for my own purposes, however, that’s proving tricky. My first idea was to utilize the AttributeSet parameter that you pass to many View derived objects’ constructors, in order to transform code like this:

button -text hello -layout_width 100 -layout_height 34

Into something that Android can digest. Unfortunately, that’s proving difficult, as the internals of Android barf on the class I’m passing them, even though it, to my knowledge, implements the AttributeSet interface without any problems.

This really made me wish that they would hurry up and release the source code, because I would have a far better understanding of the relationship between the XML attributes and the Java methods if I had access to what’s going on under the hood. Harumph.

My next attempt was a hack that takes things like -text and transforms them into calls to setText, but that isn’t as good, because you need a little bit more custom code for things like the layout parameters, which require special handling. However, lacking the source code, or some help from the Google folks with access to it (which isn’t likely over the next few days as they’re doubtless off consuming turkeys), it looks like that’s what I’ll have to do.