Thursday, November 13, 2008

Grep is fun

I'm not what you would call a UNIX power user. I'm more of a UNIX casual user. I don't agree with the zealots who claim that the command line is king, and shell scripts keep the world spinning. It's a pain to remember the myriad of switches, and to remember the differences in those switches between platforms (BSD find requires a pathname, GNU find does not), etc. Maybe my distaste of the command line is why my primary computer is a Mac.

In any case, I was rooting around in the Android source code today, looking for examples involving the NotificationManager. I managed to whip up the following pipeline, of which I am proud.

grep -rl --include=*.java NotificationManager . | xargs grep -l "\.clear" | xargs mate

To break that down,

grep -rl --include=*.java NotificationManager . This will recurse in the current directory, finding all .java files which contain the word NotificationManager. The resulting list of files will be passed to the next step.
xargs grep -l "\.clear" This looks for the string ".clear" in any of the files that came from the first command. It outputs the list of matching files to the next command.
xargs mate This launches TextMate, my editor of choice, with all of the files found in the previous step.

To summarize, this pipeline opens a text editor with every .java file in the android source code that contains both of the strings "NotificationManager" and ".clear". In my case, I was looking for all invocations of NotificationManager.clear(). Since the Android source code seems to be pretty consistent about explicitly importing all classes used, this scheme works pretty well.

If anybody knows of any command-line tools for searching a code base for Java-aware constructs (such as method definition, method invocation, etc.), please let me know! Something like that would be really, really handy. Eclipse provides all that, but it doesn't look trivial to get all of the Android source code into Eclipse projects.

Tuesday, November 11, 2008

AnyCut's "Make Your Own" Demystified

I installed AnyCut on my G1 a little while ago. AnyCut is a small app that allows you to create shortcuts to, well, almost anything. It has a mysterious "Make Your Own" option that isn't really documented at all. I started to play with it a bit, and it's not as bad as you would think.

The Make Your Own screen has three fields: Action, Data, and Type. I believe that these line up with the constructor parameters of public Intent(String action, Uri uri, Context packageContext, Class<?> cls). The Action parameter is one of the string consts in the Intent class (such as android.intent.action.VIEW, not Intent.ACTION_VIEW), and the Data parameter should really be called Uri, because it includes the Uri of the item upon which you want to act. See also the list of common intents. I imagine that the Type parameter is a fully-qualified class name, but it can be left blank for most uses. Finally, I suspect that the packageContext parameter is supplied by AnyCut itself.

I haven't yet had a chance to verify any of this against the source code (which I came across while writing this post), but it might be worth a look.