Search in sources :

Example 11 with ICameraService

use of android.hardware.ICameraService in project android_frameworks_base by DirtyUnicorns.

the class CameraManager method getCameraCharacteristics.

/**
     * <p>Query the capabilities of a camera device. These capabilities are
     * immutable for a given camera.</p>
     *
     * @param cameraId The id of the camera device to query
     * @return The properties of the given camera
     *
     * @throws IllegalArgumentException if the cameraId does not match any
     *         known camera device.
     * @throws CameraAccessException if the camera device has been disconnected.
     *
     * @see #getCameraIdList
     * @see android.app.admin.DevicePolicyManager#setCameraDisabled
     */
@NonNull
public CameraCharacteristics getCameraCharacteristics(@NonNull String cameraId) throws CameraAccessException {
    CameraCharacteristics characteristics = null;
    synchronized (mLock) {
        if (!getOrCreateDeviceIdListLocked().contains(cameraId)) {
            throw new IllegalArgumentException(String.format("Camera id %s does not match any" + " currently connected camera device", cameraId));
        }
        int id = Integer.parseInt(cameraId);
        /*
             * Get the camera characteristics from the camera service directly if it supports it,
             * otherwise get them from the legacy shim instead.
             */
        ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
        if (cameraService == null) {
            throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED, "Camera service is currently unavailable");
        }
        try {
            if (!supportsCamera2ApiLocked(cameraId)) {
                // Legacy backwards compatibility path; build static info from the camera
                // parameters
                String parameters = cameraService.getLegacyParameters(id);
                CameraInfo info = cameraService.getCameraInfo(id);
                characteristics = LegacyMetadataMapper.createCharacteristics(parameters, info);
            } else {
                // Normal path: Get the camera characteristics directly from the camera service
                CameraMetadataNative info = cameraService.getCameraCharacteristics(id);
                characteristics = new CameraCharacteristics(info);
            }
        } catch (ServiceSpecificException e) {
            throwAsPublicException(e);
        } catch (RemoteException e) {
            // Camera service died - act as if the camera was disconnected
            throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED, "Camera service is currently unavailable", e);
        }
    }
    return characteristics;
}
Also used : ServiceSpecificException(android.os.ServiceSpecificException) CameraMetadataNative(android.hardware.camera2.impl.CameraMetadataNative) RemoteException(android.os.RemoteException) ICameraService(android.hardware.ICameraService) CameraInfo(android.hardware.CameraInfo) NonNull(android.annotation.NonNull)

Example 12 with ICameraService

use of android.hardware.ICameraService in project android_frameworks_base by DirtyUnicorns.

the class CameraManager method getOrCreateDeviceIdListLocked.

/**
     * Return or create the list of currently connected camera devices.
     *
     * <p>In case of errors connecting to the camera service, will return an empty list.</p>
     */
private ArrayList<String> getOrCreateDeviceIdListLocked() throws CameraAccessException {
    if (mDeviceIdList == null) {
        int numCameras = 0;
        ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
        ArrayList<String> deviceIdList = new ArrayList<>();
        // If no camera service, then no devices
        if (cameraService == null) {
            return deviceIdList;
        }
        try {
            numCameras = cameraService.getNumberOfCameras(CAMERA_TYPE_ALL);
        } catch (ServiceSpecificException e) {
            throwAsPublicException(e);
        } catch (RemoteException e) {
            // camera service just died - if no camera service, then no devices
            return deviceIdList;
        }
        for (int i = 0; i < numCameras; ++i) {
            // Non-removable cameras use integers starting at 0 for their
            // identifiers
            boolean isDeviceSupported = false;
            try {
                CameraMetadataNative info = cameraService.getCameraCharacteristics(i);
                if (!info.isEmpty()) {
                    isDeviceSupported = true;
                } else {
                    throw new AssertionError("Expected to get non-empty characteristics");
                }
            } catch (ServiceSpecificException e) {
                // propagate exception onward
                if (e.errorCode != ICameraService.ERROR_DISCONNECTED || e.errorCode != ICameraService.ERROR_ILLEGAL_ARGUMENT) {
                    throwAsPublicException(e);
                }
            } catch (RemoteException e) {
                // Camera service died - no devices to list
                deviceIdList.clear();
                return deviceIdList;
            }
            if (isDeviceSupported) {
                deviceIdList.add(String.valueOf(i));
            } else {
                Log.w(TAG, "Error querying camera device " + i + " for listing.");
            }
        }
        mDeviceIdList = deviceIdList;
    }
    return mDeviceIdList;
}
Also used : ServiceSpecificException(android.os.ServiceSpecificException) CameraMetadataNative(android.hardware.camera2.impl.CameraMetadataNative) ArrayList(java.util.ArrayList) RemoteException(android.os.RemoteException) ICameraService(android.hardware.ICameraService)

Example 13 with ICameraService

use of android.hardware.ICameraService in project android_frameworks_base by AOSPA.

the class CameraManager method openCameraDeviceUserAsync.

