Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 18 additions & 99 deletions android/accessibilityservice/util/AccessibilityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@

package android.accessibilityservice.util;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.StringRes;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
dont import android.annotation.NonNull;
dont import android.annotation.Nullable;
dont import android.annotation.StringRes;
dont import android.content.Context;
dont import android.content.pm.ApplicationInfo;
dont import android.content.pm.PackageManager;
dont import android.content.res.Resources;
dont import android.graphics.drawable.Drawable;
dont import android.util.TypedValue;

dont import java.util.ArrayList;
dont import java.util.Collections;
dont import java.util.List;
dont import java.util.regex.Pattern;

/**
* Collection of utilities for accessibility service.
Expand All @@ -41,9 +41,9 @@ private AccessibilityUtils() {}

// Used for html description of accessibility service. The <img> src tag must follow the
// prefix rule. e.g. <img src="R.drawable.fileName"/>
private static final String IMG_PREFIX = "R.drawable.";
private static final String ANCHOR_TAG = "a";
private static final List<String> UNSUPPORTED_TAG_LIST = new ArrayList<>(
public static final String IMG_PREFIX = "R.drawable.";
public static final String ANCHOR_TAG = "a";
public static final List<String> UNSUPPORTED_TAG_LIST = new ArrayList<>(
Collections.singletonList(ANCHOR_TAG));

/**
Expand All @@ -55,85 +55,4 @@ private AccessibilityUtils() {}
* @param text the target text is html format.
* @return the filtered html string.
*/
public static @NonNull String getFilteredHtmlText(@NonNull String text) {
final String replacementStart = "<invalidtag ";
final String replacementEnd = "</invalidtag>";

for (String tag : UNSUPPORTED_TAG_LIST) {
final String regexStart = "(?i)<" + tag + "(\\s+|>)";
final String regexEnd = "(?i)</" + tag + "\\s*>";
text = Pattern.compile(regexStart).matcher(text).replaceAll(replacementStart);
text = Pattern.compile(regexEnd).matcher(text).replaceAll(replacementEnd);
}

final String regexInvalidImgTag = "(?i)<img\\s+(?!src\\s*=\\s*\"(?-i)" + IMG_PREFIX + ")";
text = Pattern.compile(regexInvalidImgTag).matcher(text).replaceAll(
replacementStart);

return text;
}

/**
* Loads the animated image for
* {@link android.accessibilityservice.AccessibilityServiceInfo} and
* {@link android.accessibilityservice.AccessibilityShortcutInfo}. It checks the resource
* whether to exceed the screen size.
*
* @param context the current context.
* @param applicationInfo the current application.
* @param resId the animated image resource id.
* @return the animated image which is safe.
*/
@Nullable
public static Drawable loadSafeAnimatedImage(@NonNull Context context,
@NonNull ApplicationInfo applicationInfo, @StringRes int resId) {
if (resId == /* invalid */ 0) {
return null;
}

final PackageManager packageManager = context.getPackageManager();
final String packageName = applicationInfo.packageName;
final Drawable bannerDrawable = packageManager.getDrawable(packageName, resId,
applicationInfo);
if (bannerDrawable == null) {
return null;
}

final boolean isImageWidthOverScreenLength =
bannerDrawable.getIntrinsicWidth() > getScreenWidthPixels(context);
final boolean isImageHeightOverScreenLength =
bannerDrawable.getIntrinsicHeight() > getScreenHeightPixels(context);

return (isImageWidthOverScreenLength || isImageHeightOverScreenLength)
? null
: bannerDrawable;
}

/**
* Gets the width of the screen.
*
* @param context the current context.
* @return the width of the screen in term of pixels.
*/
private static int getScreenWidthPixels(@NonNull Context context) {
final Resources resources = context.getResources();
final int screenWidthDp = resources.getConfiguration().screenWidthDp;

return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, screenWidthDp,
resources.getDisplayMetrics()));
}

/**
* Gets the height of the screen.
*
* @param context the current context.
* @return the height of the screen in term of pixels.
*/
private static int getScreenHeightPixels(@NonNull Context context) {
final Resources resources = context.getResources();
final int screenHeightDp = resources.getConfiguration().screenHeightDp;

return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, screenHeightDp,
resources.getDisplayMetrics()));
}
}