Skip to content

Instantly share code, notes, and snippets.

@starry-shivam
Last active October 12, 2024 08:36
Show Gist options
  • Save starry-shivam/901267c26eb030eb3faf1ccd4d2bdd32 to your computer and use it in GitHub Desktop.
Save starry-shivam/901267c26eb030eb3faf1ccd4d2bdd32 to your computer and use it in GitHub Desktop.
Check if the device is running either on MIUI or HyperOS
object MiuiCheck {
/**
* Check if the device is running on MIUI.
*
* By default, HyperOS is excluded from the check.
* If you want to include HyperOS in the check, set excludeHyperOS to false.
*
* @param excludeHyperOS Whether to exclude HyperOS
* @return True if the device is running on MIUI, false otherwise
*/
fun isMiui(excludeHyperOS: Boolean = true): Boolean {
// Check if the device is manufactured by Xiaomi, Redmi, or POCO.
val brand = Build.BRAND.lowercase()
if (!setOf("xiaomi", "redmi", "poco").contains(brand)) return false
// This property is present in both MIUI and HyperOS.
val isMiui = !getProperty("ro.miui.ui.version.name").isNullOrBlank()
// This property is exclusive to HyperOS only and isn't present in MIUI.
val isHyperOS = !getProperty("ro.mi.os.version.name").isNullOrBlank()
return isMiui && (!excludeHyperOS || !isHyperOS)
}
// Private function to get the property value from build.prop.
private fun getProperty(property: String): String? {
return try {
Runtime.getRuntime().exec("getprop $property").inputStream.use { input ->
BufferedReader(InputStreamReader(input), 1024).readLine()
}
} catch (e: IOException) {
e.printStackTrace()
null
}
}
}
@jonathanfontaine
Copy link

Thanks a lot. Just curious: why do you guys need to detect if the device is running MIUI or HyperOS?
In my case, it was because the player notification of my app (PlayerNotificationManager with MediaSession) makes the phone extremely slow and unusable, the users needed to reboot the phone...

@starry-shivam
Copy link
Author

Thanks a lot. Just curious: why do you guys need to detect if the device is running MIUI or HyperOS? In my case, it was because the player notification of my app (PlayerNotificationManager with MediaSession) makes the phone extremely slow and unusable, the users needed to reboot the phone...

It's because there are some Android APIs that are broken on MIUI but somehow got fixed on HyperOS. For example, the per-app locale settings APIs introduced with Android 13 are broken on MIUI running on Android 13 as well as 14, but they seem to work perfectly fine on HyperOS based on Android 14. So, we need to differentiate to avoid blocking certain features for users whose devices/OS are capable of supporting them, based on the assumption that they're all running MIUI and that it doesn't work on MIUI. Also, note that MIUI is now an EOL skin, as Xiaomi replaced it with HyperOS. MIUI no longer gets new updates and is being replaced by HyperOS on existing devices via OTA updates.

@Ronjar
Copy link

Ronjar commented Oct 12, 2024

In my case because HyperOS doesn't grant BLUETOOTH_SCAN, ADVERTISE and CONNECT, when permission NEARBY_DEVICES is granted through their dialog. But when granted through App Permission Settings (The ones made by Google), they are granted as well. Frustrating, took me some time to find out. Also for other permissions "Open new windows while running in background" which is granted differently in stock Android.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment