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

Commit e4754ca1 authored by Valerie Tang's avatar Valerie Tang
Browse files

Implement persisting pip location

Instead of the pip window being reopened in the default bottom right location each time, it opens in the last (regular) pip window location set by the user. Should persist across reboots as well.

Test: atest TvPipGravityTest
Test: manual: created a test app that can go into regular and expanded pip. Checked that pip position is preserved across pip window close / reboot. Checked that corresponding position is correct when switching between regular / expanded mode across pip window close / reboot.
Bug: 356876580
Flag: EXEMPT BUGFIX
Change-Id: I7e306e707ae8035af83043158eb9e15a7b634dfc
parent 8305743b
Loading
Loading
Loading
Loading
+33 −4
Original line number Diff line number Diff line
@@ -42,6 +42,8 @@ import java.lang.annotation.RetentionPolicy;
 */
public class TvPipBoundsState extends PipBoundsState {

    private static final String TV_PIP_PREFS = "tv_pip_preferences";
    private static final String KEY_LAST_PIP_GRAVITY = "last_user_pip_gravity";
    public static final int ORIENTATION_UNDETERMINED = 0;
    public static final int ORIENTATION_VERTICAL = 1;
    public static final int ORIENTATION_HORIZONTAL = 2;
@@ -81,8 +83,9 @@ public class TvPipBoundsState extends PipBoundsState {
        super(context, sizeSpecSource, pipDisplayLayoutState);
        mContext = context;
        updateDefaultGravity();
        mTvPipGravity = mDefaultGravity;
        mPreviousCollapsedGravity = mDefaultGravity;
        mTvPipGravity = mContext.getSharedPreferences(TV_PIP_PREFS, Context.MODE_PRIVATE)
                .getInt(KEY_LAST_PIP_GRAVITY, mDefaultGravity);
        mPreviousCollapsedGravity = mTvPipGravity;
        mIsTvExpandedPipSupported = context.getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_EXPANDED_PICTURE_IN_PICTURE);
    }
