| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.accessibilityservice.util; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.StringRes; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.pm.ApplicationInfo; |
| 24 |
import android.content.pm.PackageManager; |
| 25 |
import android.content.res.Resources; |
| 26 |
import android.graphics.drawable.Drawable; |
| 27 |
import android.util.TypedValue; |
| 28 |
|
| 29 |
import java.util.ArrayList; |
| 30 |
import java.util.Collections; |
| 31 |
import java.util.List; |
| 32 |
import java.util.regex.Pattern; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public final class AccessibilityUtils { |
| 40 |
private AccessibilityUtils() {} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private static final String IMG_PREFIX = "R.drawable."; |
| 45 |
private static final String ANCHOR_TAG = "a"; |
| 46 |
private static final List<String> UNSUPPORTED_TAG_LIST = new ArrayList<>( |
| 47 |
Collections.singletonList(ANCHOR_TAG)); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public static @NonNull String getFilteredHtmlText(@NonNull String text) { |
| 59 |
final String replacementStart = "<invalidtag "; |
| 60 |
final String replacementEnd = "</invalidtag>"; |
| 61 |
|
| 62 |
for (String tag : UNSUPPORTED_TAG_LIST) { |
| 63 |
final String regexStart = "(?i)<" + tag + "(\\s+|>)"; |
| 64 |
final String regexEnd = "(?i)</" + tag + "\\s*>"; |
| 65 |
text = Pattern.compile(regexStart).matcher(text).replaceAll(replacementStart); |
| 66 |
text = Pattern.compile(regexEnd).matcher(text).replaceAll(replacementEnd); |
| 67 |
} |
| 68 |
|
| 69 |
final String regexInvalidImgTag = "(?i)<img\\s+(?!src\\s*=\\s*\"(?-i)" + IMG_PREFIX + ")"; |
| 70 |
text = Pattern.compile(regexInvalidImgTag).matcher(text).replaceAll( |
| 71 |
replacementStart); |
| 72 |
|
| 73 |
return text; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
@Nullable |
| 88 |
public static Drawable loadSafeAnimatedImage(@NonNull Context context, |
| 89 |
@NonNull ApplicationInfo applicationInfo, @StringRes int resId) { |
| 90 |
if (resId == 0) { |
| 91 |
return null; |
| 92 |
} |
| 93 |
|
| 94 |
final PackageManager packageManager = context.getPackageManager(); |
| 95 |
final String packageName = applicationInfo.packageName; |
| 96 |
final Drawable bannerDrawable = packageManager.getDrawable(packageName, resId, |
| 97 |
applicationInfo); |
| 98 |
if (bannerDrawable == null) { |
| 99 |
return null; |
| 100 |
} |
| 101 |
|
| 102 |
final boolean isImageWidthOverScreenLength = |
| 103 |
bannerDrawable.getIntrinsicWidth() > getScreenWidthPixels(context); |
| 104 |
final boolean isImageHeightOverScreenLength = |
| 105 |
bannerDrawable.getIntrinsicHeight() > getScreenHeightPixels(context); |
| 106 |
|
| 107 |
return (isImageWidthOverScreenLength || isImageHeightOverScreenLength) |
| 108 |
? null |
| 109 |
: bannerDrawable; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
private static int getScreenWidthPixels(@NonNull Context context) { |
| 119 |
final Resources resources = context.getResources(); |
| 120 |
final int screenWidthDp = resources.getConfiguration().screenWidthDp; |
| 121 |
|
| 122 |
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, screenWidthDp, |
| 123 |
resources.getDisplayMetrics())); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
private static int getScreenHeightPixels(@NonNull Context context) { |
| 133 |
final Resources resources = context.getResources(); |
| 134 |
final int screenHeightDp = resources.getConfiguration().screenHeightDp; |
| 135 |
|
| 136 |
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, screenHeightDp, |
| 137 |
resources.getDisplayMetrics())); |
| 138 |
} |
| 139 |
} |