Просмотр исходного кода

v1.0.2开发:权限框架开发

#Suyghur 3 лет назад
Родитель
Сommit
519d9f1743

+ 5 - 0
demo/src/main/AndroidManifest.xml

@@ -2,6 +2,7 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     package="com.yyxx.support.demo">
+
     <uses-permission android:name="android.permission.CAMERA" />
 
     <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
@@ -51,6 +52,7 @@
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"
+        android:requestLegacyExternalStorage="true"
         android:supportsRtl="true"
         android:theme="@style/AppTheme">
 
@@ -87,6 +89,9 @@
         <service
             android:name=".FloatViewService"
             android:exported="true" />
+        <meta-data
+            android:name="ScopedStorage"
+            android:value="false" />
     </application>
 
 </manifest>

+ 113 - 112
demo/src/main/java/com/yyxx/support/demo/PermissionActivity.kt

@@ -3,6 +3,7 @@ package com.yyxx.support.demo
 import android.app.Activity
 import android.content.Context
 import android.content.Intent
+import android.os.Build
 import android.os.Bundle
 import android.view.View
 import android.widget.Button
@@ -116,118 +117,118 @@ class PermissionActivity : Activity(), View.OnClickListener {
                             }
                         })
                 }
-//                2 -> {
-//                    PermissionKit.with().permission(Permission.ACCESS_BACKGROUND_LOCATION).request(this@PermissionActivity, object : IPermissionCallback {
-//                        override fun onGranted(permissions: ArrayList<String>, all: Boolean) {
-//                            toast("获取定位权限成功")
-//                        }
-//
-//                        override fun onDenied(permissions: ArrayList<String>, never: Boolean) {
-//                        }
-//
-//                        override fun onProxyFinish() {
-//                        }
-//                    })
-//                }
-//                3 -> {
-//                    val delayMillis = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
-//                        toast("当前版本不是 Android 11 以上,会自动变更为旧版的请求方式")
-//                        2000L
-//                    } else {
-//                        0L
-//                    }
-//                    postDelayed({
-//                        //不适配 Android 11 可以这样写permission(Permission.Group.STORAGE)
-//                        //适配 Android 11 需要这样写,这里无需再写 Permission.Group.STORAGE
-//                        Qojqva.with().permission(Permission.MANAGE_EXTERNAL_STORAGE).request(this@PermissionActivity, object : IPermissionCallback {
-//                            override fun onGranted(permissions: ArrayList<String>, all: Boolean) {
-//                                toast("获取存储权限成功")
-//                            }
-//
-//                            override fun onDenied(permissions: ArrayList<String>, never: Boolean) {
-//                            }
-//
-//                            override fun onProxyFinish() {
-//                            }
-//                        })
-//                    }, delayMillis)
-//                }
-//                4 -> {
-//                    val delayMillis = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
-//                        toast("当前版本不是 Android 11 以上,会自动变更为旧版的请求方式")
-//                        2000L
-//                    } else {
-//                        0L
-//                    }
-//                    postDelayed({
-//                        //不适配 Android 11 可以这样写permission(Permission.Group.STORAGE)
-//                        //适配 Android 11 需要这样写,这里无需再写 Permission.Group.STORAGE
-//                        Qojqva.with().permission(Permission.Group.STORAGE).request(this@PermissionActivity, object : IPermissionCallback {
-//                            override fun onGranted(permissions: ArrayList<String>, all: Boolean) {
-//                                toast("获取存储权限成功")
-//                            }
-//
-//                            override fun onDenied(permissions: ArrayList<String>, never: Boolean) {
-//                            }
-//
-//                            override fun onProxyFinish() {
-//                            }
-//                        })
-//                    }, delayMillis)
-//                }
-//                5 -> {
-//                    Qojqva.with().permission(Permission.REQUEST_INSTALL_PACKAGES).request(this@PermissionActivity, object : IPermissionCallback {
-//                        override fun onGranted(permissions: ArrayList<String>, all: Boolean) {
-//                            toast("获取安装包权限成功")
-//                        }
-//
-//                        override fun onDenied(permissions: ArrayList<String>, never: Boolean) {
-//                        }
-//
-//                        override fun onProxyFinish() {
-//                        }
-//                    })
-//                }
-//                6 -> {
-//                    Qojqva.with().permission(Permission.SYSTEM_ALERT_WINDOW).request(this@PermissionActivity, object : IPermissionCallback {
-//                        override fun onGranted(permissions: ArrayList<String>, all: Boolean) {
-//                            toast("获取悬浮窗权限成功")
-//                        }
-//
-//                        override fun onDenied(permissions: ArrayList<String>, never: Boolean) {
-//                        }
-//
-//                        override fun onProxyFinish() {
-//                        }
-//                    })
-//                }
-//                7 -> {
-//                    Qojqva.with().permission(Permission.NOTIFICATION_SERVICE).request(this@PermissionActivity, object : IPermissionCallback {
-//                        override fun onGranted(permissions: ArrayList<String>, all: Boolean) {
-//                            toast("获取通知栏权限成功")
-//                        }
-//
-//                        override fun onDenied(permissions: ArrayList<String>, never: Boolean) {
-//                        }
-//
-//                        override fun onProxyFinish() {
-//                        }
-//                    })
-//                }
-//                8 -> {
-//                    Qojqva.with().permission(Permission.WRITE_SETTINGS).request(this@PermissionActivity, object : IPermissionCallback {
-//                        override fun onGranted(permissions: ArrayList<String>, all: Boolean) {
-//                            toast("获取系统设置权限成功")
-//                        }
-//
-//                        override fun onDenied(permissions: ArrayList<String>, never: Boolean) {
-//                        }
-//
-//                        override fun onProxyFinish() {
-//                        }
-//                    })
-//                }
-//                9 -> PermissionActivity.startPermissionActivity(this@PermissionActivity)
+                2 -> {
+                    PermissionKit.with().permission(Permission.ACCESS_BACKGROUND_LOCATION).request(this@PermissionActivity, object : IPermissionCallback {
+                        override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
+                            toast("获取定位权限成功")
+                        }
+
+                        override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
+                        }
+
+                        override fun onProxyFinish() {
+                        }
+                    })
+                }
+                3 -> {
+                    val delayMillis = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
+                        toast("当前版本不是 Android 11 以上,会自动变更为旧版的请求方式")
+                        2000L
+                    } else {
+                        0L
+                    }
+                    postDelayed({
+                        //不适配 Android 11 可以这样写permission(Permission.Group.STORAGE)
+                        //适配 Android 11 需要这样写,这里无需再写 Permission.Group.STORAGE
+                        PermissionKit.with().permission(Permission.MANAGE_EXTERNAL_STORAGE).request(this@PermissionActivity, object : IPermissionCallback {
+                            override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
+                                toast("获取存储权限成功")
+                            }
+
+                            override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
+                            }
+
+                            override fun onProxyFinish() {
+                            }
+                        })
+                    }, delayMillis)
+                }
+                4 -> {
+                    val delayMillis = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
+                        toast("当前版本不是 Android 11 以上,会自动变更为旧版的请求方式")
+                        2000L
+                    } else {
+                        0L
+                    }
+                    postDelayed({
+                        //不适配 Android 11 可以这样写permission(Permission.Group.STORAGE)
+                        //适配 Android 11 需要这样写,这里无需再写 Permission.Group.STORAGE
+                        PermissionKit.with().permission(Permission.Group.STORAGE).request(this@PermissionActivity, object : IPermissionCallback {
+                            override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
+                                toast("获取存储权限成功")
+                            }
+
+                            override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
+                            }
+
+                            override fun onProxyFinish() {
+                            }
+                        })
+                    }, delayMillis)
+                }
+                5 -> {
+                    PermissionKit.with().permission(Permission.REQUEST_INSTALL_PACKAGES).request(this@PermissionActivity, object : IPermissionCallback {
+                        override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
+                            toast("获取安装包权限成功")
+                        }
+
+                        override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
+                        }
+
+                        override fun onProxyFinish() {
+                        }
+                    })
+                }
+                6 -> {
+                    PermissionKit.with().permission(Permission.SYSTEM_ALERT_WINDOW).request(this@PermissionActivity, object : IPermissionCallback {
+                        override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
+                            toast("获取悬浮窗权限成功")
+                        }
+
+                        override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
+                        }
+
+                        override fun onProxyFinish() {
+                        }
+                    })
+                }
+                7 -> {
+                    PermissionKit.with().permission(Permission.NOTIFICATION_SERVICE).request(this@PermissionActivity, object : IPermissionCallback {
+                        override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
+                            toast("获取通知栏权限成功")
+                        }
+
+                        override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
+                        }
+
+                        override fun onProxyFinish() {
+                        }
+                    })
+                }
+                8 -> {
+                    PermissionKit.with().permission(Permission.WRITE_SETTINGS).request(this@PermissionActivity, object : IPermissionCallback {
+                        override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
+                            toast("获取系统设置权限成功")
+                        }
+
+                        override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
+                        }
+
+                        override fun onProxyFinish() {
+                        }
+                    })
+                }
+                9 -> PermissionKit.startPermissionActivity(this@PermissionActivity)
             }
         }
     }

