Merge "Qurik: Force PreviewView to use TextureView on Samsung Fold2" into androidx-main
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 3b44754..e537265 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
@@ -64,6 +64,8 @@
 import androidx.camera.core.impl.CameraInternal;
 import androidx.camera.core.impl.ImageOutputConfig;
 import androidx.camera.core.impl.utils.Threads;
+import androidx.camera.view.internal.compat.quirk.DeviceQuirks;
+import androidx.camera.view.internal.compat.quirk.SurfaceViewStretchedQuirk;
 import androidx.camera.view.transform.CoordinateTransform;
 import androidx.camera.view.transform.OutputTransform;
 import androidx.core.content.ContextCompat;
@@ -594,14 +596,16 @@
 
     // Synthetic access
     @SuppressWarnings("WeakerAccess")
-    boolean shouldUseTextureView(@NonNull SurfaceRequest surfaceRequest,
+    static boolean shouldUseTextureView(@NonNull SurfaceRequest surfaceRequest,
             @NonNull final ImplementationMode implementationMode) {
         // TODO(b/159127402): use TextureView if target rotation is not display rotation.
         boolean isLegacyDevice = surfaceRequest.getCamera().getCameraInfoInternal()
                 .getImplementationType().equals(CameraInfo.IMPLEMENTATION_TYPE_CAMERA2_LEGACY);
-        if (surfaceRequest.isRGBA8888Required() || Build.VERSION.SDK_INT <= 24 || isLegacyDevice) {
+        boolean hasSurfaceViewQuirk = DeviceQuirks.get(SurfaceViewStretchedQuirk.class) != null;
+        if (surfaceRequest.isRGBA8888Required() || Build.VERSION.SDK_INT <= 24 || isLegacyDevice
+                || hasSurfaceViewQuirk) {
             // Force to use TextureView when the device is running android 7.0 and below, legacy
-            // level or RGBA8888 is required.
+            // level, RGBA8888 is required or SurfaceView has quirks.
             return true;
         }
         switch (implementationMode) {
diff --git a/camera/camera-view/src/main/java/androidx/camera/view/internal/compat/quirk/DeviceQuirksLoader.java b/camera/camera-view/src/main/java/androidx/camera/view/internal/compat/quirk/DeviceQuirksLoader.java
index 65e35b3..1fca5fe 100644
--- a/camera/camera-view/src/main/java/androidx/camera/view/internal/compat/quirk/DeviceQuirksLoader.java
+++ b/camera/camera-view/src/main/java/androidx/camera/view/internal/compat/quirk/DeviceQuirksLoader.java
@@ -43,6 +43,10 @@
             quirks.add(new PreviewStretchedQuirk());
         }
 
+        if (SurfaceViewStretchedQuirk.load()) {
+            quirks.add(new SurfaceViewStretchedQuirk());
+        }
+
         return quirks;
     }
 }
diff --git a/camera/camera-view/src/main/java/androidx/camera/view/internal/compat/quirk/SurfaceViewStretchedQuirk.java b/camera/camera-view/src/main/java/androidx/camera/view/internal/compat/quirk/SurfaceViewStretchedQuirk.java
new file mode 100644
index 0000000..46b49f5
--- /dev/null
+++ b/camera/camera-view/src/main/java/androidx/camera/view/internal/compat/quirk/SurfaceViewStretchedQuirk.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2021 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.camera.view.internal.compat.quirk;
+
+import android.os.Build;
+
+import androidx.camera.core.impl.Quirk;
+
+/**
+ * A quirk where SurfaceView is stretched.
+ *
+ * <p> On Samsung Galaxy Z Fold2, transform APIs (e.g. View#setScaleX) do not work as intended.
+ * b/129403806
+ */
+public class SurfaceViewStretchedQuirk implements Quirk {
+
+    // Samsung Galaxy Z Fold2 b/129403806
+    private static final String SAMSUNG = "SAMSUNG";
+    private static final String GALAXY_Z_FOLD_2 = "F2Q";
+
+    static boolean load() {
+        return SAMSUNG.equals(Build.MANUFACTURER.toUpperCase()) && GALAXY_Z_FOLD_2.equals(
+                Build.DEVICE.toUpperCase());
+    }
+}
diff --git a/camera/camera-view/src/test/java/androidx/camera/view/PreviewViewTest.java b/camera/camera-view/src/test/java/androidx/camera/view/PreviewViewTest.java
new file mode 100644
index 0000000..82c2e25
--- /dev/null
+++ b/camera/camera-view/src/test/java/androidx/camera/view/PreviewViewTest.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2021 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.camera.view;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.os.Build;
+import android.util.Size;
+
+import androidx.camera.core.CameraInfo;
+import androidx.camera.core.SurfaceRequest;
+import androidx.camera.testing.fakes.FakeCamera;
+import androidx.camera.testing.fakes.FakeCameraInfoInternal;
+import androidx.camera.view.internal.compat.quirk.QuirkInjector;
+import androidx.camera.view.internal.compat.quirk.SurfaceViewStretchedQuirk;
+
+import org.junit.After;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.annotation.Config;
+import org.robolectric.annotation.internal.DoNotInstrument;
+
+@RunWith(RobolectricTestRunner.class)
+@DoNotInstrument
+@Config(minSdk = Build.VERSION_CODES.LOLLIPOP)
+public class PreviewViewTest {
+
+    @After
+    public void tearDown() {
+        QuirkInjector.clear();
+    }
+
+    @Test
+    @Config(minSdk = Build.VERSION_CODES.N_MR1)
+    public void surfaceViewNormal_useSurfaceView() {
+        // Assert: SurfaceView is used.
+        assertThat(PreviewView.shouldUseTextureView(
+                createSurfaceRequestCompatibleWithSurfaceView(),
+                PreviewView.ImplementationMode.PERFORMANCE)).isFalse();
+    }
+
+    @Test
+    public void surfaceViewHasQuirk_useTextureView() {
+        // Arrange:
+        QuirkInjector.inject(new SurfaceViewStretchedQuirk());
+
+        // Assert: TextureView is used even the SurfaceRequest is compatible with SurfaceView.
+        assertThat(PreviewView.shouldUseTextureView(
+                createSurfaceRequestCompatibleWithSurfaceView(),
+                PreviewView.ImplementationMode.PERFORMANCE)).isTrue();
+    }
+
+    private SurfaceRequest createSurfaceRequestCompatibleWithSurfaceView() {
+        FakeCameraInfoInternal cameraInfoInternal = new FakeCameraInfoInternal();
+        cameraInfoInternal.setImplementationType(CameraInfo.IMPLEMENTATION_TYPE_CAMERA2);
+        return new SurfaceRequest(new Size(800, 600),
+                new FakeCamera(null, cameraInfoInternal),
+                /*isRGB8888Required*/ false);
+    }
+}
diff --git a/camera/camera-view/src/test/java/androidx/camera/view/internal/compat/quirk/DeviceQuirks.java b/camera/camera-view/src/test/java/androidx/camera/view/internal/compat/quirk/DeviceQuirks.java
new file mode 100644
index 0000000..d9f5964
--- /dev/null
+++ b/camera/camera-view/src/test/java/androidx/camera/view/internal/compat/quirk/DeviceQuirks.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2021 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.camera.view.internal.compat.quirk;
+
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.camera.core.impl.Quirk;
+
+import java.util.List;
+
+/**
+ * Tests version of main/.../DeviceQuirks.java, which provides device specific quirks, used for
+ * device specific workarounds.
+ * <p>
+ * In main/.../DeviceQuirks, Device quirks are loaded the first time a device workaround is
+ * encountered, and remain in memory until the process is killed. When running tests, this means
+ * that the same device quirks are used for all the tests. This causes an issue when tests modify
+ * device properties (using Robolectric for instance). Instead of force-reloading the device
+ * quirks in every test that uses a device workaround, this class internally reloads the quirks
+ * every time a device workaround is needed.
+ */
+public class DeviceQuirks {
+
+    private DeviceQuirks() {
+    }
+
+    /**
+     * Retrieves a specific device {@link Quirk} instance given its type.
+     *
+     * @param quirkClass The type of device quirk to retrieve.
+     * @return A device {@link Quirk} instance of the provided type, or {@code null} if it isn't
+     * found.
+     */
+    @SuppressWarnings("unchecked")
+    @Nullable
+    public static <T extends Quirk> T get(@NonNull final Class<T> quirkClass) {
+        final List<Quirk> quirks = DeviceQuirksLoader.loadQuirks();
+        quirks.addAll(QuirkInjector.INJECTED_QUIRKS);
+        for (final Quirk quirk : quirks) {
+            if (quirk.getClass() == quirkClass) {
+                return (T) quirk;
+            }
+        }
+        return null;
+    }
+}
diff --git a/camera/camera-view/src/test/java/androidx/camera/view/internal/compat/quirk/QuirkInjector.java b/camera/camera-view/src/test/java/androidx/camera/view/internal/compat/quirk/QuirkInjector.java
new file mode 100644
index 0000000..d7568c3
--- /dev/null
+++ b/camera/camera-view/src/test/java/androidx/camera/view/internal/compat/quirk/QuirkInjector.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2021 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.camera.view.internal.compat.quirk;
+
+import androidx.annotation.NonNull;
+import androidx.camera.core.impl.Quirk;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Inject quirks for unit tests.
+ *
+ * <p> Used with the test version of {@link DeviceQuirks} to test the behavior of quirks.
+ */
+public class QuirkInjector {
+
+    static final List<Quirk> INJECTED_QUIRKS = new ArrayList<>();
+
+    /**
+     * Inject a quirk. The injected quirk will be loaded by {@link DeviceQuirks}.
+     */
+    public static void inject(@NonNull Quirk quirk) {
+        INJECTED_QUIRKS.add(quirk);
+    }
+
+    /**
+     * Clears all injected quirks.
+     */
+    public static void clear() {
+        INJECTED_QUIRKS.clear();
+    }
+}
diff --git a/camera/camera-view/src/test/java/androidx/camera/view/internal/compat/quirk/SurfaceViewStretchedQuirkTest.java b/camera/camera-view/src/test/java/androidx/camera/view/internal/compat/quirk/SurfaceViewStretchedQuirkTest.java
new file mode 100644
index 0000000..965357e
--- /dev/null
+++ b/camera/camera-view/src/test/java/androidx/camera/view/internal/compat/quirk/SurfaceViewStretchedQuirkTest.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2021 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.camera.view.internal.compat.quirk;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.os.Build;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.annotation.Config;
+import org.robolectric.annotation.internal.DoNotInstrument;
+import org.robolectric.util.ReflectionHelpers;
+
+/**
+ * Unit test for {@link SurfaceViewStretchedQuirk}.
+ */
+@RunWith(RobolectricTestRunner.class)
+@DoNotInstrument
+@Config(minSdk = Build.VERSION_CODES.LOLLIPOP)
+public class SurfaceViewStretchedQuirkTest {
+
+    @Test
+    public void quirkExistsOnSamsungGalaxyZFold2() {
+        // Arrange.
+        ReflectionHelpers.setStaticField(Build.class, "DEVICE", "F2Q");
+        ReflectionHelpers.setStaticField(Build.class, "MANUFACTURER", "SAMSUNG");
+
+        // Act.
+        final SurfaceViewStretchedQuirk quirk = DeviceQuirks.get(SurfaceViewStretchedQuirk.class);
+
+        // Assert.
+        assertThat(quirk).isNotNull();
+    }
+}