Android ActionBarSherlock action items delaying onOptionsItemSelected

I recently came across a problem when using the excellent actionbarsherlock library (v4.1) – the action items would often only respond to a user click once the user done some other interaction with the UI. In this case, the Activity has a ViewPager with two Fragments so the user can swipe from one to the other. Each Fragment called setHasOptionsMenu(true) in onCreate, and implemented onPrepareOptionsMenu to create the Fragment-specific action items.

If the user clicked on one action item, the onOptionsItemSelected method should’ve been called immediately, but it wasn’t. It was as if all the clicks were queued up until swiping to the other fragment, at which point they’d be handled in a rush.

Turns out that I fell foul of two bugs – one in ABS: https://github.com/JakeWharton/ActionBarSherlock/issues/272 and one in Android itself: http://b.android.com/29472.

The first thing I tried was in the comments of the ABS issue – to modify ActionMenuView (part of ABS) so it inherits from LinearLayout instead of the custom IcsLinearLayout. This worked for 2.2+, but broke 2.1 devices – the action bar wouldn’t show at all until swiping.

Further reading of the comments in that issue led me to the Android bug, whereby handling onPrepareOptionsMenu in each Fragment causes a race condition (which in turn prevents the UI from responding to clicks).

The solution: implement onPrepareOptionsMenu (or onCreateOptionsMenu) in the containing Activity class, and use setHasOptionsMenu(false) in each Fragment (or don’t use it all).

Posted in Uncategorized | Leave a comment

On the Android Calendar Widget

This is something that’s puzzled me for a while. The default Android calendar app widget is pretty ugly-looking and (to me) a weird size. Other calendar widgets available on the Market either suck up a lot of battery (by forcing the phone to wake up regularly and reload), look ugly or they’re somehow clunky to use.

How can it be so hard to make an app widget display your next few events in a fairly unobtrusive and ‘nice’ way? I don’t want it to be endlessly customisable – in fact, I want the opposite, I want it to just work – and I don’t want it to drain the juice of my phone.

So why hasn’t anyone (that I can find) made such a seemingly simple widget? Answer – there’s no API for the calendar. So you’re stuck with a widget that drains the battery but looks nice (and has to directly read the calendar sqlite db), or the default Google one which doesn’t look good but doesn’t drain the juice either.

I’m currently investigating making one that is the best of both worlds. It’s a shame any calendar/event changes aren’t broadcast – or at least, they don’t seem to be.

Posted in dev | Tagged , , | Leave a comment

Google: you’re too fast!

I’ve been working for a while on making an Android app to track my bike rides, runs, walks and other outdoor pursuits. I wanted it to be able to store the routes I took and share them with friends (via the usual suspects – email, facebook, etc). I wanted it to track stats for each trip like speed, average speed, altitude and so on.

Now what’ve Google done? They’ve gone and released MyTracks (http://mytracks.appspot.com/), which is exactly what I was planning! And it’s free, and open source!

What’s a dev to do? Be quicker, I guess!

Posted in dev | Tagged , | Leave a comment

DIY Home security system – part4 – the software

Last part.

By now everything should be built and hooked up – the IR light, the camera and the mini itx computer with PCI capture card. All that’s left to do is install some software and configure it.

I used arch linux and can recommend that you do the same – it’s fast and small. The pc will most likely be running without a monitor most of the time (at least it has in my case) so install whatever you’re happy administering over a network connection.

I did connect a monitor to the system just to install – the more adventurous may choose to use a serial cable.

Roughly, the steps I took were:

  1. Install arch linux.
  2. Install motion (pacman -S motion)
  3. Configure motion (I used this motion.conf, it records to /var/local/motion/ using ffmpeg to mpeg4). See the motion website for configuration help.
  4. Modify /etc/modprobe.d/modprobe.conf to include this line:
    options bttv card=77 tuner=-1

    If you bought the same PCI capture card I did, from dealextreme.

  5. Add motion to the list of services run at startup by modifying /etc/rc.conf and adding ‘motion’ to the end of the ‘DAEMONS’ line (normally at the bottom).
  6. That’s it! You should now, on reboot, have video clips recorded to you hdd whenever the system detects motion.

I wrote a simple python program to serve those files, with preview using a web server. Email me if you’d like a copy.

Posted in diy, Uncategorized | Tagged , | Leave a comment

DIY Home security system – part3 – putting it together

Hopefully by now everything should’ve arrived.

The PC bits you should be able to put together without the help of this site but the camera, light and software are what this site’s about.

The IR Light

The LEDS from dealextreme need to be mounted on a heatsink – I used a bit of copper pipe I had lying around from refitting my kitchen.
I could describe how I assembled it, but it’d be much easier to show a photo of its current state:

You can see there’s not a lot of weatherproofing there – but I did put some epoxy resin round anything that might corrode. I’ve since noticed some rust below the capacitor of the driver – not good, but not too horrendous after almost a year of active service.
I used superglue to attach the LEDs to the heatsink so heat would conduct between the two, then some epoxy round the join to strengthen it. No more nails worked well to stick the copper pipe to the fencepost.
So in summary, it ain’t that pretty, but it works fantastically.
Just out of shot is the camera, above the light.
The power cable is some two-core (possibly lighting?) cable I got from B&Q. It runs the length of the garden from inside the house, and is connected using a terminal block to the same 12V DC power supply that powers the mini-itx board.

The Camera

Running alongside the power cable is some corresponding coaxial cable, carrying the video signal from the camera. I elected to use coax because it needs to run about 20-30m to inside the house, so it needs to be shielded.
I used some 15mm plastic pipe as a conduit for the power and video cable – I’d definitely recommend using 22mm as it was a real squeeze running both cables through it. Or maybe even cable conduit ;)
The camera from dealextreme as a bunch of IR leds built-in. The advantage of these is that they only come on when it’s dark, whereas the ones above are always on. The disadvantage of them is that they’re rubbish – they’re why I build the hideous contraption above in the first place. I just took the camera apart and disconnected the LEDs – dead easy.

