Friday, August 5, 2011

How to reload your .zshrc

Stupid me, after editing my .zshrc I always invoked a new shell to apply those changes. Now I found out, that you can use your shell's built-in source command to reload your zsh settings.
sk@supernova:~/ > source ~/.zshrc
or, even shorter:
sk@supernova:~/ > . ~/.zshrc

Wednesday, June 8, 2011

Java: Customizing enums

After reading this you might wonder why I would write about something obvious like that. And I agree. It is quite obvious. Once you know about it. However, first I didn't know and neither did the other Java programmers I've spoken to. So I thought it's worth writing up a quick blog post.

Probably all of you already know about enums and use them as type-safe flags or enumerations:
enum Cardsuit { CLUBS, DIAMONDS, SPADES, HEARTS };
That is pretty basic and probably everyone learned that in Java 101. What was new to me, though, is that you can customize the enum { } pretty much like a class.

So for example, one day I was working on a XML parser implementation, where depending on an attribute value of a XML element I had to make a decision. Since in XML everything is a String I wrote a quick method which compares an enum to a String. But I didn't know where to put it in my code. I figured the best place would be within the enum itself. Not really thinking that it would work, but still curious I tried the following:
I tried to compile it and voilĂ : it worked! No errors, no warnings.

In my actual XML parser I then would only have to do the following:
if ( Cardsuit.CLUBS.compareToString(xmlString) ) { .... }
Of course there would have been many other ways to do this, but this solution seems quite clean and elegant to me.

Monday, May 9, 2011

Android: Hide your application's media files from the Gallery App

If you have a media heavy application it is very likely that you store the image, sound or video data on the sd-card, since it gives you a lot more space than bundling everything with the application. Also, it enables you to dynamically add, remove or modify your media data.


However, the problem that comes with it is, that all your media is public now and can be read by other applications, e.g. the Gallery App, which scans the whole sd-card for image files. To prevent other applications from scanning and displaying your media files, you can simply add an empty file called .nomedia in the root folder of your media data.

From the official Android documentation:
Name of the file signaling the media scanner to ignore media in the containing directory and its subdirectories. Developers should use this to avoid application graphics showing up in the Gallery and likewise prevent application sounds and music from showing up in the Music app. 

Wednesday, April 13, 2011

Debugging Client-Server Communication

Especially in mobile development you most likely have some client-server communication going on, which is often the source of a lot of problems and long debugging sessions. To make the debugging easier it is helpful to see what each side sends or receives. There are two UNIX tools that can come in quite handy in such situations:
  • netcat - to listen to all TCP and UDP connections on a specific port
  • cURL - to send 'fake' requests to a server
Both a very powerful tools and you can do a lot of stuff with them (e.g. write your own web server with a shell script). However, in this post we will keep it simple and focus on debugging a HTTP (JSON) communication between a web server and a mobile client (iPhone, Android, but could be pretty much any kind of client).


Listening to incoming requests

We start a listening server with
nc -lk $ip $port 
  • -l: listen
  • -k: forces nc to stay listening for another connection after its current connection is completed.
  • $ip: the IP/interface you want to bind to. Use 0.0.0.0 to bind to all interfaces and IPs.
  • $port: the port you want to bind to. Doesn't really matter which one you use, as long as the client uses the same one to connect to.

In practice it might look like this:
sk@supernova:~/ > nc -lk 0.0.0.0 8080
POST /rpc HTTP/1.1
Accept: application/json
Content-type: application/json
Accept-Encoding: gzip
Host: 192.168.2.3
Content-Length: 152
Connection: Keep-Alive

{"id":3,"jsonrpc":"2.0","method":"sendTestMessage","params":{"clientVersion":"0.11.4-debug","user":"sk@geekmind.net","imei":"32420214181983746","message":"This is a test message :-)"}}
In green the HTTP header and in blue the actual content, in this case a JSON-RPC message. While the header is mostly irrelevant to us, it might in some cases contain useful information to detect the source of the problem. However, more interesting, and in most cases more prone to errors is our own "custom protocol" and its implementation. By evaluating the content section we can now easily compare if there is a difference between what we were expecting to receive and what the client was actually sending out.