+ 0 - 5
library_support/build.gradle

@@ -36,11 +36,6 @@ android {
         preDexLibraries = false
     }
 
-    compileOptions {
-        sourceCompatibility JavaVersion.VERSION_1_8
-        targetCompatibility JavaVersion.VERSION_1_8
-    }
-
     //api23以上使用 httpClient
     useLibrary 'org.apache.http.legacy'
 }

+ 1 - 1
library_support/buildJar.gradle

@@ -1,5 +1,5 @@
 def SDK_BASE_NAME = 'yyxx_support'
-def SDK_VERSION = '1.0.1'
+def SDK_VERSION = '1.0.2'
 def SEPARATOR = '_'
 def SDK_DST_PATH = 'build/jar/'
 def ZIP_FILE = file('build/intermediates/aar_main_jar/release/classes.jar')

+ 7 - 0
library_support/proguard-rules.pro

@@ -133,3 +133,10 @@
 -keep class cn.yyxx.support.cache.**{ public <fields>; public <methods>;}
 -keep class cn.yyxx.support.multidex.**{ public <fields>; public <methods>;}
 -keep class cn.yyxx.support.volley.**{ *;}
+-keep class cn.yyxx.support.permission.PermissionKit{  public <fields>; public <methods>;}
+-keep class cn.yyxx.support.permission.Permission{ *;}
+-keep class cn.yyxx.support.permission.Permission$Group{ *;}
+-keep class cn.yyxx.support.permission.PermissionKitActivity{  public <fields>; public <methods>;}
+-keep class cn.yyxx.support.permission.IPermissionCallback{  public <fields>; public <methods>;}
+
+

