Create SettingsTransitionHelper

This transition helper is created to share with settings that are
injected/out of Settings codebase to ensure the page transitions are
consistent across Settings app.

Bug: 177480673
Test: rebuild and test with Settings
Change-Id: Ibb51d102db4d5365ae82f81d91c4bae7092d6e31
diff --git a/packages/SettingsLib/SettingsTransition/Android.bp b/packages/SettingsLib/SettingsTransition/Android.bp
new file mode 100644
index 0000000..c12f6f7
--- /dev/null
+++ b/packages/SettingsLib/SettingsTransition/Android.bp
@@ -0,0 +1,22 @@
+package {
+    // See: http://go/android-license-faq
+    // A large-scale-change added 'default_applicable_licenses' to import
+    // all of the 'license_kinds' from "frameworks_base_license"
+    // to get the below license kinds:
+    //   SPDX-license-identifier-Apache-2.0
+    default_applicable_licenses: ["frameworks_base_license"],
+}
+
+android_library {
+    name: "SettingsLibSettingsTransition",
+
+    srcs: ["src/**/*.java"],
+    resource_dirs: ["res"],
+
+    static_libs: [
+        "com.google.android.material_material",
+    ],
+
+    sdk_version: "system_current",
+    min_sdk_version: "21",
+}
diff --git a/packages/SettingsLib/SettingsTransition/AndroidManifest.xml b/packages/SettingsLib/SettingsTransition/AndroidManifest.xml
new file mode 100644
index 0000000..11660a5f
--- /dev/null
+++ b/packages/SettingsLib/SettingsTransition/AndroidManifest.xml
@@ -0,0 +1,23 @@
+<?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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+          package="com.android.settingslib.transition">
+
+    <uses-sdk android:minSdkVersion="21" />
+
+</manifest>
diff --git a/packages/SettingsLib/SettingsTransition/res/values/dimens.xml b/packages/SettingsLib/SettingsTransition/res/values/dimens.xml
new file mode 100644
index 0000000..0630ca8
--- /dev/null
+++ b/packages/SettingsLib/SettingsTransition/res/values/dimens.xml
@@ -0,0 +1,19 @@
+<?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.
+-->
+
+<resources>
+    <dimen name="settings_shared_axis_x_slide_distance">96dp</dimen>
+</resources>
\ No newline at end of file
diff --git a/packages/SettingsLib/SettingsTransition/src/com/android/settingslib/transition/SettingsTransitionHelper.java b/packages/SettingsLib/SettingsTransition/src/com/android/settingslib/transition/SettingsTransitionHelper.java
new file mode 100644
index 0000000..f99fda0
--- /dev/null
+++ b/packages/SettingsLib/SettingsTransition/src/com/android/settingslib/transition/SettingsTransitionHelper.java
@@ -0,0 +1,103 @@
+/*
+ * 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.
+ */
+
+package com.android.settingslib.transition;
+
+import android.app.Activity;
+import android.content.Context;
+import android.util.Log;
+import android.view.Window;
+import android.view.animation.AnimationUtils;
+import android.view.animation.Interpolator;
+
+import com.google.android.material.transition.platform.MaterialSharedAxis;
+import com.google.android.material.transition.platform.SlideDistanceProvider;
+
+/**
+ * A helper class to apply Settings Transition
+ */
+public class SettingsTransitionHelper {
+
+    private static final String TAG = "SettingsTransitionHelper";
+    private static final long DURATION = 450L;
+
+    private static MaterialSharedAxis createSettingsSharedAxis(Context context, boolean forward) {
+        final MaterialSharedAxis transition = new MaterialSharedAxis(MaterialSharedAxis.X, forward);
+        transition.excludeTarget(android.R.id.statusBarBackground, true);
+        transition.excludeTarget(android.R.id.navigationBarBackground, true);
+
+        final SlideDistanceProvider forwardDistanceProvider =
+                (SlideDistanceProvider) transition.getPrimaryAnimatorProvider();
+        final int distance = context.getResources().getDimensionPixelSize(
+                R.dimen.settings_shared_axis_x_slide_distance);
+        forwardDistanceProvider.setSlideDistance(distance);
+        transition.setDuration(DURATION);
+
+        final Interpolator interpolator =
+                AnimationUtils.loadInterpolator(context,
+                        android.R.interpolator.fast_out_extra_slow_in);
+        transition.setInterpolator(interpolator);
+
+        // TODO(b/177480673): Update fade through threshold once (cl/362065364) is released
+
+        return transition;
+    }
+
+    /**
+     * Apply the forward transition to the {@link Activity}, including Exit Transition and Enter
+     * Transition.
+     *
+     * The Exit Transition takes effect when leaving the page, while the Enter Transition is
+     * triggered when the page is launched/entering.
+     */
+    public static void applyForwardTransition(Activity activity) {
+        if (activity == null) {
+            Log.w(TAG, "applyForwardTransition: Invalid activity!");
+            return;
+        }
+        final Window window = activity.getWindow();
+        if (window == null) {
+            Log.w(TAG, "applyForwardTransition: Invalid window!");
+            return;
+        }
+        final MaterialSharedAxis forward = createSettingsSharedAxis(activity, true);
+        window.setExitTransition(forward);
+        window.setEnterTransition(forward);
+    }
+
+    /**
+     * Apply the backward transition to the {@link Activity}, including Return Transition and
+     * Reenter Transition.
+     *
+     * Return Transition will be used to move Views out of the scene when the Window is preparing
+     * to close. Reenter Transition will be used to move Views in to the scene when returning from a
+     * previously-started Activity.
+     */
+    public static void applyBackwardTransition(Activity activity) {
+        if (activity == null) {
+            Log.w(TAG, "applyBackwardTransition: Invalid activity!");
+            return;
+        }
+        final Window window = activity.getWindow();
+        if (window == null) {
+            Log.w(TAG, "applyBackwardTransition: Invalid window!");
+            return;
+        }
+        final MaterialSharedAxis backward = createSettingsSharedAxis(activity, false);
+        window.setReturnTransition(backward);
+        window.setReenterTransition(backward);
+    }
+}