Posted in diy, Uncategorized | Tagged , | Leave a comment

DIY Home security system – part2 – the hardware

The Server

As I mentioned in part1, the CCTV camera has to connect to something to record the footage, and since I wanted the system to be extensible I decided to build a pc. I went with fairly standard off-the-shelf bits and pieces to make the pc, in order to keep costs down.

I wanted it to be as small, quiet and have as low power consumption as possible, but still be powerful enough to record and transcode the video streams from more than one SD camera at once (PAL, in this case). I really, really wanted to try out the ARM-powered beagleboard because it’s powerful, is optimised for video transcoding and is really really small, but was restricted in that it doesn’t have any PCI slots and so couldn’t take a video input card. So that ruled that out.

What I settled for was a mini-itx x86 motherboard, in this case with a dual core Atom 330 processor. Sadly this motherboard isn’t fanless and so isn’t silent, but it certainly isn’t loud so it’ll do. The total list of hardware I bought was:

So all in, about £180. Which isn’t too bad, although it’s a shame most of the cost was on the case and power supplies, not the more interesting stuff :)

I half-wish I’d spent a bit more and bought a fanless motherboard to make it really quiet as it’s not quite as quiet as I’d hoped. You can hear it if you know it’s on, but you probably wouldn’t notice it if you didn’t already know.

Anyway, assembling the lot was dead easy. It took about 20 minutes. I’ll upload a photo soon.

The Camera

I want to be able to record in the dark without giving the game away, so aside from the computer that sits humming quietly to itself in the cupboard, I needed some sort of infra-red sensitive camera. These aren’t as exotic as they may sound – most CCDs of digital cameras (including webcams and the like) can pick up IR without problems. A little look around dealextreme.com (which is a fantastic site, btw) lead me to the following bits and pieces:

  • IR-sensitive weatherproof camera – about £14.42
  • 3*high power 1W infra red LEDS – total £9.14
  • A bit of copper pipe, left over from our kitchen installation – for a base and heatsink for the LEDs
  • Another 12V DC power supply – £16.99 Edit: Not necessary. The single power supply above was sufficient.

So about £40 all in for the bits for the camera. Not too bad, again annoying that the power supply was a fair chunk of the cost.

That should be everything I need to bathe the garden in lovely, invisible infra red. Next post covers the LEDs and bit of piping :)

Posted in diy, Uncategorized | Tagged , | Leave a comment

DIY Home security system – part1

Over the past few years of living in Cambridge, my girlfriend and I have had five bikes stolen between us, which seems like an awful lot. Two of which were from the building of our rented flat and so we weren’t able to do much to help secure the building, but since then we’ve bought a house and had three bikes stolen from the garden. This series of events made me pretty mad, and ‘encouraged’ to try to secure our belongings a little better.

Sadly, I can’t go back in time, but maybe some glorified CCTV will help for the inevitable follow-up thieving attempts!

Anyway, onto the details of the project.

My requirements when thinking of a solution boiled down to the following:

  • Must be able to record (video a must, audio nice-to-have) people getting into the garden and opening the locked bikeshed
  • Must be able to record in the dark
  • I want to record the thieves in the act, not scare them off. If they’re scared off, they’ll just come back, so it must not alert the thieves when they’re detected
  • I’d like to be able extend it – add more cameras, microphones – that sort of thing, and also be a learning exercise, so a custom made solution would be preferred.
  • Similar to above, it’d be nice if the ‘system’ could at some point handle some form of home automation. I’m thinking of lights and music in the garden, potentially voice-controlled; but this is distant future stuff.

So with the above criteria, I decided upon a possibly over-engineered solution to the problem – to rig up an IR-sensitive camera pointing at the bikeshed with some IR led emitters lighting the place up, and have the camera connected to a small, low-power computer with motion detection software running on it.

I decided to use a mini-itx board with a new-fangled, low-power atom processor to do the motion detection, and put it in a lovely wall-mountable case. I considered using an ARM-powered beagleboard, but sadly the high cost of the required peripherals put me off. It would’ve been ideal, however – high performance, low power and small sized board which can run linux. If only there weren’t so many extra bits I’d need for the project (case, CF card, usb-ethernet adapter, usb video input dongle…).

I’ll post again with details of my adventures with this set of geekery as they happen, with a view to providing a tutorial over the series.

Posted in diy, Uncategorized | Tagged , | Leave a comment