/**
     * Helper for opening a connection to a camera with the given ID.
     *
     * @param cameraId The unique identifier of the camera device to open
     * @param callback The callback for the camera. Must not be null.
     * @param handler  The handler to invoke the callback on. Must not be null.
     * @param uid      The UID of the application actually opening the camera.
     *                 Must be USE_CALLING_UID unless the caller is a service
     *                 that is trusted to open the device on behalf of an
     *                 application and to forward the real UID.
     *
     * @throws CameraAccessException if the camera is disabled by device policy,
     * too many camera devices are already open, or the cameraId does not match
     * any currently available camera device.
     *
     * @throws SecurityException if the application does not have permission to
     * access the camera
     * @throws IllegalArgumentException if callback or handler is null.
     * @return A handle to the newly-created camera device.
     *
     * @see #getCameraIdList
     * @see android.app.admin.DevicePolicyManager#setCameraDisabled
     */
private CameraDevice openCameraDeviceUserAsync(String cameraId, CameraDevice.StateCallback callback, Handler handler, final int uid) throws CameraAccessException {
    CameraCharacteristics characteristics = getCameraCharacteristics(cameraId);
    CameraDevice device = null;
    synchronized (mLock) {
        ICameraDeviceUser cameraUser = null;
        android.hardware.camera2.impl.CameraDeviceImpl deviceImpl = new android.hardware.camera2.impl.CameraDeviceImpl(cameraId, callback, handler, characteristics);
        ICameraDeviceCallbacks callbacks = deviceImpl.getCallbacks();
        int id;
        try {
            id = Integer.parseInt(cameraId);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Expected cameraId to be numeric, but it was: " + cameraId);
        }
        try {
            if (supportsCamera2ApiLocked(cameraId)) {
                // Use cameraservice's cameradeviceclient implementation for HAL3.2+ devices
                ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
                if (cameraService == null) {
                    throw new ServiceSpecificException(ICameraService.ERROR_DISCONNECTED, "Camera service is currently unavailable");
                }
                cameraUser = cameraService.connectDevice(callbacks, id, mContext.getOpPackageName(), uid);
            } else {
                // Use legacy camera implementation for HAL1 devices
                Log.i(TAG, "Using legacy camera HAL.");
                cameraUser = CameraDeviceUserShim.connectBinderShim(callbacks, id);
            }
        } catch (ServiceSpecificException e) {
            if (e.errorCode == ICameraService.ERROR_DEPRECATED_HAL) {
                throw new AssertionError("Should've gone down the shim path");
            } else if (e.errorCode == ICameraService.ERROR_CAMERA_IN_USE || e.errorCode == ICameraService.ERROR_MAX_CAMERAS_IN_USE || e.errorCode == ICameraService.ERROR_DISABLED || e.errorCode == ICameraService.ERROR_DISCONNECTED || e.errorCode == ICameraService.ERROR_INVALID_OPERATION) {
                // Received one of the known connection errors
                // The remote camera device cannot be connected to, so
                // set the local camera to the startup error state
                deviceImpl.setRemoteFailure(e);
                if (e.errorCode == ICameraService.ERROR_DISABLED || e.errorCode == ICameraService.ERROR_DISCONNECTED || e.errorCode == ICameraService.ERROR_CAMERA_IN_USE) {
                    // Per API docs, these failures call onError and throw
                    throwAsPublicException(e);
                }
            } else {
                // Unexpected failure - rethrow
                throwAsPublicException(e);
            }
        } catch (RemoteException e) {
            // Camera service died - act as if it's a CAMERA_DISCONNECTED case
            ServiceSpecificException sse = new ServiceSpecificException(ICameraService.ERROR_DISCONNECTED, "Camera service is currently unavailable");
            deviceImpl.setRemoteFailure(sse);
            throwAsPublicException(sse);
        }
        // TODO: factor out callback to be non-nested, then move setter to constructor
        // For now, calling setRemoteDevice will fire initial
        // onOpened/onUnconfigured callbacks.
        // This function call may post onDisconnected and throw CAMERA_DISCONNECTED if
        // cameraUser dies during setup.
        deviceImpl.setRemoteDevice(cameraUser);
        device = deviceImpl;
    }
    return device;
}
Also used : ServiceSpecificException(android.os.ServiceSpecificException) RemoteException(android.os.RemoteException) ICameraService(android.hardware.ICameraService)

Example 14 with ICameraService

use of android.hardware.ICameraService in project android_frameworks_base by AOSPA.

the class CameraManager method getCameraCharacteristics.

/**
     * <p>Query the capabilities of a camera device. These capabilities are
     * immutable for a given camera.</p>
     *
     * @param cameraId The id of the camera device to query
     * @return The properties of the given camera
     *
     * @throws IllegalArgumentException if the cameraId does not match any
     *         known camera device.
     * @throws CameraAccessException if the camera device has been disconnected.
     *
     * @see #getCameraIdList
     * @see android.app.admin.DevicePolicyManager#setCameraDisabled
     */
