Skip to content

Commit

Permalink
Publish the media3 session controller test app
Browse files Browse the repository at this point in the history
Issue: #78
PiperOrigin-RevId: 642277900
  • Loading branch information
icbaker authored and Copybara-Service committed Jun 11, 2024
1 parent b763673 commit f54991e
Show file tree
Hide file tree
Showing 80 changed files with 5,019 additions and 0 deletions.
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* Add `SessionError` and use it in `SessionResult` and `LibraryResult`
instead of the error code to provide more information about the error
and how to resolve the error if possible.
* Publish the code for the media3 controller test app that can be used to
test interactions with apps publishing a media session.
* UI:
* Add customisation of various icons in `PlayerControlView` through xml
attributes to allow different drawables per `PlayerView` instance,
Expand Down
4 changes: 4 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ project(modulePrefix + 'test-session-common').projectDir = new File(rootDir, 'li
include modulePrefix + 'test-session-current'
project(modulePrefix + 'test-session-current').projectDir = new File(rootDir, 'libraries/test_session_current')

// MediaController test app.
include modulePrefix + 'testapp-controller'
project(modulePrefix + 'testapp-controller').projectDir = new File(rootDir, 'testapps/controller')

apply from: 'core_settings.gradle'
5 changes: 5 additions & 0 deletions testapps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Android media test apps

This directory contains applications that can be used to test an application's
integration with Android media APIs. Browse the individual test applications
and their READMEs to learn more.
4 changes: 4 additions & 0 deletions testapps/controller/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Media3 controller test app

This is the media3 controller test application. It allows media apps to verify
their media session implementation.
70 changes: 70 additions & 0 deletions testapps/controller/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 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.
apply from: '../../constants.gradle'
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
namespace 'androidx.media3.testapp.controller'

compileSdk project.ext.compileSdkVersion

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = '1.8'
}

defaultConfig {
versionName project.ext.releaseVersion
versionCode project.ext.releaseVersionCode
minSdkVersion project.ext.minSdkVersion
targetSdkVersion project.ext.appTargetSdkVersion
multiDexEnabled true
vectorDrawables.useSupportLibrary = true
}

buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles = [
'proguard-rules.txt',
getDefaultProguardFile('proguard-android-optimize.txt')
]
signingConfig signingConfigs.debug
}
debug {
jniDebuggable = true
}
}

lintOptions {
// The test app isn't indexed, and doesn't have translations.
disable 'GoogleAppIndexingWarning','MissingTranslation'
}
}

dependencies {
implementation 'androidx.core:core-ktx:' + androidxCoreVersion
implementation 'androidx.appcompat:appcompat:' + androidxAppCompatVersion
implementation 'androidx.multidex:multidex:' + androidxMultidexVersion
implementation 'com.google.android.material:material:' + androidxMaterialVersion
implementation 'androidx.media:media:' + androidxMediaVersion
implementation project(modulePrefix + 'lib-session')
implementation project(modulePrefix + 'lib-datasource')
}
20 changes: 20 additions & 0 deletions testapps/controller/lint.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2023 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.
-->
<lint>
<issue id="UnsafeOptInUsageError">
<option name="opt-in" value="androidx.media3.common.util.UnstableApi" />
</issue>
</lint>
2 changes: 2 additions & 0 deletions testapps/controller/proguard-rules.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Proguard rules specific to the media3 controller test app.

76 changes: 76 additions & 0 deletions testapps/controller/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="androidx.media3.testapp.controller">

<uses-sdk/>

<uses-permission
android:name="android.permission.INTERNET"/>
<uses-permission
tools:ignore="QueryAllPackagesPermission"
android:name="android.permission.QUERY_ALL_PACKAGES"/>

<queries>
<intent>
<action android:name="android.media.browse.MediaBrowserService" />
</intent>
<intent>
<action android:name="androidx.media3.session.MediaSessionService" />
</intent>
<intent>
<action android:name="androidx.media3.session.MediaLibraryService" />
</intent>
</queries>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".LaunchActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".MediaAppControllerActivity"
android:exported="true">

<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>

<service
android:name=".LaunchActivity$NotificationListener"
android:label="@string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 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 androidx.media3.testapp.controller

import android.app.Activity
import android.media.AudioManager
import android.view.View
import android.widget.AdapterView
import android.widget.Spinner
import android.widget.ToggleButton
import androidx.appcompat.app.AppCompatActivity
import androidx.media.AudioFocusRequestCompat
import androidx.media.AudioManagerCompat

