1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 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 * Collection of utilities for accessibility service. 36 * 37 * @hide 38 */ 39 public final class AccessibilityUtils { AccessibilityUtils()40 private AccessibilityUtils() {} 41 42 // Used for html description of accessibility service. The <img> src tag must follow the 43 // prefix rule. e.g. <img src="R.drawable.fileName"/> 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 * Gets the filtered html string for 51 * {@link android.accessibilityservice.AccessibilityServiceInfo} and 52 * {@link android.accessibilityservice.AccessibilityShortcutInfo}. It filters 53 * the <img> tag which do not meet the custom specification and the <a> tag. 54 * 55 * @param text the target text is html format. 56 * @return the filtered html string. 57 */ getFilteredHtmlText(@onNull String text)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 * Loads the animated image for 78 * {@link android.accessibilityservice.AccessibilityServiceInfo} and 79 * {@link android.accessibilityservice.AccessibilityShortcutInfo}. It checks the resource 80 * whether to exceed the screen size. 81 * 82 * @param context the current context. 83 * @param applicationInfo the current application. 84 * @param resId the animated image resource id. 85 * @return the animated image which is safe. 86 */ 87 @Nullable loadSafeAnimatedImage(@onNull Context context, @NonNull ApplicationInfo applicationInfo, @StringRes int resId)88 public static Drawable loadSafeAnimatedImage(@NonNull Context context, 89 @NonNull ApplicationInfo applicationInfo, @StringRes int resId) { 90 if (resId == /* invalid */ 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 * Gets the width of the screen. 114 * 115 * @param context the current context. 116 * @return the width of the screen in term of pixels. 117 */ getScreenWidthPixels(@onNull Context context)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 * Gets the height of the screen. 128 * 129 * @param context the current context. 130 * @return the height of the screen in term of pixels. 131 */ getScreenHeightPixels(@onNull Context context)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 } 140