Adds the StyleManager class.

StyleManager provides APIs for the Contacts app to retrieve icons from
third party sync adapter packages to display in the Contacts UI.
StyleManager also keeps a Cache of the requested icons in memory.
diff --git a/Android.mk b/Android.mk
index a570324..2632f47 100644
--- a/Android.mk
+++ b/Android.mk
@@ -10,8 +10,6 @@
 LOCAL_PACKAGE_NAME := Contacts
 LOCAL_CERTIFICATE := shared
 
-LOCAL_STATIC_JAVA_LIBRARIES := googlelogin-client
-
 include $(BUILD_PACKAGE)
 
 # Use the folloing include to make our test apk.
diff --git a/src/com/android/contacts/StyleManager.java b/src/com/android/contacts/StyleManager.java
new file mode 100644
index 0000000..bd93edd
--- /dev/null
+++ b/src/com/android/contacts/StyleManager.java
@@ -0,0 +1,301 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.WeakHashMap;
+
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.content.res.TypedArray;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.util.AttributeSet;
+import android.util.Log;
+import android.util.Xml;
+
+
+public final class StyleManager extends BroadcastReceiver {
+
+    public static final String TAG = "StyleManager";
+
+    private static StyleManager sInstance = null;
+
+    private WeakHashMap<String, Bitmap> mIconCache;
+    private HashMap<String, StyleSet> mStyleSetCache;
+
+    /*package*/ static final String DEFAULT_MIMETYPE = "default-icon";
+    private static final String ICON_SET_META_DATA = "com.android.contacts.iconset";
+    private static final String TAG_ICON_SET = "icon-set";
+    private static final String TAG_ICON = "icon";
+    private static final String TAG_ICON_DEFAULT = "icon-default";
+    private static final String KEY_JOIN_CHAR = "|";
+
+    private Context mContext;
+
+    private StyleManager(Context context) {
+        mIconCache = new WeakHashMap<String, Bitmap>();
+        mStyleSetCache = new HashMap<String, StyleSet>();
+        mContext = context;
+        registerIntentReceivers();
+    }
+
+    /**
+     * Returns an instance of StyleManager. This method enforces that only a single instance of this
+     * class exists at any one time in a process.
+     *
+     * @param context A context object
+     * @return StyleManager object
+     */
+    public static StyleManager getInstance(Context context) {
+        if (sInstance == null) {
+            sInstance = new StyleManager(context);
+        }
+        return sInstance;
+    }
+
+    private void registerIntentReceivers() {
+        IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
+        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
+        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
+        filter.addDataScheme("package");
+        mContext.registerReceiver(this, filter);
+    }
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        final String action = intent.getAction();
+        final String packageName = intent.getData().getSchemeSpecificPart();
+
+        if (Intent.ACTION_PACKAGE_REMOVED.equals(action)
+                || Intent.ACTION_PACKAGE_ADDED.equals(action)
+                || Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
+            onPackageChange(packageName);
+        }
+    }
+
+    public void onPackageChange(String packageName) {
+        Iterator<String> itr;
+
+        // Remove cached icons for this package
+        for (itr = mIconCache.keySet().iterator(); itr.hasNext(); ) {
+            if (itr.next().startsWith(packageName + KEY_JOIN_CHAR)) {
+                itr.remove();
+            }
+        }
+
+        // Remove the cached style set for this package
+        mStyleSetCache.remove(packageName);
+    }
+
+    /**
+     * Get the default icon for a given package. If no icon is specified for that package
+     * null is returned.
+     *
+     * @param packageName
+     * @return Bitmap holding the default icon.
+     */
+    public Bitmap getDefaultIcon(String packageName) {
+        return getMimetypeIcon(packageName, DEFAULT_MIMETYPE);
+    }
+
+    /**
+     * Get the icon associated with a mimetype for a given package. If no icon is specified for that
+     * package null is returned.
+     *
+     * @param packageName
+     * @return Bitmap holding the default icon.
+     */
+    public Bitmap getMimetypeIcon(String packageName, String mimetype) {
+        String key = getKey(packageName, mimetype);
+        if (!mIconCache.containsKey(key)) {
+            // Cache miss
+
+            // loadIcon() may return null, which is fine since, if no icon was found we want to
+            // store a null value so we know not to look next time.
+            mIconCache.put(key, loadIcon(packageName, mimetype));
+        }
+        return mIconCache.get(key);
+    }
+
+    private Bitmap loadIcon(String packageName, String mimetype) {
+        StyleSet ss = null;
+
+        if (!mStyleSetCache.containsKey(packageName)) {
+            // Cache miss
+            try {
+                StyleSet inflated = inflateStyleSet(packageName);
+                mStyleSetCache.put(packageName, inflated);
+            } catch (InflateException e) {
+                // If inflation failed keep a null entry so we know not to try again.
+                Log.w(TAG, "Inflation failed: " + e);
+                mStyleSetCache.put(packageName, null);
+            }
+        }
+
+        ss = mStyleSetCache.get(packageName);
+        if (ss == null) {
+            return null;
+        }
+
+        int iconRes;
+        if ((iconRes = ss.getIconRes(mimetype)) == -1) {
+            return null;
+        }
+
+        return BitmapFactory.decodeResource(mContext.getResources(),
+                iconRes, null);
+    }
+
+    private StyleSet inflateStyleSet(String packageName) throws InflateException {
+        final PackageManager pm = mContext.getPackageManager();
+        final ApplicationInfo ai;
+
+        try {
+            ai = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
+        } catch (NameNotFoundException e) {
+            return null;
+        }
+
+        XmlPullParser parser = ai.loadXmlMetaData(pm, ICON_SET_META_DATA);
+        final AttributeSet attrs = Xml.asAttributeSet(parser);
+
+        if (parser == null) {
+            return null;
+        }
+
+        try {
+            int type;
+            while ((type = parser.next()) != XmlPullParser.START_TAG
+                    && type != XmlPullParser.END_DOCUMENT) {
+                // Drain comments and whitespace
+            }
+
+            if (type != XmlPullParser.START_TAG) {
+                throw new InflateException("No start tag found");
+            }
+
+            if (!TAG_ICON_SET.equals(parser.getName())) {
+                throw new InflateException("Top level element must be StyleSet");
+            }
+
+            // Parse all children actions
+            StyleSet styleSet = new StyleSet();
+            final int depth = parser.getDepth();
+            while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
+                    && type != XmlPullParser.END_DOCUMENT) {
+                if (type == XmlPullParser.END_TAG) {
+                    continue;
+                }
+
+                TypedArray a;
+
+                String mimetype;
+                if (TAG_ICON.equals(parser.getName())) {
+                    a = mContext.obtainStyledAttributes(attrs, android.R.styleable.Icon);
+                    mimetype = a.getString(com.android.internal.R.styleable.Icon_mimeType);
+                    if (mimetype != null) {
+                        styleSet.addIcon(mimetype,
+                                a.getResourceId(com.android.internal.R.styleable.Icon_icon, -1));
+                    }
+                } else if (TAG_ICON_DEFAULT.equals(parser.getName())) {
+                    a = mContext.obtainStyledAttributes(attrs, android.R.styleable.IconDefault);
+                    styleSet.addIcon(DEFAULT_MIMETYPE,
+                            a.getResourceId(
+                                    com.android.internal.R.styleable.IconDefault_icon, -1));
+                } else {
+                    throw new InflateException("Expected " + TAG_ICON + " or "
+                            + TAG_ICON_DEFAULT + " tag");
+                }
+            }
+            return styleSet;
+
+        } catch (XmlPullParserException e) {
+            throw new InflateException("Problem reading XML", e);
+        } catch (IOException e) {
+            throw new InflateException("Problem reading XML", e);
+        }
+    }
+
+    private String getKey(String packageName, String mimetype) {
+        return packageName + KEY_JOIN_CHAR + mimetype;
+    }
+
+    public static class InflateException extends Exception {
+        public InflateException(String message) {
+            super(message);
+        }
+
+        public InflateException(String message, Throwable throwable) {
+            super(message, throwable);
+        }
+    }
+
+    private static class StyleSet {
+        private HashMap<String, Integer> mMimetypeIconResMap;
+
+        public StyleSet() {
+            mMimetypeIconResMap = new HashMap<String, Integer>();
+        }
+
+        public int getIconRes(String mimetype) {
+            if (!mMimetypeIconResMap.containsKey(mimetype)) {
+                return -1;
+            }
+            return mMimetypeIconResMap.get(mimetype);
+        }
+
+        public void addIcon(String mimetype, int res) {
+            if (mimetype == null) {
+                return;
+            }
+            mMimetypeIconResMap.put(mimetype, res);
+        }
+    }
+
+    //-------------------------------------------//
+    //-- Methods strictly for testing purposes --//
+    //-------------------------------------------//
+
+    /*package*/ int getIconCacheSize() {
+        return mIconCache.size();
+    }
+
+    /*package*/ int getStyleSetCacheSize() {
+        return mStyleSetCache.size();
+    }
+
+    /*package*/ boolean isStyleSetCacheHit(String packageName) {
+        return mStyleSetCache.containsKey(packageName);
+    }
+
+    /*package*/ boolean isIconCacheHit(String packageName, String mimetype) {
+        return mIconCache.containsKey(getKey(packageName, mimetype));
+    }
+
+    //-------------------------------------------//
+}
diff --git a/tests/Android.mk b/tests/Android.mk
index 47782e3..ef11c5e 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -3,7 +3,7 @@
 
 # We only want this apk build for tests.
 LOCAL_MODULE_TAGS := tests
