Munich Android Mobile Meeting

Tomorrow (January 28th), I’ll be in Munich for this:

http://www.mobile-monday.de/web/guest/android

Unfortunately, the event filled up super fast and I barely managed to squeeze in, so there are probably no spaces left. Still, if anyone would like to meet up afterwards, send me an email soonish. If you managed to get in, and would like to see a demo of Hecl, let me know, because I’d like to have the chance to show that off some too!

Hopefully it will be a valuable experience. Android is a pretty convincing system in many ways, but of course has yet to fully come into contact with reality, so I’m curious to meet some of the people involved and hear about it from them. I really hope it doesn’t end up being a rehash of information that’s available on the web, as it’s a two hour drive from Innsbruck.

New Hecl Release

After four or so months, it’s time for another Hecl “checkpoint”. Those who are seriously interested really ought to be getting Hecl from subversion, however, it’s a good idea to tie up the loose ends every now and then, and put something that’s easy to fetch for the casual user.

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

This release contains a number of interesting things:

  • Alpha support for Java integration. For example:

      java java.lang.System sys
      set ms [sys currentTimeMillis]
    
  • Alpha support for Google Android. This utilizes the Java integration system above heavily. What sets it apart from just writing code in Java is that on top of the building blocks, I’ve started to write little bits of Hecl code to make common things very straightforward, layering on top of the Java API.

  • Numerous fixes in the code, documentation, and build system.

Tcl and Tk 8.5 out

Actually, they were out a few weeks ago, but I’ve been on vacation at the in-laws’ near Monselice, Italy. The release announcement is here:

http://www.tcl.tk/software/tcltk/8.5.tml

It’s worth writing about, because it’s been a long time in the making, and contains a number of improvements. Particularly noteworthy in my opinion are ‘real’ hash tables (dicts), an even better sandbox model (Tcl and Java have the best sandboxes that I know of), and a theme engine for Tk that, in the right hands, significantly improves how it looks.

In the fickle world of language fads, Tcl doesn’t get the respect that it should these days. Like anything, it has problems, but what doesn’t, when examined up close? It’s ironic that for years it has gotten things right like Unicode support, and OS-level threading, that are currently being worked on in Ruby.

Hecl obviously owes a great deal to Tcl, but since it’s a new language (more or less necessitated by cell phone limitations), we’re free to take a stab at improving things that Tcl doesn’t get right.

In any case, Tcl is worth a look if you want to play with something that’s fundamentally different from a lot of popular languages, and the core team has put a lot of work into the latest release, so give it a whirl (perhaps using tkcon instead of the readline-less tclsh executable) with an open mind, and see if you don’t learn something new.

Android Task List / Selector

Credit for finding out how to do this goes to dims, who has been doing lots of cool Android hacking lately:

http://davanum.wordpress.com/2007/12/18/android-task-manager-primitive-prototype/

If you’re interested in Android and what it can do, you should be following his online journal!

As a way to keep pushing and testing Hecl, I decided to see if I could copy what he’d done in Hecl. It has indeed produced a few improvements to the Java code. That done, making it work was short and sweet:

set layoutparams [linearlayoutparams -new {FILL_PARENT WRAP_CONTENT}
set layout [linearlayout -new $context]
$layout setorientation VERTICAL

# Create ourselves some commands to access Android internals.
java android.app.ActivityManagerNative activitymanagernative
java android.app.IActivityManager iactivitymanager
java {android.app.IActivityManager$TaskInfo} taskinfo
java android.content.ComponentName componentname

java java.util.List javalist
set am [activitymanagernative getdefault]
set tasks [$am gettasks 10 0 [null]]
set len [$tasks size]
set tasklist [list]
for {set i 0} {< $i $len} {incr $i} {
    set baseactivity [[$tasks get $i] -field baseactivity]
    lappend $tasklist "Task: [$baseactivity getpackagename]"
}

$layout addview [textview -new $context -layoutparams $layoutparams 
         -text "Currently running tasks"]

set lview [basiclist $context $tasklist -layoutparams $layoutparams]
$layout addview $lview
$lview requestfocus

set callback [callback -new [list [list SelectTask]]]
$lview setonitemclicklistener $callback

heclcmd setcontentview $layout

proc SelectTask {parent view position id} {
    [activitymanagernative getdefault] movetasktofront $position
}

It could have been squeezed further by avoiding some of the temporary variables, and perhaps also by implementing a mapping between java.util.List and Hecl lists.

I think one of the next things I want to do is work on some infrastructure so that people will be able to actually work on scripts on their phone. That’s sort of a departure from the past (most phone keyboards would be sheer torture for inputting long scripts), but it’s a cool demo in any case.

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.