I gave myself a personal goal around mid to late November; to finish an STO Android app with the dataset that Phil Casgrain had given me access to.  The app is now available on the Android market as STO on the Go though it’s almost impossible to find using the search.

I’m selling the app for $1.00 though there is a good chance it will be getting an increase in the next version, which will have at least maps and favourites features.

As I mentioned earlier I decided to start playing around with Clojure on my Windows box. This was an excellent way to get a quick introduction to the language, although there were aspects of the Windows platform that made going any further than toying around somewhat pointless. Although, this could simply be my bias towards Unix style systems.

When I decided to set up clojure on my Ubuntu installation, it felt like there was a huge wall in my way that made absolutely no sense. This was really because I had made a few mistakes when installing my editor of choice (emacs) from apt. That being, I had installed various emacs plugins such as swank and slime which caused some really frustrating problems to arise when running swank-clojure. I found in the end, the easiest thing to do is get a full install of emacs going, without any extra bells and whistles; that’s what the ELPA is for, and it does a damn good job at it.

Once my emacs was back to a good state, I was able to get the clojure-jack-in function to work the way I was expecting it to. Finally I was able to get hacking! On the windows box, I was (and still am) doing a text adventure game from the Land of Lisp book. I’ve found this to be an excellent way of stepping into the language, especially since the book assumes you are using common lisp. This has lead to a fair amount of research into finding out clojure equivalents to CL macros. Meanwhile, on my Ubuntu machine I’m working on a project that I’m hoping will make one part of my life a bit easier.

I’ve found that when it comes to doing tasks, such as groceries, it can often be a pain. My partner and I use our phones for entering our grocery list and each time it’s excruciating. These keyboards are annoying to use, our Nexus ones have those poorly placed heat sensitive buttons that your thumbs always manage to hit, and the program we are using doesn’t learn very well. So I’ve decided that the first step in making this better would be to have an application that makes at least entering the data easier. I’ve decided that I’m going to implement this tool using Clojure, because I’ve found I’m loving the way you do things in the language (it feels more mathematical). I also feel that since I’m extremely green with the language that I’ll be required to keep things simple (I still cannot get [& args] working for whatever reason!).

In an attempt to keep things simple, I’m avoiding the database… well that’s a lie. I’m avoiding the Relational database. I’ve always found that when I use a tool like Rails I tend to over-complicate my models for whatever stupid reason and it just leads to me abandoning the project.  I’m also interested in playing with document stores (in my case MongoDB) since I love to use hashes/JSON.  It just so happens that there is a library called congomongo which is pretty easy to setup and start playing with MongoDB. Once I’m done getting this part of the application working, my next plan is to play with the Noir microframework (similar to Sinatra or Flask) to implement my web app.  If I decide to continue working on this, it probably won’t scale all that well, but at least it’s simple enough to not overwhelm me at first.

A while back a friend of mine suggested I should get into programming some clojure. I figured I would, but wanted to get my environment working. This included all of the setup for emacs to get slime, swank, leiningen, etc working, which I still cannot really get working.

I was looking around for something that would at least get me knocking out some clojure code before I commit any efforts to actually getting my environment working on. I discovered this awesome prebuilt package called Clojure Box. I don’t really know what is all there, but at least it got me up and running with clojure so I can play around in the REPL while learning the language.

I am a bit bummed that the tool is windows only, but it’s not a big deal for now since all I’ve really been doing is writing snippets in buffers I’m probably going to throw away.

As for learning the language I’m starting to follow this introduction to clojure guide I found. It *really* covers the basics, but every time I try to jump too far ahead in new languages I just get lost and confused anyway.

As part of my day job at Shopify I’ve been implementing a new front end using the language CoffeeScript. Originally, the front-end was regular JavaScript, but since Rails 3.1 will be supporting CS we might as well start using it.

As a whole, the language isn’t that bad. I do appreciate many of the helpers it provides for enumerating hashes and arrays, and bindings (aka fat arrow) are awesome. It’s made writing some complex client code that relys on context far easier, especially since the new features like .bind aren’t available on most (if any) mobile devices (although this is easily fixed with something like 5 lines of code).

