|
@@ -18,8 +18,12 @@ import androidx.annotation.Nullable;
|
|
|
import androidx.core.app.ActivityCompat;
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
|
+import com.benyanyi.loglib.Jlog;
|
|
|
import com.benyanyi.permissionlib.callback.PermissionCallBack;
|
|
|
import com.benyanyi.permissionlib.dialog.PermissionDialogInfo;
|
|
|
+import com.benyanyi.permissionlib.shortcut.RuntimeSettingPage;
|
|
|
+import com.benyanyi.permissionlib.shortcut.ShortcutPermissionChecker;
|
|
|
+import com.benyanyi.permissionlib.type.PermissionType;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
@@ -41,9 +45,16 @@ public final class PermissionFragment extends Fragment {
|
|
|
private List<String> oList = new ArrayList<>();
|
|
|
|
|
|
private String[] permissions;
|
|
|
- private String[] permissions2;
|
|
|
private PermissionCallBack callBack;
|
|
|
|
|
|
+ private boolean manageExternalStorage = false;
|
|
|
+ private boolean shortcutPermission = false;
|
|
|
+
|
|
|
+ private boolean checkManageExternalStorage = false;//判断是否请求访问全部文件权限
|
|
|
+ private boolean checkShortcutPermission = false;//判断是否请求创建快捷方式权限
|
|
|
+
|
|
|
+ private static final String MARK = Build.MANUFACTURER.toLowerCase();
|
|
|
+
|
|
|
@Override
|
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
@@ -56,18 +67,18 @@ public final class PermissionFragment extends Fragment {
|
|
|
*/
|
|
|
void setPermissions(String[] 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);
|
|
|
+ manageExternalStorage = true;
|
|
|
}
|
|
|
+ } else if (permission.equalsIgnoreCase(PermissionType.SHORTCUT)) {
|
|
|
+ shortcutPermission = true;
|
|
|
} else {
|
|
|
list1.add(permission);
|
|
|
}
|
|
|
}
|
|
|
this.permissions = list1.toArray(new String[0]);
|
|
|
- this.permissions2 = list2.toArray(new String[0]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -92,16 +103,19 @@ public final class PermissionFragment extends Fragment {
|
|
|
*/
|
|
|
void startForPermissionResult() {
|
|
|
if (checkPermission()) {
|
|
|
- if (permissions2 != null && permissions2.length <= 0) {
|
|
|
+ if (!manageExternalStorage && !shortcutPermission) {
|
|
|
if (this.callBack != null) {
|
|
|
this.callBack.onSuccess();
|
|
|
this.callBack.onComplete();
|
|
|
PermissionHelper.removeFragment();
|
|
|
}
|
|
|
} else {
|
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && manageExternalStorage && !Environment.isExternalStorageManager()) {
|
|
|
Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
|
|
|
startActivity(intent);
|
|
|
+ } else if (shortcutPermission && !checkShortcut()) {
|
|
|
+ RuntimeSettingPage settingPage = new RuntimeSettingPage(getActivity());
|
|
|
+ settingPage.start();
|
|
|
} else {
|
|
|
if (this.callBack != null) {
|
|
|
this.callBack.onSuccess();
|
|
@@ -127,7 +141,7 @@ public final class PermissionFragment extends Fragment {
|
|
|
}
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
for (String permission : permissions) {
|
|
|
- if (ContextCompat.checkSelfPermission(getActivity(), permission) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ if (!permission.equalsIgnoreCase(PermissionType.SHORTCUT) && ContextCompat.checkSelfPermission(getActivity(), permission) != PackageManager.PERMISSION_GRANTED) {
|
|
|
if (!oList.contains(permission)) {
|
|
|
oList.add(permission);
|
|
|
}
|
|
@@ -135,7 +149,7 @@ public final class PermissionFragment extends Fragment {
|
|
|
}
|
|
|
} else {
|
|
|
for (String permission : permissions) {
|
|
|
- if (ActivityCompat.checkSelfPermission(getActivity(), permission) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ if (!permission.equalsIgnoreCase(PermissionType.SHORTCUT) && ActivityCompat.checkSelfPermission(getActivity(), permission) != PackageManager.PERMISSION_GRANTED) {
|
|
|
if (!oList.contains(permission)) {
|
|
|
oList.add(permission);
|
|
|
}
|
|
@@ -145,6 +159,28 @@ public final class PermissionFragment extends Fragment {
|
|
|
return oList.isEmpty();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 判断是否有创建快捷方式权限
|
|
|
+ *
|
|
|
+ * @return 是否有创建快捷方式权限
|
|
|
+ */
|
|
|
+ private boolean checkShortcut() {
|
|
|
+ Jlog.v(MARK);
|
|
|
+ boolean result = false;
|
|
|
+ if (MARK.contains("huawei")) {
|
|
|
+ result = ShortcutPermissionChecker.checkOnEMUI(getActivity());
|
|
|
+ } else if (MARK.contains("xiaomi")) {
|
|
|
+ result = ShortcutPermissionChecker.checkOnMIUI(getActivity());
|
|
|
+ } else if (MARK.contains("oppo")) {
|
|
|
+ result = ShortcutPermissionChecker.checkOnOPPO(getActivity());
|
|
|
+ } else if (MARK.contains("vivo")) {
|
|
|
+ result = ShortcutPermissionChecker.checkOnVIVO(getActivity());
|
|
|
+ } else if (MARK.contains("samsung") || MARK.contains("meizu")) {
|
|
|
+ result = true;
|
|
|
+ }
|
|
|
+ return !result;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 请求权限
|
|
|
*/
|
|
@@ -217,13 +253,17 @@ public final class PermissionFragment extends Fragment {
|
|
|
@Override
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
+ Jlog.d();
|
|
|
List<String> list = hasAllPermissionsGranted(permissions, grantResults);
|
|
|
boolean boo = list.isEmpty() && (this.code1 == requestCode || this.code2 == requestCode);
|
|
|
if (boo) {
|
|
|
- if (this.permissions2 != null && this.permissions2.length > 0) {
|
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
|
|
|
+ if (manageExternalStorage || shortcutPermission) {
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && manageExternalStorage && !Environment.isExternalStorageManager()) {
|
|
|
Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
|
|
|
startActivity(intent);
|
|
|
+ } else if (shortcutPermission && !checkShortcut()) {
|
|
|
+ RuntimeSettingPage settingPage = new RuntimeSettingPage(getActivity());
|
|
|
+ settingPage.start();
|
|
|
} else {
|
|
|
if (this.callBack != null) {
|
|
|
this.callBack.onSuccess();
|
|
@@ -238,11 +278,13 @@ public final class PermissionFragment extends Fragment {
|
|
|
PermissionHelper.removeFragment();
|
|
|
}
|
|
|
}
|
|
|
- } else if (this.permissions2 != null && this.permissions2.length > 0
|
|
|
- && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
|
|
|
+ } else if (manageExternalStorage && 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 (shortcutPermission && !checkShortcut()) {
|
|
|
+ RuntimeSettingPage settingPage = new RuntimeSettingPage(getActivity());
|
|
|
+ settingPage.start();
|
|
|
} else {
|
|
|
oList = list;
|
|
|
if (this.code1 == requestCode) {
|
|
@@ -275,12 +317,39 @@ public final class PermissionFragment extends Fragment {
|
|
|
@Override
|
|
|
public void onResume() {
|
|
|
super.onResume();
|
|
|
+ Jlog.d("onResume:" + isStop);
|
|
|
if (this.isStop) {
|
|
|
if (checkPermission()) {
|
|
|
- if (this.permissions2 != null && this.permissions2.length > 0) {
|
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
|
|
|
+ Jlog.v();
|
|
|
+ if (manageExternalStorage || shortcutPermission) {
|
|
|
+ Jlog.d(manageExternalStorage);
|
|
|
+ Jlog.d(checkManageExternalStorage);
|
|
|
+ Jlog.d(shortcutPermission);
|
|
|
+ Jlog.d(checkShortcutPermission);
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && manageExternalStorage && !Environment.isExternalStorageManager()) {
|
|
|
+ if (!checkManageExternalStorage) {
|
|
|
+ Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
|
|
|
+ startActivity(intent);
|
|
|
+ checkManageExternalStorage = true;
|
|
|
+ } else {
|
|
|
+ Jlog.d();
|
|
|
+ list.add(Manifest.permission.MANAGE_EXTERNAL_STORAGE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (shortcutPermission && !checkShortcut()) {
|
|
|
+ Jlog.d(checkShortcutPermission);
|
|
|
+ if (!checkShortcutPermission) {
|
|
|
+ RuntimeSettingPage settingPage = new RuntimeSettingPage(getActivity());
|
|
|
+ settingPage.start();
|
|
|
+ } else {
|
|
|
+ list.add(PermissionType.SHORTCUT);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Jlog.d(list.size());
|
|
|
+ if (!list.isEmpty()) {
|
|
|
if (this.callBack != null) {
|
|
|
- this.callBack.onFailure(this.permissions2);
|
|
|
+ this.callBack.onFailure(list.toArray(new String[0]));
|
|
|
this.callBack.onComplete();
|
|
|
PermissionHelper.removeFragment();
|
|
|
}
|
|
@@ -299,13 +368,39 @@ public final class PermissionFragment extends Fragment {
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
+ Jlog.d(manageExternalStorage);
|
|
|
+ Jlog.d(checkManageExternalStorage);
|
|
|
+ Jlog.d(shortcutPermission);
|
|
|
+ Jlog.d(checkShortcutPermission);
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && manageExternalStorage && !Environment.isExternalStorageManager()) {
|
|
|
+ if (!checkManageExternalStorage) {
|
|
|
+ Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
|
|
|
+ startActivity(intent);
|
|
|
+ checkManageExternalStorage = true;
|
|
|
+ } else {
|
|
|
+ Jlog.d();
|
|
|
+ list.add(Manifest.permission.MANAGE_EXTERNAL_STORAGE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (shortcutPermission && !checkShortcut()) {
|
|
|
+ Jlog.d(checkShortcutPermission);
|
|
|
+ if (!checkShortcutPermission) {
|
|
|
+ RuntimeSettingPage settingPage = new RuntimeSettingPage(getActivity());
|
|
|
+ settingPage.start();
|
|
|
+ } else {
|
|
|
+ list.add(PermissionType.SHORTCUT);
|
|
|
+ }
|
|
|
+ }
|
|
|
if (this.isShow) {
|
|
|
showDialog();
|
|
|
} else if (this.callBack != null) {
|
|
|
+ oList.addAll(list);
|
|
|
this.callBack.onFailure(this.oList.toArray(new String[0]));
|
|
|
this.callBack.onComplete();
|
|
|
PermissionHelper.removeFragment();
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
this.isStop = false;
|
|
@@ -315,6 +410,7 @@ public final class PermissionFragment extends Fragment {
|
|
|
public void onStop() {
|
|
|
super.onStop();
|
|
|
this.isStop = true;
|
|
|
+ Jlog.d("onStop");
|
|
|
}
|
|
|
|
|
|
private void setFailure() {
|