Refactoring to have app shutdown handled correctly.
Involves adding a canBeResumed flag to Application and using it to determine if AppMan should remove the application from the lists. Also add a big bunch of unit tests for AppMan. Fixes: https://bugs.launchpad.net/bugs/1305128.
Approved by PS Jenkins bot, Michał Sawicz.