Wednesday, 17 February 2010

Ubuntu: How to automatically lock & unlock screen with Android phone

Evan Boldt has a useful script: Unlock your screen with ANY USB device. I've modified it slightly to work with my HTC Magic / Google Android device. A script is run every minute, which polls the system log every 2 seconds to determine when the USB device is plugged in in or unplugged. It then forwards the relevant command to GNOME Screensaver. The script is posted below for your interest.

Firstly, run lsusb and locate your phone. In my case, it was the highlighted line:

Bus 002 Device 002: ID 08ff:2580 AuthenTec, Inc. AES2501 Fingerprint Sensor
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 002: ID 045e:0752 Microsoft Corp.
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 002: ID 045e:0039 Microsoft Corp. IntelliMouse Optical
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 006: ID 0bb4:0c02 High Tech Computer Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Copy everything from ID to the end of the line (i.e. "ID 0bb4:0c02 High Tech Computer Corp.") This will be the ID used to determine when your device is plugged in & unplugged.

Paste this file into your /home/username/bin folder:


#Replace with the ID of your USB device
id="ID 0bb4:0c02 High Tech Computer Corp." # Example: id="ID 05ac:1292 Apple, Inc"

#runs every 2 seconds
for ((i=0; i<=30; i++))
if [ -z "`lsusb | grep "$id"`" ]

    echo "Device is NOT plugged in"

    if [ -n "`DISPLAY=:0 gnome-screensaver-command --query | grep "is active"`" ]
    if [ -e /tmp/autoUnlock.lock ]
    #stop locking the screen
    rm /tmp/autoUnlock.lock


elif [ -e /tmp/autoUnlock.lock ]

    DISPLAY=:0 notify-send -t 5000 –icon=dialog-info “Device Disconnected” “Bye!”

    #lock the desktop
    DISPLAY=:0 gnome-screensaver-command --lock

    rm /tmp/autoUnlock.lock


    echo "Android IS plugged in"
    if [ ! -e /tmp/autoUnlock.lock ]
    DISPLAY=:0 gnome-screensaver-command --deactivate
    DISPLAY=:0 notify-send -t 5000 --icon=dialog-info "Device Connected" "Welcome Back!"
    touch /tmp/autoUnlock.lock

    ##Uncomment the 3 following lines if you would like your computer to remind you if you lock your screen without disconnecting the device
    #echo "Don't forget your device!" > /tmp/androidReminder
    #DISPLAY=:0 festival --tts /tmp/androidReminder
    #rm /tmp/androidReminder

sleep 2

Now you simply add a line to your crontab to run this script every minute:

crontab -e

add the line:

* * * * * bash /home/username/bin/autoUnlock & >/dev/null 2>&1

That's it! Now, when you unplug your Android phone it should lock the screen. When you plug in again, the screen unlocks. Easy!

More information:

1 comment:

swiz said...

nice...that is a slick. Not 100% why I would need to implement the script, but I think I'll give it a shot. Finally made the move away from was hard to drop the BlackBerry, but the Droid made it easier.