6

Our app runs as a background service, continuously getting the images from the frontal camera and then doing things with the images. However, when the user tries to open another app that uses the camera, two things happen: either the new app crashes and ours continues, or our crashes and the new one gets access to the camera.

Now, if our app loses control over the camera, there's an exception that we can catch, and we can then start trying to access it until we get access and things go back to normal.

The problem is when the other apps crash because of ours. Is there any way to detect this, so we can pause our app until we can access the camera again?

AndydnA
  • 61
  • 1
  • 2
  • 1
    relevant [question](http://security.stackexchange.com/questions/40284/how-can-i-detect-which-background-application-is-using-the-camera-microphone-or) – Bill Jul 21 '16 at 14:42

2 Answers2

0

This happens because camera is an exclusive resource.

I recommend you to read the Camera documentation, which says:

If your application does not properly release the camera, all subsequent attempts to access the camera, including those by your own application, will fail and may cause your or other applications to be shut down.

Jean Vitor
  • 893
  • 1
  • 18
  • 24
  • Our app needs constant access to the camera. We want to release it when another app tries to use it, because right now the other apps just crash with no notification to the user of the reason (although some phones notify and even bring our app from the background to the foreground). Constantly releasing, doing our analysis, and then opening the camera again would be too slow (a quick test on the camera opening took around 350 ms. The app need to run at a minimum of 15 fps, but 20+ would be good) – AndydnA Jul 21 '16 at 15:09
  • Why do you need camera access when your app isn't open? You can release the camera in the onpause method and then reinitialize it in the onresume method. – drew Jul 21 '16 at 15:20
  • The app throws a RuntimeException If opening the camera fails, by any reason. There's a code [here](http://stackoverflow.com/questions/15862621/how-to-check-if-camera-is-opened-by-any-application) that may be useful. – Jean Vitor Jul 21 '16 at 15:27
  • 2
    @drew Our app monitors drivers, analyses the face, and triggers warnings when the driver is distracted or sleeping. The driver might want to check the time or answer a call, but our app has to keep going or we'll be forcing him to restart our app every time he finishes doing whatever caused it to pause. So it runs in the background, always analyzing and always using the camera. However, if the driver decides to use the camera (which he really shouldn't do while he's driving, but that's not in our control), then we have to pause our app. – AndydnA Jul 21 '16 at 15:41
  • 2
    @JeanVitor the question is about detecting when another app tries to open the camera that is currently being used by our app. – AndydnA Jul 21 '16 at 15:42
0

Unfortunately, Camera API or Android SDK in general don't provide this kind of info. The good news is that gradually, more new devices let you open the front-facing and the rear-facing cameras independently. This could reduce the probability of a clash.

From your description, it may be legitimate to crash the applications that compete with yours over access to camera, but, jokes aside, there is nothing you can do about it. It's their fault that they don't check the RuntimeException correctly.

Android has recently tightened the isolation between apps, so it may be impossible to track which applications are started (see App to monitor other apps on android), unless your app has system privileges.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307