Layoutlib: Properly dispose of bitmap delegate.

This is done by initializing the android.util.Finalizers that's
reponsible for calling out to the native bitmap destructor.
Also implemented the native bitmap destructor

Also fix Bridge by removing obsolete API methods, and removing
some unneeded synchronized blocks now that the whole rendering
(and scene creation) is protected by a synchronized on the bridge
object anyway.

Change-Id: Ie1792da6db354836542dfc11f457fe4a6d78ddfb
diff --git a/bridge/src/android/graphics/Bitmap_Delegate.java b/bridge/src/android/graphics/Bitmap_Delegate.java
index 392462f..0920497 100644
--- a/bridge/src/android/graphics/Bitmap_Delegate.java
+++ b/bridge/src/android/graphics/Bitmap_Delegate.java
@@ -162,8 +162,7 @@
     }
 
     /*package*/ static void nativeDestructor(int nativeBitmap) {
-        // FIXME implement native delegate
-        throw new UnsupportedOperationException("Native delegate needed for Bitmap");
+        sManager.removeDelegate(nativeBitmap);
     }
 
     /*package*/ static void nativeRecycle(int nativeBitmap) {
diff --git a/bridge/src/com/android/layoutlib/bridge/Bridge.java b/bridge/src/com/android/layoutlib/bridge/Bridge.java
index 53da2ca..efa0f27 100644
--- a/bridge/src/com/android/layoutlib/bridge/Bridge.java
+++ b/bridge/src/com/android/layoutlib/bridge/Bridge.java
@@ -32,6 +32,7 @@
 
 import android.graphics.Bitmap;
 import android.graphics.Typeface_Delegate;
+import android.util.Finalizers;
 
 import java.lang.ref.SoftReference;
 import java.lang.reflect.Field;
@@ -114,19 +115,12 @@
      */
     @Override
     public boolean init(String fontOsLocation, Map<String, Map<String, Integer>> enumValueMap) {
+        sEnumValueMap = enumValueMap;
+
+        Finalizers.init();
+
         BridgeAssetManager.initSystem();
 
-        return sinit(fontOsLocation, enumValueMap);
-    }
-
-    @Override
-    public boolean dispose() {
-        BridgeAssetManager.clearSystem();
-        return true;
-    }
-
-    private static synchronized boolean sinit(String fontOsLocation,
-            Map<String, Map<String, Integer>> enumValueMap) {
 
         // When DEBUG_LAYOUT is set and is not 0 or false, setup a default listener
         // on static (native) methods which prints the signature on the console and
@@ -165,8 +159,6 @@
             return false;
         }
 
-        sEnumValueMap = enumValueMap;
-
         // now parse com.android.internal.R (and only this one as android.R is a subset of
         // the internal version), and put the content in the maps.
         try {
@@ -214,6 +206,12 @@
         return true;
     }
 
+    @Override
+    public boolean dispose() {
+        BridgeAssetManager.clearSystem();
+        return true;
+    }
+
     /**
      * Sets a 9 patch in a project cache or in the framework cache.
      * @param value the path of the 9 patch
@@ -423,70 +421,4 @@
 
         return null;
     }
-
-
-    // ---------- OBSOLETE API METHODS ----------
-
-    /*
-     * For compatilibty purposes, we implement the old deprecated version of computeLayout.
-     * (non-Javadoc)
-     * @see com.android.layoutlib.api.ILayoutBridge#computeLayout(com.android.layoutlib.api.IXmlPullParser, java.lang.Object, int, int, java.lang.String, java.util.Map, java.util.Map, com.android.layoutlib.api.IProjectCallback, com.android.layoutlib.api.ILayoutLog)
-     */
-    @Deprecated
-    public com.android.layoutlib.api.ILayoutResult computeLayout(IXmlPullParser layoutDescription,
-            Object projectKey,
-            int screenWidth, int screenHeight, String themeName,
-            Map<String, Map<String, IResourceValue>> projectResources,
-            Map<String, Map<String, IResourceValue>> frameworkResources,
-            IProjectCallback customViewLoader, ILayoutLog logger) {
-        throw new UnsupportedOperationException();
-    }
-
-    /*
-     * For compatilibty purposes, we implement the old deprecated version of computeLayout.
-     * (non-Javadoc)
-     * @see com.android.layoutlib.api.ILayoutBridge#computeLayout(com.android.layoutlib.api.IXmlPullParser, java.lang.Object, int, int, java.lang.String, boolean, java.util.Map, java.util.Map, com.android.layoutlib.api.IProjectCallback, com.android.layoutlib.api.ILayoutLog)
-     */
-    @Deprecated
-    public com.android.layoutlib.api.ILayoutResult computeLayout(IXmlPullParser layoutDescription,
-            Object projectKey,
-            int screenWidth, int screenHeight, String themeName, boolean isProjectTheme,
-            Map<String, Map<String, IResourceValue>> projectResources,
-            Map<String, Map<String, IResourceValue>> frameworkResources,
-            IProjectCallback customViewLoader, ILayoutLog logger) {
-        throw new UnsupportedOperationException();
-    }
-
-    /*
-     * For compatilibty purposes, we implement the old deprecated version of computeLayout.
-     * (non-Javadoc)
-     * @see com.android.layoutlib.api.ILayoutBridge#computeLayout(com.android.layoutlib.api.IXmlPullParser, java.lang.Object, int, int, int, float, float, java.lang.String, boolean, java.util.Map, java.util.Map, com.android.layoutlib.api.IProjectCallback, com.android.layoutlib.api.ILayoutLog)
-     */
-    @Deprecated
-    public com.android.layoutlib.api.ILayoutResult computeLayout(IXmlPullParser layoutDescription,
-            Object projectKey,
-            int screenWidth, int screenHeight, int density, float xdpi, float ydpi,
-            String themeName, boolean isProjectTheme,
-            Map<String, Map<String, IResourceValue>> projectResources,
-            Map<String, Map<String, IResourceValue>> frameworkResources,
-            IProjectCallback customViewLoader, ILayoutLog logger) {
-        throw new UnsupportedOperationException();
-    }
-
-    /*
-     * (non-Javadoc)
-     * @see com.android.layoutlib.api.ILayoutBridge#computeLayout(com.android.layoutlib.api.IXmlPullParser, java.lang.Object, int, int, boolean, int, float, float, java.lang.String, boolean, java.util.Map, java.util.Map, com.android.layoutlib.api.IProjectCallback, com.android.layoutlib.api.ILayoutLog)
-     */
-    @Deprecated
-    public com.android.layoutlib.api.ILayoutResult computeLayout(IXmlPullParser layoutDescription,
-            Object projectKey,
-            int screenWidth, int screenHeight, boolean renderFullSize,
-            int density, float xdpi, float ydpi,
-            String themeName, boolean isProjectTheme,
-            Map<String, Map<String, IResourceValue>> projectResources,
-            Map<String, Map<String, IResourceValue>> frameworkResources,
-            IProjectCallback customViewLoader, ILayoutLog logger) {
-        throw new UnsupportedOperationException();
-    }
-
 }
