Fix flicker in ApiDemos when auto enter PiP from landscape

Video: http://recall/-/aaaaaabFQoRHlzixHdtY/cNwi8qI0qukRlaIwutzclD
Bug: 179720719
Test: auto enter PiP from landscape using ApiDemos app, see video
Change-Id: Ic421b3766fbc15bf699885863710a14c3178867f
diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml
index d75a5f5..8b6e5ef 100644
--- a/samples/ApiDemos/AndroidManifest.xml
+++ b/samples/ApiDemos/AndroidManifest.xml
@@ -302,6 +302,7 @@
                   android:label="@string/activity_picture_in_picture_auto_enter"
                   android:resizeableActivity="true"
                   android:supportsPictureInPicture="true"
+                  android:theme="@style/Theme.NoActionBar"
                   android:configChanges=
                       "screenSize|smallestScreenSize|screenLayout|orientation">
             <intent-filter>
diff --git a/samples/ApiDemos/res/layout/picture_in_picture_auto_enter.xml b/samples/ApiDemos/res/layout/picture_in_picture_auto_enter.xml
index 228e8f9..09912b5 100644
--- a/samples/ApiDemos/res/layout/picture_in_picture_auto_enter.xml
+++ b/samples/ApiDemos/res/layout/picture_in_picture_auto_enter.xml
@@ -15,37 +15,31 @@
 -->
 
 <!-- Demonstrates Picture-In-Picture with auto enter enabled. -->
-<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
+    android:layout_height="match_parent"
+    android:orientation="vertical">
 
+    <!-- layout params would be changed programmatically -->
     <ImageView android:id="@+id/image"
-        android:layout_width="0dp"
-        android:layout_height="0dp"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
         android:scaleType="fitXY"
-        android:src="@drawable/sample_1"
-        app:layout_constraintTop_toTopOf="parent"
-        app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintDimensionRatio="H,16:9" />
+        android:src="@drawable/sample_1" />
 
     <Switch android:id="@+id/source_rect_hint_toggle"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_margin="8dp"
         android:padding="8dp"
-        android:text="@string/activity_picture_in_picture_source_rect_hint_toggle"
-        app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintBottom_toTopOf="@id/change_orientation" />
+        android:text="@string/activity_picture_in_picture_source_rect_hint_toggle" />
 
     <Button android:id="@+id/change_orientation"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_margin="8dp"
         android:padding="8dp"
-        android:text="@string/activity_picture_in_picture_change_orientation"
-        app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintBottom_toBottomOf="parent" />
+        android:text="@string/activity_picture_in_picture_change_orientation" />
 
-</androidx.constraintlayout.widget.ConstraintLayout>
+</LinearLayout>
diff --git a/samples/ApiDemos/res/values/styles.xml b/samples/ApiDemos/res/values/styles.xml
index bca6101..2da03f0 100644
--- a/samples/ApiDemos/res/values/styles.xml
+++ b/samples/ApiDemos/res/values/styles.xml
@@ -129,4 +129,10 @@
 	<item name="android:windowAllowEnterTransitionOverlap">false</item>
     </style>
 
+    <!-- A theme without action bar -->
+    <style name="Theme.NoActionBar" parent="android:Theme.Material.Light">
+        <item name="windowActionBar">false</item>
+        <item name="android:windowNoTitle">true</item>
+    </style>
+
 </resources>
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/PictureInPictureAutoEnter.java b/samples/ApiDemos/src/com/example/android/apis/app/PictureInPictureAutoEnter.java
index 770b0d2..05f4558 100644
--- a/samples/ApiDemos/src/com/example/android/apis/app/PictureInPictureAutoEnter.java
+++ b/samples/ApiDemos/src/com/example/android/apis/app/PictureInPictureAutoEnter.java
@@ -24,18 +24,14 @@
 import android.os.Bundle;
 import android.util.Rational;
 import android.view.View;
+import android.view.WindowManager;
 import android.widget.CompoundButton;
