浏览代码

PDFTool(Android) - 修复无MANAGE_EXTERNAL_STORAGE权限导致的bug

liuxiaolong 9 月之前
父节点
当前提交
aa3a071c1d

+ 8 - 8
ComPDFKit_Tools/src/main/AndroidManifest.xml

@@ -38,14 +38,14 @@
             android:exported="true"
             android:theme="@style/Tools_Base_Theme"
             android:windowSoftInputMode="adjustPan">
-            <intent-filter tools:ignore="AppLinkUrlError"
-                android:scheme="http">
-                <action android:name="android.intent.action.VIEW" />
-                <action android:name="android.intent.action.SEND" />
-
-                <category android:name="android.intent.category.DEFAULT" />
-                <data android:mimeType="application/pdf" />
-            </intent-filter>
+<!--            <intent-filter tools:ignore="AppLinkUrlError"-->
+<!--                android:scheme="http">-->
+<!--                <action android:name="android.intent.action.VIEW" />-->
+<!--                <action android:name="android.intent.action.SEND" />-->
+
+<!--                <category android:name="android.intent.category.DEFAULT" />-->
+<!--                <data android:mimeType="application/pdf" />-->
+<!--            </intent-filter>-->
         </activity>
 
     </application>

+ 2 - 1
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/common/basic/activity/CBasicPDFActivity.java

@@ -10,6 +10,7 @@
 package com.compdfkit.tools.common.basic.activity;
 
 
+import android.Manifest;
 import android.graphics.Color;
 import android.os.Build;
 import android.os.Environment;
