Skip to content

Commit ca412cd

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 <2137610394@qq.com>
1 parent ecc56ef commit ca412cd

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-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
@@ -419,10 +422,16 @@ class FullBottomSheet
419422

420423
bottomSheetFavoriteButton.addOnCheckedChangeListener(this)
421424

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

433+
434+
426435
bottomSheetPlaylistButton.setOnClickListener {
427436
ViewCompat.performHapticFeedback(it, HapticFeedbackConstantsCompat.CONTEXT_CLICK)
428437
val playlistBottomSheet = BottomSheetDialog(context)
@@ -1395,4 +1404,55 @@ class FullBottomSheet
13951404
}
13961405
}
13971406

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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,5 +471,7 @@
471471
<string name="compact_grid">Grid (compact)</string>
472472
<string name="sort_by_album_year">Album year</string>
473473
<string name="sort_by_album_artist_year"><![CDATA[Album artist -> year]]></string>
474+
<string name="media_control_text">System Media Controller</string>
475+
<string name="media_control_text_error">Unable to open media output settings</string>
474476

475477
</resources>

0 commit comments

Comments
 (0)