OSDN Git Service

widgets scaling fix. Use container's compatibility info and display metrics when...
authorMitsuru Oshima <oshima@google.com>
Fri, 3 Jul 2009 03:06:08 +0000 (20:06 -0700)
committerMitsuru Oshima <oshima@google.com>
Tue, 7 Jul 2009 04:13:39 +0000 (21:13 -0700)
core/java/android/app/ApplicationContext.java
core/java/android/content/res/CompatibilityInfo.java
core/java/android/content/res/Resources.java

index 61cd0fe..38ea686 100644 (file)
@@ -88,8 +88,10 @@ import android.os.FileUtils.FileStatus;
 import android.telephony.TelephonyManager;
 import android.text.ClipboardManager;
 import android.util.AndroidRuntimeException;
+import android.util.DisplayMetrics;
 import android.util.Log;
 import android.view.ContextThemeWrapper;
+import android.view.Display;
 import android.view.LayoutInflater;
 import android.view.WindowManagerImpl;
 import android.view.accessibility.AccessibilityManager;
@@ -149,6 +151,7 @@ class ReceiverRestrictedContext extends ContextWrapper {
  */
 class ApplicationContext extends Context {
     private final static String TAG = "ApplicationContext";
+    private final static boolean DEBUG = false;
     private final static boolean DEBUG_ICONS = false;
 
     private static final Object sSync = new Object();
@@ -1238,7 +1241,7 @@ class ApplicationContext extends Context {
     @Override
     public int checkUriPermission(Uri uri, String readPermission,
             String writePermission, int pid, int uid, int modeFlags) {
-        if (false) {
+        if (DEBUG) {
             Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
                     + readPermission + " writePermission=" + writePermission
                     + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
@@ -1340,6 +1343,19 @@ class ApplicationContext extends Context {
             c.mRestricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
             c.init(pi, null, mMainThread);
             if (c.mResources != null) {
+                Resources newRes = c.mResources;
+                if (mResources.getCompatibilityInfo().applicationScale !=
+                    newRes.getCompatibilityInfo().applicationScale) {
+                    DisplayMetrics dm = mMainThread.getDisplayMetricsLocked(false);
+                    c.mResources = new Resources(newRes.getAssets(), dm,
+                            newRes.getConfiguration(),
+                            mResources.getCompatibilityInfo().copy());
+                    if (DEBUG) {
+                        Log.d(TAG, "loaded context has different scaling. Using container's" +
+                                " compatiblity info:" + mResources.getDisplayMetrics());
+                    }
+
+                }
                 return c;
             }
         }
@@ -1459,7 +1475,7 @@ class ApplicationContext extends Context {
         if ((mode&MODE_WORLD_WRITEABLE) != 0) {
             perms |= FileUtils.S_IWOTH;
         }
-        if (false) {
+        if (DEBUG) {
             Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
                   + ", perms=0x" + Integer.toHexString(perms));
         }
index 4e6fe07..dfe304d 100644 (file)
@@ -159,15 +159,33 @@ public class CompatibilityInfo {
         }
     }
 
+    private CompatibilityInfo(int appFlags, int compFlags, float scale, float invertedScale) {
+        this.appFlags = appFlags;
+        mCompatibilityFlags = compFlags;
+        applicationScale = scale;
+        applicationInvertedScale = invertedScale;
+    }
+
     private CompatibilityInfo() {
-        appFlags = ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS
+        this(ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS
                 | ApplicationInfo.FLAG_SUPPORTS_NORMAL_SCREENS
-                | ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS;
-        applicationScale = applicationInvertedScale = 1.0f;
-        mCompatibilityFlags = EXPANDABLE | CONFIGURED_EXPANDABLE;
+                | ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS,
+                EXPANDABLE | CONFIGURED_EXPANDABLE,
+                1.0f,
+                1.0f);
     }
 
     /**
+     * Returns the copy of this instance.
+     */
+    public CompatibilityInfo copy() {
+        CompatibilityInfo info = new CompatibilityInfo(appFlags, mCompatibilityFlags,
+                applicationScale, applicationInvertedScale);
+        info.setVisibleRect(mXOffset, mWidth, mHeight);
+        return info;
+    }
+    /**
      * Sets the application's visible rect in compatibility mode.
      * @param xOffset the application's x offset that is added to center the content.
      * @param widthPixels the application's width in real pixels on the screen.
@@ -470,4 +488,4 @@ public class CompatibilityInfo {
             return mVisibleInsets;
         }
     }
-}
\ No newline at end of file
+}
index d7512bb..49ad656 100644 (file)
@@ -129,7 +129,7 @@ public class Resources {
      */
     public Resources(AssetManager assets, DisplayMetrics metrics,
             Configuration config) {
-        this(assets, metrics, config, null);
+        this(assets, metrics, config, (ApplicationInfo) null);
     }
 
     /**
@@ -166,6 +166,26 @@ public class Resources {
     }
 
     /**
+     * Creates a new resources that uses the given compatibility info. Used to create
+     * a context for widgets using the container's compatibility info.
+     * {@see ApplicationContext#createPackageCotnext}.
+     * @hide
+     */
+    public Resources(AssetManager assets, DisplayMetrics metrics,
+            Configuration config, CompatibilityInfo info) {
+        mAssets = assets;
+        mMetrics.setToDefaults();
+        mCompatibilityInfo = info;
+        updateConfiguration(config, metrics);
+        assets.ensureStringBlocks();
+        if (mCompatibilityInfo.isScalingRequired()) {
+            mPreloadedDrawables = emptySparseArray();
+        } else {
+            mPreloadedDrawables = sPreloadedDrawables;
+        }
+    }
+
+    /**
      * Return a global shared Resources object that provides access to only
      * system resources (no application resources), and is not configured for 
      * the current screen (can not use dimension units, does not change based