Sending "fake" requests to a server

Sometimes the problem may not be in the client's implementation of our communication protocol, but on the server side. The complementary approach to the above example is to send "fake" requests to our server and look at the response we get.

With cURL it is as simply as that:
curl -v -i -X POST -d $data $uri
  • -v: verbose
  • -i: include HTTP headers in the output
  • -X: HTTP request type. Defaults to GET if none given
  • -d: data

Applied it might look like this:
sk@supernova:~/ > curl -v -i -X POST -d '{"id":1,"jsonrpc":"2.0","method":"sendTestMessage","params":{"clientVersion":"0.11.4-debug","user":"sk@geekmind.net","imei":"1111111111111111111"}}' http://android.geekmind.net/rpc
* About to connect() to android.geekmind.net port 80 (#0)
*   Trying 74.125.43.121... connected
* Connected to android.geekmind.net (74.125.43.121) port 80 (#0)
> POST /rpc HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3
> Host: android.geekmind.net
> Accept: */*
> Content-Length: 157
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Content-Type: text/html; charset=utf-8
Content-Type: text/html; charset=utf-8
< Cache-Control: no-cache
Cache-Control: no-cache
< Expires: Fri, 01 Jan 1990 00:00:00 GMT
Expires: Fri, 01 Jan 1990 00:00:00 GMT
< Date: Fri, 08 Apr 2011 09:19:32 GMT
Date: Fri, 08 Apr 2011 09:19:32 GMT
< Server: Google Frontend
Server: Google Frontend
< Transfer-Encoding: chunked
Transfer-Encoding: chunked
<
* Connection #0 to host android.geekmind.net left intact
* Closing connection #0
{"jsonrpc": "2.0", "id": 1, "error": "1", "message": "ERROR: user and IMEI do not match!"}}  
In red the response we got from the server. In this case we used a real username with a wrong IMEI and tested if the server generates the right response. In our case the server correctly produced an error.

If you're interested in reading more about the actual implementation of client-server communication on mobile devices, check out my blog post Android: Simple HttpClient to send/receive JSON Objects. The code is available on GitHub.

If you want to directly monitor a real connection I suggest that you have a look at tools like Wireshark and tcpdump. Wireshark has a GUI and is fairly easy to understand. It also helps to have a server instance running on your local machine (development server), so you don't need to figure out how to tap into your phones WiFi connection.

Wednesday, March 30, 2011

Visual Life

A collection of promo videos of Inte's Visual Life contest. Interesting people and inspiring to watch.


Check them out, if you get a chance.

Saturday, February 12, 2011

The Age of Fire - Futuristic Interfaces

Here is a really well-made video about futuristic interfaces in a completely interconnected home environment. It shows the beauty and usefulness that comes with it, but also points out the inherent problems of complete interconnectivity.


Interesting challenges for User Interface Engineers and Designers :-)

Wednesday, January 26, 2011

Shortcuts to improve your bash & zsh productivity

So, you hate using a terminal? That might be, because you use the arrow keys to navigate character by character through a long command just to change a paramater at the other end of the line, right? Here's a list of my most-used bash & zsh shortcuts, that will definitely boost your productivity and will help you to improve your command line experience.

ShortcutAction
CTRL + AMove to the beginning of the line
CTRL + EMove to the end of the line
CTRL + [left arrow]Move one word backward (on some systems this is ALT + B)
CTRL + [right arrow]Move one word forward (on some systems this is ALT + F)
CTRL + U (bash)Clear the characters on the line before the current cursor position
CTRL + U (zsh)If you're using the zsh, this will clear the entire line
CTRL + KClear the characters on the line after the current cursor position
ESC + [backspace]Delete the word in front of the cursor
CTRL + WDelete the word in front of the cursor
ALT + DDelete the word after the cursor
CTRL + RSearch history
CTRL + GEscape from search mode
CTRL + _Undo the last change
CTRL + LClear screen
CTRL + SStop output to screen
CTRL + QRe-enable screen output
CTRL + CTerminate/kill current foreground process
CTRL + ZSuspend/stop current foreground process
CommandAction
!!Execute last command in history
!abcExecute last command in history beginning with abc
!abc:pPrint last command in history beginning with abc

History Search

Press CTRL + R to search through the history. Continue pressing CTRL + R until you find the entry you're looking for. Press [ENTER] to execute the current expression. Press [Right Arrow] to modify the current expression. Press CTRL + G to escape from search mode.

Special setup for Mac OS X

Go to Terminal -> Preferences -> Settings -> Keyboard
  • To enable the use of the ALT or OPTION key, select use option as meta key
  • To enable the CTRL + [left arrow] and CTRL + [right arrow] shortcuts, select
    • control cursor left and set it to \033b and
    • control cursor right and set it to \033f.
You know of any other important shortcuts that I missed? Please let me know in the comments.

Wednesday, January 19, 2011

Android: Better Logging Output

Ever got annoyed by the flood of information in Android's adb logcat output? Luckily Google Engineer Jeff Sharkey put together a Python script to make the output visually more appealing and easier to follow. Below are a few tipps on how to effectively combine coloredlogcat.py with adb logcat's parameters and options.

Colored Logcat Output
Setup
  1. Download coloredlogcat.py from Jeff Sharky's blog
  2. Copy the script to your local ~/bin folder and make it executable
  3. mkdir ~/bin cp ~/Downloads/coloredl chmod +x ~/bin/coloredlogcat.py
  4. Add ~/bin permanently to your $PATH variable. Either in your .bashrc or .zshrc (or whatever shell you use) add or modify this line:
  5. export PATH=$HOME/bin:$PATH
Usage
You can either use it directly, by simply entering coloredlogcat.py, or you can use adb logcat with all it's parameters and just pipe it into coloredlogcat.py

Show only DEBUG output from the MyApp TAG. Everything else is SILENT: *:s (not shown)
adb -d logcat "*:S MyApp:D" | coloredlogcat.py
Show only WARNING, ERROR and FATAL output. Great for checking on exceptions.
adb -d logcat "*:W" | coloredlogcat.py
References
Logcat priorities
  • V — Verbose (lowest priority)
  • D — Debug
  • I — Info
  • W — Warning
  • E — Error
  • F — Fatal
  • S — Silent (highest priority, on which nothing is ever printed)
Special options
  • adb logcat -c clears the entire log

Saturday, January 15, 2011

Linux WiFi: operation not possible due to RF-kill

Some people have been experiencing WiFi problems with Ubuntu 10.10 since an update that happend just before Christmas 2010. The problem seems to be a bug in a kernel module, which prevents the rfkill's soft and hard block from syncing correctly.

In practice that means you can press your laptop's WiFi button as often as you want, it doesn't change anything. Your wireless adapter will always appear as disabled. If you try to start the wireless interface manually you get the following error message:
sk@ubuntu:~# sudo ifconfig wlan0 up
SIOCSIFFLAGS: Operation not possible due to RF-kill
If you do a rfkill list all you should get an output similar to this:
sk@ubuntu:~$ sudo rfkill list all
0: hp-wifi: Wireless LAN
Soft blocked: yes
Hard blocked: no
1: phy0: Wireless LAN
Soft blocked: yes
Hard blocked: no
Depending on the state of your WiFi button the Hard blocked will either be yes or no. Press the WiFi button and run rfkill list all again to make sure the value for Hard blocked changes.

The actual problem is, that the Soft blocked value is always set to yes, because for some reason the syncing between the hardware block and the software block doesn't work as supposed. To override this behavior, you can just run rfkill unblock wifi and it should work again. Double check by entering rfkill list all again and make sure it looks like this, with all values set to no:
sk@ubuntu:~$ sudo rfkill list all
0: hp-wifi: Wireless LAN
Soft blocked: no
Hard blocked: no
1: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
If you then run ifconfig wlan0 up (or enable the wireless via your desktop's network manager) you should be able to connect to a wireless network again without any further issues.

The configuration, which I've been using:
  • Compaq Presario CQ60
  • Atheros AR5001 Wireless Network Adapter
  • Ubuntu 10.10
I also read, that some people could fix it, by turning off the WiFi button during the boot process and then switch it on again, once the system is up and running. It seems that the system expects the hard block to be set to off. This is still a bug, but it could explain why then the soft block didn't sync with the hard block any more.

Monday, November 1, 2010

Rapid Prototyping of Web Applications

Ryan Singer from 37 Signals gave a really interesting talk at the Future of Web Apps in London about the rapid prototyping process of web applications.


For me, the most important insight was to start off with building a really bare-bone HTML framework, which then involves step-by-step by having already right from the beginning a programmer working on the data models and controllers in parallel with a web designer improving the markup and CSS, instead of going the traditional way of letting only a graphic designer spending days (or even weeks) bringing his design ideas to perfection and blocking other people (i.e. programmers) from getting started, to then finally figure out a couple of weeks later that from a UX perspective the whole thing doesn't work.

Kinda obvious and simple once you hear it. However, most people in the industry still have their way of thinking that you have to tinker a design visually to perfection, but don't get the point that a static design is only a small proportion of what makes an application gonna work or not.

UI Design (Photo by Lucas Mascaro)

Something I personally like a lot, is what I call "back to kindergarden" or the pen, paper & scissors approach, which basically means that we draw all pages and UI elements on paper by using different sized pens of various colors and then play around with them and walk through different use cases by moving the small paper snippets around and then discuss our ideas with other team members including developers. Including the developers is very important, because they're the ones who actually have to build the whole thing. They sometimes might restrict the creativity with bringing up all these ambiguous technological limits, but they're quite crucial when it comes down to decision making about feasibility and time estimates.

Tipp: having pre-designed templates, which can be printed out makes it a lot easier to throw away the current ideas and start over without having to completely start all the way from scratch again.

Tuesday, October 12, 2010

Hissing noise when connecting earbuds to MacBook

Recently I bought some high quality Sennheiser earbuds, but since using them together with my MacBook I was annoyed by a constant hissing noise when playing back very quiet parts of a song. For a while I didn't bother and just accepted it as it is, but later on I played around with some tools in /Application/Utilities and found an application called Audio Midi Setup.


Luckily I was listening to some calm music while randomly fooling around with these settings and when I changed the bit rate of the Built-In Output from 2ch-16bit to 2ch-24bit all the sudden I got an impressive increase of sound output with almost no hissing noise. It's still not perfect, but definitely much better compared to what I was tortured with before. Although, increasing the bit rate all the way up to 2ch-32bit didn't cause any significant improvement to 24 bit, that's why I went with 2ch-24bit for now.


If someone can explain what the correlation between bit rate and hissing noise is, please let me know in the comments. I'm curious to know :-)

Monday, October 4, 2010

Android: Implementing your own Listener

In Android development it's quite common to use things like OnClickListener and similar listeners to implement functionality triggered by a click on a button or other state changes. You probably use them all the time, without even knowing how they work. And actually you don't need to know. But it's worthwhile investing some time, because once you got the idea it's quite simple to implement your own listeners, which can come in handy here and there.

Listeners are pretty much callback functions implemented through Java Interfaces:
Interfaces are commonly used in the Java language for callbacks. Java does not allow the passing of methods (procedures) as arguments. Therefore, the practice is to define an interface and use it as the argument and use the method signature knowing that the signature will be later implemented.
I will explain it with a simple example. Lets say we create our own custom view for our new innovative user interface that is a bit more complex so that a normal Android Button just isn't satisfactory enough for our needs. Lets say we want to have something like a color picker in it:

Sunday, July 25, 2010

Android: Display dialogs in fullscreen

Ever wondered how to maximize a dialog to the screen's full size? The first thing probably everyone would naturally do is to set
android:layout_width="fill_parent" android:layout_height="fill_parent"
in your mydialog.xml layout file. However, that doesn't seem to have any effect at all.

The solution that works is not much more complicated than that, but unfortunately not that easy to think of in the first place. What you have to do is, when you setup your Dialog in code, you have to set (again) your layout (as you do in the .xml file) after you set your content view.
I don't know why it works when set in code and not when set in the .xml file, but for now I just accept that it does. If you know the answer to this please let me know in the comments.