Lock on unplug

8 years, 8 months ago


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.

# Set mode and group for (some) devices made by HTC
SUBSYSTEM=="usb", ATTRS{idVendor}=="18d1", MODE="0660", GROUP="mygroup"
# Nexus One without debugging enabled
SUBSYSTEM=="usb", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4e11", \
  ATTRS{serial}=="HT123P456789", ENV{IS_MY_PHONE}="yes"
# Nexus One with debugging enabled
SUBSYSTEM=="usb", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4e12", \
  ATTRS{serial}=="HT123P456789", ENV{IS_MY_PHONE}="yes"
# You could unlock on connect if you want:
#ENV{IS_MY_PHONE}=="yes", ACTION=="add", RUN+="/somewhere/unlock"
# Lock when my phone goes away
ENV{IS_MY_PHONE}=="yes", ACTION=="remove", RUN+="/somewhere/lock"
exec su user -c ". /home/user/.dbus/session-bus/*-0; \
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.


mas90 8 years, 8 months ago

Have you seen Matt's bluemon (http://www.matthew.ath.cx/projects/bluemon/)? Basically the same but without the USB cable. :-) Probably more secure too (unusually!); it's almost certainly trivial to fake a USB serial number given a USB device chip.

PS: my browser (Firefox 3.6.3 / Windows) doesn't trust your SSL certificate.

Link | Reply

Chris Boyle 8 years, 8 months ago

Oops, looks like I was supplying the wrong intermediate certificate, fixed now. I had seen bluemon, but at work I've had so much trouble with bluetooth (too many other devices at a guess) that I doubt it would be reliable, and at home I'd wonder about the possibility of accidentally sitting in a position with strong signal strength in the room directly below the machine in question. Probably worth giving it another try though.

Link | Reply

Akash Khandale 6 years, 11 months ago

I have downloaded the apk file,But how to install/compile it on emulator i'm working on it.help me.

in adv thanks.

Link | Reply

Chris Boyle 6 years, 11 months ago

I don't see an apk mentioned in this post, but if you want to install an apk on a running emulator, "adb -e install your-file-here.apk" should work. (The -e means direct the command to the emulator, in case you also have a real device connected.)

Link | Reply

New Comment


required (not published)