My main complaints about CS are with some of the edge cases it has. For example, I needed to have a bound anonymous function within a setTimeout. In JS I would typically perform something similar to:

setTimeout(
  // May be a little bit off here, but I think that's how to do ECMA5 binds
  function(){ this.doSomething(); }.bind(this),
  150);

The above is fairly straight forward, and the language doing anything I’d consider unexpected. Though if we were to do the same thing in CS, I’d expect it to be something like this:

setTimeout( => @doSomething(), 150)

This actually doesn’t work, the coffee compiler will complain. It’s somewhat understandable, there some weird stuff going on here and it’s difficult to parse. A solution I often used was this:

setTimeout( (=> @doSomething()), 150)

Now I have all these extra brackets that I shouldn’t need, and it’s frustrating because I’m trying to use th language the way it seems to have been designed. Now this had been bothering me for some time now, and decided to twitter rage/complain about it. This did get some attention from someone who runs the CS twitter account who gave me some suggestions. Those of which I found to be somewhat hacks, or wouldn’t work in my case.

One solution was to have (in my case) the fetch method bound to the products instead of having a fat arrow function declaration. Now, since the actual model that products Points to is a Spine model, I really don’t feel like going in and hacking something (that already works the way I want/expect) for such a minor (but annoying) problem with CS. Another solution was to essentially create a function that is in essence setTimeout but reversed, so it’s something like:

delay = (delay, function) ->
  setTimeout(function, delay)

Which I’m also not super keen on, once again it’s going through a bunch of extra steps to fix an inherent problem with the language itself. Now I often do mix up the arguments used in setTimeout, and I would love to see that get fixed/added in the future.

I mentioned the problem I was having to one of our awesome (and way smarter than me) interns about it and he actually came up with an almost perfect solution.

setTimeout( =>
  @doSomething()
,150)

Aside from that preceding comma and needing to remember to un-indent, it’s not a big deal and I like that I’m not having to go in and hack apart Javascript to fix minor issues.

Although I do seem to be a bit bitter about my experience with CoffeeScript, I do have to say that I like what the language is bringing to the table, I just hope it gets some improvements for CS 2.0 or something. Perhaps having some kind of explicit scope declaration (like begin end or curly brackets), would help out a fair amount. Though, I think I can keep dreaming for having curly brackets, there seems to be some kind of aversion towards them.

I’m really curious to know why initializing objects, primarily Hashes (maps, dictionaries, key-value lookups, whatever you want to call them) are such a pain to initialize.

In your typical Java scenario this is how you initialize your Hash:

HashMap hashyMcHash = new HashMap();
hashyMcHash.put("foo", "bar");
hashyMcHash.put("car", "jar");
hashyMcHash.put("arr", "rawr");
// And... so on

Meanwhile, most sane languages allow you to do crazy things like this:

hashyMcHash = {foo: "bar", car: "jar", arr: "rawr"};

Now, in languages like Python, Ruby, JavaScript those are just parts of the language and you kinda get them for free, so I can understand something like that not being available in Java. Now, lets look at another statically typed language that I think took a fair compromise between how to initialize a Hash.

NSDictionary hashyMcHash = [[NSDictionary alloc] initWithObjectsAndKeys: @"foo", @"bar", @"car", @"jar", @"arr", @"rawr", nil];

To me this seems like a pretty fair compromise for initializing our Hashes since it allows me to state what some default values in it will be in code that is fairly readable. I have an idea of how one could do it in Java, though don’t know if it exists or not.

// Constructor would probably look like this:
// HashMap(K[] keys, V[] values)
HashMap hashyMcHash =
                new HashMap(
                    ["foo", "car", "arr"],
                    ["bar", "jar", "rawr"],
                );

If this does exist, please let me know because I’d really love something like this. If it doesn’t… why?

So let’s say you have an application where you need to store the users password. Perhaps it’s a configuration setting that they need to use to log into some service you use or have created. The easiest (and most insecure) way to store the password would be to use the SharedPreferences for your application.

So in our preference XML we have an EditTextPreference which will be used to store the password. Though, we need to do a few things first otherwise the password will be plainly visible and there is the chance that the password might get saved in the auto-complete for the keyboard (SwiftKey is particularly bad for this).

