Loading libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipBoundsState.java +33 −4 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); } Loading Loading @@ -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; } Loading Loading @@ -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. */ Loading libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/tv/TvPipGravityTest.java +77 −1 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -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 Loading Loading @@ -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, Loading Loading @@ -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); } } Loading
libs/WindowManager/Shell/src/com/android/wm/shell/pip/tv/TvPipBoundsState.java +33 −4 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); } Loading Loading @@ -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; } Loading Loading @@ -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. */ Loading
libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/pip/tv/TvPipGravityTest.java +77 −1 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -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 Loading Loading @@ -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, Loading Loading @@ -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); } }