OSDN Git Service

Follow up on issue #8159072: Spinner widget should be RTL'ized
authorDianne Hackborn <hackbod@google.com>
Fri, 19 Apr 2013 21:09:37 +0000 (14:09 -0700)
committerDianne Hackborn <hackbod@google.com>
Fri, 19 Apr 2013 21:50:27 +0000 (14:50 -0700)
Only allow through changing direction configs for drawables.

Explicitly map layout direction values to an index in the
preload arrays.

Drawables that don't vary by configuration should go in to both
the rtl and ltr preloads.

Change-Id: Ib92dd11738082a795e02d1d4191adb54702d651c

core/java/android/content/res/Configuration.java
core/java/android/content/res/Resources.java

index 86d6ee7..905ae0d 100644 (file)
@@ -1219,12 +1219,12 @@ public final class Configuration implements Parcelable, Comparable<Configuration
      * Return the layout direction. Will be either {@link View#LAYOUT_DIRECTION_LTR} or
      * {@link View#LAYOUT_DIRECTION_RTL}.
      *
-     * @return the layout direction
+     * @return Returns {@link View#LAYOUT_DIRECTION_RTL} if the configuration
+     * is {@link #SCREENLAYOUT_LAYOUTDIR_RTL}, otherwise {@link View#LAYOUT_DIRECTION_LTR}.
      */
     public int getLayoutDirection() {
-        // We need to substract one here as the configuration values are using "0" as undefined thus
-        // having LRT set to "1" and RTL set to "2"
-        return ((screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK) >> SCREENLAYOUT_LAYOUTDIR_SHIFT) - 1;
+        return (screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK) == SCREENLAYOUT_LAYOUTDIR_RTL
+                ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR;
     }
 
     /**
index d8d5f2b..42f4faf 100644 (file)
@@ -1985,13 +1985,14 @@ public class Resources {
         }
     }
 
-    static private final int VARYING_CONFIGS = ActivityInfo.activityInfoConfigToNative(
-            ActivityInfo.CONFIG_LAYOUT_DIRECTION);
-
-    private boolean verifyPreloadConfig(int changingConfigurations, int resourceId, String name) {
-        // We dont want to preloadd a Drawable when there is both a LTR and RTL version of it
+    private boolean verifyPreloadConfig(int changingConfigurations, int allowVarying,
+            int resourceId, String name) {
+        // We allow preloading of resources even if they vary by font scale (which
+        // doesn't impact resource selection) or density (which we handle specially by
+        // simply turning off all preloading), as well as any other configs specified
+        // by the caller.
         if (((changingConfigurations&~(ActivityInfo.CONFIG_FONT_SCALE |
-                ActivityInfo.CONFIG_DENSITY)) & VARYING_CONFIGS) != 0) {
+                ActivityInfo.CONFIG_DENSITY)) & ~allowVarying) != 0) {
             String resName;
             try {
                 resName = getResourceName(resourceId);
@@ -2017,6 +2018,9 @@ public class Resources {
         return true;
     }
 
+    static private final int LAYOUT_DIR_CONFIG = ActivityInfo.activityInfoConfigToNative(
+            ActivityInfo.CONFIG_LAYOUT_DIRECTION);
+
     /*package*/ Drawable loadDrawable(TypedValue value, int id)
             throws NotFoundException {
 
@@ -2041,11 +2045,12 @@ public class Resources {
         if (dr != null) {
             return dr;
         }
-        final int layoutDirection = mConfiguration.getLayoutDirection();
-        Drawable.ConstantState cs = isColorDrawable
-                ? sPreloadedColorDrawables.get(key)
-                : (sPreloadedDensity == mConfiguration.densityDpi
-                        ? sPreloadedDrawables[layoutDirection].get(key) : null);
+        Drawable.ConstantState cs;
+        if (isColorDrawable) {
+            cs = sPreloadedColorDrawables.get(key);
+        } else {
+            cs = sPreloadedDrawables[mConfiguration.getLayoutDirection()].get(key);
+        }
         if (cs != null) {
             dr = cs.newDrawable(this);
         } else {
@@ -2119,12 +2124,26 @@ public class Resources {
             cs = dr.getConstantState();
             if (cs != null) {
                 if (mPreloading) {
-                    if (verifyPreloadConfig(cs.getChangingConfigurations(), value.resourceId,
-                            "drawable")) {
-                        if (isColorDrawable) {
+                    final int changingConfigs = cs.getChangingConfigurations();
+                    if (isColorDrawable) {
+                        if (verifyPreloadConfig(changingConfigs, 0, value.resourceId,
+                                "drawable")) {
                             sPreloadedColorDrawables.put(key, cs);
-                        } else {
-                            sPreloadedDrawables[layoutDirection].put(key, cs);
+                        }
+                    } else {
+                        if (verifyPreloadConfig(changingConfigs,
+                                LAYOUT_DIR_CONFIG, value.resourceId, "drawable")) {
+                            if ((changingConfigs&LAYOUT_DIR_CONFIG) == 0) {
+                                // If this resource does not vary based on layout direction,
+                                // we can put it in all of the preload maps.
+                                sPreloadedDrawables[0].put(key, cs);
+                                sPreloadedDrawables[1].put(key, cs);
+                            } else {
+                                // Otherwise, only in the layout dir we loaded it for.
+                                final LongSparseArray<Drawable.ConstantState> preloads
+                                        = sPreloadedDrawables[mConfiguration.getLayoutDirection()];
+                                preloads.put(key, cs);
+                            }
                         }
                     }
                 } else {
@@ -2190,7 +2209,8 @@ public class Resources {
 
             csl = ColorStateList.valueOf(value.data);
             if (mPreloading) {
-                if (verifyPreloadConfig(value.changingConfigurations, value.resourceId, "color")) {
+                if (verifyPreloadConfig(value.changingConfigurations, 0, value.resourceId,
+                        "color")) {
                     sPreloadedColorStateLists.put(key, csl);
                 }
             }
@@ -2239,7 +2259,8 @@ public class Resources {
 
         if (csl != null) {
             if (mPreloading) {
-                if (verifyPreloadConfig(value.changingConfigurations, value.resourceId, "color")) {
+                if (verifyPreloadConfig(value.changingConfigurations, 0, value.resourceId,
+                        "color")) {
                     sPreloadedColorStateLists.put(key, csl);
                 }
             } else {