Android Open Source - PrettyBundle Pretty Bundle






From Project

Back to project page PrettyBundle.

License

The source code is released under:

Apache License

If you think the Android project PrettyBundle 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 com.tale.prettybundle;
// ww  w.  j  a  v  a  2  s  .c  om
import android.app.Activity;
import android.app.Fragment;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import java.util.LinkedHashMap;
import java.util.Map;


/**
 * Bundle "injection" utilities. Use this class to simplify binding bundle values.
 * <p/>
 * Injecting bundle from your activity is as easy as:
 * <pre><code>
 * public class ExampleActivity extends Activity {
 *   {@literal @}Extra String stringExtra1;
 *   {@literal @}Extra String stringExtra2;
 * <p/>
 *   {@literal @}Override protected void onCreate(Bundle savedInstanceState) {
 *     super.onCreate(savedInstanceState);
 *     PrettyBundle.inject(this);
 *   }
 * }
 * </code></pre>
 */
public final class PrettyBundle {
    public static final String INJECTOR_SUFFIX = "$$BundleInjector";
    public static final String ANDROID_PREFIX = "android.";
    public static final String JAVA_PREFIX = "java.";

    private PrettyBundle() {
        throw new AssertionError("No instances.");
    }

    private static final String TAG = "PrettyBundle";
    private static boolean debug = false;

    static final Map<Class<?>, Injector<Object>> INJECTORS =
            new LinkedHashMap<Class<?>, Injector<Object>>();

    private static final Injector<Object> NOP_INJECTOR = new Injector<Object>() {
        @Override public void inject(Object target, Bundle extras) {

        }
    };

    /**
     * Control whether debug logging is enabled.
     */
    public static void setDebug(boolean debug) {
        PrettyBundle.debug = debug;
    }

    public static void inject(Activity activity) {
        if (activity == null) {
            return;
        }

        inject(activity, activity.getIntent().getExtras());
    }

    public static void inject(Fragment fragment) {
        if (fragment == null) {
            return;
        }

        inject(fragment, fragment.getArguments());
    }

    public static void inject(Service service, Intent intent) {
        if (service == null) {
            return;
        }

        inject(service, intent == null ? null : intent.getExtras());
    }

    private static void inject(Object target, Bundle extras) {
        Class<?> targetClass = target.getClass();
        try {
            if (debug) Log.d(TAG, "Looking up view injector for " + targetClass.getName());
            Injector<Object> injector = findInjectorForClass(targetClass);
            if (injector != null) {
                injector.inject(target, extras);
            }
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Unable to inject views for " + target, e);
        }
    }

    private static Injector<Object> findInjectorForClass(Class<?> cls)
            throws IllegalAccessException, InstantiationException {
        Injector<Object> injector = INJECTORS.get(cls);
        if (injector != null) {
            if (debug) Log.d(TAG, "HIT: Cached in injector map.");
            return injector;
        }
        String clsName = cls.getName();
        if (clsName.startsWith(ANDROID_PREFIX) || clsName.startsWith(JAVA_PREFIX)) {
            if (debug) Log.d(TAG, "MISS: Reached framework class. Abandoning search.");
            return NOP_INJECTOR;
        }

        try {
            Class<?> injectorClass = Class.forName(clsName + INJECTOR_SUFFIX);
            //noinspection unchecked
            injector = (Injector<Object>) injectorClass.newInstance();
            if (debug) Log.d(TAG, "HIT: Class loaded injection class.");
        } catch (ClassNotFoundException e) {
            if (debug) Log.d(TAG, "Not found. Trying superclass " + cls.getSuperclass().getName());
            injector = findInjectorForClass(cls.getSuperclass());
        }
        INJECTORS.put(cls, injector);
        return injector;
    }

}




Java Source Code List

com.tale.prettybundle.ExtraBinderProvider.java
com.tale.prettybundle.ExtraBinder.java
com.tale.prettybundle.Extra.java
com.tale.prettybundle.Injector.java
com.tale.prettybundle.PrettyBundle.java
com.tale.prettybundle.internal.ExtraAnnotatedClass.java
com.tale.prettybundle.internal.ExtraClassesGrouped.java
com.tale.prettybundle.internal.ExtraInjectorClassBuilder.java
com.tale.prettybundle.internal.ExtraUtilityClassBuilder.java
com.tale.prettybundle.internal.PrettyBundleProcessor.java
com.tale.prettybundle.internal.SupportedType.java
com.tale.prettybundle.sample.ActivitiesClassTest.java
com.tale.prettybundle.sample.ActivitiesTest.java
com.tale.prettybundle.sample.ApplicationTest.java
com.tale.prettybundle.sample.BaseActivityUnitTestCase.java
com.tale.prettybundle.sample.InjectArrayExtrasTest.java
com.tale.prettybundle.sample.InjectParcelableExtrasTest.java
com.tale.prettybundle.sample.InjectPrimaryTypeExtrasFragmentTest.java
com.tale.prettybundle.sample.InjectPrimaryTypeExtrasTest.java
com.tale.prettybundle.sample.InjectStringExtrasTest.java
com.tale.prettybundle.sample.MainActivity.java
com.tale.prettybundle.sample.MenuActivity.java
com.tale.prettybundle.sample.TestArrayExtrasActivity.java
com.tale.prettybundle.sample.TestExtraOnFragmentActivity.java
com.tale.prettybundle.sample.TestParcelableActivity.java
com.tale.prettybundle.sample.TestPrimaryTypeDisplayActivity.java
com.tale.prettybundle.sample.TestPrimaryTypeSetterActivity.java
com.tale.prettybundle.sample.TestStringExtra1Activity.java
com.tale.prettybundle.sample.TestStringExtra2Activity.java
com.tale.prettybundle.sample.data.Person.java
com.tale.prettybundle.sample.espresso.ExtViewActions.java
com.tale.prettybundle.sample.fragments.TestPrimaryExtraFragment.java
com.tale.prettybundle.sample.services.TestPrimaryTypeService.java