Merge "Add demo for content PiP"
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
25
samples/ApiDemos/res/layout/picture_in_picture_content.xml
Normal file
25
samples/ApiDemos/res/layout/picture_in_picture_content.xml
Normal 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" />
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user