Skip to content

Commit 8712e61

Browse files
committed
feat: Optimized media output panel availability checks
Adjusted media output panel availability check logic based on Android version. - For Android 14 and above, assume the panel is always available. - For Android 12 and 13, check if the system UI can handle the media output dialog intent. - For Android 11 and below, check if the Settings app can handle the media output panel intent. If the panel is unavailable, hide the media control buttons. Signed-off-by: ghhccghk <[email protected]>
1 parent 8faae40 commit 8712e61

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

app/src/main/java/org/akanework/gramophone/ui/components/FullBottomSheet.kt

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import android.animation.ValueAnimator
44
import android.annotation.SuppressLint
55
import android.content.ContentUris
66
import android.content.Context
7+
import android.content.Intent
78
import android.content.SharedPreferences
9+
import android.content.pm.ApplicationInfo
10+
import android.content.pm.PackageManager
811
import android.content.res.ColorStateList
912
import android.content.res.Configuration
1013
import android.graphics.Color
@@ -418,10 +421,16 @@ class FullBottomSheet
418421

419422
bottomSheetFavoriteButton.addOnCheckedChangeListener(this)
420423

421-
bottomSheetMediaControl.setOnClickListener {
422-
SystemMediaControlResolver(context).intentSystemMediaDialog()
424+
if (isMediaOutputPanelSupported(context)){
425+
bottomSheetMediaControl.setOnClickListener {
426+
SystemMediaControlResolver(context).intentSystemMediaDialog()
427+
}
428+
} else {
429+
bottomSheetMediaControl.visibility = GONE
423430
}
424431

432+
433+
425434
bottomSheetPlaylistButton.setOnClickListener {
426435
ViewCompat.performHapticFeedback(it, HapticFeedbackConstantsCompat.CONTEXT_CLICK)
427436
val playlistBottomSheet = BottomSheetDialog(context)
@@ -1400,4 +1409,55 @@ class FullBottomSheet
14001409
}
14011410
}
14021411

1412+
fun isMediaOutputPanelSupported(context: Context): Boolean {
1413+
return when {
1414+
Build.VERSION.SDK_INT >= 34 -> {
1415+
// Android 14+ 系统一定支持
1416+
true
1417+
}
1418+
Build.VERSION.SDK_INT >= 31 -> {
1419+
// Android 12~13
1420+
val intent = Intent().apply {
1421+
action = "com.android.systemui.action.LAUNCH_MEDIA_OUTPUT_DIALOG"
1422+
setPackage("com.android.systemui")
1423+
putExtra("package_name", context.packageName)
1424+
}
1425+
isSystemIntentAvailable(context, intent)
1426+
}
1427+
Build.VERSION.SDK_INT == 30 -> {
1428+
// Android 11
1429+
val intent = Intent().apply {
1430+
flags = Intent.FLAG_ACTIVITY_NEW_TASK
1431+
action = "com.android.settings.panel.action.MEDIA_OUTPUT"
1432+
putExtra("com.android.settings.panel.extra.PACKAGE_NAME", context.packageName)
1433+
}
1434+
isSystemIntentAvailable(context, intent)
1435+
}
1436+
else -> {
1437+
// Android 10 及以下
1438+
val intent = Intent().apply {
1439+
flags = Intent.FLAG_ACTIVITY_NEW_TASK
1440+
action = "com.android.settings.panel.action.MEDIA_OUTPUT"
1441+
putExtra("com.android.settings.panel.extra.PACKAGE_NAME", context.packageName)
1442+
}
1443+
isSystemIntentAvailable(context, intent)
1444+
}
1445+
}
1446+
}
1447+
1448+
private fun isSystemIntentAvailable(context: Context, intent: Intent): Boolean {
1449+
val resolveInfoList = context.packageManager.queryIntentActivities(intent, 0)
1450+
for (resolveInfo in resolveInfoList) {
1451+
val activityInfo = resolveInfo.activityInfo
1452+
val applicationInfo: ApplicationInfo? = activityInfo?.applicationInfo
1453+
if (applicationInfo != null && (applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM) != 0) {
1454+
return true
1455+
}
1456+
}
1457+
return false
1458+
}
1459+
1460+
1461+
1462+
14031463
}

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,9 @@
467467
<string name="compact_grid">Grid (compact)</string>
468468
<string name="sort_by_album_year">Album year</string>
469469
<string name="sort_by_album_artist_year"><![CDATA[Album artist -> year]]></string>
470+
<string name="media_control_text">System Media Controller</string>
471+
<string name="media_control_text_error">Unable to open media output settings</string>
472+
470473
<string name="forbidden_symbol_error">Invalid character in the name</string>
471474
<string name="translators">Translators</string>
472475
<string name="settings_replaygain">ReplayGain</string>

0 commit comments

Comments
 (0)