Revert "Revert "Update animation for fingerprint edu page""

This reverts commit ba0a012b5783c71ff1f589949fbbde3a41d20b54.

Fix: 204719520
Test: Settings should not crash when entering fingerprint edu page
Change-Id: If79fc50bbd6b198e7fccc52fce66c27cf3761d6d
diff --git a/res/layout/sfps_enroll_find_sensor_layout.xml b/res/layout/sfps_enroll_find_sensor_layout.xml
new file mode 100644
index 0000000..fe74e58
--- /dev/null
+++ b/res/layout/sfps_enroll_find_sensor_layout.xml
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 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.
+-->
+
+<com.google.android.setupdesign.GlifLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:id="@+id/setup_wizard_layout"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    style="?attr/fingerprint_layout_theme">
+
+    <LinearLayout
+        style="@style/SudContentFrame"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:gravity="center_vertical"
+        android:orientation="vertical"
+        android:clipToPadding="false"
+        android:clipChildren="false">
+
+        <com.airbnb.lottie.LottieAnimationView
+            android:id="@+id/illustration_lottie"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:scaleType="centerInside"
+            app:lottie_imageAssetsFolder="images"
+            app:lottie_autoPlay="true"
+            app:lottie_loop="true"
+            app:lottie_rawRes="@raw/fingerprint_edu_lottie"/>
+
+        <com.airbnb.lottie.LottieAnimationView
+            android:id="@+id/illustration_lottie_portrait"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:scaleType="centerInside"
+            android:visibility="gone"
+            app:lottie_imageAssetsFolder="images"
+            app:lottie_autoPlay="true"
+            app:lottie_loop="true"
+            app:lottie_rawRes="@raw/fingerprint_edu_lottie_portrait"/>
+
+    </LinearLayout>
+</com.google.android.setupdesign.GlifLayout>
diff --git a/res/raw/fingerprint_edu_lottie.json b/res/raw/fingerprint_edu_lottie.json
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/res/raw/fingerprint_edu_lottie.json
diff --git a/res/raw/fingerprint_edu_lottie_portrait.json b/res/raw/fingerprint_edu_lottie_portrait.json
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/res/raw/fingerprint_edu_lottie_portrait.json
diff --git a/src/com/android/settings/biometrics/fingerprint/FingerprintEnrollFindSensor.java b/src/com/android/settings/biometrics/fingerprint/FingerprintEnrollFindSensor.java
index 3cea962..405c090 100644
--- a/src/com/android/settings/biometrics/fingerprint/FingerprintEnrollFindSensor.java
+++ b/src/com/android/settings/biometrics/fingerprint/FingerprintEnrollFindSensor.java
@@ -21,13 +21,14 @@
 import android.hardware.fingerprint.FingerprintManager;
 import android.hardware.fingerprint.FingerprintSensorPropertiesInternal;
 import android.os.Bundle;
+import android.view.OrientationEventListener;
+import android.view.Surface;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.accessibility.AccessibilityManager;
 
 import androidx.annotation.Nullable;
 
-import com.airbnb.lottie.LottieAnimationView;
 import com.android.settings.R;
 import com.android.settings.Utils;
 import com.android.settings.biometrics.BiometricEnrollBase;
@@ -35,6 +36,7 @@
 import com.android.settings.biometrics.BiometricUtils;
 import com.android.settings.password.ChooseLockSettingsHelper;
 
+import com.airbnb.lottie.LottieAnimationView;
 import com.google.android.setupcompat.template.FooterBarMixin;
 import com.google.android.setupcompat.template.FooterButton;
 
@@ -52,6 +54,10 @@
     private FingerprintEnrollSidecar mSidecar;
     private boolean mNextClicked;
     private boolean mCanAssumeUdfps;
+    private boolean mCanAssumeSidefps;
+
+    private OrientationEventListener mOrientationEventListener;
+    private int mPreviousRotation = 0;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -61,6 +67,7 @@
         final List<FingerprintSensorPropertiesInternal> props =
                 fingerprintManager.getSensorPropertiesInternal();
         mCanAssumeUdfps = props != null && props.size() == 1 && props.get(0).isAnyUdfpsType();
