|
@@ -1,5 +1,6 @@
|
|
package com.benyanyi.permissionlib;
|
|
package com.benyanyi.permissionlib;
|
|
|
|
|
|
|
|
+import android.Manifest;
|
|
import android.annotation.TargetApi;
|
|
import android.annotation.TargetApi;
|
|
import android.app.AlertDialog;
|
|
import android.app.AlertDialog;
|
|
import android.app.Fragment;
|
|
import android.app.Fragment;
|
|
@@ -8,6 +9,8 @@ import android.content.Intent;
|
|
import android.content.pm.PackageManager;
|
|
import android.content.pm.PackageManager;
|
|
import android.os.Build;
|
|
import android.os.Build;
|
|
import android.os.Bundle;
|
|
import android.os.Bundle;
|
|
|
|
+import android.os.Environment;
|
|
|
|
+import android.provider.Settings;
|
|
import android.text.TextUtils;
|
|
import android.text.TextUtils;
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.NonNull;
|
|
@@ -15,6 +18,9 @@ import androidx.annotation.Nullable;
|
|
import androidx.core.app.ActivityCompat;
|
|
import androidx.core.app.ActivityCompat;
|
|
import androidx.core.content.ContextCompat;
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
|
|
|
+import com.benyanyi.permissionlib.callback.PermissionCallBack;
|
|
|
|
+import com.benyanyi.permissionlib.dialog.PermissionDialogInfo;
|
|
|
|
+
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
@@ -31,10 +37,12 @@ public final class PermissionFragment extends Fragment {
|
|
private final int code2 = 0x32;
|
|
private final int code2 = 0x32;
|
|
private int permissionCode = 0x30;
|
|
private int permissionCode = 0x30;
|
|
private boolean isStop = false;
|
|
private boolean isStop = false;
|
|
|
|
+ private boolean isShow = false;
|
|
|
|
|
|
private List<String> oList = new ArrayList<>();
|
|
private List<String> oList = new ArrayList<>();
|
|
|
|
|
|
private String[] permissions;
|
|
private String[] permissions;
|
|
|
|
+ private String[] permissions2;
|
|
private PermissionCallBack callBack;
|
|
private PermissionCallBack callBack;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -44,24 +52,64 @@ public final class PermissionFragment extends Fragment {
|
|
this.isStop = false;
|
|
this.isStop = false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 设置权限
|
|
|
|
+ */
|
|
void setPermissions(String[] permissions) {
|
|
void setPermissions(String[] permissions) {
|
|
- this.permissions = permissions;
|
|
|
|
|
|
+ List<String> list1 = new ArrayList<>();
|
|
|
|
+ List<String> list2 = new ArrayList<>();
|
|
|
|
+ for (String permission : permissions) {
|
|
|
|
+ if (permission.equalsIgnoreCase(Manifest.permission.MANAGE_EXTERNAL_STORAGE)) {
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
|
|
|
+ list2.add(permission);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ list1.add(permission);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ this.permissions = list1.toArray(new String[0]);
|
|
|
|
+ this.permissions2 = list2.toArray(new String[0]);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 设置弹窗
|
|
|
|
+ */
|
|
void setInfo(PermissionDialogInfo info) {
|
|
void setInfo(PermissionDialogInfo info) {
|
|
this.info = info;
|
|
this.info = info;
|
|
|
|
+ if (this.info != null && this.info.isShow) {
|
|
|
|
+ this.isShow = true;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 设置回调
|
|
|
|
+ */
|
|
void setCallBack(PermissionCallBack callBack) {
|
|
void setCallBack(PermissionCallBack callBack) {
|
|
this.callBack = callBack;
|
|
this.callBack = callBack;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 开始请求权限
|
|
|
|
+ */
|
|
void startForPermissionResult() {
|
|
void startForPermissionResult() {
|
|
if (checkPermission()) {
|
|
if (checkPermission()) {
|
|
- if (this.callBack != null) {
|
|
|
|
- this.callBack.onPermissionSuccess();
|
|
|
|
- this.callBack.onPermissionComplete();
|
|
|
|
- PermissionHelper.removeFragment();
|
|
|
|
|
|
+ if (permissions2 != null && permissions2.length <= 0) {
|
|
|
|
+ if (this.callBack != null) {
|
|
|
|
+ this.callBack.onPermissionSuccess();
|
|
|
|
+ this.callBack.onPermissionComplete();
|
|
|
|
+ PermissionHelper.removeFragment();
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
|
|
|
|
+ Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
|
|
|
|
+ startActivity(intent);
|
|
|
|
+ } else {
|
|
|
|
+ if (this.callBack != null) {
|
|
|
|
+ this.callBack.onPermissionSuccess();
|
|
|
|
+ this.callBack.onPermissionComplete();
|
|
|
|
+ PermissionHelper.removeFragment();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
requestPermission(code1);
|
|
requestPermission(code1);
|
|
@@ -98,6 +146,9 @@ public final class PermissionFragment extends Fragment {
|
|
return oList.isEmpty();
|
|
return oList.isEmpty();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 请求权限
|
|
|
|
+ */
|
|
@TargetApi(Build.VERSION_CODES.M)
|
|
@TargetApi(Build.VERSION_CODES.M)
|
|
private void requestPermission(int code) {
|
|
private void requestPermission(int code) {
|
|
requestPermissions(permissions, code);
|
|
requestPermissions(permissions, code);
|
|
@@ -130,10 +181,10 @@ public final class PermissionFragment extends Fragment {
|
|
builder.setPositiveButton(positiveText, new DialogInterface.OnClickListener() {
|
|
builder.setPositiveButton(positiveText, new DialogInterface.OnClickListener() {
|
|
@Override
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
+ dialog.dismiss();
|
|
if (positiveClick != null) {
|
|
if (positiveClick != null) {
|
|
- positiveClick.onClick(dialog, PermissionFragment.this.oList.toArray(new String[0]));
|
|
|
|
|
|
+ positiveClick.onClick(callBack, PermissionFragment.this.oList.toArray(new String[0]));
|
|
} else {
|
|
} else {
|
|
- dialog.dismiss();
|
|
|
|
requestPermission(code2);
|
|
requestPermission(code2);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -141,10 +192,10 @@ public final class PermissionFragment extends Fragment {
|
|
builder.setNegativeButton(negativeText, new DialogInterface.OnClickListener() {
|
|
builder.setNegativeButton(negativeText, new DialogInterface.OnClickListener() {
|
|
@Override
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
+ dialog.dismiss();
|
|
if (negativeClick != null) {
|
|
if (negativeClick != null) {
|
|
- negativeClick.onClick(dialog, PermissionFragment.this.oList.toArray(new String[0]));
|
|
|
|
|
|
+ negativeClick.onClick(callBack, PermissionFragment.this.oList.toArray(new String[0]));
|
|
} else {
|
|
} else {
|
|
- dialog.dismiss();
|
|
|
|
if (callBack != null) {
|
|
if (callBack != null) {
|
|
callBack.onPermissionFailure(PermissionFragment.this.oList.toArray(new String[0]));
|
|
callBack.onPermissionFailure(PermissionFragment.this.oList.toArray(new String[0]));
|
|
callBack.onPermissionComplete();
|
|
callBack.onPermissionComplete();
|
|
@@ -158,19 +209,41 @@ public final class PermissionFragment extends Fragment {
|
|
} else {
|
|
} else {
|
|
setFailure();
|
|
setFailure();
|
|
}
|
|
}
|
|
|
|
+ this.isShow = false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 权限请求后回调
|
|
|
|
+ */
|
|
@Override
|
|
@Override
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
List<String> list = hasAllPermissionsGranted(permissions, grantResults);
|
|
List<String> list = hasAllPermissionsGranted(permissions, grantResults);
|
|
boolean boo = list.isEmpty() && (this.code1 == requestCode || this.code2 == requestCode);
|
|
boolean boo = list.isEmpty() && (this.code1 == requestCode || this.code2 == requestCode);
|
|
if (boo) {
|
|
if (boo) {
|
|
- if (this.callBack != null) {
|
|
|
|
- this.callBack.onPermissionSuccess();
|
|
|
|
- this.callBack.onPermissionComplete();
|
|
|
|
- PermissionHelper.removeFragment();
|
|
|
|
|
|
+ if (this.permissions2 != null && this.permissions2.length > 0) {
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
|
|
|
|
+ Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
|
|
|
|
+ startActivity(intent);
|
|
|
|
+ } else {
|
|
|
|
+ if (this.callBack != null) {
|
|
|
|
+ this.callBack.onPermissionSuccess();
|
|
|
|
+ this.callBack.onPermissionComplete();
|
|
|
|
+ PermissionHelper.removeFragment();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (this.callBack != null) {
|
|
|
|
+ this.callBack.onPermissionSuccess();
|
|
|
|
+ this.callBack.onPermissionComplete();
|
|
|
|
+ PermissionHelper.removeFragment();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ } else if (this.permissions2 != null && this.permissions2.length > 0
|
|
|
|
+ && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
|
|
|
|
+ && !Environment.isExternalStorageManager()) {
|
|
|
|
+ Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
|
|
|
|
+ startActivity(intent);
|
|
} else {
|
|
} else {
|
|
oList = list;
|
|
oList = list;
|
|
if (this.code1 == requestCode) {
|
|
if (this.code1 == requestCode) {
|
|
@@ -205,13 +278,31 @@ public final class PermissionFragment extends Fragment {
|
|
super.onResume();
|
|
super.onResume();
|
|
if (this.isStop) {
|
|
if (this.isStop) {
|
|
if (checkPermission()) {
|
|
if (checkPermission()) {
|
|
- if (this.callBack != null) {
|
|
|
|
- this.callBack.onPermissionSuccess();
|
|
|
|
- this.callBack.onPermissionComplete();
|
|
|
|
- PermissionHelper.removeFragment();
|
|
|
|
|
|
+ if (this.permissions2 != null && this.permissions2.length > 0) {
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
|
|
|
|
+ if (this.callBack != null) {
|
|
|
|
+ this.callBack.onPermissionFailure(this.permissions2);
|
|
|
|
+ this.callBack.onPermissionComplete();
|
|
|
|
+ PermissionHelper.removeFragment();
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (this.callBack != null) {
|
|
|
|
+ this.callBack.onPermissionSuccess();
|
|
|
|
+ this.callBack.onPermissionComplete();
|
|
|
|
+ PermissionHelper.removeFragment();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (this.callBack != null) {
|
|
|
|
+ this.callBack.onPermissionSuccess();
|
|
|
|
+ this.callBack.onPermissionComplete();
|
|
|
|
+ PermissionHelper.removeFragment();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- if (this.callBack != null) {
|
|
|
|
|
|
+ if (this.isShow) {
|
|
|
|
+ showDialog();
|
|
|
|
+ } else if (this.callBack != null) {
|
|
this.callBack.onPermissionFailure(this.oList.toArray(new String[0]));
|
|
this.callBack.onPermissionFailure(this.oList.toArray(new String[0]));
|
|
this.callBack.onPermissionComplete();
|
|
this.callBack.onPermissionComplete();
|
|
PermissionHelper.removeFragment();
|
|
PermissionHelper.removeFragment();
|