@@ -236,6 +236,50 @@ public static class CameraInfo {
236236 /**
237237 * Creates a new Camera object to access a particular hardware camera.
238238 *
239+ * <p>When <code>force</code> is set to false, this will throw an exception
240+ * if the same camera is already opened by other clients. If true, the other
241+ * client will be disconnected from the camera they opened. If the device
242+ * can only support one camera running at a time, all camera-using clients
243+ * will be disconnected from their cameras.
244+ *
245+ * <p>A camera being held by an application can be taken away by other
246+ * applications at any time. Before the camera is taken, applications will
247+ * get {@link #CAMERA_ERROR_RELEASED} and have some time to clean up. Apps
248+ * receiving this callback must immediately stop video recording and then
249+ * call {@link #release()} on their camera object. Otherwise, it will be
250+ * released by the frameworks in a short time. After receiving
251+ * CAMERA_ERROR_RELEASED, apps should not call any method except <code>
252+ * release</code> and {@link #isReleased()}. After a camera is taken away,
253+ * all methods will throw exceptions except <code>isReleased</code> and
254+ * <code>release</code>. Apps can use <code>isReleased</code> to see if the
255+ * camera has been taken away. If the camera is taken away, the apps can
256+ * silently finish themselves or show a dialog.
257+ *
258+ * <p>Applications with android.permission.KEEP_CAMERA can request to keep
259+ * the camera. That is, the camera will not be taken by other applications
260+ * while it is opened. The permission can only be obtained by trusted
261+ * platform applications, such as those implementing lock screen security
262+ * features.
263+ *
264+ * @param cameraId the hardware camera to access, between 0 and
265+ * {@link #getNumberOfCameras()}-1.
266+ * @param force true to take the ownership from the existing client if the
267+ * camera has been opened by other clients.
268+ * @param keep true if the applications do not want other apps to take the
269+ * camera. Only the apps with android.permission.KEEP_CAMERA can keep
270+ * the camera.
271+ * @return a new Camera object, connected, locked and ready for use.
272+ * @hide
273+ */
274+ public static Camera open (int cameraId , boolean force , boolean keep ) {
275+ return new Camera (cameraId , force , keep );
276+ }
277+
278+ /**
279+ * Creates a new Camera object to access a particular hardware camera. If
280+ * the same camera is opened by other applications, this will throw a
281+ * RuntimeException.
282+ *
239283 * <p>You must call {@link #release()} when you are done using the camera,
240284 * otherwise it will remain locked and be unavailable to other applications.
241285 *
@@ -255,13 +299,13 @@ public static class CameraInfo {
255299 * @param cameraId the hardware camera to access, between 0 and
256300 * {@link #getNumberOfCameras()}-1.
257301 * @return a new Camera object, connected, locked and ready for use.
258- * @throws RuntimeException if connection to the camera service fails (for
259- * example, if the camera is in use by another process or device policy
260- * manager has disabled the camera).
302+ * @throws RuntimeException if opening the camera fails (for example, if the
303+ * camera is in use by another process or device policy manager has
304+ * disabled the camera).
261305 * @see android.app.admin.DevicePolicyManager#getCameraDisabled(android.content.ComponentName)
262306 */
263307 public static Camera open (int cameraId ) {
264- return new Camera (cameraId );
308+ return new Camera (cameraId , false , false );
265309 }
266310
267311 /**
@@ -276,13 +320,13 @@ public static Camera open() {
276320 for (int i = 0 ; i < numberOfCameras ; i ++) {
277321 getCameraInfo (i , cameraInfo );
278322 if (cameraInfo .facing == CameraInfo .CAMERA_FACING_BACK ) {
279- return new Camera (i );
323+ return new Camera (i , false , false );
280324 }
281325 }
282326 return null ;
283327 }
284328
285- Camera (int cameraId ) {
329+ Camera (int cameraId , boolean force , boolean keep ) {
286330 mShutterCallback = null ;
287331 mRawImageCallback = null ;
288332 mJpegCallback = null ;
@@ -299,7 +343,7 @@ public static Camera open() {
299343 mEventHandler = null ;
300344 }
301345
302- native_setup (new WeakReference <Camera >(this ), cameraId );
346+ native_setup (new WeakReference <Camera >(this ), cameraId , force , keep );
303347 }
304348
305349 /**
@@ -312,7 +356,8 @@ protected void finalize() {
312356 release ();
313357 }
314358
315- private native final void native_setup (Object camera_this , int cameraId );
359+ private native final void native_setup (Object camera_this , int cameraId ,
360+ boolean force , boolean keep );
316361 private native final void native_release ();
317362
318363
@@ -326,6 +371,18 @@ public final void release() {
326371 mFaceDetectionRunning = false ;
327372 }
328373
374+ /**
375+ * Whether the camera is released. When any camera method throws an
376+ * exception, applications can use this to check whether the camera has been
377+ * taken by other clients. If true, it means other clients have taken the
378+ * camera. The applications can silently finish themselves or show a dialog.
379+ *
380+ * @return whether the camera is released.
381+ * @see #open(int, boolean, boolean)
382+ * @hide
383+ */
384+ public native final boolean isReleased ();
385+
329386 /**
330387 * Unlocks the camera to allow another process to access it.
331388 * Normally, the camera is locked to the process with an active Camera
@@ -1321,6 +1378,17 @@ public Face() {
13211378 */
13221379 public static final int CAMERA_ERROR_UNKNOWN = 1 ;
13231380
1381+ /**
1382+ * Camera was released because another client has opened the camera. The
1383+ * application should call {@link #release()} after getting this. The apps
1384+ * should not call any method except <code>release</code> and {@link #isReleased()}
1385+ * after this.
1386+ *
1387+ * @see Camera.ErrorCallback
1388+ * @hide
1389+ */
1390+ public static final int CAMERA_ERROR_RELEASED = 2 ;
1391+
13241392 /**
13251393 * Media server died. In this case, the application must release the
13261394 * Camera object and instantiate a new one.
0 commit comments