Fix flicker in ApiDemos when auto enter PiP from landscape am: d1cc26450c

Original change: https://googleplex-android-review.googlesource.com/c/platform/development/+/14444974

Change-Id: Ib3929cecf85e1212516be23d5b3d16f4b0d11f02
This commit is contained in:
Hongwei Wang
2021-05-13 04:31:30 +00:00
committed by Automerger Merge Worker
4 changed files with 61 additions and 28 deletions

View File

@@ -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>

View File

@@ -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>

View File

@@ -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>

View File

@@ -24,18 +24,14 @@ import android.graphics.Rect;
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 @@ public class PictureInPictureAutoEnter extends Activity {
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 @@ public class PictureInPictureAutoEnter extends Activity {
// 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 @@ public class PictureInPictureAutoEnter extends Activity {
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());
}