Add ability to enter PiP on back pressed in ApiDemos

Bug: 235268241
Test: Press back when in com.example.android.apis/.app.PictureInPicture
Change-Id: I8ceaaf3b1215505bd4919ef5f5da291df62c1049
This commit is contained in:
Hongwei Wang
2022-06-14 13:50:09 -07:00
parent f2a1d8b3e3
commit 8d408e82c1
4 changed files with 24 additions and 0 deletions

View File

@@ -61,6 +61,7 @@
android:icon="@drawable/app_sample_code"
android:hardwareAccelerated="true"
android:supportsRtl="true"
android:enableOnBackInvokedCallback="true"
android:theme="@android:style/Theme.Material.Light.DarkActionBar">
<!-- This is how we can request a library but still allow the app

View File

@@ -69,6 +69,13 @@
android:padding="8dp"
android:text="@string/activity_picture_in_picture_seamless_resize_toggle" />
<Switch
android:id="@+id/enter_pip_on_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="@string/activity_picture_in_picture_enter_pip_on_back_toggle" />
<RadioGroup
android:id="@+id/current_position"
android:layout_width="match_parent"

View File

@@ -66,6 +66,7 @@
<string name="activity_picture_in_picture_auto_pip_toggle">Enable auto PiP</string>
<string name="activity_picture_in_picture_source_rect_hint_toggle">Enable source rect hint</string>
<string name="activity_picture_in_picture_seamless_resize_toggle">Enable seamless resize</string>
<string name="activity_picture_in_picture_enter_pip_on_back_toggle">Enter PiP on back</string>
<string name="enter_content_pip">Enter content PiP</string>
<string name="enter_picture_in_picture">Manually enter PiP</string>
<string name="action_custom_close">Close PiP</string>

View File

@@ -44,6 +44,7 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.window.OnBackInvokedDispatcher;
import com.example.android.apis.R;
@@ -54,6 +55,7 @@ public class PictureInPicture extends Activity {
private static final String EXTRA_ENABLE_AUTO_PIP = "auto_pip";
private static final String EXTRA_ENABLE_SOURCE_RECT_HINT = "source_rect_hint";
private static final String EXTRA_ENABLE_SEAMLESS_RESIZE = "seamless_resize";
private static final String EXTRA_ENTER_PIP_ON_BACK = "enter_pip_on_back";
private static final String EXTRA_CURRENT_POSITION = "current_position";
private static final int TABLET_BREAK_POINT_DP = 700;
@@ -98,6 +100,7 @@ public class PictureInPicture extends Activity {
private Switch mAutoPipToggle;
private Switch mSourceRectHintToggle;
private Switch mSeamlessResizeToggle;
private Switch mEnterPipOnBackToggle;
private RadioGroup mCurrentPositionGroup;
private List<RemoteAction> mPipActions;
private RemoteAction mCloseAction;
@@ -114,6 +117,7 @@ public class PictureInPicture extends Activity {
mAutoPipToggle = findViewById(R.id.auto_pip_toggle);
mSourceRectHintToggle = findViewById(R.id.source_rect_hint_toggle);
mSeamlessResizeToggle = findViewById(R.id.seamless_resize_toggle);
mEnterPipOnBackToggle = findViewById(R.id.enter_pip_on_back);
mCurrentPositionGroup = findViewById(R.id.current_position);
// Attach listeners
@@ -121,6 +125,15 @@ public class PictureInPicture extends Activity {
mAutoPipToggle.setOnCheckedChangeListener(mOnToggleChangedListener);
mSourceRectHintToggle.setOnCheckedChangeListener(mOnToggleChangedListener);
mSeamlessResizeToggle.setOnCheckedChangeListener(mOnToggleChangedListener);
mEnterPipOnBackToggle.setOnCheckedChangeListener(mOnToggleChangedListener);
getOnBackInvokedDispatcher().registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT, () -> {
if (mEnterPipOnBackToggle.isChecked()) {
enterPictureInPictureMode();
} else {
finish();
}
});
mCurrentPositionGroup.setOnCheckedChangeListener(mOnPositionChangedListener);
findViewById(R.id.enter_pip_button).setOnClickListener(v -> enterPictureInPictureMode());
findViewById(R.id.enter_content_pip_button).setOnClickListener(v -> enterContentPip());
@@ -132,6 +145,8 @@ public class PictureInPicture extends Activity {
intent.getBooleanExtra(EXTRA_ENABLE_SOURCE_RECT_HINT, false));
mSeamlessResizeToggle.setChecked(
intent.getBooleanExtra(EXTRA_ENABLE_SEAMLESS_RESIZE, false));
mEnterPipOnBackToggle.setChecked(
intent.getBooleanExtra(EXTRA_ENTER_PIP_ON_BACK, false));
final int positionId = "end".equalsIgnoreCase(
intent.getStringExtra(EXTRA_CURRENT_POSITION))
? R.id.radio_current_end