@NonNull
public CameraCharacteristics getCameraCharacteristics(@NonNull String cameraId) throws CameraAccessException {
    CameraCharacteristics characteristics = null;
    synchronized (mLock) {
        if (!getOrCreateDeviceIdListLocked().contains(cameraId)) {
            throw new IllegalArgumentException(String.format("Camera id %s does not match any" + " currently connected camera device", cameraId));
        }
        int id = Integer.parseInt(cameraId);
        /*
             * Get the camera characteristics from the camera service directly if it supports it,
             * otherwise get them from the legacy shim instead.
             */
        ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
        if (cameraService == null) {
            throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED, "Camera service is currently unavailable");
        }
        try {
            if (!supportsCamera2ApiLocked(cameraId)) {
                // Legacy backwards compatibility path; build static info from the camera
                // parameters
                String parameters = cameraService.getLegacyParameters(id);
                CameraInfo info = cameraService.getCameraInfo(id);
                characteristics = LegacyMetadataMapper.createCharacteristics(parameters, info);
            } else {
                // Normal path: Get the camera characteristics directly from the camera service
                CameraMetadataNative info = cameraService.getCameraCharacteristics(id);
                characteristics = new CameraCharacteristics(info);
            }
        } catch (ServiceSpecificException e) {
            throwAsPublicException(e);
        } catch (RemoteException e) {
            // Camera service died - act as if the camera was disconnected
            throw new CameraAccessException(CameraAccessException.CAMERA_DISCONNECTED, "Camera service is currently unavailable", e);
        }
    }
    return characteristics;
}
Also used : ServiceSpecificException(android.os.ServiceSpecificException) CameraMetadataNative(android.hardware.camera2.impl.CameraMetadataNative) RemoteException(android.os.RemoteException) ICameraService(android.hardware.ICameraService) CameraInfo(android.hardware.CameraInfo) NonNull(android.annotation.NonNull)

Example 15 with ICameraService

use of android.hardware.ICameraService in project android_frameworks_base by ResurrectionRemix.

the class CameraManager method getOrCreateDeviceIdListLocked.

/**
     * Return or create the list of currently connected camera devices.
     *
     * <p>In case of errors connecting to the camera service, will return an empty list.</p>
     */
private ArrayList<String> getOrCreateDeviceIdListLocked() throws CameraAccessException {
    if (mDeviceIdList == null) {
        int numCameras = 0;
        ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
        ArrayList<String> deviceIdList = new ArrayList<>();
        // If no camera service, then no devices
        if (cameraService == null) {
            return deviceIdList;
        }
        try {
            numCameras = cameraService.getNumberOfCameras(CAMERA_TYPE_ALL);
            /* Force exposing only two cameras
                 * if the package name does not falls in this bucket
                 */
            boolean exposeAuxCamera = true;
            String packageName = ActivityThread.currentOpPackageName();
            String packageList = SystemProperties.get("camera.auxdisable.packagelist");
            if (packageList.length() > 0) {
                TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
                splitter.setString(packageList);
                for (String str : splitter) {
                    if (packageName.equals(str)) {
                        exposeAuxCamera = false;
                        break;
                    }
                }
            }
            if (exposeAuxCamera == false && (numCameras > 2)) {
                numCameras = 2;
            }
        } catch (ServiceSpecificException e) {
            throwAsPublicException(e);
        } catch (RemoteException e) {
            // camera service just died - if no camera service, then no devices
            return deviceIdList;
        }
        for (int i = 0; i < numCameras; ++i) {
            // Non-removable cameras use integers starting at 0 for their
            // identifiers
            boolean isDeviceSupported = false;
            try {
                CameraMetadataNative info = cameraService.getCameraCharacteristics(i);
                if (!info.isEmpty()) {
                    isDeviceSupported = true;
                } else {
                    throw new AssertionError("Expected to get non-empty characteristics");
                }
            } catch (ServiceSpecificException e) {
                // propagate exception onward
                if (e.errorCode != ICameraService.ERROR_DISCONNECTED || e.errorCode != ICameraService.ERROR_ILLEGAL_ARGUMENT) {
                    throwAsPublicException(e);
                }
            } catch (RemoteException e) {
                // Camera service died - no devices to list
                deviceIdList.clear();
                return deviceIdList;
            }
            if (isDeviceSupported) {
                deviceIdList.add(String.valueOf(i));
            } else {
                Log.w(TAG, "Error querying camera device " + i + " for listing.");
            }
        }
        mDeviceIdList = deviceIdList;
    }
    return mDeviceIdList;
}
Also used : ServiceSpecificException(android.os.ServiceSpecificException) ArrayList(java.util.ArrayList) TextUtils(android.text.TextUtils) CameraMetadataNative(android.hardware.camera2.impl.CameraMetadataNative) RemoteException(android.os.RemoteException) ICameraService(android.hardware.ICameraService)

Aggregations

ICameraService (android.hardware.ICameraService)15 RemoteException (android.os.RemoteException)15 ServiceSpecificException (android.os.ServiceSpecificException)15 CameraMetadataNative (android.hardware.camera2.impl.CameraMetadataNative)10 NonNull (android.annotation.NonNull)5 CameraInfo (android.hardware.CameraInfo)5 ArrayList (java.util.ArrayList)5 TextUtils (android.text.TextUtils)3