Fix scaleType in CameraView + Add support for all scaleTypes from PreviewView.

ScaleTypes in CameraView has been broken since it began using PreviewView. Since CameraView uses PreviewView for display, PreviewView should handle scale types for it.

Relnote: "CameraView.ScaleType has been removed. Instead, use PreviewView.ScaleType to set/get a scale type with CameraView."
Bug: 153014831
Test: Manual testing using CameraViewFragment
Change-Id: Ia8974a3c8c271ec1dfd2736c2e02d42b27f531c9
diff --git a/camera/camera-view/src/main/java/androidx/camera/view/CameraView.java b/camera/camera-view/src/main/java/androidx/camera/view/CameraView.java
index fa524da..588289f 100644
--- a/camera/camera-view/src/main/java/androidx/camera/view/CameraView.java
+++ b/camera/camera-view/src/main/java/androidx/camera/view/CameraView.java
@@ -121,7 +121,6 @@
                 }
             };
     private PreviewView mPreviewView;
-    private ScaleType mScaleType = ScaleType.CENTER_CROP;
     // For accessibility event
     private MotionEvent mUpEvent;
 
@@ -174,7 +173,7 @@
         if (attrs != null) {
             TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CameraView);
             setScaleType(
-                    ScaleType.fromId(
+                    PreviewView.ScaleType.fromId(
                             a.getInteger(R.styleable.CameraView_scaleType,
                                     getScaleType().getId())));
             setPinchToZoomEnabled(
@@ -262,7 +261,7 @@
         if (savedState instanceof Bundle) {
             Bundle state = (Bundle) savedState;
             super.onRestoreInstanceState(state.getParcelable(EXTRA_SUPER));
-            setScaleType(ScaleType.fromId(state.getInt(EXTRA_SCALE_TYPE)));
+            setScaleType(PreviewView.ScaleType.fromId(state.getInt(EXTRA_SCALE_TYPE)));
             setZoomRatio(state.getFloat(EXTRA_ZOOM_RATIO));
             setPinchToZoomEnabled(state.getBoolean(EXTRA_PINCH_TO_ZOOM_ENABLED));
             setFlash(FlashModeConverter.valueOf(state.getString(EXTRA_FLASH)));
@@ -344,11 +343,11 @@
     /**
      * Returns the scale type used to scale the preview.
      *
-     * @return The current {@link ScaleType}.
+     * @return The current {@link PreviewView.ScaleType}.
      */
     @NonNull
-    public ScaleType getScaleType() {
-        return mScaleType;
+    public PreviewView.ScaleType getScaleType() {
+        return mPreviewView.getScaleType();
     }
 
     /**
@@ -356,13 +355,10 @@
      *
      * <p>This controls how the view finder should be scaled and positioned within the view.
      *
-     * @param scaleType The desired {@link ScaleType}.
+     * @param scaleType The desired {@link PreviewView.ScaleType}.
      */
-    public void setScaleType(@NonNull ScaleType scaleType) {
-        if (scaleType != mScaleType) {
-            mScaleType = scaleType;
-            requestLayout();
-        }
+    public void setScaleType(@NonNull PreviewView.ScaleType scaleType) {
+        mPreviewView.setScaleType(scaleType);
     }
 
     /**
@@ -705,40 +701,6 @@
         return mCameraModule.isTorchOn();
     }
 
-    /** Options for scaling the bounds of the view finder to the bounds of this view. */
-    public enum ScaleType {
-        /**
-         * Scale the view finder, maintaining the source aspect ratio, so the view finder fills the
-         * entire view. This will cause the view finder to crop the source image if the camera
-         * aspect ratio does not match the view aspect ratio.
-         */
-        CENTER_CROP(0),
-        /**
-         * Scale the view finder, maintaining the source aspect ratio, so the view finder is
-         * entirely contained within the view.
-         */
-        CENTER_INSIDE(1);
-
-        private final int mId;
-
-        int getId() {
-            return mId;
-        }
-
-        ScaleType(int id) {
-            mId = id;
-        }
-
-        static ScaleType fromId(int id) {
-            for (ScaleType st : values()) {
-                if (st.mId == id) {
-                    return st;
-                }
-            }
-            throw new IllegalArgumentException();
-        }
-    }
-
     /**
      * The capture mode used by CameraView.
      *
diff --git a/camera/camera-view/src/main/java/androidx/camera/view/PreviewView.java b/camera/camera-view/src/main/java/androidx/camera/view/PreviewView.java
index 753f65f..77cbf0c 100644
--- a/camera/camera-view/src/main/java/androidx/camera/view/PreviewView.java
+++ b/camera/camera-view/src/main/java/androidx/camera/view/PreviewView.java
@@ -242,41 +242,60 @@
          * This may cause the preview to be cropped if the camera preview aspect ratio does not
          * match that of its container {@link PreviewView}.
          */
-        FILL_START,
+        FILL_START(0),
         /**
          * Scale the preview, maintaining the source aspect ratio, so it fills the entire
          * {@link PreviewView}, and center it inside the view.
          * This may cause the preview to be cropped if the camera preview aspect ratio does not
          * match that of its container {@link PreviewView}.
          */
-        FILL_CENTER,
+        FILL_CENTER(1),
         /**
          * Scale the preview, maintaining the source aspect ratio, so it fills the entire
          * {@link PreviewView}, and align it to the bottom right corner of the view.
          * This may cause the preview to be cropped if the camera preview aspect ratio does not
          * match that of its container {@link PreviewView}.
          */
-        FILL_END,
+        FILL_END(2),
         /**
          * Scale the preview, maintaining the source aspect ratio, so it is entirely contained
          * within the {@link PreviewView}, and align it to the top left corner of the view.
          * Both dimensions of the preview will be equal or less than the corresponding dimensions
          * of its container {@link PreviewView}.
          */
-        FIT_START,
+        FIT_START(3),
         /**
          * Scale the preview, maintaining the source aspect ratio, so it is entirely contained
          * within the {@link PreviewView}, and center it inside the view.
          * Both dimensions of the preview will be equal or less than the corresponding dimensions
          * of its container {@link PreviewView}.
          */
-        FIT_CENTER,
+        FIT_CENTER(4),
         /**
          * Scale the preview, maintaining the source aspect ratio, so it is entirely contained
          * within the {@link PreviewView}, and align it to the bottom right corner of the view.
          * Both dimensions of the preview will be equal or less than the corresponding dimensions
          * of its container {@link PreviewView}.
          */
-        FIT_END
+        FIT_END(5);
+
+        private final int mId;
+
+        ScaleType(int id) {
+            mId = id;
+        }
+
+        int getId() {
+            return mId;
+        }
+
+        static ScaleType fromId(int id) {
+            for (ScaleType scaleType : values()) {
+                if (scaleType.mId == id) {
+                    return scaleType;
+                }
+            }
+            throw new IllegalArgumentException("Unknown scale type id " + id);
+        }
     }
 }
diff --git a/camera/camera-view/src/main/res/values/attrs.xml b/camera/camera-view/src/main/res/values/attrs.xml
index 3809abd..daeba49 100644
--- a/camera/camera-view/src/main/res/values/attrs.xml
+++ b/camera/camera-view/src/main/res/values/attrs.xml
@@ -1,5 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2019 The Android Open Source Project
+<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2019 The Android Open Source Project
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
@@ -14,11 +13,16 @@
 limitations under the License.
 -->
 <resources>
+    <attr name="scaleType" format="enum">
+        <enum name="fillStart" value="0" />
+        <enum name="fillCenter" value="1" />
+        <enum name="fillEnd" value="2" />
+        <enum name="fitStart" value="3" />
+        <enum name="fitCenter" value="4" />
+        <enum name="fitEnd" value="5" />
+    </attr>
     <declare-styleable name="CameraView">
-        <attr name="scaleType" format="enum">
-            <enum name="centerCrop" value="0" />
-            <enum name="centerInside" value="1" />
-        </attr>
+        <attr name="scaleType" />
         <attr name="lensFacing" format="enum">
             <enum name="none" value="0" />
             <enum name="front" value="1" />
diff --git a/camera/integration-tests/viewtestapp/src/main/java/androidx/camera/integration/view/CameraViewFragment.java b/camera/integration-tests/viewtestapp/src/main/java/androidx/camera/integration/view/CameraViewFragment.java
index 000a7eb4..8acba10 100644
--- a/camera/integration-tests/viewtestapp/src/main/java/androidx/camera/integration/view/CameraViewFragment.java
+++ b/camera/integration-tests/viewtestapp/src/main/java/androidx/camera/integration/view/CameraViewFragment.java
@@ -33,7 +33,7 @@
 import androidx.camera.core.CameraSelector;
 import androidx.camera.view.CameraView;
 import androidx.camera.view.CameraView.CaptureMode;
-import androidx.camera.view.CameraView.ScaleType;
+import androidx.camera.view.PreviewView;
 import androidx.core.content.ContextCompat;
 import androidx.fragment.app.Fragment;
 
@@ -156,18 +156,15 @@
                     });
         }
 
-        mToggleCropButton.setChecked(mCameraView.getScaleType() == ScaleType.CENTER_CROP);
-        mToggleCropButton.setOnCheckedChangeListener(
-                new CompoundButton.OnCheckedChangeListener() {
-                    @Override
-                    public void onCheckedChanged(CompoundButton b, boolean checked) {
-                        if (checked) {
-                            mCameraView.setScaleType(ScaleType.CENTER_CROP);
-                        } else {
-                            mCameraView.setScaleType(ScaleType.CENTER_INSIDE);
-                        }
-                    }
-                });
+        mToggleCropButton.setChecked(
+                mCameraView.getScaleType() == PreviewView.ScaleType.FILL_CENTER);
+        mToggleCropButton.setOnCheckedChangeListener((b, checked) -> {
+            if (checked) {
+                mCameraView.setScaleType(PreviewView.ScaleType.FILL_CENTER);
+            } else {
+                mCameraView.setScaleType(PreviewView.ScaleType.FIT_CENTER);
+            }
+        });
 
         if (mModeButton != null) {
             updateModeButtonIcon();