Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e69f6148 authored by Hongwei Wang's avatar Hongwei Wang Committed by Android (Google) Code Review
Browse files

Merge "Create source rect hint in PiP flicker test" into main

parents dd7a630a 7b2e67e8
Loading
Loading
Loading
Loading
+2 −12
Original line number Original line Diff line number Diff line
@@ -27,15 +27,6 @@
         where things are arranged differently and to circle back up to the top once we reach the
         where things are arranged differently and to circle back up to the top once we reach the
         bottom. -->
         bottom. -->


    <!-- View used for testing sourceRectHint. -->
    <View
        android:id="@+id/source_rect"
        android:layout_width="320dp"
        android:layout_height="180dp"
        android:visibility="gone"
        android:background="@android:color/holo_green_light"
        />

    <Button
    <Button
        android:id="@+id/enter_pip"
        android:id="@+id/enter_pip"
        android:layout_width="wrap_content"
        android:layout_width="wrap_content"
@@ -122,12 +113,11 @@
            android:onClick="onRatioSelected"/>
            android:onClick="onRatioSelected"/>
    </RadioGroup>
    </RadioGroup>


    <Button
    <CheckBox
        android:id="@+id/set_source_rect_hint"
        android:id="@+id/set_source_rect_hint"
        android:layout_width="wrap_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set SourceRectHint"
        android:text="Set SourceRectHint"/>
        android:onClick="setSourceRectHint"/>


    <TextView
    <TextView
        android:layout_width="wrap_content"
        android:layout_width="wrap_content"
+53 −26
Original line number Original line Diff line number Diff line
@@ -43,10 +43,10 @@ import android.media.MediaMetadata;
import android.media.session.MediaSession;
import android.media.session.MediaSession;
import android.media.session.PlaybackState;
import android.media.session.PlaybackState;
import android.os.Bundle;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.Log;
import android.util.Rational;
import android.util.Rational;
import android.view.View;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CheckBox;
@@ -70,7 +70,7 @@ public class PipActivity extends Activity {
     */
     */
    private static final String TITLE_STATE_PAUSED = "TestApp media is paused";
    private static final String TITLE_STATE_PAUSED = "TestApp media is paused";


    private static final Rational RATIO_DEFAULT = null;
    private static final Rational RATIO_DEFAULT = new Rational(16, 9);
    private static final Rational RATIO_SQUARE = new Rational(1, 1);
    private static final Rational RATIO_SQUARE = new Rational(1, 1);
    private static final Rational RATIO_WIDE = new Rational(2, 1);
    private static final Rational RATIO_WIDE = new Rational(2, 1);
    private static final Rational RATIO_TALL = new Rational(1, 2);
    private static final Rational RATIO_TALL = new Rational(1, 2);
@@ -88,8 +88,7 @@ public class PipActivity extends Activity {
            "com.android.wm.shell.flicker.testapp.ASPECT_RATIO";
            "com.android.wm.shell.flicker.testapp.ASPECT_RATIO";


    private final PictureInPictureParams.Builder mPipParamsBuilder =
    private final PictureInPictureParams.Builder mPipParamsBuilder =
            new PictureInPictureParams.Builder()
            new PictureInPictureParams.Builder();
                    .setAspectRatio(RATIO_DEFAULT);
    private MediaSession mMediaSession;
    private MediaSession mMediaSession;
    private final PlaybackState.Builder mPlaybackStateBuilder = new PlaybackState.Builder()
    private final PlaybackState.Builder mPlaybackStateBuilder = new PlaybackState.Builder()
            .setActions(ACTION_PLAY | ACTION_PAUSE | ACTION_STOP)
            .setActions(ACTION_PLAY | ACTION_PAUSE | ACTION_STOP)
@@ -139,6 +138,9 @@ public class PipActivity extends Activity {
        }
        }
    };
    };


    private Rational mAspectRatio = RATIO_DEFAULT;
    private boolean mEnableSourceRectHint;

    @Override
    @Override
    public void onCreate(Bundle savedInstanceState) {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);
