Add demo for content PiP

Bug: 165793661
Video: http://recall/-/aaaaaabFQoRHlzixHdtY/e9Mp5rvxYBrNOdGSUoSXHO
Test: manual, see Video
Change-Id: I026844cf5eabf30fe553641b36daed6e31e4f893
This commit is contained in:
Hongwei Wang
2021-12-22 16:11:42 -08:00
parent 832d90d98d
commit ec0d9449a1
5 changed files with 124 additions and 10 deletions

View File

@@ -299,6 +299,15 @@
</intent-filter>
</activity>
<activity android:name=".app.ContentPictureInPicture"
android:label="@string/activity_picture_in_picture"
android:resizeableActivity="true"
android:supportsPictureInPicture="true"
android:theme="@style/Theme.NoActionBar"
android:configChanges=
"screenSize|smallestScreenSize|screenLayout|orientation">
</activity>
<activity android:name=".app.MaxAspectRatio$Square"
android:label="@string/activity_max_aspect_ratio_square"
android:resizeableActivity="false"

View File

@@ -35,13 +35,7 @@
android:orientation="vertical">
<!-- layout params would be changed programmatically -->
<com.example.android.apis.view.FixedAspectRatioImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/sample_1"
app:aspectRatio="16/9" />
<include layout="@layout/picture_in_picture_content" />
<ScrollView
android:id="@+id/control_group"

View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2021 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.
-->
<com.example.android.apis.view.FixedAspectRatioImageView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/sample_1"
app:aspectRatio="16/9" />

View File

@@ -0,0 +1,51 @@
/*
* Copyright (C) 2021 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.
*/
package com.example.android.apis.app;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.ResultReceiver;
import com.example.android.apis.R;
public class ContentPictureInPicture extends Activity {
private ResultReceiver mOnStopReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.picture_in_picture_content);
mOnStopReceiver = getIntent().getParcelableExtra(PictureInPicture.KEY_ON_STOP_RECEIVER);
}
@Override
protected void onStop() {
super.onStop();
if (mOnStopReceiver != null) {
mOnStopReceiver.send(0 /* resultCode */, Bundle.EMPTY);
}
}
@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode,
Configuration newConfig) {
if (!isInPictureInPictureMode) {
finish();
}
}
}

View File

@@ -17,16 +17,21 @@
package com.example.android.apis.app;
import android.app.Activity;
import android.app.ActivityOptions;
import android.app.PictureInPictureParams;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.ResultReceiver;
import android.util.Rational;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Switch;
@@ -39,6 +44,19 @@ public class PictureInPicture extends Activity {
private static final String EXTRA_ENABLE_SEAMLESS_RESIZE = "seamless_resize";
private static final String EXTRA_CURRENT_POSITION = "current_position";
private static final int TABLET_BREAK_POINT_DP = 700;
public static final String KEY_ON_STOP_RECEIVER = "on_stop_receiver";
private final ResultReceiver mOnStopReceiver = new ResultReceiver(
new Handler(Looper.myLooper())) {
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
// Container activity for content-pip has stopped, replace the placeholder
// with actual content in this host activity.
mImageView.setImageResource(R.drawable.sample_1);
}
};
private final View.OnLayoutChangeListener mOnLayoutChangeListener =
(v, oldLeft, oldTop, oldRight, oldBottom, newLeft, newTop, newRight, newBottom) -> {
updatePictureInPictureParams();
@@ -51,7 +69,7 @@ public class PictureInPicture extends Activity {
(v, id) -> updateContentPosition(id);
private LinearLayout mContainer;
private View mImageView;
private ImageView mImageView;
private View mControlGroup;
private Switch mAutoPipToggle;
private Switch mSourceRectHintToggle;
@@ -123,13 +141,30 @@ public class PictureInPicture extends Activity {
}
}
/**
* 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
* - Construct the PictureInPictureParams with source rect hint and aspect ratio from bounds
* - Start the new content PiP container Activity with the ActivityOptions
*/
private void enterContentPip() {
// TBD
final Intent intent = new Intent(this, ContentPictureInPicture.class);
intent.putExtra(KEY_ON_STOP_RECEIVER, mOnStopReceiver);
final Rect bounds = new Rect();
mImageView.getGlobalVisibleRect(bounds);
final PictureInPictureParams params = new PictureInPictureParams.Builder()
.setSourceRectHint(bounds)
.setAspectRatio(new Rational(bounds.width(), bounds.height()))
.build();
final ActivityOptions opts = ActivityOptions.makeLaunchIntoPip(params);
startActivity(intent, opts.toBundle());
// Swap the mImageView to placeholder content.
mImageView.setImageResource(R.drawable.black_box);
}
private void updateLayout(Configuration configuration) {
mImageView.addOnLayoutChangeListener(mOnLayoutChangeListener);
final boolean isTablet = configuration.screenWidthDp >= 800;
final boolean isTablet = configuration.smallestScreenWidthDp >= TABLET_BREAK_POINT_DP;
final boolean isLandscape =
(configuration.orientation == Configuration.ORIENTATION_LANDSCAPE);
final boolean isPictureInPicture = isInPictureInPictureMode();