diff --git a/bridge/src/com/android/layoutlib/bridge/impl/DelegateManager.java b/bridge/src/com/android/layoutlib/bridge/impl/DelegateManager.java
index 169d751..b272963 100644
--- a/bridge/src/com/android/layoutlib/bridge/impl/DelegateManager.java
+++ b/bridge/src/com/android/layoutlib/bridge/impl/DelegateManager.java
@@ -59,9 +59,7 @@
      * @return the delegate or null if not found.
      */
     public T getDelegate(int native_object) {
-        synchronized (mDelegates) {
-            return mDelegates.get(native_object);
-        }
+        return mDelegates.get(native_object);
     }
 
     /**
@@ -70,11 +68,9 @@
      * @return a unique native int to identify the delegate
      */
     public int addDelegate(T newDelegate) {
-        synchronized (mDelegates) {
-            int native_object = ++mDelegateCounter;
-            mDelegates.put(native_object, newDelegate);
-            return native_object;
-        }
+        int native_object = ++mDelegateCounter;
+        mDelegates.put(native_object, newDelegate);
+        return native_object;
     }
 
     /**
@@ -82,8 +78,6 @@
      * @param native_object the native int.
      */
     public void removeDelegate(int native_object) {
-        synchronized (mDelegates) {
-            mDelegates.remove(native_object);
-        }
+        mDelegates.remove(native_object);
     }
 }