@@ -130,8 +133,9 @@ public class TvPipBoundsState extends PipBoundsState {
    /** Resets the TV PiP state for a new activity. */
    public void resetTvPipState() {
        mTvFixedPipOrientation = ORIENTATION_UNDETERMINED;
        mTvPipGravity = mDefaultGravity;
        mPreviousCollapsedGravity = mDefaultGravity;
        mTvPipGravity = mContext.getSharedPreferences(TV_PIP_PREFS, Context.MODE_PRIVATE)
                .getInt(KEY_LAST_PIP_GRAVITY, mDefaultGravity);
        mPreviousCollapsedGravity = mTvPipGravity;
        mIsTvPipExpanded = false;
        mTvPipManuallyCollapsed = false;
    }
@@ -184,7 +188,32 @@ public class TvPipBoundsState extends PipBoundsState {

    /** Sets the current gravity of the TV PiP. */
    public void setTvPipGravity(int gravity) {
        if (gravity == mTvPipGravity) {
            return;
        }
        mTvPipGravity = gravity;
        int gravityToSave = gravity;
        if (mIsTvExpandedPipSupported
                && mDesiredTvExpandedAspectRatio != 0
                && !mTvPipManuallyCollapsed) {
            // If expanded, calculate corresponding collapsed gravity.
            int currentX = gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
            int currentY = gravity & Gravity.VERTICAL_GRAVITY_MASK;
            int previousCollapsedX = mPreviousCollapsedGravity
                    & Gravity.HORIZONTAL_GRAVITY_MASK;
            int previousCollapsedY = mPreviousCollapsedGravity
                    & Gravity.VERTICAL_GRAVITY_MASK;
            if (getTvFixedPipOrientation() == ORIENTATION_HORIZONTAL) {
                gravityToSave = previousCollapsedX | currentY;
            } else {
                gravityToSave = currentX | previousCollapsedY;
            }
        }
        // Save the current collapsed gravity.
        mContext.getSharedPreferences(TV_PIP_PREFS, Context.MODE_PRIVATE)
                .edit()
                .putInt(KEY_LAST_PIP_GRAVITY, gravityToSave)
                .apply();
    }

    /** Returns the current gravity of the TV PiP. */
+77 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static android.view.KeyEvent.KEYCODE_DPAD_UP;

import static org.junit.Assert.assertEquals;

import android.content.Context;
import android.view.Gravity;

import com.android.wm.shell.ShellTestCase;
@@ -42,6 +43,9 @@ import java.util.Locale;

public class TvPipGravityTest extends ShellTestCase {

    private static final String TV_PIP_PREFS = "tv_pip_preferences";
    private static final String KEY_LAST_PIP_GRAVITY = "last_user_pip_gravity";

    private static final float VERTICAL_EXPANDED_ASPECT_RATIO = 1f / 3;
    private static final float HORIZONTAL_EXPANDED_ASPECT_RATIO = 3f;

@@ -62,6 +66,8 @@ public class TvPipGravityTest extends ShellTestCase {
        assumeTelevision();

        MockitoAnnotations.initMocks(this);
        mContext.getSharedPreferences(TV_PIP_PREFS, Context.MODE_PRIVATE)
                .edit().clear().commit();
        mPipDisplayLayoutState = new PipDisplayLayoutState(mContext, mDisplayController,
                mShellInit);
        // Directly call onInit instead of using ShellInit
@@ -154,14 +160,17 @@ public class TvPipGravityTest extends ShellTestCase {
    }

    @Test
    public void updateGravity_collapse() {
    public void updateGravity_collapseVertical() {
        // Vertical expansion
        mTvPipBoundsState.setDesiredTvExpandedAspectRatio(VERTICAL_EXPANDED_ASPECT_RATIO, true);
        assertGravityAfterCollapse(Gravity.CENTER_VERTICAL | Gravity.RIGHT,
                Gravity.BOTTOM | Gravity.RIGHT);
        assertGravityAfterCollapse(Gravity.CENTER_VERTICAL | Gravity.LEFT,
                Gravity.BOTTOM | Gravity.LEFT);
    }

    @Test
    public void updateGravity_collapseHorizontal() {
        // Horizontal expansion
        mTvPipBoundsState.setDesiredTvExpandedAspectRatio(HORIZONTAL_EXPANDED_ASPECT_RATIO, true);
        assertGravityAfterCollapse(Gravity.TOP | Gravity.CENTER_HORIZONTAL,
@@ -345,4 +354,71 @@ public class TvPipGravityTest extends ShellTestCase {
                Gravity.BOTTOM | Gravity.RIGHT);
    }

    @Test
    public void persistence_loadsLastGravityOnStart() {
        // Save a non-default gravity to SharedPreferences.
        mContext.getSharedPreferences(TV_PIP_PREFS, Context.MODE_PRIVATE).edit()
                .putInt(KEY_LAST_PIP_GRAVITY, Gravity.TOP | Gravity.LEFT).commit();
        TvPipBoundsState newState = new TvPipBoundsState(mContext, mSizeSpecSource,
                mPipDisplayLayoutState);
        checkGravity(newState.getTvPipGravity(), Gravity.TOP | Gravity.LEFT);
    }

    @Test
    public void persistence_loadsLastGravityOnReset() {
        // Save a non-default gravity to SharedPreferences.
        mContext.getSharedPreferences(TV_PIP_PREFS, Context.MODE_PRIVATE).edit()
                .putInt(KEY_LAST_PIP_GRAVITY, Gravity.BOTTOM | Gravity.LEFT).commit();
        mTvPipBoundsState.resetTvPipState();
        checkGravity(mTvPipBoundsState.getTvPipGravity(), Gravity.BOTTOM | Gravity.LEFT);
    }

    @Test
    public void persistence_savesCollapsedGravityWhenExpandedHorizontal() {
        // Horizontal expansion from non-default corner.
        mTvPipBoundsState.setDesiredTvExpandedAspectRatio(HORIZONTAL_EXPANDED_ASPECT_RATIO, true);
        assertGravityAfterExpansion(Gravity.TOP | Gravity.LEFT,
                Gravity.TOP | Gravity.CENTER_HORIZONTAL);
        int savedGravity = mContext.getSharedPreferences(TV_PIP_PREFS, Context.MODE_PRIVATE)
                .getInt(KEY_LAST_PIP_GRAVITY, -1);
        checkGravity(savedGravity, Gravity.TOP | Gravity.LEFT);
    }

    @Test
    public void persistence_savesCollapsedGravityWhenExpandedVertical() {
        // Vertical expansion from non-default corner.
        mTvPipBoundsState.setDesiredTvExpandedAspectRatio(VERTICAL_EXPANDED_ASPECT_RATIO, true);
        assertGravityAfterExpansion(Gravity.TOP | Gravity.RIGHT,
                Gravity.CENTER_VERTICAL | Gravity.RIGHT);
        int savedGravity = mContext.getSharedPreferences(TV_PIP_PREFS, Context.MODE_PRIVATE)
                .getInt(KEY_LAST_PIP_GRAVITY, -1);
        checkGravity(savedGravity, Gravity.TOP | Gravity.RIGHT);
    }

    @Test
    public void persistence_savesMovedCollapsedGravityWhenExpandedHorizontal() {
        // Horizontal Expansion.
        mTvPipBoundsState.setDesiredTvExpandedAspectRatio(HORIZONTAL_EXPANDED_ASPECT_RATIO, true);
        assertGravityAfterExpansion(Gravity.BOTTOM | Gravity.RIGHT,
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        // Move the expanded PiP up.
        moveAndCheckGravity(KEYCODE_DPAD_UP, Gravity.TOP | Gravity.CENTER_HORIZONTAL, true);
        int savedGravity = mContext.getSharedPreferences(TV_PIP_PREFS, Context.MODE_PRIVATE)
                .getInt(KEY_LAST_PIP_GRAVITY, -1);
        checkGravity(savedGravity, Gravity.TOP | Gravity.RIGHT);
    }

    @Test
    public void persistence_savesMovedCollapsedGravityWhenExpandedVertical() {
        // Vertical Expansion.
        mTvPipBoundsState.setDesiredTvExpandedAspectRatio(VERTICAL_EXPANDED_ASPECT_RATIO, true);
        assertGravityAfterExpansion(Gravity.BOTTOM | Gravity.RIGHT,
                Gravity.CENTER_VERTICAL | Gravity.RIGHT);
        // Move the expanded PiP left.
        moveAndCheckGravity(KEYCODE_DPAD_LEFT, Gravity.CENTER_VERTICAL | Gravity.LEFT, true);
        int savedGravity = mContext.getSharedPreferences(TV_PIP_PREFS, Context.MODE_PRIVATE)
                .getInt(KEY_LAST_PIP_GRAVITY, -1);
        checkGravity(savedGravity, Gravity.BOTTOM | Gravity.LEFT);
    }

}