Foreground application time

In my Ionic application how could I record for how much time any application was on foreground?

How about listening to the pause and resume events from Platform, and triggering timers based on that?