Skip to content

Commit 4ec3084

Browse files
committed
Fix: Refactored the SystemMediaControlResolver
The SystemMediaControlResolver has been refactored to better handle various Android versions and OEM customizations. It also introduces dedicated checks for One UI versions and displays a Toast message when media output settings cannot be opened. Specific changes include: * The logic for launching the system media control dialog has been refactored into a new private function `startSystemMediaControl()`. * Added a new function `getOneUIVersionReadable()` to retrieve the One UI version. * Fall back to `startSystemMediaControl()` when launching OEM-specific media control dialogs fails. * Display a Toast message when media output settings cannot be opened. * Added new strings in `strings.xml`.
1 parent 951938d commit 4ec3084

1 file changed

Lines changed: 18 additions & 18 deletions

File tree

app/src/main/java/org/akanework/gramophone/logic/utils/exoplayer/oem/SystemMediaControlResolver.kt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,22 @@ class SystemMediaControlResolver(val context: Context) {
6666
if (!tag) {
6767
Toast.makeText(context, R.string.media_control_text_error, Toast.LENGTH_SHORT).show()
6868
}
69-
} else {
70-
// zh: Android 11 及以下
69+
} else if (Build.VERSION.SDK_INT == 30) {
70+
// Android 11
7171
val tag = startNativeMediaDialogForAndroid11(context)
7272
if (!tag) {
7373
Toast.makeText(context, R.string.media_control_text_error, Toast.LENGTH_SHORT).show()
7474
}
75+
} else {
76+
val intent = Intent().apply {
77+
flags = Intent.FLAG_ACTIVITY_NEW_TASK
78+
action = "com.android.settings.panel.action.MEDIA_OUTPUT"
79+
putExtra("com.android.settings.panel.extra.PACKAGE_NAME", context.packageName)
80+
}
81+
val tag = startNativeMediaDialog(intent)
82+
if (!tag) {
83+
Toast.makeText(context, R.string.media_control_text_error, Toast.LENGTH_SHORT).show()
84+
}
7585
}
7686
}
7787

@@ -127,9 +137,12 @@ class SystemMediaControlResolver(val context: Context) {
127137
* zh: 获取 One UI 版本字符串(如 6.0.0),非三星或无此属性则返回 null
128138
* en: Get One UI version string (e.g. 6.0.0), return null if not Samsung or no such property
129139
*/
130-
private fun getOneUIVersionReadable(): String? {
131-
return try {1
132-
val value = getSystemProperties("ro.build.version.oneui")
140+
@SuppressLint("PrivateApi")
141+
fun getOneUIVersionReadable(): String? {
142+
return try {
143+
val systemProperties = Class.forName("android.os.SystemProperties")
144+
val get = systemProperties.getMethod("get", String::class.java)
145+
val value = (get.invoke(null, "ro.build.version.oneui") as String).trim()
133146
if (value.isEmpty()) return null
134147
val code = value.toIntOrNull() ?: return null
135148
val major = code / 10000
@@ -140,17 +153,4 @@ class SystemMediaControlResolver(val context: Context) {
140153
null
141154
}
142155
}
143-
144-
@SuppressLint("PrivateApi")
145-
private fun getSystemProperties(key: String): String {
146-
val ret: String = try {
147-
Class.forName("android.os.SystemProperties").getDeclaredMethod("get", String::class.java).invoke(null, key) as String
148-
} catch (iAE: IllegalArgumentException) {
149-
throw iAE
150-
} catch (e: Exception) {
151-
""
152-
}
153-
return ret
154-
}
155-
156156
}

0 commit comments

Comments
 (0)