camera/camera-core/src/main/java/androidx/camera/core/CameraX.java
/**
     * Initializes camera stack on the given thread and retry recursively until timeout.
     */
Initializes camera stack on the given thread and retry recursively until timeout.
Initializes camera stack on the given thread and retry recursively until timeout .
private void initAndRetryRecursively(
            @NonNull Executor cameraExecutor,
            long startMs,
            @NonNull Context context,
            @NonNull CallbackToFutureAdapter.Completer<Void> completer) {
        cameraExecutor.execute(() -> {
            try {
                mAppContext = ContextUtil.getApplicationFromContext(context);
                if (mAppContext == null) {
                    mAppContext = ContextUtil.getApplicationContext(context);
                }
                CameraFactory.Provider cameraFactoryProvider =
                        mCameraXConfig.getCameraFactoryProvider(null);
                if (cameraFactoryProvider == null) {
                    throw new InitializationException(new IllegalArgumentException(
                            "Invalid app configuration provided. Missing "
                                    + "CameraFactory."));
                }
                CameraThreadConfig cameraThreadConfig = CameraThreadConfig.create(mCameraExecutor,
                        mSchedulerHandler);
                CameraSelector availableCamerasLimiter =
                        mCameraXConfig.getAvailableCamerasLimiter(null);
                mCameraFactory = cameraFactoryProvider.newInstance(mAppContext,
                        cameraThreadConfig, availableCamerasLimiter);
                CameraDeviceSurfaceManager.Provider surfaceManagerProvider =
                        mCameraXConfig.getDeviceSurfaceManagerProvider(null);
                if (surfaceManagerProvider == null) {
                    throw new InitializationException(new IllegalArgumentException(
                            "Invalid app configuration provided. Missing "
                                    + "CameraDeviceSurfaceManager."));
                }
                mSurfaceManager = surfaceManagerProvider.newInstance(mAppContext,
                        mCameraFactory.getCameraManager(),
                        mCameraFactory.getAvailableCameraIds());
                UseCaseConfigFactory.Provider configFactoryProvider =
                        mCameraXConfig.getUseCaseConfigFactoryProvider(null);
                if (configFactoryProvider == null) {
                    throw new InitializationException(new IllegalArgumentException(
                            "Invalid app configuration provided. Missing "
                                    + "UseCaseConfigFactory."));
                }
                mDefaultConfigFactory = configFactoryProvider.newInstance(mAppContext);
                if (cameraExecutor instanceof CameraExecutor) {
                    CameraExecutor executor = (CameraExecutor) cameraExecutor;
                    executor.init(mCameraFactory);
                }
                mCameraRepository.init(mCameraFactory);
                CameraValidator.validateCameras(mAppContext, mCameraRepository,
                        availableCamerasLimiter);
                setStateToInitialized();
                completer.set(null);
            } catch (CameraValidator.CameraIdListIncorrectException | InitializationException
                    | RuntimeException e) {
                if (SystemClock.elapsedRealtime() - startMs
                        < WAIT_INITIALIZED_TIMEOUT_MILLIS - RETRY_SLEEP_MILLIS) {
                    Logger.w(TAG, "Retry init. Start time " + startMs + " current time "
                            + SystemClock.elapsedRealtime(), e);
                    HandlerCompat.postDelayed(mSchedulerHandler, () -> initAndRetryRecursively(
                            cameraExecutor, startMs, mAppContext, completer), RETRY_TOKEN,
                            RETRY_SLEEP_MILLIS);
                } else {
                    synchronized (mInitializeLock) {
                        mInitState = InternalInitState.INITIALIZING_ERROR;
                    }
                    if (e instanceof CameraValidator.CameraIdListIncorrectException) {
                        Logger.e(TAG, "The device might underreport the amount of the cameras. "
                                + "Finish the initialize task since we are already reaching the "
                                + "maximum number of retries.");
                        completer.set(null);
                    } else if (e instanceof InitializationException) {
                        completer.setException(e);
                    } else {
                        completer.setException(new InitializationException(e));
                    }
                }
            }
        });
    }
