diff --git a/lib_base/src/main/java/com/android/base/permission/DefaultPermissionUIProvider.java b/lib_base/src/main/java/com/android/base/permission/DefaultPermissionUIProvider.java index 51f21a8..61face0 100644 --- a/lib_base/src/main/java/com/android/base/permission/DefaultPermissionUIProvider.java +++ b/lib_base/src/main/java/com/android/base/permission/DefaultPermissionUIProvider.java @@ -3,29 +3,28 @@ package com.android.base.permission; import android.content.Context; import android.content.DialogInterface; import android.graphics.Color; -import android.support.annotation.NonNull; import android.support.v7.app.AlertDialog; -import android.text.SpannableStringBuilder; -import android.text.Spanned; -import android.text.style.ForegroundColorSpan; import com.android.base.R; -import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.ToastUtils; -import java.util.Arrays; -import java.util.List; +import org.jetbrains.annotations.NotNull; + +import static com.android.base.permission.PermissionUtils.createPermissionAskAgainText; +import static com.android.base.permission.PermissionUtils.createPermissionDeniedTip; +import static com.android.base.permission.PermissionUtils.createPermissionRationaleText; + class DefaultPermissionUIProvider implements IPermissionUIProvider { - private static final String COLOR_STRING = "#FF4081"; + private static final int COLOR_STRING = Color.parseColor("#FF4081"); @Override - public void showPermissionRationaleDialog(Context context, final String[] permission, - final DialogInterface.OnClickListener onContinueListener, DialogInterface.OnClickListener onCancelListener) { + public void showPermissionRationaleDialog(@NotNull Context context, @NotNull final String[] permission, + @NotNull final DialogInterface.OnClickListener onContinueListener, @NotNull DialogInterface.OnClickListener onCancelListener) { AlertDialog dialog = new AlertDialog.Builder(context) - .setMessage(DefaultPermissionResourceProvider.createPermissionRationaleText(context, permission)) + .setMessage(createPermissionRationaleText(context, permission, COLOR_STRING)) .setCancelable(false) .setPositiveButton(R.string.Base_Confirm, onContinueListener) .setNegativeButton(R.string.Base_Cancel, onCancelListener) @@ -35,12 +34,12 @@ class DefaultPermissionUIProvider implements IPermissionUIProvider { } @Override - public void showAskAgainDialog(Context context, final String[] permission, - DialogInterface.OnClickListener onToSetPermissionListener, - DialogInterface.OnClickListener onCancelListener) { + public void showAskAgainDialog(@NotNull Context context, @NotNull final String[] permission, + @NotNull DialogInterface.OnClickListener onToSetPermissionListener, + @NotNull DialogInterface.OnClickListener onCancelListener) { AlertDialog dialog = new AlertDialog.Builder(context) - .setMessage(DefaultPermissionResourceProvider.createPermissionAskAgainText(context, permission)) + .setMessage(createPermissionAskAgainText(context, permission, COLOR_STRING)) .setCancelable(false) .setPositiveButton(R.string.Base_to_set_permission, onToSetPermissionListener) .setNegativeButton(R.string.Base_Cancel, onCancelListener) @@ -50,50 +49,8 @@ class DefaultPermissionUIProvider implements IPermissionUIProvider { } @Override - public void showPermissionDeniedTip(Context contexts, String[] permission) { - ToastUtils.showShort(DefaultPermissionResourceProvider.createPermissionDeniedTip(contexts, permission)); - } - - private static final class DefaultPermissionResourceProvider { - - static CharSequence createPermissionRationaleText(Context context, @NonNull String[] perms) { - String permissionText = createPermissionText(context, Arrays.asList(perms)); - return tintText(context.getString(R.string.Base_request_permission_rationale, permissionText), permissionText); - } - - static CharSequence createPermissionAskAgainText(Context context, @NonNull String[] permission) { - String permissionText = createPermissionText(context, Arrays.asList(permission)); - String appName = AppUtils.getAppName(); - String content = context.getString(R.string.Base_permission_denied_ask_again_rationale, appName, permissionText); - return tintText(content, permissionText); - } - - static CharSequence createPermissionDeniedTip(Context context, String[] permission) { - String permissionText = createPermissionText(context, Arrays.asList(permission)); - return tintText(context.getString( - R.string.Base_permission_denied, permissionText), permissionText); - } - - private static CharSequence tintText(String content, String perms) { - SpannableStringBuilder ssb = new SpannableStringBuilder(content); - int indexPerm = content.indexOf(perms); - ssb.setSpan(new ForegroundColorSpan(Color.parseColor(COLOR_STRING)), indexPerm, indexPerm + perms.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - return ssb; - } - - private static String createPermissionText(Context context, @NonNull List perms) { - List permList = Permission.transformText(context, perms); - String characterSeg = context.getString(R.string.Base_character_seg); - StringBuilder sb = new StringBuilder(); - int size = permList.size(); - for (int i = 0; i < size; i++) { - sb.append(permList.get(i)); - if (i < size - 1) { - sb.append(characterSeg); - } - } - return sb.toString(); - } + public void showPermissionDeniedTip(@NotNull Context contexts, @NotNull String[] permission) { + ToastUtils.showShort(createPermissionDeniedTip(contexts, permission, COLOR_STRING)); } } diff --git a/lib_base/src/main/java/com/android/base/permission/IPermissionUIProvider.java b/lib_base/src/main/java/com/android/base/permission/IPermissionUIProvider.java index a92ff3d..fc25fe6 100644 --- a/lib_base/src/main/java/com/android/base/permission/IPermissionUIProvider.java +++ b/lib_base/src/main/java/com/android/base/permission/IPermissionUIProvider.java @@ -2,6 +2,7 @@ package com.android.base.permission; import android.content.Context; import android.content.DialogInterface; +import android.support.annotation.NonNull; public interface IPermissionUIProvider { @@ -14,7 +15,7 @@ public interface IPermissionUIProvider { * @param onContinueListener 继续 * @param onCancelListener 取消 */ - void showPermissionRationaleDialog(Context context, final String[] permission, final DialogInterface.OnClickListener onContinueListener, DialogInterface.OnClickListener onCancelListener); + void showPermissionRationaleDialog(@NonNull Context context, @NonNull String[] permission, @NonNull DialogInterface.OnClickListener onContinueListener, @NonNull DialogInterface.OnClickListener onCancelListener); /** * 拒绝权限后,询问是否去设置界面授予应用权限 @@ -24,7 +25,7 @@ public interface IPermissionUIProvider { * @param onContinueListener 继续 * @param onCancelListener 取消 */ - void showAskAgainDialog(Context context, final String[] permission, DialogInterface.OnClickListener onContinueListener, DialogInterface.OnClickListener onCancelListener); + void showAskAgainDialog(@NonNull Context context, @NonNull String[] permission, @NonNull DialogInterface.OnClickListener onContinueListener, @NonNull DialogInterface.OnClickListener onCancelListener); /** * 权限被拒绝后,展示一个提示消息,比如 toast @@ -32,6 +33,6 @@ public interface IPermissionUIProvider { * @param contexts 上下文 * @param permission 被拒绝的权限 */ - void showPermissionDeniedTip(Context contexts, String[] permission); + void showPermissionDeniedTip(@NonNull Context contexts, @NonNull String[] permission); } diff --git a/lib_base/src/main/java/com/android/base/permission/PermissionUtils.java b/lib_base/src/main/java/com/android/base/permission/PermissionUtils.java new file mode 100644 index 0000000..aa7e020 --- /dev/null +++ b/lib_base/src/main/java/com/android/base/permission/PermissionUtils.java @@ -0,0 +1,61 @@ +package com.android.base.permission; + +import android.content.Context; +import android.support.annotation.ColorInt; +import android.support.annotation.NonNull; +import android.text.SpannableStringBuilder; +import android.text.Spanned; +import android.text.style.ForegroundColorSpan; + +import com.android.base.R; +import com.blankj.utilcode.util.AppUtils; + +import java.util.Arrays; +import java.util.List; + +/** + * @author Ztiany + * Email: ztiany3@gmail.com + * Date : 2019-08-22 14:17 + */ +public class PermissionUtils { + + public static String createPermissionText(Context context, @NonNull List perms) { + List permList = Permission.transformText(context, perms); + String characterSeg = context.getString(R.string.Base_character_seg); + StringBuilder sb = new StringBuilder(); + int size = permList.size(); + for (int i = 0; i < size; i++) { + sb.append(permList.get(i)); + if (i < size - 1) { + sb.append(characterSeg); + } + } + return sb.toString(); + } + + static CharSequence createPermissionRationaleText(Context context, @NonNull String[] perms, @ColorInt int color) { + String permissionText = createPermissionText(context, Arrays.asList(perms)); + return tintText(context.getString(R.string.Base_request_permission_rationale, permissionText), permissionText, color); + } + + static CharSequence createPermissionAskAgainText(Context context, @NonNull String[] permission, @ColorInt int color) { + String permissionText = createPermissionText(context, Arrays.asList(permission)); + String appName = AppUtils.getAppName(); + String content = context.getString(R.string.Base_permission_denied_ask_again_rationale, appName, permissionText); + return tintText(content, permissionText, color); + } + + static CharSequence createPermissionDeniedTip(Context context, String[] permission, @ColorInt int color) { + String permissionText = createPermissionText(context, Arrays.asList(permission)); + return tintText(context.getString(R.string.Base_permission_denied, permissionText), permissionText, color); + } + + private static CharSequence tintText(String content, String perms, @ColorInt int color) { + SpannableStringBuilder ssb = new SpannableStringBuilder(content); + int indexPerm = content.indexOf(perms); + ssb.setSpan(new ForegroundColorSpan(color), indexPerm, indexPerm + perms.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + return ssb; + } + +} diff --git a/lib_base/src/main/res/values/base_strings.xml b/lib_base/src/main/res/values/base_strings.xml index 3bd1c2a..98157f1 100644 --- a/lib_base/src/main/res/values/base_strings.xml +++ b/lib_base/src/main/res/values/base_strings.xml @@ -22,7 +22,7 @@ 通讯录 位置信息 麦克风 - 获取手机信息 + 手机信息 身体传感器 短信 存储空间