For sake of argument, lets say our preferences xml is called “preferences.xml” and the key for the password is “password”.

What you will need to do is the following

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceClickListener;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MyPreferenceActivity extends PreferenceActivity implements OnPreferenceClickListener {
    private Preference passwordPref;

    public void onCreate(Bundle icicle){
        super.onCreate(icicle);
        addPreferences(R.xml.preferences);

        passwordPref = findPreference("password");
        passwordPref.setOnPreferenceClickListener(this);
    }

    public boolean onPreferenceClick(Preference preference){
        if(preference.getKey().equals("password")){
            EditTextPreference pref = (EditTextPreference) preference;
            EditText field = pref.getEditText();
            // This informs the keyboard not to show up the autocomplete.
            // Ensure that you have set this input type, otherwise users
            // may complain that your application is saving their passwords.
            field.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
            // This gives us the masking that you see in your password fields
            field.setTransformationMethod(new PasswordTransformationMethod());
        }
        // We still return false so that the system will handle everything else.
        // We just needed to ensure that if this preference is the password pref
        // that it's properly protected from prying eyes.
        return false;
    }
}

And there you have it. With this in place, entering password with the on-screen keyboard won’t show any text or save it.

As I mentioned earlier, this doesn’t save the passwords safely to the device. On a normal device this isn’t that much of a concern, though you should probably inform your user that the passwords are not encrypted. This issue becomes a bit more serious if there is malicious software on a device that is also rooted, since the naughty app could scrape through all the data directories looking for usernames and passwords stored in preference files.

For an application I’m working on I needed to create a slider that would allow the user to easily create custom colours. While trying to look around on the internet The most common response was the following:

There is no “slider” component in the BlackBerry API — you’ll have to invent it.

Or this:

The GaugeField can be used for user interaction. With the field focused, the user can press Alt and roll the trackball to change the slider value. Alternatively, the user can press the menu key and choose Change option, which will bring up a dialog with instructions for changing the value.

I’m sorry, but the first solution is kinda bullshit, and the second one sounds like a good way to get a bunch of support tickets from your users saying that your application doesn’t work or is broken or something. Shit I don’t want to deal with.

Anyway, I decided to go out and create a SliderField for the BlackBerry which you can go ahead and use. Currently it displays the slider dongle/widget in blue if it’s inactive or red if the field is in focus. Ugly as old hell, but it’s pretty easy to replace that with something prettier looking.

package com.zeebu.colorgo.field;

/**
* This is released under the public domain, but feel free to give credit to Chris Saunders if you want.
*/

import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.XYRect;

public class SliderField extends Field {

    private static final int MAX_SLIDER_SKIP = 20;
    private static final int BASE_SLIDER_SKIP = 1;
    private static final int NAVIGATION_TIME = 300;

    private int sliderWidgetColor;
    private int scaleColorAtMin = Color.BLACK;
    private int scaleColorAtMax = Color.LIGHTBLUE;

    private int fieldWidth;
    private int fieldHeight;

    private int min;
    private int max;
    private int currentValue;
    private long lastNavigationTime = 0;
    private int sliderSkip = BASE_SLIDER_SKIP;

    private int[] xPts;
    private int[] yPts;
    private int[] colors;

    public SliderField(int min, int max, int sliderWidth, int sliderWidgetHeight) {
        super();
        this.fieldWidth = sliderWidth;
        this.fieldHeight = sliderWidgetHeight;
        this.min = min;
        this.max = max;
        this.currentValue = (max — min) / 2;

        xPts = new int[] {0, 0, fieldWidth, fieldWidth};
        yPts = new int[] {fieldHeight / 4, (fieldHeight * 3) / 4, (fieldHeight * 3) / 4, fieldHeight / 4, };
        colors =  new int[4];
        updateColorsArray();
    }

    protected void layout(int width, int height) {
        setExtent(fieldWidth, fieldHeight);
    }

    public boolean isFocusable() {
        return true;
    }

    public void drawFocus(Graphics g, boolean on) {}

