Merge "Refactor some transition codes." into sc-dev
diff --git a/src/com/android/settings/SettingsActivity.java b/src/com/android/settings/SettingsActivity.java
index 923c2bd..7dd5fe4 100644
--- a/src/com/android/settings/SettingsActivity.java
+++ b/src/com/android/settings/SettingsActivity.java
@@ -233,18 +233,6 @@
 
     @Override
     protected void onCreate(Bundle savedState) {
-        if (FeatureFlagUtils.isEnabled(this, FeatureFlags.SILKY_HOME)) {
-            // Enable Activity transitions
-            getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
-            final MaterialSharedAxis enterTransition = new MaterialSharedAxis(
-                    MaterialSharedAxis.X, /* forward */true);
-            getWindow().setEnterTransition(enterTransition);
-
-            final MaterialSharedAxis returnTransition = new MaterialSharedAxis(
-                    MaterialSharedAxis.X, /* forward */false);
-            getWindow().setReturnTransition(returnTransition);
-        }
-
         super.onCreate(savedState);
         Log.d(LOG_TAG, "Starting onCreate");
         long startTime = System.currentTimeMillis();
diff --git a/src/com/android/settings/core/SettingsBaseActivity.java b/src/com/android/settings/core/SettingsBaseActivity.java
index 05635cb..6af95b2 100644
--- a/src/com/android/settings/core/SettingsBaseActivity.java
+++ b/src/com/android/settings/core/SettingsBaseActivity.java
@@ -77,6 +77,7 @@
 
     protected CollapsingToolbarLayout mCollapsingToolbarLayout;
     private int mCategoriesUpdateTaskCount;
+    private Toolbar mToolbar;
 
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -130,20 +131,38 @@
     }
 
     @Override
+    public void setActionBar(@androidx.annotation.Nullable Toolbar toolbar) {
+        super.setActionBar(toolbar);
+
+        mToolbar = toolbar;
+    }
+
+    @Override
     public boolean onNavigateUp() {
         if (!super.onNavigateUp()) {
-            finish();
+            finishAfterTransition();
         }
         return true;
     }
 
     @Override
+    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
+        final int id = item.getItemId();
+        if (id == android.R.id.home) {
+            // Make the up button behave the same as the back button.
+            finishAfterTransition();
+            return true;
+        }
+        return super.onOptionsItemSelected(item);
+    }
+
+    @Override
     public void startActivity(Intent intent) {
         if (!Utils.isPageTransitionEnabled(this)) {
             super.startActivity(intent);
             return;
         }
-        super.startActivity(intent, getActivityOptionsBundle());
+        super.startActivity(intent, createActivityOptionsBundleForTransition(null));
     }
 
     @Override
@@ -152,11 +171,7 @@
             super.startActivity(intent, options);
             return;
         }
-        if (options != null) {
-            super.startActivity(intent, getMergedBundleForTransition(options));
-            return;
-        }
-        super.startActivity(intent, getActivityOptionsBundle());
+        super.startActivity(intent, createActivityOptionsBundleForTransition(options));
     }
 
     @Override
@@ -167,7 +182,8 @@
             super.startActivityForResult(intent, requestCode);
             return;
         }
-        super.startActivityForResult(intent, requestCode, getActivityOptionsBundle());
+        super.startActivityForResult(intent, requestCode,
+                createActivityOptionsBundleForTransition(null));
     }
 
     @Override
@@ -177,12 +193,8 @@
             super.startActivityForResult(intent, requestCode, options);
             return;
         }
-        if (options != null) {
-            super.startActivityForResult(intent, requestCode,
-                    getMergedBundleForTransition(options));
-            return;
-        }
-        super.startActivityForResult(intent, requestCode, getActivityOptionsBundle());
+        super.startActivityForResult(intent, requestCode,
+                createActivityOptionsBundleForTransition(options));
     }
 
     @Override
@@ -192,7 +204,8 @@
             super.startActivityForResultAsUser(intent, requestCode, userHandle);
             return;
         }
-        super.startActivityForResultAsUser(intent, requestCode, getActivityOptionsBundle(),
+        super.startActivityForResultAsUser(intent, requestCode,
+                createActivityOptionsBundleForTransition(null),
                 userHandle);
     }
 
@@ -243,17 +256,6 @@
     }
 
     @Override
-    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
-        final int id = item.getItemId();
-        if (id == android.R.id.home) {
-            // Make the up button behave the same as the back button.
-            onBackPressed();
-            return true;
-        }
-        return super.onOptionsItemSelected(item);
-    }
-
-    @Override
     public void setTitle(CharSequence title) {
         if (mCollapsingToolbarLayout != null) {
             mCollapsingToolbarLayout.setTitle(title);
@@ -341,20 +343,21 @@
         }
     }
 
-    private Bundle getActivityOptionsBundle() {
-        final Toolbar toolbar = findViewById(R.id.action_bar);
-        return ActivityOptions.makeSceneTransitionAnimation(this, toolbar,
-                "shared_element_view").toBundle();
-    }
-
-    private Bundle getMergedBundleForTransition(@NonNull Bundle options) {
-        final Bundle mergedBundle = new Bundle();
-        mergedBundle.putAll(options);
-        final Bundle activityOptionsBundle = getActivityOptionsBundle();
-        if (activityOptionsBundle != null) {
-            mergedBundle.putAll(activityOptionsBundle);
+    @androidx.annotation.Nullable
+    private Bundle createActivityOptionsBundleForTransition(
+            @androidx.annotation.Nullable Bundle options) {
+        if (mToolbar == null) {
+            Log.w(TAG, "setActionBar(Toolbar) is not called. Cannot apply settings transition!");
+            return options;
         }
-        return mergedBundle;
+        final Bundle transitionOptions = ActivityOptions.makeSceneTransitionAnimation(this,
+                mToolbar, "shared_element_view").toBundle();
+        if (options == null) {
+            return transitionOptions;
+        }
+        final Bundle mergedOptions = new Bundle(options);
+        mergedOptions.putAll(transitionOptions);
+        return mergedOptions;
     }
 
     public interface CategoryListener {