Fix CTL scrim not working with activity transitions

Seems to be an issue with how transitions draws
its views, and how CTL draws the scrim. Fixed by
moving the scrim draw command to the preceding view
before the Toolbar.

Also fixed an issue where the title would be drawn
in the wrong position when we have window insets.

BUG: 29748380

Change-Id: I83b9a7ae894cd36ab9ca72cd1fe5d111aa6f2b07
diff --git a/design/src/android/support/design/widget/CollapsingToolbarLayout.java b/design/src/android/support/design/widget/CollapsingToolbarLayout.java
index e354989..0a2cb97 100644
--- a/design/src/android/support/design/widget/CollapsingToolbarLayout.java
+++ b/design/src/android/support/design/widget/CollapsingToolbarLayout.java
@@ -105,6 +105,7 @@
     private Toolbar mToolbar;
     private View mToolbarDirectChild;
     private View mDummyView;
+    private int mToolbarDrawIndex;
 
     private int mExpandedMarginStart;
     private int mExpandedMarginTop;
@@ -303,15 +304,16 @@
     protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
         // This is a little weird. Our scrim needs to be behind the Toolbar (if it is present),
         // but in front of any other children which are behind it. To do this we intercept the
-        // drawChild() call, and draw our scrim first when drawing the toolbar
-        ensureToolbar();
-        if (child == mToolbar && mContentScrim != null && mScrimAlpha > 0) {
+        // drawChild() call, and draw our scrim after the preceding view is drawn
+        boolean invalidate = super.drawChild(canvas, child, drawingTime);
+
+        if (mContentScrim != null && mScrimAlpha > 0 && isToolbarChildDrawnNext(child)) {
             mContentScrim.mutate().setAlpha(mScrimAlpha);
             mContentScrim.draw(canvas);
+            invalidate = true;
         }
 
-        // Carry on drawing the child...
-        return super.drawChild(canvas, child, drawingTime);
+        return invalidate;
     }
 
     @Override
@@ -357,6 +359,10 @@
         mRefreshToolbar = false;
     }
 
+    private boolean isToolbarChildDrawnNext(View child) {
+        return mToolbarDrawIndex >= 0 && mToolbarDrawIndex == indexOfChild(child) + 1;
+    }
+
     /**
      * Returns the direct child of this layout, which itself is the ancestor of the
      * given view.
@@ -452,6 +458,7 @@
             getViewOffsetHelper(child).onViewLayout();
         }
 
+        ensureToolbar();
         // Finally, set our minimum height to enable proper AppBarLayout collapsing
         if (mToolbar != null) {
             if (mCollapsingTitleEnabled && TextUtils.isEmpty(mCollapsingTextHelper.getText())) {
@@ -460,10 +467,16 @@
             }
             if (mToolbarDirectChild == null || mToolbarDirectChild == this) {
                 setMinimumHeight(getHeightWithMargins(mToolbar));
+                mToolbarDrawIndex = indexOfChild(mToolbar);
             } else {
                 setMinimumHeight(getHeightWithMargins(mToolbarDirectChild));
+                mToolbarDrawIndex = indexOfChild(mToolbarDirectChild);
             }
+        } else {
+            mToolbarDrawIndex = -1;
         }
+
+        updateScrimVisibility();
     }
 
     private static int getHeightWithMargins(@NonNull final View view) {
@@ -1198,7 +1211,11 @@
     final int getMaxOffsetForPinChild(View child) {
         final ViewOffsetHelper offsetHelper = getViewOffsetHelper(child);
         final LayoutParams lp = (LayoutParams) child.getLayoutParams();
-        return getHeight() - (offsetHelper.getLayoutTop() + child.getHeight() + lp.bottomMargin);
+        return getHeight()
+                + (mLastInsets != null ? mLastInsets.getSystemWindowInsetTop() : 0)
+                - offsetHelper.getLayoutTop()
+                - child.getHeight()
+                - lp.bottomMargin;
     }
 
     private class OffsetUpdateListener implements AppBarLayout.OnOffsetChangedListener {