    protected boolean navigationMovement(int dx, int dy, int status, int time) {
        if(System.currentTimeMillis() — lastNavigationTime < NAVIGATION_TIME) {
            sliderSkip *= 2;
            if(sliderSkip > MAX_SLIDER_SKIP) {
                sliderSkip = MAX_SLIDER_SKIP;
            }
        } else {
            sliderSkip = BASE_SLIDER_SKIP;
        }
        lastNavigationTime = System.currentTimeMillis();

        if(dx > 0) {
            currentValue += sliderSkip;
            currentValue = (currentValue > max) ? max : currentValue;
        } else if(dx < 0) {
            currentValue -= sliderSkip;
            currentValue = (currentValue < min) ? min : currentValue;
        } else {
            return super.navigationMovement(dx, dy, status, time);
        }
        invalidate();
        fieldChangeNotify(0);
        return true;
    }

    public boolean touchEvent(TouchEvent event) {
        int message = event.getEvent();
        int touchX = event.getX(1);
        int touchY = event.getY(1);
        if(message == TouchEvent.DOWN || message == TouchEvent.MOVE) {
            currentValue = determineCurrentValue(touchX);
            invalidate();
            fieldChangeNotify(0);
            return true;
        }
        return false;
    }

    protected int determineCurrentValue(int positionInField) {
        float ratio = (float) positionInField / (float) fieldWidth;
        return (int) (ratio * max);
    }

    protected int determineWidgetXPositionOnSlider() {
        float ratio = (float) currentValue / (float) max;
        ratio = (ratio < 0.0) ? 0.0f : ratio;
        ratio = (ratio > 1.0) ? 1.0f : ratio;
        return (int) (ratio * fieldWidth);
    }

    protected void paint(Graphics graphics) {

        graphics.drawShadedFilledPath(xPts, yPts, null, colors, null);

        int cX = determineWidgetXPositionOnSlider();
        int cY = fieldHeight / 2;
        int pX = cX + (cY);
        int pY = cY;
        int qX = cX;
        int qY = cY + (cY);
        int startAngle = 0;
        int endAngle = 0;

        graphics.setColor(Color.BLUE);
        if(isFocus()) {
            graphics.setColor(Color.RED);
        }
        graphics.fillEllipse(cX, cY, pX, pY, qX, qY, startAngle, endAngle);

    }

    public int getCurrentValue() {
        return currentValue;
    }

    public void setScaleColorAtMin(int scaleColorAtMin) {
        this.scaleColorAtMin = scaleColorAtMin;
        updateColorsArray();
    }

    public void setScaleColorAtMax(int scaleColorAtMax) {
        this.scaleColorAtMax = scaleColorAtMax;
        updateColorsArray();
    }

    public void updateColorsArray() {
        for(int i = 0; i < colors.length; ++i) {
            if(i < colors.length / 2) {
                colors[i] = scaleColorAtMin;
            } else {
                colors[i] = scaleColorAtMax;
            }
        }
        invalidate();
    }

}

Here is a little preview of the sliders:
SliderFields in Action!!!

Last weekend I was in Toronto to attend a Conference called gamercamp as well as check out some pretty awesome chiptune artists Starscream and Anamanguchi.

While on my way home, I happened to be on the Tram with some fellow patrons sitting a few seats away from me talking typical geek stuff. Gaming, development, HTML5, etc. Unfortunately someone decided to shoot down HTML5 because “anyone” can look at the code and take it.

Firstly, it shouldn’t matter because there is a good chance not a lot of people are ever going to see or play your games. And even if they do, who gives a shit if they look at it. Most people who would look at it are probably going to be developers curious how you did something and want to learn, which is awesome. We need to share our tricks and ideas in order for all of us to get better.

Now if you want to ensure that people cannot just walk away with your code leaving you crying because they “stole from you”, then apply the GPL to it. It’s pretty much a middle finger to any douchebag who would even consider stealing your stuff, and now you’ve laid down the rules they probably won’t because they don’t want to risk having your license spread into their own code.

Also, when you have gotten to the point where you have gotten big enough for people to steal you stuff it probably won’t matter anymore anyway. So share as much of your shit as possible, at least then you’ll have a better looking portfolio, and hey you may even become famous in the community or something.