-LOCAL_CERTIFICATE := platform
+LOCAL_CERTIFICATE := shared
 
 LOCAL_JAVA_LIBRARIES := android.test.runner
 
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index ca28a6a..7f845f9 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -19,11 +19,12 @@
 
     <application>
         <uses-library android:name="android.test.runner" />
+        <meta-data android:name="com.android.contacts.iconset" android:resource="@xml/iconset" />
     </application>
 
-    <instrumentation android:name="ContactsLaunchPerformance"
+    <instrumentation android:name="android.test.InstrumentationTestRunner"
         android:targetPackage="com.android.contacts"
-        android:label="Contacts Launch Performance">
+        android:label="Contacts app tests">
     </instrumentation>
 
 </manifest> 
diff --git a/tests/res/drawable/default_icon.png b/tests/res/drawable/default_icon.png
new file mode 100644
index 0000000..cea0eb3
--- /dev/null
+++ b/tests/res/drawable/default_icon.png
Binary files differ
diff --git a/tests/res/drawable/phone_icon.png b/tests/res/drawable/phone_icon.png
new file mode 100644
index 0000000..4e613ec
--- /dev/null
+++ b/tests/res/drawable/phone_icon.png
Binary files differ
diff --git a/tests/res/xml/iconset.xml b/tests/res/xml/iconset.xml
new file mode 100644
index 0000000..d910815
--- /dev/null
+++ b/tests/res/xml/iconset.xml
@@ -0,0 +1,8 @@
+<icon-set
+    xmlns:android="http://schemas.android.com/apk/res/android">
+
+    <icon-default android:icon="@drawable/default_icon" />
+    <icon android:mimeType="vnd.android.cursor.item/phone" 
+        android:icon="@drawable/phone_icon" />
+
+</icon-set>
\ No newline at end of file
diff --git a/tests/src/com/android/contacts/StyleManagerTests.java b/tests/src/com/android/contacts/StyleManagerTests.java
new file mode 100644
index 0000000..48e7cba
--- /dev/null
+++ b/tests/src/com/android/contacts/StyleManagerTests.java
@@ -0,0 +1,129 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts;
+
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.LargeTest;
+
+import java.util.Arrays;
+
+import com.android.contacts.StyleManager;
+import com.android.contacts.tests.R;
+
+/**
+ * Tests for the StyleManager class.
+ */
+@LargeTest
+public class StyleManagerTests extends AndroidTestCase {
+
+    public static final String LOG_TAG = "StyleManagerTests";
+
+    private StyleManager mStyleManager;
+    private static final String PACKAGE_NAME = "com.android.contacts.tests";
+    private static final String PHONE_MIMETYPE = "vnd.android.cursor.item/phone";
+    private Context mContext;
+
+    public StyleManagerTests() {
+        super();
+    }
+
+    @Override
+    public void setUp() {
+        mContext = getContext();
+        mStyleManager = StyleManager.getInstance(mContext);
+    }
+
+    public void testGetMimetypeIcon() {
+        Bitmap phoneIconFromSm = mStyleManager.getMimetypeIcon(PACKAGE_NAME, PHONE_MIMETYPE);
+        int smHeight = phoneIconFromSm.getHeight();
+        int smWidth = phoneIconFromSm.getWidth();
+
+        Bitmap phoneIconFromRes = BitmapFactory.decodeResource(mContext.getResources(),
+                R.drawable.phone_icon, null);
+        int resHeight = phoneIconFromRes.getHeight();
+        int resWidth = phoneIconFromRes.getWidth();
+
+        int[] smPixels = new int[smWidth*smHeight];
+        phoneIconFromSm.getPixels(smPixels, 0, smWidth, 0, 0, smWidth, smHeight);
+
+        int[] resPixels = new int[resWidth*resHeight];
+        phoneIconFromRes.getPixels(resPixels, 0, resWidth, 0, 0, resWidth, resHeight);
+
+        assertTrue(Arrays.equals(smPixels, resPixels));
+    }
+
+    public void testGetMissingMimetypeIcon() {
+        Bitmap postalIconFromSm = mStyleManager.getMimetypeIcon(PACKAGE_NAME,
+                "vnd.android.cursor.item/postal-address");
+
+        assertNull(postalIconFromSm);
+    }
+
+    public void testGetDefaultIcon() {
+        Bitmap defaultIconFromSm = mStyleManager.getDefaultIcon(PACKAGE_NAME);
+
+        int smHeight = defaultIconFromSm.getHeight();
+        int smWidth = defaultIconFromSm.getWidth();
+
+        Bitmap defaultIconFromRes = BitmapFactory.decodeResource(mContext.getResources(),
+                R.drawable.default_icon, null);
+        int resHeight = defaultIconFromRes.getHeight();
+        int resWidth = defaultIconFromRes.getWidth();
+
+        int[] smPixels = new int[smWidth*smHeight];
+        defaultIconFromSm.getPixels(smPixels, 0, smWidth, 0, 0, smWidth, smHeight);
+
+        int[] resPixels = new int[resWidth*resHeight];
+        defaultIconFromRes.getPixels(resPixels, 0, resWidth, 0, 0, resWidth, resHeight);
+
+        assertTrue(Arrays.equals(smPixels, resPixels));
+    }
+
+    public void testCaching() {
+        // Clear cache
+        mStyleManager.onPackageChange(PACKAGE_NAME);
+        assertTrue(mStyleManager.getIconCacheSize() == 0);
+        assertTrue(mStyleManager.getStyleSetCacheSize() == 0);
+
+        // Getting the icon should add it to the cache.
+        mStyleManager.getDefaultIcon(PACKAGE_NAME);
+        assertTrue(mStyleManager.getIconCacheSize() == 1);
+        assertTrue(mStyleManager.getStyleSetCacheSize() == 1);
+        assertTrue(mStyleManager.isIconCacheHit(PACKAGE_NAME, StyleManager.DEFAULT_MIMETYPE));
+        assertFalse(mStyleManager.isIconCacheHit(PACKAGE_NAME, PHONE_MIMETYPE));
+        assertTrue(mStyleManager.isStyleSetCacheHit(PACKAGE_NAME));
+
+        mStyleManager.getMimetypeIcon(PACKAGE_NAME, PHONE_MIMETYPE);
+        assertTrue(mStyleManager.getIconCacheSize() == 2);
+        assertTrue(mStyleManager.getStyleSetCacheSize() == 1);
+        assertTrue(mStyleManager.isIconCacheHit(PACKAGE_NAME, StyleManager.DEFAULT_MIMETYPE));
+        assertTrue(mStyleManager.isIconCacheHit(PACKAGE_NAME, PHONE_MIMETYPE));
+        assertTrue(mStyleManager.isStyleSetCacheHit(PACKAGE_NAME));
+
+        // Clear cache
+        mStyleManager.onPackageChange(PACKAGE_NAME);
+        assertTrue(mStyleManager.getIconCacheSize() == 0);
+        assertTrue(mStyleManager.getStyleSetCacheSize() == 0);
+        assertFalse(mStyleManager.isIconCacheHit(PACKAGE_NAME, StyleManager.DEFAULT_MIMETYPE));
+        assertFalse(mStyleManager.isIconCacheHit(PACKAGE_NAME, PHONE_MIMETYPE));
+        assertFalse(mStyleManager.isStyleSetCacheHit(PACKAGE_NAME));
+    }
+
+}