@Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    int previewWidth = DEFAULT_PREVIEW_WIDTH;
    int previewHeight = DEFAULT_PREVIEW_HEIGHT;
    if (cameraSource != null) {
      Size size = cameraSource.getPreviewSize();
      if (size != null) {
        previewWidth = size.getWidth();
        previewHeight = size.getHeight();
      }
    }
    // Swap width and height sizes when in portrait, since it will be rotated 90 degrees.
    if (isPortraitMode()) {
      int tmp = previewWidth;
      previewWidth = previewHeight;
      previewHeight = tmp;
    }

    BarcodeCentralFilter.storeCameraSize(new Size(previewWidth, previewHeight));

    final int layoutWidth = right - left;
    final int layoutHeight = bottom - top;

    if (isPortraitMode()) {
      // Fit the width.
      int childHeight = (int) (((double) layoutWidth / (double) previewWidth) * previewHeight);
      int marginTop = (layoutHeight - childHeight) / 2;
      for (int i = 0; i < getChildCount(); i++) {
        getChildAt(i).layout(0, marginTop, layoutWidth, childHeight + marginTop);
      }
    } else {
      // Fit the height.
      int childWidth = (int) (((double) layoutHeight / (double) previewHeight) * previewWidth);
      int marginLeft = (layoutWidth - childWidth) / 2;
      for (int i = 0; i < getChildCount(); i++) {
        getChildAt(i).layout(marginLeft, 0, childWidth + marginLeft, layoutHeight);
      }
    }

    tryToStart();
  }
Stops the camera preview and releases the resources of the underlying camera source.
java/com/google/android/apps/authenticator/barcode/preview/CameraSourcePreview.java