Android Open Source - ascent Ascent






From Project

Back to project page ascent.

License

The source code is released under:

Apache License

If you think the Android project ascent listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.jamesbaca.ascent;
/* www . j  a va 2s. co  m*/
import android.content.res.AssetManager;
import android.graphics.Typeface;

import java.util.HashMap;

/**
 * Created by jamesbaca on 9/28/14.
 */
public class Ascent {

    String mFontAssetFolder = "fonts/";
    HashMap<String, Typeface> mTypefaces = new HashMap<String, Typeface>();
    AssetManager mAssetManager;

    public void setAssetManager(AssetManager aAssetManager){
        mAssetManager = aAssetManager;
    }

    public void setAssetsFolder(String path){
        mFontAssetFolder = path;
    }

    public void add(String identifier, Typeface font){
        mTypefaces.put(identifier, font);
    }

    public Typeface getTypeface(String identifier){
        if(mTypefaces.get(identifier) != null){
            return mTypefaces.get(identifier);
        }else if(mAssetManager != null){
            Typeface lTypeface = Typeface.createFromAsset(mAssetManager, mFontAssetFolder+identifier);
            mTypefaces.put(identifier, lTypeface);
            return lTypeface;
        }else{
            return null;
        }
    }

    public void inject(Object target){
        try {
            FontHelper helper = getHelperForClass(target.getClass());
            if (helper != null) {
                helper.applyFont(target, this);
            }
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new UnableToInjectException(target, e);
        }
    }

    @SuppressWarnings("unchecked")
    protected FontHelper getHelperForClass(Class<?> cls) throws NoSuchMethodException {

        if(cls == null){
            return null;
        }
        String clsName = cls.getName();
        FontHelper fontHelper = null;

        try {
            String generatedClassName = clsName + InjectedAscent.SUFFIX;
            fontHelper = (FontHelper) Class.forName(generatedClassName).newInstance();
        } catch (ClassNotFoundException e) {
            fontHelper = getHelperForClass(cls.getSuperclass());
        } catch (InstantiationException e) {
            return null;
        } catch (IllegalAccessException e) {
            return null;
        }

        return fontHelper;
    }
    
    public class UnableToInjectException extends RuntimeException{

        public UnableToInjectException(Object target, Exception e) {
        }
    }
}




Java Source Code List

net.jamesbaca.ascent.Ascent.java
net.jamesbaca.ascent.FontHelper.java
net.jamesbaca.ascent.Font.java
net.jamesbaca.ascent.InjectedAscent.java
net.jamesbaca.ascent.demo.ApplicationTest.java
net.jamesbaca.ascent.demo.MainActivity.java
net.jamesbaca.ascent.internal.AnnotatedField.java
net.jamesbaca.ascent.internal.AnnotationsConverter.java
net.jamesbaca.ascent.internal.EnclosingClass.java
net.jamesbaca.ascent.internal.FontClassWriter.java
net.jamesbaca.ascent.internal.FontProcessor.java
net.jamesbaca.ascent.internal.WriterFactory.java
net.jamesbaca.ascent.mymodule.app.ApplicationTest.java
net.jamesbaca.ascent.mymodule.app.DemoApplicationModule.java
net.jamesbaca.ascent.mymodule.app.DemoApplication.java
net.jamesbaca.ascent.mymodule.app.MainActivity.java