Created
May 12, 2017 12:01
-
-
Save olegcherr/dc79af4a0a82473e560e1b191a2bea02 to your computer and use it in GitHub Desktop.
Android: Checking MIUI permissions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2017. | |
* qsboy.com 版权所有 | |
* https://github.com/JasonQS/Anti-recall/blob/ec45306/Java/XCheckPermission.java | |
*/ | |
package com.qiansheng.messagecapture; | |
import android.app.AppOpsManager; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.os.Binder; | |
import android.util.Log; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.lang.reflect.Method; | |
public class XCheckPermission { | |
static final String TAG = "XCheckPermission"; | |
static boolean checkFloatWindowPermission(Context context) { | |
AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); | |
try { | |
Class clazz = AppOpsManager.class; | |
Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class); | |
return AppOpsManager.MODE_ALLOWED == (int) method.invoke(manager, 24, Binder.getCallingUid(), context.getPackageName()); | |
} catch (Exception e) { | |
Log.e(TAG, Log.getStackTraceString(e)); | |
return false; | |
} | |
} | |
static boolean checkFileReadPermission(){ | |
return false; | |
} | |
/** | |
* 小米 ROM 权限申请 | |
*/ | |
static void applyMiuiPermission(Context context) { | |
int versionCode = getMiuiVersion(); | |
if (versionCode == 7) { | |
goToMiuiPermissionActivity_V7(context); | |
} else if (versionCode == 8) { | |
goToMiuiPermissionActivity_V8(context); | |
} else { | |
Log.e(TAG, "this is a special MIUI rom version, its version code " + versionCode); | |
} | |
} | |
/** | |
* 获取小米 rom 版本号,获取失败返回 -1 | |
* | |
* @return miui rom version code, if fail , return -1 | |
*/ | |
private static int getMiuiVersion() { | |
String version = getSystemProperty("ro.miui.ui.version.name"); | |
if (version != null) { | |
try { | |
return Integer.parseInt(version.substring(1)); | |
} catch (Exception e) { | |
Log.e(TAG, "get miui version code error, version : " + version); | |
Log.e(TAG, Log.getStackTraceString(e)); | |
} | |
} | |
return -1; | |
} | |
private static boolean isIntentAvailable(Intent intent, Context context) { | |
return intent != null && context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0; | |
} | |
/** | |
* 小米 V7 版本 ROM权限申请 | |
*/ | |
private static void goToMiuiPermissionActivity_V7(Context context) { | |
Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR"); | |
intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity"); | |
intent.putExtra("extra_pkgname", context.getPackageName()); | |
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
if (isIntentAvailable(intent, context)) { | |
context.startActivity(intent); | |
} else { | |
Log.e(TAG, "Intent is not available!"); | |
} | |
} | |
/** | |
* 小米 V8 版本 ROM权限申请 | |
*/ | |
private static void goToMiuiPermissionActivity_V8(Context context) { | |
Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR"); | |
intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity"); | |
intent.putExtra("extra_pkgname", context.getPackageName()); | |
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
if (isIntentAvailable(intent, context)) { | |
context.startActivity(intent); | |
} else { | |
Log.e(TAG, "Intent is not available!"); | |
} | |
} | |
private static String getSystemProperty(String propName) { | |
String line; | |
BufferedReader input = null; | |
try { | |
Process p = Runtime.getRuntime().exec("getprop " + propName); | |
input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024); | |
line = input.readLine(); | |
input.close(); | |
} catch (IOException ex) { | |
Log.e(TAG, "Unable to read system property " + propName, ex); | |
return null; | |
} finally { | |
if (input != null) { | |
try { | |
input.close(); | |
} catch (IOException e) { | |
Log.e(TAG, "Exception while closing InputStream", e); | |
} | |
} | |
} | |
return line; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment