Preventing and detecting state inference attacks on Android