Programming

setForeground() missing in Android 3.0 services

While upgrading my application’s code to use the Android 3.0 SDK, I stumbled upon a small problem in my service code: the setForeground() function has been removed from Android 3.0 completely, making the compilation process fail. The function removal broke the compatibility methods I had copied straight from the Android documentation ages ago for backwards compatibility:

Note: The methods startForeground() and stopForeground() were introduced in Android 2.0 (API Level 5). In order to run your service in the foreground on older versions of the platform, you must use the previous setForeground() method—see the startForeground() documentation for information about how to provide backward compatibility.

(So in other words, if the user’s device has Android 2.x, the startForeground() function must be called. If the user’s device has 1.x, setForeground() should be called instead. setForeground() will not work on Android 2.x, and vice-versa).

Fortunately, this was an easy fix – the documentation has been updated with a new, more future-proof code sample for calling the right functions depending on the device’s Android version. In my case I had modified the old sample code to my liking, so I decided to add a small function to my service that will call the setForeground() method through reflection instead:

Problem solved!

Comment icon 1 Comment »

Announcing Simple Textile for WordPress

Simple Textile is a simple, easy-to-use WordPress plugin for processing posts with the Textile markup language. ST uses the latest 2.2 version of Textile, taken from the TextPattern source code, ensuring full compatibility with the latest versions of PHP.

One thing I have always hated in WordPress was the process of actually writing something: because of the default WYSIWYG editor, which functions like a word processor (but doesn’t work nearly as well). I prefer writing things in markup languages such as Markdown or Textile. And in order to solve this problem, I decided to write Simple Textile. Other plugins do exist, but when testing them, many did not function at all and others were buggy.

Using ST is simple: when writing a new post (or editing an old one), you will see something like this below the editor:

Simple Textile toggle in the editor

Simply tick the checkbox, and all default HTML filtering will be disabled for the entry, and the posts will be rendered through Textile instead. HTML can still be mixed with the Textile markup in case you want to add images or media, or to do other things that you can’t with Textile.

The source code of the plugin is available at GitHub, and feedback is welcome!

Comment icon Read more or comment »

Introducing Desktops51

Have you ever wanted to see what kind of computer desktops other people have? Even if you haven’t, why not waste some time browsing and rating them at Desktops51 – a new project of mine!

Desktops51 is a free website that allows users to upload and browse computer desktops. No registration is required to browse or upload, either!

So hop on and browse some wallpapers. And why not upload one while you’re at it?

And now for the technicalities

Desktops51 is developed with the Ruby on Rails web framework. PostgreSQL is used for data storage, and nginx is used with Phusion Passenger to run the website. For hosting, a simple Linode 512 VPS is used (they’re the best VPS host in the whole world, but that’s a story for another day!)

Images are currently stored locally and processed using RMagick (a Ruby ImageMagick frontend) into numerous thumbnail sizes for use throughout the site. In case the site gets more popular, I will have to look into other options like Amazon S3.

In summary

Desktops51 has been a fun project to implement in the past week. But we’re not done yet – here’s some ideas that I have:

  • The ability to give sources to wallpapers; one should be able to give a link or upload it directly
  • Notifications: get an e-mail if someone comments on your desktop, etc.
  • Better administration tools: the moderation features are very minimal at this point
  • A “Desktops51 e-mail newsletter”: users can opt in to receive one every week with the most popular wallpapers and more!

I hope to get all this done soon!

Comment icon Read more or comment »

A new way to blog

If you’ve ever visited my website before, you’ll probably notice that the template has changed once again. But that’s not all – some major changes have also been made under the hood…

My website/blog-that-I-don’t-write-on-very-often is now powered by nanoc, a static site generator. What this means is that nothing dynamic happens when you load a new page – it’s all been generated beforehand. I already had experience using nanoc with the website for my “brand name for freeware projects”, rojekti, so this wasn’t a very difficult process.

There wasn’t anything wrong with my previous blogging softawre, WordPress – I just find nanoc more comfortable to use, and writing is much easier to do in vim instead of a WYSIWYG editor.

In any case, here’s a small outlining of the steps involved.

Creating the new layout

Some experimenting with gradients and color schemes in Photoshop, and a couple of hours to write the markup and stylesheet.

Nothing special here.

Creating the nanoc website

But this is where things get interesting. First off, I adapt the new website layout to the nanoc project, and I enable and configure some things like the blogging helper.

But what’s a blog without posts? I obviously want to have the old posts I had on the WordPress installation to be on the website.

Well, in the one and a half years I’ve had this blog I haven’t written that much, so I “migrated” new posts completely by hand. This was a good idea in any case, because it gave me the opportunity to rewrite the posts in Textile).

And once I’m done, I add an Atom feed and a sitemap file for crawlers. I make the front page display the latest 5 posts and the rest in a simple list: I’ll have to look at generating real pagination later.

But what about comments?

Of course, when your blog is statically generated, a dynamic commenting system might be rather difficult to implement. Fortunately, there are services to provide commenting for websites.

One of them is Disqus. Many WordPress/Blogger/whatever users actually use it instead of their built-in comment systems because it’s better.

And best of all, Disqus can be embedded to any page using a few lines of JavaScript – so that was easy!

…But what about the existing comments?

Not that my blog has ever been a very popular internet destination, but there are some useful comments on the posts I’ve made. So how do I migrate them into Disqus?

Disqus provides an API. It has a function to create new comments. And to make things even easier, someone’s made a Ruby wrapper for the API!

So I export comments using phpMyAdmin to CSV, and write a quick Ruby script to read them in and send them to Disqus. Easy!

What else?

I really like the “new workflow” of writing posts – it could actually inspire me to write more often. A major problem I had with WordPress was the WYSIWYG editor: it’s just painful to use. This is obviously a problem common to any publishing application.

I already mentioned this before, but by default nanoc processes my blog posts as Textile). Textile is a simple markup language designed exactly for the purpose. And if I never need HTML, mixing it with Textile is very easy.

Using a static site generator feels just right to me, but having to execute nanoc create_item posts/hello-world might not be fit for everyone. And that’s exactly why software like WordPress exists. Use the right tool for the job!

Oh, and one last thing – I also version the blog using git. A great example of how versatile modern DVCS’s can be.

Comment icon Read more or comment »

Why can JavaScript & alert lock up a browser?

JavaScript often provides “Web 2.0″ functionality to websites. But one of the simplest commands that is still being used to date is the alert box:

Simple, right? Useful? Yes. But what about websites with malicious intents (we’re using this word in a “light way”; what can JS do?): what if we want to lock up the browser?

Create an infinite loop of alert boxes. The only way out seems to be killing the browser process (or with Chrome, the tab process). This brings us to an question – why?

A good example of this is how in Firefox 2, the Password Manager dialog was intrusive, like this (it also blocked any user input until a button was pushed):

While in Firefox 3, it shows up a prompt like this at the top of the page, that does not block any user interaction (like an alert prompt):

You can see where I’m aiming at. Why can’t we have something like this for the alert box too? Why does the alert box block the browser? The user should still be able to interact with the browser even with an infinite loop of them. A similar solution is used for some security alerts in Internet Explorer, but not any user JavaScript.

There’s actually a Mozilla bug tracker issue about it. From 2000.

Related reading here, here and here.

Comment icon 3 Comments »