Skip to content

Commit

Permalink
修复读取到的清单文件不是当前应用的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Mar 6, 2021
1 parent 02e6f3d commit 36c883c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ android {
dependencies {
// 权限请求框架:https://github.com/getActivity/XXPermissions
implementation 'com.hjq:xxpermissions:10.0'
implementation 'com.hjq:xxpermissions:10.2'
}
```

Expand Down Expand Up @@ -134,8 +134,8 @@ public class XxxActivity extends AppCompatActivity {

| 功能及细节 | [XXPermissions](https://github.com/getActivity/XXPermissions) | [AndPermission](https://github.com/yanzhenjie/AndPermission) | [RxPermissions](https://github.com/tbruyelle/RxPermissions) | [PermissionsDispatcher](https://github.com/permissions-dispatcher/PermissionsDispatcher) | [EasyPermissions](https://github.com/googlesamples/easypermissions) | [PermissionX](https://github.com/guolindev/PermissionX) | [AndroidUtilCode](https://github.com/Blankj/AndroidUtilCode) |
| :--------: | :------------: | :------------: | :------------: | :------------: | :------------: | :------------: | :------------: |
| 对应版本 | 10.0 | 2.0.3 | 0.12 | 4.8.0 | 3.0.0 | 1.4.0 | 1.30.5 |
| 框架体积 | [24 KB](https://bintray.com/getactivity/maven/xxpermissions#files/com/hjq/xxpermissions) | [127 KB](https://mvnrepository.com/artifact/com.yanzhenjie/permission) | [28 KB](https://jitpack.io/#com.github.tbruyelle/rxpermissions) | [91 KB](https://bintray.com/hotchemi/org.permissionsdispatcher/permissionsdispatcher-processor#files/org/permissionsdispatcher/permissionsdispatcher-processor) | [48 KB](https://bintray.com/easygoogle/EasyPermissions/easypermissions#files/pub/devrel/easypermissions) | [32 KB](https://bintray.com/guolindev/maven/permissionx#files/com/permissionx/guolindev/permissionx) | [483 KB](https://bintray.com/blankj/maven/UtilCode#files/com/blankj/utilcode) |
| 对应版本 | 10.2 | 2.0.3 | 0.12 | 4.8.0 | 3.0.0 | 1.4.0 | 1.30.5 |
| 框架体积 | [25 KB](https://bintray.com/getactivity/maven/xxpermissions#files/com/hjq/xxpermissions) | [127 KB](https://mvnrepository.com/artifact/com.yanzhenjie/permission) | [28 KB](https://jitpack.io/#com.github.tbruyelle/rxpermissions) | [91 KB](https://bintray.com/hotchemi/org.permissionsdispatcher/permissionsdispatcher-processor#files/org/permissionsdispatcher/permissionsdispatcher-processor) | [48 KB](https://bintray.com/easygoogle/EasyPermissions/easypermissions#files/pub/devrel/easypermissions) | [32 KB](https://bintray.com/guolindev/maven/permissionx#files/com/permissionx/guolindev/permissionx) | [483 KB](https://bintray.com/blankj/maven/UtilCode#files/com/blankj/utilcode) |
| 安装包权限 ||||||||
| 悬浮窗权限 ||||||||
| 通知栏权限 ||||||||
Expand Down
Binary file modified XXPermissions.apk
Binary file not shown.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.permissions.demo"
minSdkVersion 14
targetSdkVersion 30
versionCode 1000
versionName "10.0"
versionCode 1020
versionName "10.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {

defaultConfig {
minSdkVersion 11
versionCode 1000
versionName "10.0"
versionCode 1020
versionName "10.2"
}

// 使用 JDK 1.8
Expand All @@ -30,7 +30,7 @@ publish {
userOrg = 'getactivity'
groupId = 'com.hjq'
artifactId = 'xxpermissions'
publishVersion = '10.0'
publishVersion = '10.2'
desc = 'Android 6.0 permissions adaptation framework'
website = "https://github.com/getActivity/XXPermissions"
}
Expand Down
11 changes: 8 additions & 3 deletions library/src/main/java/com/hjq/permissions/PermissionChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,20 @@ static void checkStoragePermission(Context context, List<String> requestPermissi
return;
}

int cookie = PermissionUtils.findApkCookie(context);
if (cookie == -1) {
return;
}

try {
XmlResourceParser parser = context.getAssets().openXmlResourceParser("AndroidManifest.xml");
XmlResourceParser parser = context.getAssets().openXmlResourceParser(cookie, "AndroidManifest.xml");
// 是否读取到文件尾
while (parser.getEventType() != XmlResourceParser.END_DOCUMENT) {
// 当前节点是否为标签头部
if (parser.getEventType() == XmlResourceParser.START_TAG) {
// 获取标签名称
String tagName = parser.getName();
if ("application".equals(tagName)) {
String nodeName = parser.getName();
if ("application".equals(nodeName)) {
String namespace = "http://schemas.android.com/apk/res/android";
int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;

Expand Down
39 changes: 39 additions & 0 deletions library/src/main/java/com/hjq/permissions/PermissionUtils.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.hjq.permissions;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AppOpsManager;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
Expand Down Expand Up @@ -436,4 +438,41 @@ static FragmentActivity findFragmentActivity(Context context) {
} while (context != null);
return null;
}

/**
* 获取当前应用 Apk 在 AssetManager 中的 Cookie
*/
@SuppressWarnings("JavaReflectionMemberAccess")
@SuppressLint("PrivateApi")
static int findApkCookie(Context context) {
AssetManager assets = context.getAssets();
String path = context.getApplicationInfo().sourceDir;
int cookie = -1;
try {
try {
// 为什么不直接通过反射 AssetManager.findCookieForPath 方法来判断?因为这个 API 属于反射黑名单,反射执行不了
Method method = assets.getClass().getDeclaredMethod("addOverlayPath", String.class);
cookie = (int) method.invoke(assets, path);
} catch (Exception e) {
// NoSuchMethodException
// IllegalAccessException
// InvocationTargetException
e.printStackTrace();
Method method = assets.getClass().getDeclaredMethod("getApkPaths");
String[] apkPaths = (String[]) method.invoke(assets);
for (int i = 0; i < apkPaths.length; i++) {
if (apkPaths[i].equals(path)) {
cookie = i + 1;
break;
}
}
}
} catch (Exception e) {
// NoSuchMethodException
// IllegalAccessException
// InvocationTargetException
e.printStackTrace();
}
return cookie;
}
}

0 comments on commit 36c883c

Please sign in to comment.