update permission util

androidx
Zhanty 5 years ago
parent 8bb6b52250
commit 19a744db26
  1. 75
      lib_base/src/main/java/com/android/base/permission/DefaultPermissionUIProvider.java
  2. 7
      lib_base/src/main/java/com/android/base/permission/IPermissionUIProvider.java
  3. 61
      lib_base/src/main/java/com/android/base/permission/PermissionUtils.java
  4. 2
      lib_base/src/main/res/values/base_strings.xml

@ -3,29 +3,28 @@ package com.android.base.permission;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.graphics.Color; import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog; 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.android.base.R;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ToastUtils; import com.blankj.utilcode.util.ToastUtils;
import java.util.Arrays; import org.jetbrains.annotations.NotNull;
import java.util.List;
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 { class DefaultPermissionUIProvider implements IPermissionUIProvider {
private static final String COLOR_STRING = "#FF4081"; private static final int COLOR_STRING = Color.parseColor("#FF4081");
@Override @Override
public void showPermissionRationaleDialog(Context context, final String[] permission, public void showPermissionRationaleDialog(@NotNull Context context, @NotNull final String[] permission,
final DialogInterface.OnClickListener onContinueListener, DialogInterface.OnClickListener onCancelListener) { @NotNull final DialogInterface.OnClickListener onContinueListener, @NotNull DialogInterface.OnClickListener onCancelListener) {
AlertDialog dialog = new AlertDialog.Builder(context) AlertDialog dialog = new AlertDialog.Builder(context)
.setMessage(DefaultPermissionResourceProvider.createPermissionRationaleText(context, permission)) .setMessage(createPermissionRationaleText(context, permission, COLOR_STRING))
.setCancelable(false) .setCancelable(false)
.setPositiveButton(R.string.Base_Confirm, onContinueListener) .setPositiveButton(R.string.Base_Confirm, onContinueListener)
.setNegativeButton(R.string.Base_Cancel, onCancelListener) .setNegativeButton(R.string.Base_Cancel, onCancelListener)
@ -35,12 +34,12 @@ class DefaultPermissionUIProvider implements IPermissionUIProvider {
} }
@Override @Override
public void showAskAgainDialog(Context context, final String[] permission, public void showAskAgainDialog(@NotNull Context context, @NotNull final String[] permission,
DialogInterface.OnClickListener onToSetPermissionListener, @NotNull DialogInterface.OnClickListener onToSetPermissionListener,
DialogInterface.OnClickListener onCancelListener) { @NotNull DialogInterface.OnClickListener onCancelListener) {
AlertDialog dialog = new AlertDialog.Builder(context) AlertDialog dialog = new AlertDialog.Builder(context)
.setMessage(DefaultPermissionResourceProvider.createPermissionAskAgainText(context, permission)) .setMessage(createPermissionAskAgainText(context, permission, COLOR_STRING))
.setCancelable(false) .setCancelable(false)
.setPositiveButton(R.string.Base_to_set_permission, onToSetPermissionListener) .setPositiveButton(R.string.Base_to_set_permission, onToSetPermissionListener)
.setNegativeButton(R.string.Base_Cancel, onCancelListener) .setNegativeButton(R.string.Base_Cancel, onCancelListener)
@ -50,50 +49,8 @@ class DefaultPermissionUIProvider implements IPermissionUIProvider {
} }
@Override @Override
public void showPermissionDeniedTip(Context contexts, String[] permission) { public void showPermissionDeniedTip(@NotNull Context contexts, @NotNull String[] permission) {
ToastUtils.showShort(DefaultPermissionResourceProvider.createPermissionDeniedTip(contexts, permission)); ToastUtils.showShort(createPermissionDeniedTip(contexts, permission, COLOR_STRING));
}
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<String> perms) {
List<String> 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();
}
} }
} }

@ -2,6 +2,7 @@ package com.android.base.permission;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.support.annotation.NonNull;
public interface IPermissionUIProvider { public interface IPermissionUIProvider {
@ -14,7 +15,7 @@ public interface IPermissionUIProvider {
* @param onContinueListener 继续 * @param onContinueListener 继续
* @param onCancelListener 取消 * @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 onContinueListener 继续
* @param onCancelListener 取消 * @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 * 权限被拒绝后展示一个提示消息比如 toast
@ -32,6 +33,6 @@ public interface IPermissionUIProvider {
* @param contexts 上下文 * @param contexts 上下文
* @param permission 被拒绝的权限 * @param permission 被拒绝的权限
*/ */
void showPermissionDeniedTip(Context contexts, String[] permission); void showPermissionDeniedTip(@NonNull Context contexts, @NonNull String[] permission);
} }

@ -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<String> perms) {
List<String> 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;
}
}

@ -22,7 +22,7 @@
<string name="Base_permission_name_contacts">通讯录</string> <string name="Base_permission_name_contacts">通讯录</string>
<string name="Base_permission_name_location">位置信息</string> <string name="Base_permission_name_location">位置信息</string>
<string name="Base_permission_name_microphone">麦克风</string> <string name="Base_permission_name_microphone">麦克风</string>
<string name="Base_permission_name_phone">获取手机信息</string> <string name="Base_permission_name_phone">手机信息</string>
<string name="Base_permission_name_sensors">身体传感器</string> <string name="Base_permission_name_sensors">身体传感器</string>
<string name="Base_permission_name_sms">短信</string> <string name="Base_permission_name_sms">短信</string>
<string name="Base_permission_name_storage">存储空间</string> <string name="Base_permission_name_storage">存储空间</string>

Loading…
Cancel
Save