Merge "Stop controller from steal preference clicks" into rvc-dev
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 85b25e4..dee8176 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -590,6 +590,7 @@
 
     <style name="TextAppearance.Tab" parent="@android:style/TextAppearance.DeviceDefault.Medium">
         <item name="android:textAllCaps">false</item>
+        <item name="android:textSize">14sp</item>
         <item name="android:fontFamily">@*android:string/config_headlineFontFamilyMedium</item>
     </style>
 
diff --git a/src/com/android/settings/accessibility/AccessibilityUtil.java b/src/com/android/settings/accessibility/AccessibilityUtil.java
index 8da6fbb..76fb3ff 100644
--- a/src/com/android/settings/accessibility/AccessibilityUtil.java
+++ b/src/com/android/settings/accessibility/AccessibilityUtil.java
@@ -21,9 +21,11 @@
 import android.accessibilityservice.AccessibilityServiceInfo;
 import android.content.ComponentName;
 import android.content.Context;
+import android.content.res.Resources;
 import android.os.Build;
 import android.provider.Settings;
 import android.text.TextUtils;
+import android.util.TypedValue;
 import android.view.accessibility.AccessibilityManager;
 
 import androidx.annotation.IntDef;
@@ -350,4 +352,32 @@
                         "Unsupported userShortcutType " + shortcutType);
         }
     }
+
+    /**
+     * Gets the width of the screen.
+     *
+     * @param context the current context.
+     * @return the width of the screen in terms of pixels.
+     */
+    public static int getScreenWidthPixels(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 terms of pixels.
+     */
+    public static int getScreenHeightPixels(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()));
+    }
 }
diff --git a/src/com/android/settings/accessibility/AnimatedImagePreference.java b/src/com/android/settings/accessibility/AnimatedImagePreference.java
index 1609a3c..61e439f 100644
--- a/src/com/android/settings/accessibility/AnimatedImagePreference.java
+++ b/src/com/android/settings/accessibility/AnimatedImagePreference.java
@@ -34,6 +34,7 @@
 public class AnimatedImagePreference extends Preference {
 
     private Uri mImageUri;
+    private int mMaxHeight = -1;
 
     AnimatedImagePreference(Context context) {
         super(context);
@@ -45,20 +46,26 @@
         super.onBindViewHolder(holder);
 
         final ImageView imageView = holder.itemView.findViewById(R.id.animated_img);
-        if (imageView != null && mImageUri != null) {
+        if (imageView == null) {
+            return;
+        }
+
+        if (mImageUri != null) {
             imageView.setImageURI(mImageUri);
 
             final Drawable drawable = imageView.getDrawable();
-            if (drawable != null) {
-                if (drawable instanceof AnimatedImageDrawable) {
-                    ((AnimatedImageDrawable) drawable).start();
-                }
+            if (drawable instanceof AnimatedImageDrawable) {
+                ((AnimatedImageDrawable) drawable).start();
             }
         }
+
+        if (mMaxHeight > -1) {
+            imageView.setMaxWidth(mMaxHeight);
+        }
     }
 
     /**
-     * Set image uri to display image in {@link ImageView}
+     * Sets image uri to display image in {@link ImageView}
      *
      * @param imageUri the Uri of an image
      */
@@ -68,4 +75,16 @@
             notifyChanged();
         }
     }
+
+    /**
+     * Sets the maximum height of the view.
+     *
+     * @param maxHeight the maximum height of ImageView in terms of pixels.
+     */
+    public void setMaxHeight(int maxHeight) {
+        if (maxHeight != mMaxHeight) {
+            mMaxHeight = maxHeight;
+            notifyChanged();
+        }
+    }
 }
diff --git a/src/com/android/settings/accessibility/HtmlTextPreference.java b/src/com/android/settings/accessibility/HtmlTextPreference.java
index fcf4725..4c528a3 100644
--- a/src/com/android/settings/accessibility/HtmlTextPreference.java
+++ b/src/com/android/settings/accessibility/HtmlTextPreference.java
@@ -23,9 +23,6 @@
 
 import androidx.preference.PreferenceViewHolder;
 