+        mCanAssumeSidefps = props != null && props.size() == 1 && props.get(0).isAnySidefpsType();
         setContentView(getContentView());
         mFooterBarMixin = getLayout().getMixin(FooterBarMixin.class);
         mFooterBarMixin.setSecondaryButton(
@@ -72,6 +79,8 @@
                         .build()
         );
 
+        listenOrientationEvent();
+
         if (mCanAssumeUdfps) {
             setHeaderText(R.string.security_settings_udfps_enroll_find_sensor_title);
             setDescriptionText(R.string.security_settings_udfps_enroll_find_sensor_message);
@@ -90,6 +99,28 @@
                 lottieAnimationView.setAnimation(R.raw.udfps_edu_a11y_lottie);
             }
 
+        } else if (mCanAssumeSidefps) {
+            setHeaderText(R.string.security_settings_fingerprint_enroll_find_sensor_title);
+            setDescriptionText(R.string.security_settings_fingerprint_enroll_find_sensor_message);
+            final LottieAnimationView lottieAnimationView = findViewById(R.id.illustration_lottie);
+            final LottieAnimationView lottieAnimationViewPortrait =
+                    findViewById(R.id.illustration_lottie_portrait);
+            final int rotation = getApplicationContext().getDisplay().getRotation();
+            switch(rotation) {
+                case Surface.ROTATION_90:
+                    lottieAnimationView.setVisibility(View.GONE);
+                    lottieAnimationViewPortrait.setVisibility(View.VISIBLE);
+                    break;
+                case Surface.ROTATION_270:
+                    lottieAnimationView.setVisibility(View.GONE);
+                    lottieAnimationViewPortrait.setVisibility(View.VISIBLE);
+                    lottieAnimationViewPortrait.setRotation(180);
+                    break;
+                default:
+                    lottieAnimationView.setVisibility(View.VISIBLE);
+                    lottieAnimationViewPortrait.setVisibility(View.GONE);
+                    break;
+            }
         } else {
             setHeaderText(R.string.security_settings_fingerprint_enroll_find_sensor_title);
             setDescriptionText(R.string.security_settings_fingerprint_enroll_find_sensor_message);
@@ -145,6 +176,8 @@
     protected int getContentView() {
         if (mCanAssumeUdfps) {
             return R.layout.udfps_enroll_find_sensor_layout;
+        } else if (mCanAssumeSidefps) {
+            return R.layout.sfps_enroll_find_sensor_layout;
         }
         return R.layout.fingerprint_enroll_find_sensor;
     }
@@ -220,6 +253,7 @@
 
     @Override
     protected void onDestroy() {
+        stopListenOrientationEvent();
         super.onDestroy();
         if (mAnimation != null) {
             mAnimation.stopAnimation();
@@ -297,4 +331,37 @@
     public int getMetricsCategory() {
         return SettingsEnums.FINGERPRINT_FIND_SENSOR;
     }
+
+    private void listenOrientationEvent() {
+        if (!mCanAssumeSidefps) {
+            // Do nothing if the device doesn't support SideFPS.
+            return;
+        }
+        mOrientationEventListener = new OrientationEventListener(this) {
+            @Override
+            public void onOrientationChanged(int orientation) {
+                final int currentRotation = getDisplay().getRotation();
+                if ((mPreviousRotation == Surface.ROTATION_90
+                        && currentRotation == Surface.ROTATION_270) || (
+                        mPreviousRotation == Surface.ROTATION_270
+                                && currentRotation == Surface.ROTATION_90)) {
+                    mPreviousRotation = currentRotation;
+                    recreate();
+                }
+            }
+        };
+        mOrientationEventListener.enable();
+        mPreviousRotation = getDisplay().getRotation();
+    }
+
+    private void stopListenOrientationEvent() {
+        if (!mCanAssumeSidefps) {
+            // Do nothing if the device doesn't support SideFPS.
+            return;
+        }
+        if (mOrientationEventListener != null) {
+            mOrientationEventListener.disable();
+        }
+        mOrientationEventListener = null;
+    }
 }