@@ -156,6 +158,14 @@ public class PipActivity extends Activity {
        findViewById(R.id.media_session_stop)
        findViewById(R.id.media_session_stop)
                .setOnClickListener(v -> updateMediaSessionState(STATE_STOPPED));
                .setOnClickListener(v -> updateMediaSessionState(STATE_STOPPED));


        final CheckBox setSourceRectHintCheckBox = findViewById(R.id.set_source_rect_hint);
        setSourceRectHintCheckBox.setOnCheckedChangeListener((v, isChecked) -> {
            if (mEnableSourceRectHint != isChecked) {
                mEnableSourceRectHint = isChecked;
                updateSourceRectHint();
            }
        });

        mMediaSession = new MediaSession(this, "WMShell_TestApp");
        mMediaSession = new MediaSession(this, "WMShell_TestApp");
        mMediaSession.setPlaybackState(mPlaybackStateBuilder.build());
        mMediaSession.setPlaybackState(mPlaybackStateBuilder.build());
        mMediaSession.setCallback(new MediaSession.Callback() {
        mMediaSession.setCallback(new MediaSession.Callback() {
@@ -250,47 +260,64 @@ public class PipActivity extends Activity {
        }
        }
    }
    }


    /**
    private void updateSourceRectHint() {
     * Adds a temporary view used for testing sourceRectHint.
        if (!mEnableSourceRectHint) return;
     *
        // Similar to PipUtils#getEnterPipWithOverlaySrcRectHint, crop the display bounds
     */
        // as source rect hint based on the current aspect ratio.
    public void setSourceRectHint(View v) {
        final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        View rectView = findViewById(R.id.source_rect);
        final Rect displayBounds = new Rect(0, 0,
        if (rectView != null) {
                displayMetrics.widthPixels, displayMetrics.heightPixels);
            rectView.setVisibility(View.VISIBLE);
        final Rect sourceRectHint = getEnterPipWithOverlaySrcRectHint(
            rectView.getViewTreeObserver().addOnGlobalLayoutListener(
                displayBounds, mAspectRatio.floatValue());
                    new ViewTreeObserver.OnGlobalLayoutListener() {
        mPipParamsBuilder
                        @Override
                .setAspectRatio(mAspectRatio)
                        public void onGlobalLayout() {
                .setSourceRectHint(sourceRectHint);
                            Rect boundingRect = new Rect();
                            rectView.getGlobalVisibleRect(boundingRect);
                            mPipParamsBuilder.setSourceRectHint(boundingRect);
        setPictureInPictureParams(mPipParamsBuilder.build());
        setPictureInPictureParams(mPipParamsBuilder.build());
                            rectView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
    }
                    });

            rectView.invalidate(); // changing the visibility, invalidating to redraw the view
    /**
     * Crop a Rect matches the aspect ratio and pivots at the center point.
     * This is a counterpart of {@link PipUtils#getEnterPipWithOverlaySrcRectHint}
     */
    private Rect getEnterPipWithOverlaySrcRectHint(Rect appBounds, float aspectRatio) {
        final float appBoundsAspectRatio = appBounds.width() / (float) appBounds.height();
        final int width, height;
        int left = appBounds.left;
        int top = appBounds.top;
        if (appBoundsAspectRatio < aspectRatio) {
            width = appBounds.width();
            height = (int) (width / aspectRatio);
            top = appBounds.top + (appBounds.height() - height) / 2;
        } else {
            height = appBounds.height();
            width = (int) (height * aspectRatio);
            left = appBounds.left + (appBounds.width() - width) / 2;
        }
        }
        return new Rect(left, top, left + width, top + height);
    }
    }


    public void onRatioSelected(View v) {
    public void onRatioSelected(View v) {
        switch (v.getId()) {
        switch (v.getId()) {
            case R.id.ratio_default:
            case R.id.ratio_default:
                mPipParamsBuilder.setAspectRatio(RATIO_DEFAULT);
                mAspectRatio = RATIO_DEFAULT;
                break;
                break;


            case R.id.ratio_square:
            case R.id.ratio_square:
                mPipParamsBuilder.setAspectRatio(RATIO_SQUARE);
                mAspectRatio = RATIO_SQUARE;
                break;
                break;


            case R.id.ratio_wide:
            case R.id.ratio_wide:
                mPipParamsBuilder.setAspectRatio(RATIO_WIDE);
                mAspectRatio = RATIO_WIDE;
                break;
                break;


            case R.id.ratio_tall:
            case R.id.ratio_tall:
                mPipParamsBuilder.setAspectRatio(RATIO_TALL);
                mAspectRatio = RATIO_TALL;
                break;
                break;
        }
        }
        setPictureInPictureParams(mPipParamsBuilder.setAspectRatio(mAspectRatio).build());
        if (mEnableSourceRectHint) {
            updateSourceRectHint();
        }
    }
    }


    private void updateMediaSessionState(int newState) {
    private void updateMediaSessionState(int newState) {