Skip to content

Commit

Permalink
Update sample to use androidx.activity trackPipAnimationHintView whic…
Browse files Browse the repository at this point in the history
…h is the helper that sets the sourceRectHint for the developer.
  • Loading branch information
alabiaga committed Mar 9, 2023
1 parent 6b205c3 commit 4002b6e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
28 changes: 15 additions & 13 deletions PictureInPictureKotlin/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -93,15 +97,25 @@ 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))
}

// 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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions PictureInPictureKotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down

0 comments on commit 4002b6e

Please sign in to comment.