private void initAndRetryRecursively ( @ NonNull Executor cameraExecutor , long startMs , @ NonNull Context context , @ NonNull CallbackToFutureAdapter . Completer < Void > completer ) { cameraExecutor . execute ( ( ) -> { try { mAppContext = ContextUtil . getApplicationFromContext ( context ) ; if ( mAppContext == null ) { mAppContext = ContextUtil . getApplicationContext ( context ) ; } CameraFactory . Provider cameraFactoryProvider = mCameraXConfig . getCameraFactoryProvider ( null ) ; if ( cameraFactoryProvider == null ) { throw new InitializationException ( new IllegalArgumentException ( "Invalid app configuration provided. Missing " + "CameraFactory." ) ) ; } CameraThreadConfig cameraThreadConfig = CameraThreadConfig . create ( mCameraExecutor , mSchedulerHandler ) ; CameraSelector availableCamerasLimiter = mCameraXConfig . getAvailableCamerasLimiter ( null ) ; mCameraFactory = cameraFactoryProvider . newInstance ( mAppContext , cameraThreadConfig , availableCamerasLimiter ) ; CameraDeviceSurfaceManager . Provider surfaceManagerProvider = mCameraXConfig . getDeviceSurfaceManagerProvider ( null ) ; if ( surfaceManagerProvider == null ) { throw new InitializationException ( new IllegalArgumentException ( "Invalid app configuration provided. Missing " + "CameraDeviceSurfaceManager." ) ) ; } mSurfaceManager = surfaceManagerProvider . newInstance ( mAppContext , mCameraFactory . getCameraManager ( ) , mCameraFactory . getAvailableCameraIds ( ) ) ; UseCaseConfigFactory . Provider configFactoryProvider = mCameraXConfig . getUseCaseConfigFactoryProvider ( null ) ; if ( configFactoryProvider == null ) { throw new InitializationException ( new IllegalArgumentException ( "Invalid app configuration provided. Missing " + "UseCaseConfigFactory." ) ) ; } mDefaultConfigFactory = configFactoryProvider . newInstance ( mAppContext ) ; if ( cameraExecutor instanceof CameraExecutor ) { CameraExecutor executor = ( CameraExecutor ) cameraExecutor ; executor . init ( mCameraFactory ) ; } mCameraRepository . init ( mCameraFactory ) ; CameraValidator . validateCameras ( mAppContext , mCameraRepository , availableCamerasLimiter ) ; setStateToInitialized ( ) ; completer . set ( null ) ; } catch ( CameraValidator . CameraIdListIncorrectException | InitializationException | RuntimeException e ) { if ( SystemClock . elapsedRealtime ( ) - startMs < WAIT_INITIALIZED_TIMEOUT_MILLIS - RETRY_SLEEP_MILLIS ) { Logger . w ( TAG , "Retry init. Start time " + startMs + " current time " + SystemClock . elapsedRealtime ( ) , e ) ; HandlerCompat . postDelayed ( mSchedulerHandler , ( ) -> initAndRetryRecursively ( cameraExecutor , startMs , mAppContext , completer ) , RETRY_TOKEN , RETRY_SLEEP_MILLIS ) ; } else { synchronized ( mInitializeLock ) { mInitState = InternalInitState . INITIALIZING_ERROR ; } if ( e instanceof CameraValidator . CameraIdListIncorrectException ) { Logger . e ( TAG , "The device might underreport the amount of the cameras. " + "Finish the initialize task since we are already reaching the " + "maximum number of retries." ) ; completer . set ( null ) ; } else if ( e instanceof InitializationException ) { completer . setException ( e ) ; } else { completer . setException ( new InitializationException ( e ) ) ; } } } } ) ; }
Initializes camera stack on the given thread and retry recursively until timeout.
Initializes camera stack on the given thread and retry recursively until timeout .
// TODO(b/161302102): Remove the stored context. Only make use of //  the context within the called method. // Please ensure only validate the camera at the last of the initialization. // Set completer to null if the init was successful. // Ignore the camera validation failure if it reaches the maximum retry // time. Set complete. // For any unexpected RuntimeException, catch it instead of crashing.