@@ -165,7 +166,7 @@ public class CBasicPDFActivity extends CPermissionActivity {
         boolean isExternalFile = !TextUtils.isEmpty(document.getAbsolutePath()) &&
                 document.getAbsolutePath().startsWith("/storage/emulated/0");
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && isExternalFile) {
-            if (!Environment.isExternalStorageManager()) {
+            if (CPermissionUtil.checkManifestPermission(this, Manifest.permission.MANAGE_EXTERNAL_STORAGE) && !Environment.isExternalStorageManager()) {
                 CPermissionUtil.openManageAllFileAppSettings(this);
                 return;
             }

+ 2 - 1
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/common/basic/fragment/CBasicPDFFragment.java

@@ -10,6 +10,7 @@
 package com.compdfkit.tools.common.basic.fragment;
 
 
+import android.Manifest;
 import android.graphics.Color;
 import android.os.Build;
 import android.os.Environment;
@@ -168,7 +169,7 @@ public class CBasicPDFFragment extends CPermissionFragment {
         boolean isExternalFile = !TextUtils.isEmpty(document.getAbsolutePath()) &&
                 document.getAbsolutePath().startsWith("/storage/emulated/0");
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && isExternalFile) {
-            if (!Environment.isExternalStorageManager()) {
+            if (CPermissionUtil.checkManifestPermission(getContext(), Manifest.permission.MANAGE_EXTERNAL_STORAGE) && !Environment.isExternalStorageManager()) {
                 CPermissionUtil.openManageAllFileAppSettings(getContext());
                 return;
             }

+ 11 - 2
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/common/utils/CPermissionUtil.java

@@ -10,6 +10,7 @@
 package com.compdfkit.tools.common.utils;
 
 
+import android.Manifest;
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
@@ -48,7 +49,11 @@ public class CPermissionUtil {
             return true;
         }
         if (Build.VERSION.SDK_INT >= VERSION_TIRAMISU) {
-            return Environment.isExternalStorageManager();
+            if (CPermissionUtil.checkManifestPermission(context, Manifest.permission.MANAGE_EXTERNAL_STORAGE)){
+                return Environment.isExternalStorageManager();
+            }else {
+                return true;
+            }
         }
         if (Build.VERSION.SDK_INT >= VERSION_R) {
             boolean hasPermission = true;
@@ -59,7 +64,11 @@ public class CPermissionUtil {
                     break;
                 }
             }
-            return Environment.isExternalStorageManager() && hasPermission;
+            if (CPermissionUtil.checkManifestPermission(context, Manifest.permission.MANAGE_EXTERNAL_STORAGE)){
+                return Environment.isExternalStorageManager() && hasPermission;
+            }else {
+                return hasPermission;
+            }
         }
         for (String perm : STORAGE_PERMISSIONS) {
             if (ContextCompat.checkSelfPermission(context, perm)

+ 40 - 2
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/common/views/directory/CFileDirectoryDatas.java

@@ -10,6 +10,15 @@
 package com.compdfkit.tools.common.views.directory;
 
 
+import static com.compdfkit.tools.common.utils.CPermissionUtil.VERSION_R;
+
+import android.Manifest;
+import android.content.Context;
+import android.os.Build;
+import android.os.Environment;
+
+import com.compdfkit.tools.common.utils.CPermissionUtil;
+
 import java.io.File;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -18,10 +27,32 @@ import java.util.List;
 public class CFileDirectoryDatas {
 
 
-    public static List<File> getDirectories(String rootDir){
+    public static List<File> getDirectories(Context context, String rootDir){
         List<File> list = new ArrayList<>();
         File file = new File(rootDir);
-        File[] files = file.listFiles(pathname -> !pathname.isHidden() && pathname.isDirectory());
+        boolean hasStoragePermission = CPermissionUtil.hasStoragePermissions(context);
+        if (Build.VERSION.SDK_INT >= VERSION_R){
+            if (!CPermissionUtil.checkManifestPermission(context, Manifest.permission.MANAGE_EXTERNAL_STORAGE) ||
+                    !Environment.isExternalStorageManager()){
+                hasStoragePermission = false;
+            }
+        }
+        List<String> publicDirectoryList = publicDirectoryList();
+        boolean finalHasStoragePermission = hasStoragePermission;
+        File[] files = file.listFiles(pathname -> {
+            if (finalHasStoragePermission){
+                return !pathname.isHidden() && pathname.isDirectory();
+            }else {
+                boolean isPublicDirectory =false;
+                for (String s : publicDirectoryList) {
+                    if (pathname.getAbsolutePath().startsWith(s)){
+                        isPublicDirectory = true;
+                        break;
+                    }
+                }
+                return !pathname.isHidden() && pathname.isDirectory() && isPublicDirectory;
+            }
+        });
         if (files == null || files.length == 0){
             return list;
         }else {
@@ -31,5 +62,12 @@ public class CFileDirectoryDatas {
         }
     }
 
+    public static List<String> publicDirectoryList(){
+        List<String> list = new ArrayList<>();
+        list.add(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
+        list.add(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath());
+        list.add(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath());
+        return list;
+    }
 
 }

+ 1 - 1
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/common/views/directory/CFileDirectoryDialog.java

@@ -205,7 +205,7 @@ public class CFileDirectoryDialog extends CBasicBottomSheetDialogFragment {
 
             @Override
             protected List<File> onRun() {
-                return CFileDirectoryDatas.getDirectories(rootDir);
+                return CFileDirectoryDatas.getDirectories(getContext(), rootDir);
             }
 
             @Override

+ 1 - 2
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/security/encryption/CDocumentEncryptionDialog.java

@@ -247,13 +247,12 @@ public class CDocumentEncryptionDialog extends CBasicBottomSheetDialogFragment i
                 CToastUtil.showLongToast(getContext(), R.string.tools_password_must_be_different);
                 return;
             }
-
             // Check the storage permissions to ensure that
             // you can select the directory and save to the corresponding directory normally.
             if (CPermissionUtil.hasStoragePermissions(getContext())) {
                 showSelectDirDialog();
             }else {
-                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+                if (CPermissionUtil.checkManifestPermission(getContext(), Manifest.permission.MANAGE_EXTERNAL_STORAGE) && Build.VERSION.SDK_INT >= CPermissionUtil.VERSION_R) {
                     CPermissionUtil.openManageAllFileAppSettings(getContext());
                 } else {
                     multiplePermissionResultLauncher.launch(CPermissionUtil.STORAGE_PERMISSIONS, result -> {

+ 1 - 1
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/security/watermark/CWatermarkEditDialog.java

@@ -168,7 +168,7 @@ public class CWatermarkEditDialog extends CBasicBottomSheetDialogFragment implem
             if (CPermissionUtil.hasStoragePermissions(getContext())) {
                 save();
             } else {
-                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+                if (CPermissionUtil.checkManifestPermission(getContext(), Manifest.permission.MANAGE_EXTERNAL_STORAGE) && Build.VERSION.SDK_INT >= CPermissionUtil.VERSION_R) {
                     CPermissionUtil.openManageAllFileAppSettings(getContext());
                 } else {
                     multiplePermissionResultLauncher.launch(CPermissionUtil.STORAGE_PERMISSIONS, result -> {

+ 2 - 1
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/signature/importcert/create/CPDFSelectDigitalSignatureDialog.java

@@ -130,7 +130,8 @@ public class CPDFSelectDigitalSignatureDialog extends DialogFragment implements
             dismiss();
         } else if (v.getId() == R.id.btn_confirm) {
             if (!CPermissionUtil.hasStoragePermissions(getContext())){
-                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+                if (Build.VERSION.SDK_INT >= CPermissionUtil.VERSION_R &&
+                CPermissionUtil.checkManifestPermission(getContext(), Manifest.permission.MANAGE_EXTERNAL_STORAGE)) {
                     CPermissionUtil.openManageAllFileAppSettings(getContext());
                 }else {
                     requestStorageLauncher.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE);

+ 6 - 4
PDFViewer/src/main/AndroidManifest.xml

@@ -1,17 +1,19 @@
 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android">
-    <uses-permission android:name="android.permission.INTERNET"/>
+
+    <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
-<!--    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />-->
+    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
+
     <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"
         android:requestLegacyExternalStorage="true"
-        android:usesCleartextTraffic="true"
         android:supportsRtl="true"
-        android:theme="@style/Basic_Theme">
+        android:theme="@style/Basic_Theme"
+        android:usesCleartextTraffic="true">
         <activity
             android:name=".home.HomeActivity"
             android:configChanges="keyboardHidden|orientation|screenSize"