+ 4 - 3
library_support/src/main/java/cn/yyxx/support/permission/BasePermissionInterceptor.java

@@ -9,6 +9,8 @@ import android.widget.Toast;
 import java.util.ArrayList;
 import java.util.List;
 
+import cn.yyxx.support.hawkeye.LogUtils;
+
 /**
  * @author #Suyghur.
  * Created on 2021/10/31
@@ -17,7 +19,6 @@ public class BasePermissionInterceptor implements IPermissionInterceptor {
     @Override
     public void requestPermissions(FragmentActivity activity, List<String> permissions, IPermissionCallback callback) {
         PermissionKitFragment.beginRequest(activity, new ArrayList<>(permissions), callback);
-
     }
 
     @Override
@@ -46,7 +47,7 @@ public class BasePermissionInterceptor implements IPermissionInterceptor {
     /**
      * 显示授权对话框
      */
-    protected void showPermissionDialog(FragmentActivity activity, List<String> permissions) {
+    private void showPermissionDialog(final FragmentActivity activity, final List<String> permissions) {
         // 这里的 Dialog 只是示例,没有用 DialogFragment 来处理 Dialog 生命周期
         new AlertDialog.Builder(activity)
                 .setTitle("授权提醒")
@@ -131,7 +132,7 @@ public class BasePermissionInterceptor implements IPermissionInterceptor {
                     }
                     break;
                 case Permission.READ_CALENDAR:
-                case  Permission.WRITE_CALENDAR:
+                case Permission.WRITE_CALENDAR:
                     hint = "日历权限";
                     if (!hints.contains(hint)) {
                         hints.add(hint);

+ 2 - 3
library_support/src/main/java/cn/yyxx/support/permission/PermissionChecker.java

@@ -140,8 +140,7 @@ final class PermissionChecker {
 
                 int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
 
-                boolean requestLegacyExternalStorage = parser.getAttributeBooleanValue(PermissionUtils.ANDROID_NAMESPACE,
-                        "requestLegacyExternalStorage", false);
+                boolean requestLegacyExternalStorage = parser.getAttributeBooleanValue(PermissionUtils.ANDROID_NAMESPACE, "requestLegacyExternalStorage", false);
                 // 如果在已经适配 Android 10 的情况下
                 if (targetSdkVersion >= Build.VERSION_CODES.Q && !requestLegacyExternalStorage &&
                         (requestPermissions.contains(Permission.MANAGE_EXTERNAL_STORAGE) || !scopedStorage)) {
@@ -311,7 +310,7 @@ final class PermissionChecker {
 //                }
 //            }
 
-            if ((sdkVersion =31) >= minSdkVersion) {
+            if ((sdkVersion = 31) >= minSdkVersion) {
 
                 if (Permission.BLUETOOTH_SCAN.equals(permission)) {
                     checkManifestPermission(manifestPermissions, Manifest.permission.BLUETOOTH_ADMIN, sdkVersion);

+ 2 - 2
library_support/src/main/java/cn/yyxx/support/permission/PermissionKit.java

@@ -131,12 +131,12 @@ public final class PermissionKit {
     /**
      * 请求权限
      */
-    public void request(Context context,IPermissionCallback callback) {
+    public void request(Context context, IPermissionCallback callback) {
 
         if (mInterceptor == null) {
             mInterceptor = getInterceptor();
         }
-        PermissionKitActivity.start(context,mPermissions,callback);
+        PermissionKitActivity.start(context, mPermissions, callback);
     }
 
     /**

+ 6 - 2
library_support/src/main/java/cn/yyxx/support/permission/PermissionKitActivity.java

@@ -11,12 +11,13 @@ import android.widget.FrameLayout;
 import java.util.ArrayList;
 import java.util.List;
 
+import cn.yyxx.support.hawkeye.LogUtils;
+
 /**
  * @author #Suyghur.
  * Created on 2021/10/31
  */
 public class PermissionKitActivity extends FragmentActivity {
-    private FrameLayout layout = null;
     private static List<String> mPermissions = null;
     private static IPermissionCallback mCallback = null;
 
@@ -42,7 +43,7 @@ public class PermissionKitActivity extends FragmentActivity {
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        layout = new FrameLayout(this);
+        FrameLayout layout = new FrameLayout(this);
         setContentView(layout);
         doRequestPermissions();
     }
@@ -50,6 +51,7 @@ public class PermissionKitActivity extends FragmentActivity {
     @Override
     protected void onDestroy() {
         super.onDestroy();
+        LogUtils.d("onDestroy");
         if (mPermissions != null) {
             mPermissions.clear();
             mPermissions = null;
@@ -60,6 +62,7 @@ public class PermissionKitActivity extends FragmentActivity {
     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
+        LogUtils.d("onActivityResult");
         if (requestCode == PermissionKit.REQUEST_CODE && !isFinishing()) {
             if (mCallback != null) {
                 mCallback.onProxyFinish();
@@ -112,6 +115,7 @@ public class PermissionKitActivity extends FragmentActivity {
         if (PermissionUtils.isGrantedPermissions(this, permissions)) {
             // 证明这些权限已经全部授予过,直接回调成功
             if (mCallback != null) {
+                LogUtils.d("证明这些权限已经全部授予过,直接回调成功");
                 mCallback.onGranted(mPermissions, true);
                 activity.finish();
             }

+ 8 - 7
library_support/src/main/java/cn/yyxx/support/permission/PermissionKitFragment.java

@@ -18,6 +18,8 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Random;
 
+import cn.yyxx.support.hawkeye.LogUtils;
+
 /**
  * @author #Suyghur.
  * Created on 2021/10/29
@@ -224,8 +226,7 @@ public final class PermissionKitFragment extends Fragment implements Runnable {
                     continue;
                 }
                 // 跳转到特殊权限授权页面
-                startActivityForResult(PermissionSettingPage.getSmartPermissionIntent(activity,
-                        PermissionUtils.asArrayList(permission)), getArguments().getInt(REQUEST_CODE));
+                startActivityForResult(PermissionSettingPage.getSmartPermissionIntent(activity, PermissionUtils.asArrayList(permission)), getArguments().getInt(REQUEST_CODE));
                 requestSpecialPermission = true;
             }
         }
@@ -241,7 +242,7 @@ public final class PermissionKitFragment extends Fragment implements Runnable {
      * 申请危险权限
      */
     public void requestDangerousPermission() {
-        FragmentActivity activity = getActivity();
+        final FragmentActivity activity = getActivity();
         Bundle arguments = getArguments();
         if (activity == null || arguments == null) {
             return;
@@ -359,19 +360,19 @@ public final class PermissionKitFragment extends Fragment implements Runnable {
         List<String> grantedPermission = PermissionUtils.getGrantedPermissions(permissions, grantResults);
 
         // 如果请求成功的权限集合大小和请求的数组一样大时证明权限已经全部授予
-        if (grantedPermission.size() != permissions.length) {
-            callback.onGranted(grantedPermission, true);
+        if (grantedPermission.size() == permissions.length) {
+            PermissionKit.getInterceptor().grantedPermissions(activity, grantedPermission, true, callback);
             return;
         }
 
         // 获取被拒绝的权限
         List<String> deniedPermission = PermissionUtils.getDeniedPermissions(permissions, grantResults);
 
-        callback.onDenied(deniedPermission, PermissionUtils.isPermissionPermanentDenied(activity, deniedPermission));
+        PermissionKit.getInterceptor().deniedPermissions(activity, deniedPermission, PermissionUtils.isPermissionPermanentDenied(activity, deniedPermission), callback);
 
         // 证明还有一部分权限被成功授予,回调成功接口
         if (!grantedPermission.isEmpty()) {
-            callback.onGranted(grantedPermission, false);
+            PermissionKit.getInterceptor().grantedPermissions(activity, grantedPermission, false, callback);
         }
     }