diff --git a/samples/ApiDemos/res/drawable/ic_call_end.xml b/samples/ApiDemos/res/drawable/ic_call_end.xml new file mode 100644 index 000000000..6e7059826 --- /dev/null +++ b/samples/ApiDemos/res/drawable/ic_call_end.xml @@ -0,0 +1,26 @@ + + + + + + diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml index 0ce80558c..eccba7ba8 100644 --- a/samples/ApiDemos/res/values/strings.xml +++ b/samples/ApiDemos/res/values/strings.xml @@ -68,6 +68,7 @@ Enable seamless resize Enter content PiP Manually enter PiP + Close PiP Current position Exit position Start diff --git a/samples/ApiDemos/src/com/example/android/apis/app/PictureInPicture.java b/samples/ApiDemos/src/com/example/android/apis/app/PictureInPicture.java index bb76bf392..60731be12 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/PictureInPicture.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/PictureInPicture.java @@ -16,12 +16,21 @@ package com.example.android.apis.app; +import static android.app.PendingIntent.FLAG_IMMUTABLE; +import static android.app.PendingIntent.FLAG_UPDATE_CURRENT; + import android.app.Activity; import android.app.ActivityOptions; +import android.app.PendingIntent; import android.app.PictureInPictureParams; +import android.app.RemoteAction; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.res.Configuration; import android.graphics.Rect; +import android.graphics.drawable.Icon; import android.os.Bundle; import android.os.Handler; import android.os.Looper; @@ -38,6 +47,9 @@ import android.widget.Switch; import com.example.android.apis.R; +import java.util.ArrayList; +import java.util.List; + 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"; @@ -46,6 +58,18 @@ public class PictureInPicture extends Activity { private static final int TABLET_BREAK_POINT_DP = 700; + private static final String ACTION_CUSTOM_CLOSE = "demo.pip.custom_close"; + private final BroadcastReceiver mRemoteActionReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + switch (intent.getAction()) { + case ACTION_CUSTOM_CLOSE: + finish(); + break; + } + } + }; + public static final String KEY_ON_STOP_RECEIVER = "on_stop_receiver"; private final ResultReceiver mOnStopReceiver = new ResultReceiver( new Handler(Looper.myLooper())) { @@ -75,6 +99,8 @@ public class PictureInPicture extends Activity { private Switch mSourceRectHintToggle; private Switch mSeamlessResizeToggle; private RadioGroup mCurrentPositionGroup; + private List mPipActions; + private RemoteAction mCloseAction; @Override protected void onCreate(Bundle savedInstanceState) { @@ -115,6 +141,12 @@ public class PictureInPicture extends Activity { updateLayout(getResources().getConfiguration()); } + @Override + protected void onStart() { + super.onStart(); + setupPipActions(); + } + @Override protected void onUserLeaveHint() { // Only used when auto PiP is disabled. This is to simulate the behavior that an app @@ -141,6 +173,12 @@ public class PictureInPicture extends Activity { } } + @Override + protected void onStop() { + super.onStop(); + unregisterReceiver(mRemoteActionReceiver); + } + /** * This is what we expect most host Activity would do to trigger content PiP. * - Get the bounds of the view to be transferred to content PiP @@ -179,6 +217,23 @@ public class PictureInPicture extends Activity { } } + private void setupPipActions() { + final IntentFilter remoteActionFilter = new IntentFilter(); + remoteActionFilter.addAction(ACTION_CUSTOM_CLOSE); + registerReceiver(mRemoteActionReceiver, remoteActionFilter); + final Intent intent = new Intent(ACTION_CUSTOM_CLOSE).setPackage(getPackageName()); + mCloseAction = new RemoteAction( + Icon.createWithResource(this, R.drawable.ic_call_end), + getString(R.string.action_custom_close), + getString(R.string.action_custom_close), + PendingIntent.getBroadcast(this, 0 /* requestCode */, intent, + FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE)); + + // Add close action as a regular PiP action + mPipActions = new ArrayList<>(1); + mPipActions.add(mCloseAction); + } + private void setupPictureInPictureLayout() { mControlGroup.setVisibility(View.GONE); final LinearLayout.LayoutParams imageLp = new LinearLayout.LayoutParams( @@ -284,7 +339,9 @@ public class PictureInPicture extends Activity { .setSourceRectHint(mSourceRectHintToggle.isChecked() ? new Rect(imageViewRect) : null) .setSeamlessResizeEnabled(mSeamlessResizeToggle.isChecked()) - .setAspectRatio(new Rational(imageViewRect.width(), imageViewRect.height())); + .setAspectRatio(new Rational(imageViewRect.width(), imageViewRect.height())) + .setActions(mPipActions) + .setCloseAction(mCloseAction); setPictureInPictureParams(builder.build()); }