/** Helper class to manage audio focus requests and the UI surrounding this feature. */
class AudioFocusHelper(activity: Activity) :
View.OnClickListener,
AudioManager.OnAudioFocusChangeListener,
AdapterView.OnItemSelectedListener {
private val audioManager: AudioManager =
activity.getSystemService(AppCompatActivity.AUDIO_SERVICE) as AudioManager
private val toggleButton: ToggleButton = activity.findViewById(R.id.audio_focus_button)
private val focusTypeSpinner: Spinner = activity.findViewById(R.id.audio_focus_type)

private val selectedFocusType: Int
get() = FOCUS_TYPES[focusTypeSpinner.selectedItemPosition]

companion object {
private val FOCUS_TYPES =
intArrayOf(
AudioManager.AUDIOFOCUS_GAIN,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
)
}

init {
toggleButton.setOnClickListener(this)
this.focusTypeSpinner.onItemSelectedListener = this
}

override fun onClick(v: View) =
if (toggleButton.isChecked) {
gainAudioFocus()
} else {
abandonAudioFocus()
}

override fun onAudioFocusChange(focusChange: Int) =
when (focusChange) {
AudioManager.AUDIOFOCUS_GAIN,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK -> toggleButton.isChecked = true
else -> toggleButton.isChecked = false
}

override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
// If we're holding audio focus and the type should change, automatically
// request the new type of focus.
if (toggleButton.isChecked) {
gainAudioFocus()
}
}

override fun onNothingSelected(parent: AdapterView<*>?) {
// Nothing to do.
}

private fun gainAudioFocus() {
val audioFocusRequest: AudioFocusRequestCompat =
AudioFocusRequestCompat.Builder(selectedFocusType).setOnAudioFocusChangeListener(this).build()
AudioManagerCompat.requestAudioFocus(audioManager, audioFocusRequest)
}

private fun abandonAudioFocus() {
val audioFocusRequest: AudioFocusRequestCompat =
AudioFocusRequestCompat.Builder(selectedFocusType).setOnAudioFocusChangeListener(this).build()
AudioManagerCompat.abandonAudioFocusRequest(audioManager, audioFocusRequest)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 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 androidx.media3.testapp.controller

import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable

/** Utilities for [Bitmap]s. */
object BitmapUtils {
/**
* Converts a [Drawable] to an appropriately sized [Bitmap].
*
* @param resources Resources for the current [android.content.Context].
* @param drawable The [Drawable] to convert to a Bitmap.
* @param downScale Will downscale the Bitmap to `R.dimen.app_icon_size` dp.
* @return A Bitmap, no larger than `R.dimen.app_icon_size` dp if desired.
*/
fun convertDrawable(resources: Resources, drawable: Drawable, downScale: Boolean): Bitmap {
val bitmap: Bitmap
if (drawable is BitmapDrawable) {
bitmap = drawable.bitmap
} else {
bitmap =
Bitmap.createBitmap(
drawable.intrinsicWidth,
drawable.intrinsicHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
}
if (!downScale) {
return bitmap
}
val iconSize: Int = resources.getDimensionPixelSize(R.dimen.app_icon_size)
return if (bitmap.height > iconSize || bitmap.width > iconSize) {
// Which needs to be scaled to fit.
val height: Int = bitmap.height
val width: Int = bitmap.width
val scaleHeight: Int
val scaleWidth: Int

// Calculate the new size based on which dimension is larger.
if (height > width) {
scaleHeight = iconSize
scaleWidth = (width * iconSize.toFloat() / height).toInt()
} else {
scaleWidth = iconSize
scaleHeight = (height * iconSize.toFloat() / width).toInt()
}
Bitmap.createScaledBitmap(bitmap, scaleWidth, scaleHeight, false)
} else {
bitmap
}
}

/**
* Creates a Material Design compliant [androidx.appcompat.widget.Toolbar] icon from a given full
* sized icon.
*
* @param resources Resources for the current [android.content.Context].
* @param icon The bitmap to convert.
* @return A scaled Bitmap of the appropriate size and in-built padding.
*/
fun createToolbarIcon(resources: Resources, icon: Bitmap): Bitmap {
val padding: Int = resources.getDimensionPixelSize(R.dimen.margin_small)
val iconSize: Int = resources.getDimensionPixelSize(R.dimen.toolbar_icon_size)
val sizeWithPadding = iconSize + 2 * padding

// Create a Bitmap backed Canvas to be the toolbar icon.
val toolbarIcon: Bitmap =
Bitmap.createBitmap(sizeWithPadding, sizeWithPadding, Bitmap.Config.ARGB_8888)
val canvas = Canvas(toolbarIcon)
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)

// Resize the app icon to Material Design size.
val scaledIcon: Bitmap = Bitmap.createScaledBitmap(icon, iconSize, iconSize, false)
canvas.drawBitmap(scaledIcon, padding.toFloat(), padding.toFloat(), null)
return toolbarIcon
}
}
Loading

0 comments on commit f54991e

Please sign in to comment.