Skip to content

Commit

Permalink
Add workaround for TextView.setCompoundDrawablesRelative() on JB MR1.
Browse files Browse the repository at this point in the history
Due to a bug, TextView.setCompoundDrawablesRelative() is a no-op on JB
MR1 if the text view has ever been measured. This is fixed in JB MR2
onwards, but causes some ugly bugs in Chrome on JB MR1: e.g. the
favicons often don't appear on the bookmarks page.

BUG=361709
NOTRY=true

Review URL: https://codereview.chromium.org/232083005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@263137 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
newt@chromium.org committed Apr 11, 2014
1 parent b582760 commit a51ec8d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions base/android/java/src/org/chromium/base/ApiCompatibilityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.RemoteViews;
import android.widget.TextView;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* Utility class to use new APIs that were added after ICS (API level 14).
*/
public class ApiCompatibilityUtils {

private static final String TAG = "ApiCompatibilityUtils";

private ApiCompatibilityUtils() {
}

Expand Down Expand Up @@ -172,6 +178,24 @@ public static int getPaddingEnd(View view) {
public static void setCompoundDrawablesRelative(TextView textView, Drawable start, Drawable top,
Drawable end, Drawable bottom) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
// On JB MR1, setCompoundDrawablesRelative() is a no-op if the view has ever been
// measured: due to a bug, it doesn't call resetResolvedDrawables(). Unfortunately,
// resetResolvedDrawables() is a hidden method so we can't call it directly. Instead,
// we use reflection.
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
try {
Method resetResolvedDrawables = TextView.class.getDeclaredMethod(
"resetResolvedDrawables");
resetResolvedDrawables.setAccessible(true);
resetResolvedDrawables.invoke(textView);
} catch (NoSuchMethodException e) {
Log.w(TAG, e);
} catch (IllegalAccessException e) {
Log.w(TAG, e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
textView.setCompoundDrawablesRelative(start, top, bottom, end);
} else {
textView.setCompoundDrawables(start, top, bottom, end);
Expand Down

0 comments on commit a51ec8d

Please sign in to comment.