Sign in

Posts in June 2010

Lock on unplug

2010-06-23 00:32:04 -

Here's a hack for Gnome (vaguely relevant to Android too) that I've been using for a little while. When I get up from a computer, I generally want to lock the screensaver. I also pick up my phone from a USB dock (not the Nexus One Desktop Dock; that doesn't connect to a computer). So why not have the screensaver lock automatically when the phone disappears from USB? This is helpful when you're hurriedly picking up the phone to answer a call, and might otherwise forget to lock the screen. Of course, if you get a lot of calls that you want to answer while looking at your computer, this is not for you. :-)

This is pretty easy to achieve using udev. First we ask udev to run a script when this device disappears. The following goes in /etc/udev/rules.d/51-android.rules (or a similar name; the number is used for ordering). While I'm at it, I'll set sensible permissions and group ownership, using the first line.

 1 # Set mode and group for (some) devices made by HTC
 2 SUBSYSTEM=="usb", ATTRS{idVendor}=="18d1", MODE="0660", GROUP="mygroup"
 3 # Nexus One without debugging enabled
 4 SUBSYSTEM=="usb", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4e11", \
 5   ATTRS{serial}=="HT123P456789", ENV{IS_MY_PHONE}="yes"
 6 # Nexus One with debugging enabled
 7 SUBSYSTEM=="usb", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4e12", \
 8   ATTRS{serial}=="HT123P456789", ENV{IS_MY_PHONE}="yes"
 9 # You could unlock on connect if you want:
10 #ENV{IS_MY_PHONE}=="yes", ACTION=="add", RUN+="/somewhere/unlock"
11 # Lock when my phone goes away
12 ENV{IS_MY_PHONE}=="yes", ACTION=="remove", RUN+="/somewhere/lock"
  • The device doesn't have to be a Nexus One or a phone at all, run lsusb to find your idVendor/idProduct values (for Android, this may differ when you turn on/off USB debugging).
  • To find your device's serial number, run sudo lsusb -v and look for iSerial (or run adb devices if you already set that up).
  • Pick somewhere to put the lock script referenced in the last line.
  • The contents of that script should be:
1 #!/bin/sh
2 exec su user -c ". /home/user/.dbus/session-bus/*-0; \
3 export DBUS_SESSION_BUS_ADDRESS; \
4 gnome-screensaver-command --lock"

The unlock script would be the same with --deactivate. Replace user with your username, and if your display isn't :0 then you might need to do something smarter to get the right session bus. Oh, and I'm on Ubuntu Karmic (9.10); adjustments may be required for other environments. When you've done all that, run sudo reload udev to pick up the changes, unplug the phone, and watch the screen fade to black.

Question: how easy is it to fake a serial number of a USB device? Equivalently, how silly is it to have the unlock line uncommented? I'm guessing fairly easy.

Comments (2)

Chrome to Phone CL

2010-06-19 18:57:01, updated at 2010-06-25 19:24:48 -

A couple of recent Google offerings gave me an idea. First there was the demo of cloud-to-device messaging at Google I/O, with which I look forward to seeing the Android development community do great things, and to demonstrate it they used a Chrome extension, Chrome to Phone. Then yesterday, GoogleCL appeared, providing access to several Google services from the command-line.

Chrome to Phone is very nice, but it needn't be limited to just a browser button. Looking at the source of their Chrome extension, it's actually pretty easy to wire up as a bash script that just POSTs to the same backend. It should go without saying that this is a horrible hack, and if you write an app around this without first asking someone at Google, they'll probably block it. The approved way to push events to Android is to sign up for the C2DM beta with your own app. But if you want to play around with what's possible, or just cause your phone to open URLs from the command-line of your own machine, and your phone is running Froyo (Android 2.2), here's how:

  1. Get Chrome to Phone working as designed, as a Chrome extension.
  2. Extract the SACSID cookie for chrometophone.appspot.com: Spanner, Options, Under the Bonnet, Content settings, Cookies, Show cookies, search for chrometophone, click on SACSID, and on the long string next to Content, right-click, Select All, Copy.
  3. apt-get install libnotify-bin if you want notification of the result.
  4. Use a script like this one:
 1 #!/bin/sh
 2 u=`echo "$@" |sed -e 's/^market:\/\//http:\/\/market.android.com\//' \
 3   |sed -e 's/&/%26/g'`
 4 r=`curl -d "title=&url=$u&sel=" -H 'Cookie: SACSID=...your cookie here...' \
 5   -H 'X-Extension: true' 'https://chrometophone.appspot.com/send?ver=3'`
 6 cd /usr/share/icons/gnome/scalable/status
 7 case "$r" in
 8   *OK*) notify-send --icon=dialog-information.svg "$r" ;;
 9   *) notify-send --icon=dialog-warning.svg "$r" ;;
10 esac

Now you can give your script a URL, and your phone will open it. Note that the cookie might be IP-address-specific, in which case you'd need to redo the steps above to get it to work from a different IP. That fixup of market:// URLs means you can also register this script as the handler for them in GNOME…

1 gconftool-2 --set --type=string /desktop/gnome/url-handlers/market/command \
2   "/your/script/here \"%s\""
3 gconftool-2 --set --type=bool /desktop/gnome/url-handlers/market/enabled true
…and then the next time you see in a desktop browser that some idiot has created a page with a market:// link on it, you can click it and your phone will open the relevant Market listing.
Comments (0)

Hello, world!

2010-06-19 16:15:46, updated at 2010-06-19 16:15:54 -

If you were reading my previous blog, you may remember the announcement of a move to this domain. It's finally done, and this is where I'll be blogging from now on. Yes, it has taken a little while… although only the last couple of months were spent on this particular attempt.

Entries here are likely to be technical, on the subjects of software development (especially Android), web design, system administration and related areas, but I'll keep them as accessible as I can. For more detail on what might appear here, see the About page.

There is an Atom feed, which is copied to at least InsaneJournal and LiveJournal (the old name shortcipher_ij works too), I'll tell Twitter (and thereby Facebook and Buzz) when I post, and if you'd like to be emailed, the site can do that too. Some of these mechanisms let you leave comments on copies of my entries on sites other than chris.boyle.name. It's not for me to object to anyone doing that, but I don't check those places and most of them won't notify me, so any time you want me to read a comment you've left elsewhere, send me an email or an IM.

As for the comments system here, I've tried to make it easy and painless to use. Anonymous comments are not allowed (spam) but you can use a variety of external accounts: OpenID (e.g. LJ and similar sites), Facebook, Twitter. If your account is already known to me, then you can immediately leave public comments, otherwise your comments will be held for moderation, just until I notice that my phone has lit up and approve your account. When I've done that, you can receive email notification of replies.

If you have a bug or a feature request for the site, there is a bug tracker or you can contact me. There probably will be a few bugs, as the site hasn't had much testing.

Comments (0)