-import java.util.List;
-import java.util.regex.Pattern;
-
 /**
  * A custom {@link android.widget.TextView} preference that shows html text with a custom tag
  * filter.
@@ -35,7 +32,6 @@
     private int mFlag = Html.FROM_HTML_MODE_COMPACT;
     private Html.ImageGetter mImageGetter;
     private Html.TagHandler mTagHandler;
-    private List<String> mUnsupportedTagList;
 
     HtmlTextPreference(Context context) {
         super(context);
@@ -47,8 +43,8 @@
 
         final TextView summaryView = holder.itemView.findViewById(android.R.id.summary);
         if (summaryView != null && !TextUtils.isEmpty(getSummary())) {
-            final String filteredText = getFilteredText(getSummary().toString());
-            summaryView.setText(Html.fromHtml(filteredText, mFlag, mImageGetter, mTagHandler));
+            summaryView.setText(
+                    Html.fromHtml(getSummary().toString(), mFlag, mImageGetter, mTagHandler));
         }
     }
 
@@ -87,39 +83,4 @@
             notifyChanged();
         }
     }
-
-    /**
-     * Sets unsupported tag list, the text will be filtered though this list in advanced.
-     *
-     * @param unsupportedTagList the list of unsupported tags
-     */
-    public void setUnsupportedTagList(List<String> unsupportedTagList) {
-        if (unsupportedTagList != null && !unsupportedTagList.equals(mUnsupportedTagList)) {
-            mUnsupportedTagList = unsupportedTagList;
-            notifyChanged();
-        }
-    }
-
-    private String getFilteredText(String text) {
-        if (mUnsupportedTagList == null) {
-            return text;
-        }
-
-        int i = 1;
-        for (String tag : mUnsupportedTagList) {
-            if (!TextUtils.isEmpty(text)) {
-                final String index = String.valueOf(i++);
-                final String targetStart1 = "(?i)<" + tag + " ";
-                final String targetStart2 = "(?i)<" + tag + ">";
-                final String replacementStart1 = "<unsupportedtag" + index + " ";
-                final String replacementStart2 = "<unsupportedtag" + index + ">";
-                final String targetEnd = "(?i)</" + tag + ">";
-                final String replacementEnd = "</unsupportedtag" + index + ">";
-                text = Pattern.compile(targetStart1).matcher(text).replaceAll(replacementStart1);
-                text = Pattern.compile(targetStart2).matcher(text).replaceAll(replacementStart2);
-                text = Pattern.compile(targetEnd).matcher(text).replaceAll(replacementEnd);
-            }
-        }
-        return text;
-    }
 }
diff --git a/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java b/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java
index 19a0f2b..838a758 100644
--- a/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java
+++ b/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java
@@ -16,6 +16,9 @@
 
 package com.android.settings.accessibility;
 
+import static com.android.settings.accessibility.AccessibilityUtil.getScreenHeightPixels;
+import static com.android.settings.accessibility.AccessibilityUtil.getScreenWidthPixels;
+
 import android.app.Dialog;
 import android.app.settings.SettingsEnums;
 import android.content.ComponentName;
@@ -80,7 +83,6 @@
     protected CharSequence mPackageName;
     protected Uri mImageUri;
     protected CharSequence mHtmlDescription;
-    private static final String ANCHOR_TAG = "a";
     private static final String DRAWABLE_FOLDER = "drawable";
     protected static final String KEY_USE_SERVICE_PREFERENCE = "use_service";
     protected static final String KEY_GENERAL_CATEGORY = "general_categories";
@@ -94,9 +96,8 @@
     private CheckBox mSoftwareTypeCheckBox;
     private CheckBox mHardwareTypeCheckBox;
 
-    // For html description of accessibility service, third party developer must follow the rule,
-    // such as <img src="R.drawable.fileName"/>, a11y settings will get third party resources
-    // by this.
+    // For html description of accessibility service, must follow the rule, such as
+    // <img src="R.drawable.fileName"/>, a11y settings will get the resources successfully.
     private static final String IMG_PREFIX = "R.drawable.";
 
     private ImageView mImageGetterCacheView;
@@ -147,10 +148,12 @@
 
         PreferenceScreen preferenceScreen = getPreferenceScreen();
         if (mImageUri != null) {
+            final int screenHalfHeight = getScreenHeightPixels(getPrefContext()) / /* half */ 2;
             final AnimatedImagePreference animatedImagePreference = new AnimatedImagePreference(
                     getPrefContext());
             animatedImagePreference.setImageUri(mImageUri);
             animatedImagePreference.setSelectable(false);
+            animatedImagePreference.setMaxHeight(screenHalfHeight);
             preferenceScreen.addPreference(animatedImagePreference);
         }
 
