package com.yatatsu.autobundle;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

/**
 * Injection helper.
 * resolve each binding class for target.
 */
public final class AutoBundle {

    /**
     * tag for {@link Log}
     */
    private static final String TAG = "AutoBundle";

    /**
     * {@link AutoBundleBinder} implementation class will be generated by apt.
     * So if {@link Arg} does not used, impl class may not exist.
     */
    static final String BINDER_CLASS_NAME = "com.yatatsu.autobundle.AutoBundleBindingDispatcher";

    /**
     * static instance will load at the first time static method called.
     */
    private static AutoBundleBinder autoBundleBinder;

    private AutoBundle() {
        throw new AssertionError("no instances");
    }

    /**
     * assign arguments to target fields.
     *
     * target may be {@link android.app.Activity},
     * {@link android.content.BroadcastReceiver},
     * {@link android.app.Service},
     * {@link android.app.Fragment} or these compatibility class.
     *
     * @param target target which has {@link Arg} annotated fields.
     * @param args source bundle.
     */
    public static void bind(Object target, Bundle args) {
        AutoBundleBinder binder = findBinder();
        if (binder != null) {
            binder.bind(target, args);
        } else {
            Log.w(TAG, "AutoBundle cannot resolve binding with " + target.getClass());
        }
    }

    /**
     * assign to target fields from {@link Intent#getExtras()}.
     *
     * target may be {@link android.app.Activity},
     * {@link android.content.BroadcastReceiver},
     * {@link android.app.Service} or these compatibility class.
     *
     * @param target target which has {@link Arg} annotated fields.
     * @param intent source bundle.
     */
    public static void bind(Object target, Intent intent) {
        AutoBundleBinder binder = findBinder();
        if (binder != null) {
            binder.bind(target, intent);
        } else {
            Log.w(TAG, "AutoBundle cannot resolve binding with " + target.getClass());
        }
    }

    /**
     * assign to target fields from {@link Fragment#getArguments()}.
     *
     * target may be {@link Fragment} or compat.
     *
     * @param target target Fragment which has {@link Arg} annotated fields.
     */
    public static void bind(Object target) {
        AutoBundleBinder binder = findBinder();
        if (binder != null) {
            binder.bind(target);
        } else {
            Log.w(TAG, "AutoBundle cannot resolve binding with " + target.getClass());
        }
    }

    /**
     * set arguments from source fields.
     *
     * target may be {@link android.app.Activity},
     * {@link android.content.BroadcastReceiver},
     * {@link android.app.Service},
     * {@link android.app.Fragment} or these compatibility class.
     *
     * @param source source instance which has {@link Arg} annotated fields.
     * @param args target bundle.
     */
    public static void pack(Object source, Bundle args) {
        AutoBundleBinder binder = findBinder();
        if (binder != null) {
            binder.pack(source, args);
        } else {
            Log.w(TAG, "AutoBundle cannot resolve binding with " + source.getClass());
        }
    }

    static AutoBundleBinder findBinder() {
        if (autoBundleBinder == null) {
            try {
                Class<?> binderClass = Class.forName(BINDER_CLASS_NAME);
                autoBundleBinder = (AutoBundleBinder) binderClass.newInstance();
            } catch (Exception e) {
                Log.e(TAG, "binder class not found.", e);
            }
        }
        return autoBundleBinder;
    }
}
@@ -26,16 +26,45 @@
 undle;%0A%0A
+import android.app.Activity;%0A
 import a
@@ -80,24 +80,24 @@
 p.Fragment;%0A
-
 import andro
@@ -847,32 +847,796 @@
 ances%22);%0A    %7D%0A%0A
+    /**%0A     * assign to target fields from %7B@link Activity#getIntent()%7D%0A     * @param activity target activity which has %7B@link Arg%7D annotated fields.%0A     */%0A    public static void bind(Activity activity) %7B%0A        bind(activity, activity.getIntent());%0A    %7D%0A%0A    /**%0A     * assign to target fields from %7B@link Fragment#getArguments()%7D.%0A     *%0A     * target may be %7B@link Fragment%7D or compat.%0A     *%0A     * @param target target Fragment which has %7B@link Arg%7D annotated fields.%0A     */%0A    public static void bind(Object target) %7B%0A        AutoBundleBinder binder = findBinder();%0A        if (binder != null) %7B%0A            binder.bind(target);%0A        %7D else %7B%0A            Log.w(TAG, %22AutoBundle cannot resolve binding with %22 + target.getClass());%0A        %7D%0A    %7D%0A%0A
     /**%0A     * a
@@ -2786,32 +2786,32 @@
 nder != null) %7B%0A
+
             bind
@@ -2960,510 +2960,8 @@
  %7D%0A%0A
-    /**%0A     * assign to target fields from %7B@link Fragment#getArguments()%7D.%0A     *%0A     * target may be %7B@link Fragment%7D or compat.%0A     *%0A     * @param target target Fragment which has %7B@link Arg%7D annotated fields.%0A     */%0A    public static void bind(Object target) %7B%0A        AutoBundleBinder binder = findBinder();%0A        if (binder != null) %7B%0A            binder.bind(target);%0A        %7D else %7B%0A            Log.w(TAG, %22AutoBundle cannot resolve binding with %22 + target.getClass());%0A        %7D%0A    %7D%0A%0A
     

Add static method `AutoBundle.bind(Activity activity)`