|
@@ -0,0 +1,150 @@
|
|
|
+package com.cooleshow.base.utils.helper;
|
|
|
+
|
|
|
+import android.Manifest;
|
|
|
+import android.app.Activity;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.os.Build;
|
|
|
+import android.text.TextUtils;
|
|
|
+
|
|
|
+import com.cooleshow.base.R;
|
|
|
+import com.cooleshow.base.utils.LOG;
|
|
|
+import com.cooleshow.base.utils.ThreadUtils;
|
|
|
+import com.cooleshow.base.utils.Utils;
|
|
|
+import com.cooleshow.base.widgets.dialog.PermissionTipDialog;
|
|
|
+import com.tbruyelle.rxpermissions3.RxPermissions;
|
|
|
+
|
|
|
+import androidx.core.app.ActivityCompat;
|
|
|
+import androidx.fragment.app.Fragment;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Author by pq, Date on 2024/3/4.
|
|
|
+ */
|
|
|
+public class PermissionTipHelper {
|
|
|
+
|
|
|
+ private PermissionTipDialog mPermissionTipDialog;
|
|
|
+
|
|
|
+ private Runnable mRunnable;
|
|
|
+
|
|
|
+ private boolean isShow = true;
|
|
|
+
|
|
|
+ private PermissionTipHelper() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static PermissionTipHelper getInstance() {
|
|
|
+ return PermissionTipHelperHolder.mHelper;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class PermissionTipHelperHolder {
|
|
|
+ private static PermissionTipHelper mHelper = new PermissionTipHelper();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void showTipDialog(Activity activity, String[] permissions) {
|
|
|
+ showTipDialog(activity, permissions, "", "");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void showTipDialog(Fragment context, String[] permissions) {
|
|
|
+ showTipDialog(context.getActivity(), permissions, "", "");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void showTipDialog(Activity activity, String[] permissions, String title, String tip) {
|
|
|
+ if (hasPermissions(activity, permissions)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ isShow = true;
|
|
|
+ mRunnable = new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ LOG.i("showTipDialog:" + isShow);
|
|
|
+ if (!isShow) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ mPermissionTipDialog = new PermissionTipDialog(activity);
|
|
|
+ mPermissionTipDialog.show();
|
|
|
+ String des = !TextUtils.isEmpty(tip) ? tip : getTipFromPermissions(permissions);
|
|
|
+ mPermissionTipDialog.setDes(des);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ //增加延时是因为手写手机有拒绝权限并不再询问的选项,当去请求权限的时候会直接返回拒绝,这样的话就不需要显示这个提示了窗
|
|
|
+ ThreadUtils.runOnUiThreadDelayed(mRunnable, 300);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getTipFromPermissions(String[] permissions) {
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
+ for (int i = 0; i < permissions.length; i++) {
|
|
|
+ String permission = permissions[i];
|
|
|
+ String tip = createTip(permission);
|
|
|
+ stringBuilder.append(tip);
|
|
|
+ }
|
|
|
+ return stringBuilder.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private String createTip(String permission) {
|
|
|
+ if (TextUtils.equals(permission, Manifest.permission.CAMERA)) {
|
|
|
+ return Utils.getApp().getString(R.string.camera_permission_tip);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (TextUtils.equals(permission, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
|
|
+ return Utils.getApp().getString(R.string.write_storage_permission_tip);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (TextUtils.equals(permission, Manifest.permission.READ_EXTERNAL_STORAGE)) {
|
|
|
+ return Utils.getApp().getString(R.string.read_storage_permission_tip);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (TextUtils.equals(permission, Manifest.permission.RECORD_AUDIO)) {
|
|
|
+ return Utils.getApp().getString(R.string.record_audio_permission_tip);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (TextUtils.equals(permission, Manifest.permission.CALL_PHONE)) {
|
|
|
+ return Utils.getApp().getString(R.string.call_phone_permission_tip);
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean hasPermission(RxPermissions rxPermissions, String[] permissions) {
|
|
|
+ for (int i = 0; i < permissions.length; i++) {
|
|
|
+ String permission = permissions[i];
|
|
|
+ boolean granted = rxPermissions.isGranted(permission);
|
|
|
+ boolean revoked = rxPermissions.isRevoked(permission);
|
|
|
+ LOG.i("permission:" + permission + "granted:+" + granted + "-revoked:" + revoked);
|
|
|
+ if (!granted && !revoked) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断当前应用是否有指定权限,运行时权限的检测
|
|
|
+ */
|
|
|
+ public boolean hasPermissions(Context context, String[] permissions) {
|
|
|
+ if (permissions == null || permissions.length == 0) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ boolean ifSdk = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
|
|
|
+ for (String permission : permissions) {
|
|
|
+ if (ifSdk && !hasPermission(context, permission)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean hasPermission(Context context, String permission) {
|
|
|
+ return ActivityCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void dismissDialog() {
|
|
|
+ isShow = false;
|
|
|
+ if (mRunnable != null) {
|
|
|
+ ThreadUtils.getMainHandler().removeCallbacks(mRunnable);
|
|
|
+ }
|
|
|
+ mRunnable = null;
|
|
|
+ if (mPermissionTipDialog != null) {
|
|
|
+ mPermissionTipDialog.dismiss();
|
|
|
+ mPermissionTipDialog = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|