+import android.widget.LinearLayout;
 import android.widget.Switch;
 
 import com.example.android.apis.R;
 
 public class PictureInPictureAutoEnter extends Activity {
-    // Fixed the aspect ratio value as it's been specified in the layout file.
-    // Defined the value here to avoid unnecessary onTaskInfoChanged callbacks to SysUI
-    // due to 1px difference when measuring aspect ratio from width and height in
-    // layout change listener.
-    private final Rational mAspectRatio = new Rational(16, 9);
-
     private final View.OnLayoutChangeListener mOnLayoutChangeListener =
             (v, oldLeft, oldTop, oldRight, oldBottom, newLeft, newTop, newRight, newBottom) -> {
                 updatePictureInPictureParams();
@@ -47,11 +43,11 @@
     private View mImageView;
     private View mButtonView;
     private Switch mSwitchView;
+    private int mLastOrientation = -1;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        getActionBar().hide();
         setContentView(R.layout.picture_in_picture_auto_enter);
 
         mImageView = findViewById(R.id.image);
@@ -73,16 +69,52 @@
         // therefore this demo activity can be used for testing autoEnterPip behavior without
         // source rect hint when launched for the first time.
         mSwitchView.setChecked(false);
+
+        updateLayout(getResources().getConfiguration());
     }
 
     @Override
-    public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode,
-            Configuration newConfig) {
-        mButtonView.setVisibility(isInPictureInPictureMode ? View.GONE : View.VISIBLE);
-        mSwitchView.setVisibility(isInPictureInPictureMode ? View.GONE: View.VISIBLE);
+    public void onConfigurationChanged(Configuration newConfiguration) {
+        super.onConfigurationChanged(newConfiguration);
+        if (!isInPictureInPictureMode()) {
+            updateLayout(newConfiguration);
+        }
+    }
+
+    private void updateLayout(Configuration configuration) {
+        if (configuration.orientation == mLastOrientation) return;
+        mLastOrientation = configuration.orientation;
+        final boolean isLandscape = (mLastOrientation == Configuration.ORIENTATION_LANDSCAPE);
+        mButtonView.setVisibility(isLandscape ? View.GONE : View.VISIBLE);
+        mSwitchView.setVisibility(isLandscape ? View.GONE: View.VISIBLE);
+        final LinearLayout.LayoutParams layoutParams;
+        // Toggle the fullscreen mode as well.
+        // TODO(b/188001699) switch to use insets controller once the bug is fixed.
+        final View decorView = getWindow().getDecorView();
+        final int systemUiNavigationBarFlags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
+                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
+        if (isLandscape) {
+            layoutParams = new LinearLayout.LayoutParams(
+                    LinearLayout.LayoutParams.MATCH_PARENT,
+                    LinearLayout.LayoutParams.MATCH_PARENT);
+            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
+                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
+            decorView.setSystemUiVisibility(decorView.getSystemUiVisibility()
+                    | systemUiNavigationBarFlags);
+        } else {
+            layoutParams = new LinearLayout.LayoutParams(
+                    LinearLayout.LayoutParams.MATCH_PARENT,
+                    LinearLayout.LayoutParams.WRAP_CONTENT);
+            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
+            decorView.setSystemUiVisibility(decorView.getSystemUiVisibility()
+                    & ~systemUiNavigationBarFlags);
+        }
+        mImageView.setLayoutParams(layoutParams);
     }
 
     private void updatePictureInPictureParams() {
+        // do not bother PictureInPictureParams update when it's already in pip mode.
+        if (isInPictureInPictureMode()) return;
         final Rect imageViewRect = new Rect();
         mImageView.getGlobalVisibleRect(imageViewRect);
         // bail early if mImageView has not been measured yet
@@ -90,7 +122,7 @@
         final Rect sourceRectHint = mSwitchView.isChecked() ? new Rect(imageViewRect) : null;
         final PictureInPictureParams.Builder builder = new PictureInPictureParams.Builder()
                 .setAutoEnterEnabled(true)
-                .setAspectRatio(mAspectRatio)
+                .setAspectRatio(new Rational(imageViewRect.width(), imageViewRect.height()))
                 .setSourceRectHint(sourceRectHint);
         setPictureInPictureParams(builder.build());
     }