[Media TTT] Put package error logs in the log buffer with all the other

logs.

Bug: 246574567
Test: verified logs in logcat if invalid package name
Test: media.taptotransfer tests
Change-Id: I5b86508ecbfe0dd2e13f094bab236255afe850cc
This commit is contained in:
Caitlin Shkuratov
2022-09-13 18:38:01 +00:00
parent 28d585305e
commit ab8f951345
6 changed files with 41 additions and 12 deletions

View File

@@ -28,10 +28,12 @@ class MediaTttLogger(
private val deviceTypeTag: String,
private val buffer: LogBuffer
){
private val bufferTag = BASE_TAG + deviceTypeTag
/** Logs a change in the chip state for the given [mediaRouteId]. */
fun logStateChange(stateName: String, mediaRouteId: String, packageName: String?) {
buffer.log(
BASE_TAG + deviceTypeTag,
bufferTag,
LogLevel.DEBUG,
{
str1 = stateName
@@ -45,12 +47,22 @@ class MediaTttLogger(
/** Logs that we removed the chip for the given [reason]. */
fun logChipRemoval(reason: String) {
buffer.log(
BASE_TAG + deviceTypeTag,
bufferTag,
LogLevel.DEBUG,
{ str1 = reason },
{ "Chip removed due to $str1" }
)
}
/** Logs that we couldn't find information for [packageName]. */
fun logPackageNotFound(packageName: String) {
buffer.log(
bufferTag,
LogLevel.DEBUG,
{ str1 = packageName },
{ "Package $str1 could not be found" }
)
}
}
private const val BASE_TAG = "MediaTtt"

View File

@@ -19,7 +19,6 @@ package com.android.systemui.media.taptotransfer.common
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.drawable.Drawable
import android.util.Log
import com.android.internal.widget.CachingIconView
import com.android.settingslib.Utils
import com.android.systemui.R
@@ -34,8 +33,13 @@ class MediaTttUtils {
* default name and icon if we can't find the app name/icon.
*
* @param appPackageName the package name of the app playing the media.
* @param logger the logger to use for any errors.
*/
fun getIconInfoFromPackageName(context: Context, appPackageName: String?): IconInfo {
fun getIconInfoFromPackageName(
context: Context,
appPackageName: String?,
logger: MediaTttLogger
): IconInfo {
if (appPackageName != null) {
try {
val contentDescription =
@@ -52,7 +56,7 @@ class MediaTttUtils {
isAppIcon = true
)
} catch (e: PackageManager.NameNotFoundException) {
Log.w(TAG, "Cannot find package $appPackageName", e)
logger.logPackageNotFound(appPackageName)
}
}
return IconInfo(
@@ -101,5 +105,3 @@ data class IconInfo(
*/
val isAppIcon: Boolean
)
private const val TAG = "MediaTtt"

View File

@@ -140,7 +140,7 @@ class MediaTttChipControllerReceiver @Inject constructor(
super.updateView(newInfo, currentView)
val iconInfo = MediaTttUtils.getIconInfoFromPackageName(
context, newInfo.routeInfo.clientPackageName
context, newInfo.routeInfo.clientPackageName, logger
)
val iconDrawable = newInfo.appIconDrawableOverride ?: iconInfo.drawable
val iconContentDescription = newInfo.appNameOverride ?: iconInfo.contentDescription

View File

@@ -120,7 +120,7 @@ class MediaTttChipControllerSender @Inject constructor(
// App icon
val iconInfo = MediaTttUtils.getIconInfoFromPackageName(
context, newInfo.routeInfo.clientPackageName
context, newInfo.routeInfo.clientPackageName, logger
)
MediaTttUtils.setIcon(
currentView.requireViewById(R.id.app_icon),

View File

@@ -72,6 +72,19 @@ class MediaTttLoggerTest : SysuiTestCase() {
assertThat(actualString).contains(DEVICE_TYPE_TAG)
assertThat(actualString).contains(reason)
}
@Test
fun logPackageNotFound_bufferHasPackageName() {
val packageName = "this.is.a.package"
logger.logPackageNotFound(packageName)
val stringWriter = StringWriter()
buffer.dump(PrintWriter(stringWriter), tailLength = 0)
val actualString = stringWriter.toString()
assertThat(actualString).contains(packageName)
}
}
private const val DEVICE_TYPE_TAG = "TEST TYPE"

View File

@@ -39,6 +39,7 @@ class MediaTttUtilsTest : SysuiTestCase() {
private lateinit var appIconFromPackageName: Drawable
@Mock private lateinit var packageManager: PackageManager
@Mock private lateinit var applicationInfo: ApplicationInfo
@Mock private lateinit var logger: MediaTttLogger
@Before
fun setUp() {
@@ -64,7 +65,8 @@ class MediaTttUtilsTest : SysuiTestCase() {
@Test
fun getIconInfoFromPackageName_nullPackageName_returnsDefault() {
val iconInfo = MediaTttUtils.getIconInfoFromPackageName(context, appPackageName = null)
val iconInfo =
MediaTttUtils.getIconInfoFromPackageName(context, appPackageName = null, logger)
assertThat(iconInfo.isAppIcon).isFalse()
assertThat(iconInfo.contentDescription)
@@ -73,7 +75,7 @@ class MediaTttUtilsTest : SysuiTestCase() {
@Test
fun getIconInfoFromPackageName_invalidPackageName_returnsDefault() {
val iconInfo = MediaTttUtils.getIconInfoFromPackageName(context, "fakePackageName")
val iconInfo = MediaTttUtils.getIconInfoFromPackageName(context, "fakePackageName", logger)
assertThat(iconInfo.isAppIcon).isFalse()
assertThat(iconInfo.contentDescription)
@@ -82,7 +84,7 @@ class MediaTttUtilsTest : SysuiTestCase() {
@Test
fun getIconInfoFromPackageName_validPackageName_returnsAppInfo() {
val iconInfo = MediaTttUtils.getIconInfoFromPackageName(context, PACKAGE_NAME)
val iconInfo = MediaTttUtils.getIconInfoFromPackageName(context, PACKAGE_NAME, logger)
assertThat(iconInfo.isAppIcon).isTrue()
assertThat(iconInfo.drawable).isEqualTo(appIconFromPackageName)