It’s been a while since I last posted which really doesn’t count much since it was really just a link to another site.

Ever since the STO site change, I’ve been looking for something else to replace the project. I’ve kinda set my sights on the Rokon game engine for Android  the Rokon game engine for Android because I’ve found that there could be more documentation/demos to help get people started with it. I’m also hoping this will help become an opportunity for me to get more into game development in general. So look forward to crappy games being produced by me in the future!

I’m also working on a template upgrade on the site simply because the default one is kinda boring. It’s still a boxxy looking design (I’m horrible at design) but should be something a little bit nicer. It will should also have a mobile template coming out soon too so that it won’t look disastrous on an iOS or Android device.

That’s pretty much it, for recent things that are development related I guess.

So, the 26th of October has come and gone, and I’d just like to talk about my experience at AndroidTO. Before I start though, I’d just like to say I had a great time meeting with everyone and there is a very good chance I don’t remember your name. I’m not really a name person but if I see you again in public will probably recognize you, if not don’t be afraid to stop me and say hi.

The event itself was really well organized, and Puleen did a great job on keeping me on my toes (which I assume he did with the rest of the presenters as well). He’s a pretty persistent guy, but it always kept my presentation at the front of my mind.

The event ran very smoothly, besides a few technical hiccups such as the wireless; which honestly always happens at conferences. The choice of venue was really interesting; when I first heard that it was at the Polish Combattants Hall was thrown off a tad. This really wasn’t that big of a deal though, because it was right along a bunch of major TTC lines so getting there wasn’t a problem at all.

I was also really impressed with the androidTO app that was done by the great guys at http://mobicartel.com/. I was quite surprised at all the stuff they had done with making it a very polished Android application. From what I saw with Matt and Greg they are a great team, and I look forward to seeing what else they come up with in the future.

From what I’ve read around Twitter and from a few blogs, some of the presentations were a bit over peoples heads. They were pretty technical, especially the one by Manfred about the crazy stuff you can do with maven. Though, as a developer who’s been working with Android for several months now, I found the things he was talking about very intriguing. The tools he mentioned like Robotium and Roboguice look like amazing technologies that people should start looking into. If it were not for presentations such as Manfred’s I would’ve never heard about these tools, and that’s exactly what conferences like these are there for. Hopefully the things that were discussed will save everyone time from looking for these tools, especially when they don’t know what exactly to look for.

It was also nice to have seen Carmen going around taking photos of the event. I am not too sure if she was the official photographer, but it was awesome to see what she was doing. I brought my girlfriends point and shoot with me, but didn’t really want to disrupt some awesome conversations I was having with a lot of the people there. In case the organizers didn’t know, I’m also an amateur (read: wannabe) photographer and would totally be willing to go around an event in the future to take a bunch of shots at the next big Android thing going on. I also feel have some official photographers would help because then we would know in advance where to go to see all the pictures that were taken from the event.

The post event meetup was probably my favourite part of the event. This is always where everyone gets to actually learn more about people since there is actually time. There was of course even more great conversations that were going on, we got to talk about the technology even more as well as just getting to know each other more. I also found it great because I still find it difficult to tell how to know how someone is as a person, especially when most of the conversation I’ve gotten from them was on Twitter, where 2’s u’s and other various tragedies of the new millenium are required to actually say something.

I hope that everyone enjoyed my presentation, and I’ve encouraged you to begin using SQLite in your own Android projects as well. I was a bit nervous while I was up there, especially when my “Oh Shit” moment happened because I had forgotten to turn the wireless on my Macbook back on. Let me know what you liked, and how I could make the presentation better. I really love teaching people, and the only way I will know if what I’m teaching was good or not is if you let me know. Either send me an email, tweet or simply add a comment to this post. Also feel free to grab the code for use in your own projects. I warn you, the code does have it’s issues, and if you add some patches or clean some stuff up, feel free to send me the patch or or a pull request and I’ll be sure to bring it in.

I would like also like to thank my buddy Chris Millward for letting me crash at his place for my time down in Toronto for this conference. It was great to meet up with you again and talk about work, tech and everything else!

© 2011 Christopher Saunders Suffusion theme by Sayontan Sinha