A new tweak has been launched on Cydia, CleverPin. It will prevent your phone from locking under when certain circumstances are met such as listening to music, being charged, airplane mode or my favorite, when connected to a given network. You get the options to activate or deactivate the security code according to your needs.
You should note that currently this tweak is not available in Cydia. Soon it will be available in Cydia at a price of $2.99 USD. You will need to have jailbroken iPhone to install this tweak. If you don’t have jailbroken iPhone then you can follow the step by step instructions posted here to jailbreak your iPhone on iOS 4.2.1 with Redsn0w. You can watch the video embedded below to see it in action.