Thursday, September 24, 2009

Android Market Share

@wbm Agreed that android needs more market share before you should care.
Do you think that's true? I beg to differ. Getting your foot in early can earn you recognition on a new platform pretty easily. Let me explain: if you were to develop some awesome app for the iPhone right now, you would find it at the bottom of an over saturated App Store, amongst mostly mediocre products which no one bothers to browse. Most iPhone users just install Facebook, some Twitter client and a couple of silly games. Instead, the Android Market is hungry for new apps (not only because it's new, but also because of the emphasis that Google puts on third-party development and the general openness of the platform), and getting a head start can really make a difference. Your application gets a lot more exposure on the Android Market and the chances of it becoming a hit later are much more increased. Sure, it's a bit more risk, but I think it's worth it.

Tuesday, September 22, 2009

Java: parse XML from the web smartly

Up until recently, I used to fetch the entire XML document before parsing it. I've found that that can be extremely unhealthy for your application, especially if you're developing on Android, where resources are scarce and the GC is very unforgiving. The optimal way to parse XML is to parse it while it's loading, so that resources are freed much more efficient. Here's a snippet of code that demonstrates how to do this: URL oUrl = new URL(sUrl); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); MyXMLHandler handler = new MyXMLHandler(); xr.setContentHandler(handler); xr.parse(new InputSource(oUrl.openStream()));
Syntax Highlighting by Pygmentool
This way, XML is parsed as it comes in, and the application is much faster and smoother. Also, if you don't parse the whole XML this is an even greater improvement, because you can stop parsing when you have enough data (by throwing a SAXException) and it will even stop loading data, which results in faster load times and less bandwidth usage.

Sunday, September 20, 2009

Java Package Mayhem

Here's how a 350-some line Android Activity looks like, in terms of imports: import; import; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import; import; import; import; import android.content.DialogInterface; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.TextView; import com.tastekid.TasteKid.TasteKidUtils.Resource; import com.tastekid.TasteKid.TasteKidUtils.ResourceList; Thank God for Eclipse's Ctrl+Shift+O (yes, I know I bashed Eclipse before, but it seems to behave much more nicely under Ubuntu than under Windows -- it doesn't go above 300MB RAM usage).

Fetch HTTP content in Java

Since programming for Android, I hit my head against every possible snag in the Java programming language. For example, I have to fetch the content of a URL. In PHP, I'd simply do: $data = file_get_contents($url); But no, in Java, no such easiness for you! I had to write my own helper function: public static String getUrlContent(String sUrl) throws Exception { URL url = new URL(sUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.connect(); BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); String content = "", line; while ((line = rd.readLine()) != null) { content += line + "\n"; } return content; } Seems very hackish, especially the line starting with BufferedReader. The whole function is actually composed of bits of code found around the web. Bah, why didn't Google choose Python as their default programming language for Android?

Monday, September 14, 2009

SVN for an Android project

I've started working on the Android project, and I'm using SVN as my version control system (I'm the only one working on it, so I'm actually using it more as a backup and for keeping track of things). A small tip when doing this is to not simply add everything in your project directory to the repository. In one word, don't run: $ svn add * I did so, and three revisions later I was unable to commit anymore, because Eclipse had removed some of its automatically generated files and svn didn't know what to do with them. Here's what you have to have in your repository: $ svn ls .@1 AndroidManifest.xml TODO res/ src/ You don't need the rest.

Thursday, September 10, 2009


Funniest spam I ever got: Hello My Dear How are you? i hope all is well with you, i hope you may not know me, and i don\'t know who you are, My Name is Miss morin khalifa i am just broswing now i just saw your email it seams like some thing touches me all over my body, i started having some feelings in me which i have never experience in me before, so i became interested in you, l will also like to know you the more,and l want you to send an email to my email aaddress so l can give you my picture for you to know whom l am. I believe we can move from here! I am waiting for your mail to my email address above. (Remeber the distance or colour does not matter but love matters alot in life) miss morin I guess my e-mail address is so sexy it turned her on O.o

Tuesday, September 8, 2009

Playing with HMTL5 Drag & Drop

I'm just toying around with HTML5-related things, to prepare myself so I don't get smacked in the head when it starts rolling out. The most interesting feature, in my opinion, is support for Drag & Drop. What I find most interesting about this feature is being able to drag & drop files from your desktop / file browser into a webpage. Unfortunately, we are unable to upload files this way just yet (well, it's available in Firefox 3.6 Alpha, so yeah). Anyway, if you have a decent Firefox version (I have 3.5.2 on Ubuntu), you can toy around with this demo app. I built it so I can observe the different event attributes when dropping things in the gray box. This is the first step towards my goal of a fully transparent drag & drop file upload. I guess I just hate those file inputs. Also, don't forget to look at the source. It's pretty small and simple.

Thursday, September 3, 2009

Folding@Home on Ubuntu Server

I recently started folding @ home on my Jaunty-based server (which runs 24/7 with not so much activity). What I have noticed is that they do not provide a proper init.d script for it. This is the default one they tell you to use: # chkconfig: 345 93 14 # description: will start FAH client as a service cd /path/to/folding ./fah6 -verbosity 9 < /dev/null > /dev/null 2>&1 & This doesn't seem to work for me, the process does not start. What I've found to work is this: ### BEGIN INIT INFO # Provides: fah # Required-Start: $local_fs $network $named # Required-Stop: $local_fs $network $named # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start Folding@Home client ### END INIT INFO # chkconfig: 345 93 14 # description: will start FAH client as a service cd /home/felix/fah/ ./fah6 -verbosity 9 < /dev/null > /dev/null 2>&1 & There you go. Now you have no reason to not run this :) Edit: No, it doesn't work. It seems init starts the script twice, which messes things up. I think this has something to do with runlevels, but I'm too tired to figure it out now. If anyone knows a solution, please comment.

Tuesday, September 1, 2009

Verifying a hostname / IP address with PHP

If you ever need to verify a hostname or an IP address in PHP, here's how: // try to determine the IP address of the hostname // if the hostname is actually an IP, gethostbyname() will return it unchanged // if the hostname cannot be resolved, it will have the same behavior $ip = gethostbyname($address); // check if the resulting IP is valid if ($ip !== long2ip(ip2long($ip))) { echo "Invalid hostname or IP address"; }