华为、VIVO、小米、和其他
fun isHuawei():Boolean{
return ("huawei".equals(Build.BRAND, ignoreCase = true)
|| "huawei".equals(Build.MANUFACTURER, ignoreCase = true))
}
fun isHarmonyOS(): Boolean {
try {
val clz = Class.forName("com.huawei.system.BuildEx")
val method = clz.getMethod("getOsBrand")
return "harmony" == method.invoke(clz)
} catch (e: java.lang.Exception) {
Log.e(
"TAG",
"the phone not support the harmonyOS"
)
}
return false
}
fun isVivo():Boolean{
return ("vivo".equals(Build.BRAND, ignoreCase = true)
|| "vivo".equals(Build.MANUFACTURER, ignoreCase = true))
}
fun isXiaoMi():Boolean{
return ("xiaomi".equals(Build.BRAND, ignoreCase = true)
|| "xiaomi".equals(Build.MANUFACTURER, ignoreCase = true))
}
判断是否开启悬浮窗和后台弹出页面
fun hasPermission(context:Context){
return hasFloatPermission(context) && hasBgStartPermission(context)
}
fun hasFloatPermission(context: Context): Boolean {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return Settings.canDrawOverlays(context)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
val manager = context.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
?: return false
val method = AppOpsManager::class.java.getMethod(
"checkOp",
Int::class.javaPrimitiveType,
Int::class.javaPrimitiveType,
String::class.java
)
val result =
method.invoke(manager, 24, Binder.getCallingUid(), context.packageName) as Int
return AppOpsManager.MODE_ALLOWED == result
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return false
}
fun hasBgStartPermission(context: Context?): Boolean {
if (isHuawei() && isHarmonyOS()) {
return isHarmonyBgStartPermissionAllowed(context)
} else if (isVivo()) {
return isVivoBgStartPermissionAllowed(context)
} else if (isXiaoMi()) {
return isXiaomiBgStartPermissionAllowed(context)
}
return hasFloatPermission(context)
}
fun isHarmonyBgStartPermissionAllowed(context: Context): Boolean {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
val manager = context.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
?: return false
val clz = Class.forName("com.huawei.android.app.AppOpsManagerEx")
val method = clz.getDeclaredMethod(
"checkHwOpNoThrow",
AppOpsManager::class.java,
Int::class.javaPrimitiveType,
Int::class.javaPrimitiveType,
String::class.java
)
val result = method.invoke(
clz.newInstance(), *arrayOf(
manager, 100000,
Process.myUid(), context.packageName
)
) as Int
return result == 0
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return false
}
fun isVivoBgStartPermissionAllowed(context: Context): Boolean {
return getVivoBgStartPermissionStatus(context) == 0
}
@SuppressLint("Range")
fun getVivoBgStartPermissionStatus(context: Context): Int {
val uri: Uri = Uri.parse("content://com.vivo.permissionmanager.provider.permission/start_bg_activity")
val selection = "pkgname = ?"
val selectionArgs = arrayOf(context.packageName)
var state = 1
try {
context.contentResolver.query(uri, null, selection, selectionArgs, null)?.use {
if (it.moveToFirst()) {
state = it.getInt(it.getColumnIndex("currentstate"))
}
}
} catch (e: Exception) {
e.printStackTrace()
}
return state
}
fun isXiaomiBgStartPermissionAllowed(context: Context): Boolean {
val ops = context.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
try {
val op = 10021
val method: Method = ops.javaClass.getMethod("checkOpNoThrow", Int::class.javaPrimitiveType, Int::class.javaPrimitiveType, String::class.java)
val result = method.invoke(ops, op, android.os.Process.myUid(), context.packageName) as Int
return result == AppOpsManager.MODE_ALLOWED
} catch (e: Exception) {
e.printStackTrace()
}
return false
}
fun startAppDetailsSettingsByBrand(context:Context){
if(isVivo()){
startVivoPermissionSettings(context)
}else if(isHuawei()){
startHuaweiPermissionSettings(context)
}else if(isXiaoMi()){
startXiaomiPermissionSettings(context)
}else{
startCommonSettings(context)
}
}
fun startVivoPermissionSettings(context:Context){
try {
val intent = Intent()
intent.setClassName("com.vivo.permissionmanager",
"com.vivo.permissionmanager.activity.SoftPermissionDetailActivity")
intent.setAction("secure.intent.action.softPermissionDetail")
intent.putExtra("packagename",context.packageName)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
} catch(e:Exception){
startCommonSettings(context)
}
}
fun startHuaweiPermissionSettings(context:Context){
if(!isHarmonyOS()){
startCommonSettings(context)
return
}
try {
val intent = Intent()
val comp = ComponentName("com.huawei.systemmanager",
"com.huawei.permissionmanager.ui.MainActivity")
intent.setComponent(comp)
intent.putExtra("packagename",context.packageName)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
} catch(e:Exception){
startCommonSettings(context)
}
}
fun startXiaomiPermissionSettings(context:Context){
try {
val intent = Intent("miui.intent.action.APP_PERM_EDITOR")
intent.setClassName("com.miui.securitycenter",
"com.miui.permcenter.permissions.PermissionsEditorActivity")
intent.putExtra("extra_pkgname",context.packageName)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
} catch(e:Exception){
startCommonSettings(context)
}
}
fun startCommonSettings(context:Context){
val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
val parse = Uri.parse("package:${context.packageName}")
intent.setData(parse)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
}
结束