Skip to content

Commit e54a038

Browse files
committed
Optimize code by removing redundant logic
Signed-off-by: ghhccghk <2137610394@qq.com>
1 parent d598c17 commit e54a038

File tree

4 files changed

+101
-138
lines changed

4 files changed

+101
-138
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ import android.content.Intent
2727
import android.content.pm.PackageManager
2828

2929
/**
30-
* 小米妙播适配
30+
* MiPlay Audio Support
3131
*/
32-
@UnstableMediaKitApi
3332
object MiPlayAudioSupport {
3433
private const val ACTION_MIPLAY_DETAIL = "miui.intent.action.ACTIVITY_MIPLAY_DETAIL"
3534
private const val AUDIO_RECORD_CLASS = "miui.media.MiuiAudioPlaybackRecorder"
@@ -38,7 +37,7 @@ object MiPlayAudioSupport {
3837
private const val WHITE_TARGET = "com.milink.service:hide_foreground"
3938

4039
/**
41-
* 查询是否支持妙播服务
40+
* Check if MiaoBo service is supported
4241
*
4342
* https://dev.mi.com/xiaomihyperos/documentation/detail?pId=1944
4443
*/
@@ -62,7 +61,7 @@ object MiPlayAudioSupport {
6261
}
6362

6463
/**
65-
* 是否为国际版
64+
* Is it the international version
6665
*/
6766
private fun isInternationalBuild(): Boolean =
6867
try {
@@ -75,7 +74,7 @@ object MiPlayAudioSupport {
7574
}
7675

7776
/**
78-
* 检查 SystemUI 是否包含处理妙播意图的 Activity
77+
* Check whether SystemUI contains an Activity that handles the Miaobo intent.
7978
*/
8079
private fun systemUIReady(context: Context): Boolean {
8180
val intent =
@@ -92,7 +91,7 @@ object MiPlayAudioSupport {
9291
}
9392

9493
/**
95-
* 检查妙播服务是否在 SystemUI 的前台服务通知白名单中
94+
* Check whether the Miaobo service is included in the SystemUI foreground service notification whitelist.
9695
*/
9796
private fun notificationReady(context: Context): Boolean =
9897
try {

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

Lines changed: 94 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ import androidx.annotation.RequiresApi
1212
import org.akanework.gramophone.R
1313
import org.akanework.gramophone.logic.utils.exoplayer.oem.MiPlayAudioSupport.supportMiPlay
1414

15-
class SystemMediaControlResolver(val context: Context) {
16-
@OptIn(UnstableMediaKitApi::class)
17-
fun intentSystemMediaDialog() {
15+
object SystemMediaControlResolver {
16+
fun intentSystemMediaDialog(context: Context) {
1817
// val manufacturer = Build.MANUFACTURER.lowercase()
1918
when {
2019
supportMiPlay(context) -> {
@@ -25,68 +24,60 @@ class SystemMediaControlResolver(val context: Context) {
2524
"miui.systemui.miplay.MiPlayDetailActivity"
2625
)
2726
}
28-
if (!startIntent(intent)) {
29-
startSystemMediaControl()
27+
if (!startIntent(intent,context = context,)) {
28+
startSystemMediaControl(context = context)
29+
}
30+
}
31+
(getOneUIVersionReadable() != null) -> {
32+
val intent = Intent().apply {
33+
flags = Intent.FLAG_ACTIVITY_NEW_TASK
34+
setClassName(
35+
"com.samsung.android.mdx.quickboard",
36+
"com.samsung.android.mdx.quickboard.view.MediaActivity"
37+
)
38+
}
39+
if (!startIntent(intent,context = context)) {
40+
startSystemMediaControl(context = context,)
3041
}
3142
}
32-
//zh:临时禁用OneUI MediaActivity调用,等待未来确定不会被删除再添加回去
33-
// (getOneUIVersionReadable() != null) -> {
34-
// val intent = Intent().apply {
35-
// flags = Intent.FLAG_ACTIVITY_NEW_TASK
36-
// setClassName(
37-
// "com.samsung.android.mdx.quickboard",
38-
// "com.samsung.android.mdx.quickboard.view.MediaActivity"
39-
// )
40-
// }
41-
// if (!startIntent(intent)) {
42-
// startSystemMediaControl()
43-
// }
44-
// }
4543

4644
else -> {
47-
startSystemMediaControl()
45+
startSystemMediaControl(context = context,)
4846
}
4947
}
5048
}
5149

52-
private fun startSystemMediaControl(){
50+
private fun startSystemMediaControl(context: Context){
5351
if (Build.VERSION.SDK_INT >= 34) {
5452
// zh: Android 14 及以上
53+
// en:Android 14 and above
5554
val tag = startNativeMediaDialogForAndroid14(context)
5655
if (!tag) {
5756
Toast.makeText(context, R.string.media_control_text_error, Toast.LENGTH_SHORT).show()
5857
}
5958
} else if (Build.VERSION.SDK_INT >= 31) {
6059
// zh: Android 12 及以上
60+
// en: Android 14 and above
6161
val intent = Intent().apply {
6262
action = "com.android.systemui.action.LAUNCH_MEDIA_OUTPUT_DIALOG"
6363
setPackage("com.android.systemui")
6464
putExtra("package_name", context.packageName)
6565
}
66-
val tag = startNativeMediaDialog(intent)
66+
val tag = startNativeMediaDialog(context = context,intent)
6767
if (!tag) {
6868
Toast.makeText(context, R.string.media_control_text_error, Toast.LENGTH_SHORT).show()
6969
}
70-
} else if (Build.VERSION.SDK_INT == 30) {
71-
// Android 11
70+
} else{
71+
// zh: Android 11 及以下
72+
// en: Android 11 and below
7273
val tag = startNativeMediaDialogForAndroid11(context)
7374
if (!tag) {
7475
Toast.makeText(context, R.string.media_control_text_error, Toast.LENGTH_SHORT).show()
7576
}
76-
} else {
77-
val intent = Intent().apply {
78-
flags = Intent.FLAG_ACTIVITY_NEW_TASK
79-
action = "com.android.settings.panel.action.MEDIA_OUTPUT"
80-
putExtra("com.android.settings.panel.extra.PACKAGE_NAME", context.packageName)
81-
}
82-
val tag = startNativeMediaDialog(intent)
83-
if (!tag) {
84-
Toast.makeText(context, R.string.media_control_text_error, Toast.LENGTH_SHORT).show()
85-
}
8677
}
8778
}
8879

89-
private fun startNativeMediaDialog(intent: Intent): Boolean {
80+
private fun startNativeMediaDialog(context: Context,intent: Intent): Boolean {
9081
val resolveInfoList: List<ResolveInfo> =
9182
context.packageManager.queryIntentActivities(intent, 0)
9283
for (resolveInfo in resolveInfoList) {
@@ -125,7 +116,7 @@ class SystemMediaControlResolver(val context: Context) {
125116
return mediaRouter2.showSystemOutputSwitcher()
126117
}
127118

128-
private fun startIntent(intent: Intent): Boolean {
119+
private fun startIntent(intent: Intent,context: Context): Boolean {
129120
return try {
130121
context.startActivity(intent)
131122
true
@@ -134,24 +125,72 @@ class SystemMediaControlResolver(val context: Context) {
134125
}
135126
}
136127

137-
// /**
138-
// * zh: 获取 One UI 版本字符串(如 6.0.0),非三星或无此属性则返回 null
139-
// * en: Get One UI version string (e.g. 6.0.0), return null if not Samsung or no such property
140-
// */
141-
// @SuppressLint("PrivateApi")
142-
// private fun getOneUIVersionReadable(): String? {
143-
// return try {
144-
// val systemProperties = Class.forName("android.os.SystemProperties")
145-
// val get = systemProperties.getMethod("get", String::class.java)
146-
// val value = (get.invoke(null, "ro.build.version.oneui") as String).trim()
147-
// if (value.isEmpty()) return null
148-
// val code = value.toIntOrNull() ?: return null
149-
// val major = code / 10000
150-
// val minor = (code / 100) % 100
151-
// val patch = code % 100
152-
// "$major.$minor.$patch"
153-
// } catch (e: Exception) {
154-
// null
155-
// }
156-
// }
128+
/**
129+
* zh: 获取 One UI 版本字符串(如 6.0.0),非三星或无此属性则返回 null
130+
* en: Get One UI version string (e.g. 6.0.0), return null if not Samsung or no such property
131+
*/
132+
@SuppressLint("PrivateApi")
133+
private fun getOneUIVersionReadable(): String? {
134+
return try {
135+
val systemProperties = Class.forName("android.os.SystemProperties")
136+
val get = systemProperties.getMethod("get", String::class.java)
137+
val value = (get.invoke(null, "ro.build.version.oneui") as String).trim()
138+
if (value.isEmpty()) return null
139+
val code = value.toIntOrNull() ?: return null
140+
val major = code / 10000
141+
val minor = (code / 100) % 100
142+
val patch = code % 100
143+
"$major.$minor.$patch"
144+
} catch (e: Exception) {
145+
null
146+
}
147+
}
148+
149+
fun isMediaOutputPanelSupported(context: Context): Boolean {
150+
return when {
151+
Build.VERSION.SDK_INT >= 34 -> {
152+
// Android 14+ is support
153+
true
154+
}
155+
Build.VERSION.SDK_INT >= 31 -> {
156+
// Android 12~13
157+
val intent = Intent().apply {
158+
action = "com.android.systemui.action.LAUNCH_MEDIA_OUTPUT_DIALOG"
159+
setPackage("com.android.systemui")
160+
putExtra("package_name", context.packageName)
161+
}
162+
isSystemIntentAvailable(context, intent)
163+
}
164+
Build.VERSION.SDK_INT == 30 -> {
165+
// Android 11
166+
val intent = Intent().apply {
167+
flags = Intent.FLAG_ACTIVITY_NEW_TASK
168+
action = "com.android.settings.panel.action.MEDIA_OUTPUT"
169+
putExtra("com.android.settings.panel.extra.PACKAGE_NAME", context.packageName)
170+
}
171+
isSystemIntentAvailable(context, intent)
172+
}
173+
else -> {
174+
// Android 10 and below
175+
val intent = Intent().apply {
176+
flags = Intent.FLAG_ACTIVITY_NEW_TASK
177+
action = "com.android.settings.panel.action.MEDIA_OUTPUT"
178+
putExtra("com.android.settings.panel.extra.PACKAGE_NAME", context.packageName)
179+
}
180+
isSystemIntentAvailable(context, intent)
181+
}
182+
}
183+
}
184+
185+
private fun isSystemIntentAvailable(context: Context, intent: Intent): Boolean {
186+
val resolveInfoList = context.packageManager.queryIntentActivities(intent, 0)
187+
for (resolveInfo in resolveInfoList) {
188+
val activityInfo = resolveInfo.activityInfo
189+
val applicationInfo: ApplicationInfo? = activityInfo?.applicationInfo
190+
if (applicationInfo != null && (applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM) != 0) {
191+
return true
192+
}
193+
}
194+
return false
195+
}
157196
}

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

Lines changed: 0 additions & 26 deletions
This file was deleted.

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

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,9 @@ class FullBottomSheet
416416

417417
bottomSheetFavoriteButton.addOnCheckedChangeListener(this)
418418

419-
if (isMediaOutputPanelSupported(context)){
419+
if (SystemMediaControlResolver.isMediaOutputPanelSupported(context)){
420420
bottomSheetMediaControl.setOnClickListener {
421-
SystemMediaControlResolver(context).intentSystemMediaDialog()
421+
SystemMediaControlResolver.intentSystemMediaDialog(context)
422422
}
423423
} else {
424424
bottomSheetMediaControl.visibility = GONE
@@ -1315,55 +1315,6 @@ class FullBottomSheet
13151315
}
13161316
}
13171317

1318-
fun isMediaOutputPanelSupported(context: Context): Boolean {
1319-
return when {
1320-
Build.VERSION.SDK_INT >= 34 -> {
1321-
// Android 14+ 系统一定支持
1322-
true
1323-
}
1324-
Build.VERSION.SDK_INT >= 31 -> {
1325-
// Android 12~13
1326-
val intent = Intent().apply {
1327-
action = "com.android.systemui.action.LAUNCH_MEDIA_OUTPUT_DIALOG"
1328-
setPackage("com.android.systemui")
1329-
putExtra("package_name", context.packageName)
1330-
}
1331-
isSystemIntentAvailable(context, intent)
1332-
}
1333-
Build.VERSION.SDK_INT == 30 -> {
1334-
// Android 11
1335-
val intent = Intent().apply {
1336-
flags = Intent.FLAG_ACTIVITY_NEW_TASK
1337-
action = "com.android.settings.panel.action.MEDIA_OUTPUT"
1338-
putExtra("com.android.settings.panel.extra.PACKAGE_NAME", context.packageName)
1339-
}
1340-
isSystemIntentAvailable(context, intent)
1341-
}
1342-
else -> {
1343-
// Android 10 及以下
1344-
val intent = Intent().apply {
1345-
flags = Intent.FLAG_ACTIVITY_NEW_TASK
1346-
action = "com.android.settings.panel.action.MEDIA_OUTPUT"
1347-
putExtra("com.android.settings.panel.extra.PACKAGE_NAME", context.packageName)
1348-
}
1349-
isSystemIntentAvailable(context, intent)
1350-
}
1351-
}
1352-
}
1353-
1354-
private fun isSystemIntentAvailable(context: Context, intent: Intent): Boolean {
1355-
val resolveInfoList = context.packageManager.queryIntentActivities(intent, 0)
1356-
for (resolveInfo in resolveInfoList) {
1357-
val activityInfo = resolveInfo.activityInfo
1358-
val applicationInfo: ApplicationInfo? = activityInfo?.applicationInfo
1359-
if (applicationInfo != null && (applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM) != 0) {
1360-
return true
1361-
}
1362-
}
1363-
return false
1364-
}
1365-
1366-
13671318

13681319

13691320
}

0 commit comments

Comments
 (0)