Merge "Integrate custom close action in ApiDemos for PiP" into tm-dev

This commit is contained in:
Hongwei Wang
2022-04-20 17:37:44 +00:00
committed by Android (Google) Code Review
3 changed files with 85 additions and 1 deletions

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
~ Copyright (C) 2022 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M12,9c-1.6,0 -3.15,0.25 -4.6,0.72v3.1c0,0.39 -0.23,0.74 -0.56,0.9 -0.98,0.49 -1.87,1.12 -2.66,1.85 -0.18,0.18 -0.43,0.28 -0.7,0.28 -0.28,0 -0.53,-0.11 -0.71,-0.29L0.29,13.08c-0.18,-0.17 -0.29,-0.42 -0.29,-0.7 0,-0.28 0.11,-0.53 0.29,-0.71C3.34,8.78 7.46,7 12,7s8.66,1.78 11.71,4.67c0.18,0.18 0.29,0.43 0.29,0.71 0,0.28 -0.11,0.53 -0.29,0.71l-2.48,2.48c-0.18,0.18 -0.43,0.29 -0.71,0.29 -0.27,0 -0.52,-0.11 -0.7,-0.28 -0.79,-0.74 -1.69,-1.36 -2.67,-1.85 -0.33,-0.16 -0.56,-0.5 -0.56,-0.9v-3.1C15.15,9.25 13.6,9 12,9z"
android:fillColor="@android:color/white"/>
</vector>

View File

@@ -68,6 +68,7 @@
<string name="activity_picture_in_picture_seamless_resize_toggle">Enable seamless resize</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>
<string name="label_current_position">Current position</string>
<string name="label_exit_position">Exit position</string>
<string name="label_position_start">Start</string>

View File

@@ -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<RemoteAction> 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());
}