Source:
private ListenableFuture<Void> setZoomRatioInternal(float ratio, boolean updatePercentage) { if (!mIsActive) { throw new IllegalStateException("Camera is not active. Please bind use cases first."); } Rect sensorRect = mCamera2CameraControl.getSensorRect(); if (sensorRect == null) { throw new IllegalStateException("Cannot get sensor active array"); } if (ratio > getMaxZoomRatio().getValue()) { Log.e(TAG, "invalid zoomRatio: exceeds maxZoomRatio"); } else if (ratio < getMinZoomRatio().getValue()) { Log.e(TAG, "invalid zoomRatio: less than minZoomRatio"); } ratio = rangeLimit(ratio, getMaxZoomRatio().getValue(), getMinZoomRatio().getValue()); setLiveDataValue(mZoomRatioLiveData, ratio); if (updatePercentage) { setLiveDataValue(mZoomPercentageLiveData, getPercentageByRatio(ratio)); } int cropWidth = (int) (sensorRect.width() / ratio); int cropHeight = (int) (sensorRect.height() / ratio); int left = (int) ((sensorRect.width() - cropWidth) / 2.0f); int top = (int) ((sensorRect.height() - cropHeight) / 2.0f); Rect targetRegion; if (ratio == 1.0f) { targetRegion = null; } else { targetRegion = new Rect(left, top, left + cropWidth, top + cropHeight); } mCamera2CameraControl.setCropRegion(targetRegion); return CallbackToFutureAdapter.getFuture(new CallbackToFutureAdapter.Resolver<Void>() { @Nullable @Override public Object attachCompleter( @NonNull CallbackToFutureAdapter.Completer<Void> completer) throws Exception { synchronized (mActionLock) { if (mPendingZoomRatioCompleter != null) { mPendingZoomRatioCompleter.setException( new OperationCanceledException( "There is a new zoomRatio being set")); mPendingZoomRatioCompleter = null; } mPendingZoomCropRegion = targetRegion; mPendingZoomRatioCompleter = completer; } return "setZoomRatio"; } }); }

Target:
private ListenableFuture<Void> setZoomRatioInternal(float ratio, boolean updatePercentage) { if (!mIsActive) { throw new IllegalStateException("Camera is not active."); } Rect sensorRect = mCamera2CameraControl.getSensorRect(); if (sensorRect == null) { throw new IllegalStateException("Cannot get sensor active array"); } if (ratio > getMaxZoomRatio().getValue()) { Log.e(TAG, "invalid zoomRatio: exceeds maxZoomRatio"); } else if (ratio < getMinZoomRatio().getValue()) { Log.e(TAG, "invalid zoomRatio: less than minZoomRatio"); } ratio = rangeLimit(ratio, getMaxZoomRatio().getValue(), getMinZoomRatio().getValue()); setLiveDataValue(mZoomRatioLiveData, ratio); if (updatePercentage) { setLiveDataValue(mZoomPercentageLiveData, getPercentageByRatio(ratio)); } int cropWidth = (int) (sensorRect.width() / ratio); int cropHeight = (int) (sensorRect.height() / ratio); int left = (int) ((sensorRect.width() - cropWidth) / 2.0f); int top = (int) ((sensorRect.height() - cropHeight) / 2.0f); Rect targetRegion; if (ratio == 1.0f) { targetRegion = null; } else { targetRegion = new Rect(left, top, left + cropWidth, top + cropHeight); } mCamera2CameraControl.setCropRegion(targetRegion); return CallbackToFutureAdapter.getFuture(new CallbackToFutureAdapter.Resolver<Void>() { @Nullable @Override public Object attachCompleter( @NonNull CallbackToFutureAdapter.Completer<Void> completer) throws Exception { CallbackToFutureAdapter.Completer<Void> completerToSetException = null; synchronized (mCompleterLock) { if (mPendingZoomRatioCompleter != null) { completerToSetException = mPendingZoomRatioCompleter; mPendingZoomRatioCompleter = null; } mPendingZoomCropRegion = targetRegion; mPendingZoomRatioCompleter = completer; } if (completerToSetException != null) { completerToSetException.setException( new OperationCanceledException( "There is a new zoomRatio being set")); } return "setZoomRatio"; } }); }