From 4002b6ed1016b8cf8c9c0a2fb7606de194994ba7 Mon Sep 17 00:00:00 2001 From: Aaron Labiaga Date: Thu, 9 Mar 2023 13:28:53 -0500 Subject: [PATCH] Update sample to use androidx.activity trackPipAnimationHintView which is the helper that sets the sourceRectHint for the developer. --- PictureInPictureKotlin/app/build.gradle | 28 ++++++++++--------- .../android/pictureinpicture/MainActivity.kt | 23 ++++++++++----- PictureInPictureKotlin/build.gradle | 4 +-- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/PictureInPictureKotlin/app/build.gradle b/PictureInPictureKotlin/app/build.gradle index 222662e..51b077c 100644 --- a/PictureInPictureKotlin/app/build.gradle +++ b/PictureInPictureKotlin/app/build.gradle @@ -18,11 +18,11 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' android { - compileSdkVersion 31 + compileSdkVersion 33 defaultConfig { applicationId "com.example.android.pictureinpicture" minSdkVersion 31 - targetSdkVersion 31 + targetSdkVersion 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -48,22 +48,24 @@ android { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation 'androidx.core:core-ktx:1.6.0' - implementation 'androidx.appcompat:appcompat:1.3.1' - implementation 'androidx.activity:activity-ktx:1.3.0' - implementation 'androidx.media:media:1.4.0' + implementation 'androidx.core:core-ktx:1.9.0' + implementation 'androidx.appcompat:appcompat:1.6.1' + implementation 'androidx.activity:activity-ktx:1.6.1' + implementation 'androidx.media:media:1.6.0' - implementation 'androidx.constraintlayout:constraintlayout:2.0.4' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' - def lifecycle_version = '2.3.1' + def lifecycle_version = '2.6.0' implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4' + testImplementation 'junit:junit:4.13.2' androidTestImplementation 'com.google.truth:truth:1.1.3' - androidTestImplementation 'androidx.test:core-ktx:1.4.0' - androidTestImplementation 'androidx.test:runner:1.4.0' - androidTestImplementation 'androidx.test:rules:1.4.0' - androidTestImplementation 'androidx.test.ext:junit:1.1.3' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + androidTestImplementation 'androidx.test:core-ktx:1.5.0' + androidTestImplementation 'androidx.test:runner:1.5.2' + androidTestImplementation 'androidx.test:rules:1.5.0' + androidTestImplementation 'androidx.test.ext:junit:1.1.5' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' } diff --git a/PictureInPictureKotlin/app/src/main/java/com/example/android/pictureinpicture/MainActivity.kt b/PictureInPictureKotlin/app/src/main/java/com/example/android/pictureinpicture/MainActivity.kt index 1b43dab..42b55c9 100644 --- a/PictureInPictureKotlin/app/src/main/java/com/example/android/pictureinpicture/MainActivity.kt +++ b/PictureInPictureKotlin/app/src/main/java/com/example/android/pictureinpicture/MainActivity.kt @@ -24,16 +24,20 @@ 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.util.Rational import android.view.View +import androidx.activity.trackPipAnimationHintView import androidx.activity.viewModels import androidx.annotation.DrawableRes import androidx.annotation.StringRes import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle import com.example.android.pictureinpicture.databinding.MainActivityBinding +import kotlinx.coroutines.launch /** Intent action for stopwatch controls from Picture-in-Picture mode. */ private const val ACTION_STOPWATCH_CONTROL = "stopwatch_control" @@ -93,6 +97,15 @@ class MainActivity : AppCompatActivity() { ) updatePictureInPictureParams(started) } + + // Use trackPipAnimationHint view to make a smooth enter/exit pip transition. + // See https://android.devsite.corp.google.com/develop/ui/views/picture-in-picture#smoother-transition + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.STARTED) { + trackPipAnimationHintView(binding.stopwatchBackground) + } + } + // Handle events from the action icons on the picture-in-picture mode. registerReceiver(broadcastReceiver, IntentFilter(ACTION_STOPWATCH_CONTROL)) } @@ -100,8 +113,9 @@ class MainActivity : AppCompatActivity() { // This is called when the activity gets into or out of the picture-in-picture mode. override fun onPictureInPictureModeChanged( isInPictureInPictureMode: Boolean, - newConfig: Configuration? + newConfig: Configuration ) { + super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig) if (isInPictureInPictureMode) { // Hide in-app buttons. They cannot be interacted in the picture-in-picture mode, and // their features are provided as the action icons. @@ -118,8 +132,6 @@ class MainActivity : AppCompatActivity() { * [started] state of the stopwatch. */ private fun updatePictureInPictureParams(started: Boolean): PictureInPictureParams { - val visibleRect = Rect() - binding.stopwatchBackground.getGlobalVisibleRect(visibleRect) val params = PictureInPictureParams.Builder() // Set action items for the picture-in-picture mode. These are the only custom controls // available during the picture-in-picture mode. @@ -153,9 +165,6 @@ class MainActivity : AppCompatActivity() { ) // Set the aspect ratio of the picture-in-picture mode. .setAspectRatio(Rational(16, 9)) - // Specify the portion of the screen that turns into the picture-in-picture mode. - // This makes the transition animation smoother. - .setSourceRectHint(visibleRect) // Turn the screen into the picture-in-picture mode if it's hidden by the "Home" button. .setAutoEnterEnabled(true) // Disables the seamless resize. The seamless resize works great for videos where the diff --git a/PictureInPictureKotlin/build.gradle b/PictureInPictureKotlin/build.gradle index 6e35d2c..3ce528a 100644 --- a/PictureInPictureKotlin/build.gradle +++ b/PictureInPictureKotlin/build.gradle @@ -19,10 +19,10 @@ buildscript { google() mavenCentral() } - ext.kotlin_version = '1.5.21' + ext.kotlin_version = '1.8.0' dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - classpath 'com.android.tools.build:gradle:7.0.0' + classpath 'com.android.tools.build:gradle:7.0.4' } }