@@ -195,14 +198,9 @@
             introductionCategory.setTitle(title);
             preferenceScreen.addPreference(introductionCategory);
 
-            // For accessibility service, avoid malicious links made by third party developer.
-            final List<String> unsupportedTagList = new ArrayList<>();
-            unsupportedTagList.add(ANCHOR_TAG);
-
             final HtmlTextPreference htmlTextPreference = new HtmlTextPreference(getPrefContext());
             htmlTextPreference.setSummary(mHtmlDescription);
             htmlTextPreference.setImageGetter(mImageGetter);
-            htmlTextPreference.setUnsupportedTagList(unsupportedTagList);
             htmlTextPreference.setSelectable(false);
             introductionCategory.addPreference(htmlTextPreference);
         }
@@ -383,14 +381,24 @@
         mImageGetterCacheView.setAdjustViewBounds(true);
         mImageGetterCacheView.setImageURI(imageUri);
 
-        final Drawable drawable = mImageGetterCacheView.getDrawable().mutate();
-        if (drawable != null) {
-            drawable.setBounds(/* left= */0, /* top= */0, drawable.getIntrinsicWidth(),
-                    drawable.getIntrinsicHeight());
+        if (mImageGetterCacheView.getDrawable() == null) {
+            return null;
         }
 
+        final Drawable drawable =
+                mImageGetterCacheView.getDrawable().mutate().getConstantState().newDrawable();
         mImageGetterCacheView.setImageURI(null);
-        mImageGetterCacheView.setImageDrawable(null);
+        final int imageWidth = drawable.getIntrinsicWidth();
+        final int imageHeight = drawable.getIntrinsicHeight();
+        final int screenHalfHeight = getScreenHeightPixels(getPrefContext()) / /* half */ 2;
+        if ((imageWidth > getScreenWidthPixels(getPrefContext()))
+                || (imageHeight > screenHalfHeight)) {
+            return null;
+        }
+
+        drawable.setBounds(/* left= */0, /* top= */0, drawable.getIntrinsicWidth(),
+                drawable.getIntrinsicHeight());
+
         return drawable;
     }
 
diff --git a/tests/robotests/src/com/android/settings/accessibility/HtmlTextPreferenceTest.java b/tests/robotests/src/com/android/settings/accessibility/HtmlTextPreferenceTest.java
index 5ac5bad..63c7da1 100644
--- a/tests/robotests/src/com/android/settings/accessibility/HtmlTextPreferenceTest.java
+++ b/tests/robotests/src/com/android/settings/accessibility/HtmlTextPreferenceTest.java
@@ -23,7 +23,6 @@
 import android.text.Html;
 import android.view.LayoutInflater;
 import android.view.View;
-import android.widget.TextView;
 
 import androidx.preference.PreferenceViewHolder;
 
@@ -36,9 +35,6 @@
 import org.robolectric.RuntimeEnvironment;
 import org.xml.sax.XMLReader;
 
-import java.util.ArrayList;
-import java.util.List;
-
 /** Tests for {@link HtmlTextPreference} */
 @RunWith(RobolectricTestRunner.class)
 public final class HtmlTextPreferenceTest {
@@ -65,21 +61,13 @@
     }
 
     @Test
-    public void testUnsupportedTagList_keepRealContentWithoutTag() {
-        final List<String> testUnsupportedTagList = new ArrayList<>();
-        testUnsupportedTagList.add("testTag");
+    public void testTagHandler() {
         final String testStr = "<testTag>Real description</testTag>";
-        final String expectedStr = "Real description";
-        final String expectedTag = "unsupportedtag1";
 
-        mHtmlTextPreference.setUnsupportedTagList(testUnsupportedTagList);
         mHtmlTextPreference.setSummary(testStr);
         mHtmlTextPreference.setTagHandler(mTagHandler);
         mHtmlTextPreference.onBindViewHolder(mPreferenceViewHolder);
 
-        final TextView summaryView = mPreferenceViewHolder.itemView.findViewById(
-                android.R.id.summary);
-        assertThat(summaryView.getText().toString()).isEqualTo(expectedStr);
-        assertThat(mHandledTag).isEqualTo(expectedTag);
+        assertThat(mHandledTag).isEqualTo("testTag");
     }
 }