
package org.holoeverywhere.addon;

import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.WeakHashMap;

import org.holoeverywhere.ThemeManager;
import org.holoeverywhere.ThemeManager.ThemeSetter;
import org.holoeverywhere.app.ContextThemeWrapperPlus;

import android.content.Context;

public class IAddonThemes implements ThemeSetter {
    private static final class AddonThemeWrapper extends ContextThemeWrapperPlus {
        public AddonThemeWrapper(Context base, int themeres) {
            super(base, themeres);
        }
    }

    public interface ThemeResolver {
        public int resolveThemeForContext(Context context, int invalidTheme);
    }

    private Map<Context, WeakReference<AddonThemeWrapper>> mContexts;
    private int mDarkTheme = -1;
    private final ThemeResolver mDefaultThemeResolver = new ThemeResolver() {
        @Override
        public int resolveThemeForContext(Context context, int invalidTheme) {
            int theme = ThemeManager.getThemeType(context);
            if (theme == ThemeManager.INVALID) {
                theme = invalidTheme & ThemeManager.getThemeMask();
                if (theme == 0) {
                    theme = ThemeManager.DARK;
                }
            }
            theme |= mThemeFlag;
            return ThemeManager.getThemeResource(theme, false);
        }
    };

    private int mLightTheme = -1;

    private int mMixedTheme = -1;

    private final int mThemeFlag;

    public IAddonThemes() {
        mThemeFlag = ThemeManager.makeNewFlag();
        ThemeManager.registerThemeSetter(this);
    }

    public Context context(Context context) {
        return context(context, ThemeManager.DARK);
    }

    public Context context(Context context, int invalidTheme) {
        return context(context, invalidTheme, mDefaultThemeResolver);
    }

    public Context context(Context context, int invalidTheme, ThemeResolver themeResolver) {
        if (context instanceof AddonThemeWrapper) {
            return context;
        }
        AddonThemeWrapper wrapper = null;
        if (mContexts != null) {
			WeakReference<AddonThemeWrapper> wrapperReference = mContexts
					.get(context);
			wrapper = wrapperReference == null ? null : wrapperReference.get();
        }
        if (wrapper == null) {
            final int theme = themeResolver.resolveThemeForContext(context, invalidTheme);
            if (theme <= 0) {
                return null;
            }
            wrapper = new AddonThemeWrapper(context, theme);
            if (mContexts == null) {
				mContexts = new WeakHashMap<Context, WeakReference<AddonThemeWrapper>>();
            }
			mContexts.put(context,
					new WeakReference<AddonThemeWrapper>(wrapper));
        }
        return wrapper;
    }

    public int getDarkTheme() {
        return mDarkTheme;
    }

    public int getLightTheme() {
        return mLightTheme;
    }

    public int getMixedTheme() {
        return mMixedTheme;
    }

    public int getThemeFlag() {
        return mThemeFlag;
    }

    public void map(int darkTheme, int lightTheme, int mixedTheme) {
        mDarkTheme = darkTheme;
        mLightTheme = lightTheme;
        mMixedTheme = mixedTheme;
        setupThemes();
    }

    public void setDarkTheme(int darkTheme) {
        mDarkTheme = darkTheme;
        setupThemes();
    }

    public void setLightTheme(int lightTheme) {
        mLightTheme = lightTheme;
        setupThemes();
    }

    public void setMixedTheme(int mixedTheme) {
        mMixedTheme = mixedTheme;
        setupThemes();
    }

    @Override
    public void setupThemes() {
        ThemeManager.map(mThemeFlag | ThemeManager.DARK, mDarkTheme);
        ThemeManager.map(mThemeFlag | ThemeManager.LIGHT, mLightTheme);
        ThemeManager.map(mThemeFlag | ThemeManager.MIXED, mMixedTheme);
    }

    public Context unwrap(Context context) {
        if (context == null) {
            return null;
        }
        while (context instanceof AddonThemeWrapper) {
            context = ((AddonThemeWrapper) context).getBaseContext();
        }
        return context;
    }
}

@@ -2094,19 +2094,28 @@
 null) %7B%0A
-%09%09%09
+            
 WeakRefe
@@ -2139,32 +2139,19 @@
 rapper%3E 
-wrapperReference
+ref
  = mCont
@@ -2154,22 +2154,16 @@
 Contexts
-%0A%09%09%09%09%09
 .get(con
@@ -2173,37 +2173,33 @@
 t);%0A
-%09%09%09wrapper = wrapperReference
+            wrapper = ref
  == 
@@ -2216,24 +2216,11 @@
 l : 
-wrapperReference
+ref
 .get
@@ -2526,20 +2526,32 @@
 null) %7B%0A
-%09%09%09%09
+                
 mContext
@@ -2630,19 +2630,28 @@
       %7D%0A
-%09%09%09
+            
 mContext
@@ -2668,14 +2668,9 @@
 ext,
-%0A%09%09%09%09%09
+ 
 new 

Fix codestyle in #442 [deploy snapshot]